Skip to content

Commit b296262

Browse files
committed
fix long to date conversion
1 parent f8de6f0 commit b296262

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jacocoTestReport {
5252

5353
test {
5454
testLogging {
55-
events "passed", "skipped", "failed", "standardError"
55+
events "skipped", "failed", "standardError"
5656
exceptionFormat "short"
5757
}
5858
}

lib/src/main/java/com/auth0/jwt/impl/PayloadSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void serialize(ClaimsHolder holder, JsonGenerator gen, SerializerProvider
5454
gen.writeObject(safePayload);
5555
}
5656

57-
private int dateToSeconds(Date date) {
58-
return (int) (date.getTime() / 1000);
57+
private long dateToSeconds(Date date) {
58+
return date.getTime() / 1000;
5959
}
6060
}

lib/src/test/java/com/auth0/jwt/impl/PayloadDeserializerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ public void shouldGetDateWhenParsingNumericNode() throws Exception {
219219
assertThat(date.getTime(), is(seconds * 1000));
220220
}
221221

222+
@Test
223+
public void shouldGetLargeDateWhenParsingNumericNode() throws Exception {
224+
Map<String, JsonNode> tree = new HashMap<>();
225+
long seconds = Integer.MAX_VALUE + 10000L;
226+
LongNode node = new LongNode(seconds);
227+
tree.put("key", node);
228+
229+
Date date = deserializer.getDateFromSeconds(tree, "key");
230+
assertThat(date, is(notNullValue()));
231+
assertThat(date.getTime(), is(seconds * 1000));
232+
assertThat(date.getTime(), is(2147493647L * 1000));
233+
}
234+
222235
@Test
223236
public void shouldGetNullStringWhenParsingNullNode() throws Exception {
224237
Map<String, JsonNode> tree = new HashMap<>();

lib/src/test/java/com/auth0/jwt/impl/PayloadSerializerTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import java.util.HashMap;
1515
import java.util.Map;
1616

17-
import static org.hamcrest.Matchers.equalTo;
18-
import static org.hamcrest.Matchers.is;
17+
import static org.hamcrest.Matchers.*;
1918
import static org.junit.Assert.assertThat;
2019

2120
public class PayloadSerializerTest {
@@ -117,6 +116,26 @@ public void shouldSerializeCustomDateInSeconds() throws Exception {
117116
assertThat(writer.toString(), is(equalTo("{\"birthdate\":1478874}")));
118117
}
119118

119+
@Test
120+
public void shouldSerializeDatesUsingLong() throws Exception {
121+
long secs = Integer.MAX_VALUE + 10000L;
122+
Date date = new Date(secs * 1000L);
123+
Map<String, Object> claims = new HashMap<String, Object>();
124+
claims.put("iat", date);
125+
claims.put("nbf", date);
126+
claims.put("exp", date);
127+
claims.put("ctm", date);
128+
ClaimsHolder holder = new ClaimsHolder(claims);
129+
serializer.serialize(holder, jsonGenerator, serializerProvider);
130+
jsonGenerator.flush();
131+
132+
String json = writer.toString();
133+
assertThat(json, containsString("\"iat\":2147493647"));
134+
assertThat(json, containsString("\"nbf\":2147493647"));
135+
assertThat(json, containsString("\"exp\":2147493647"));
136+
assertThat(json, containsString("\"ctm\":2147493647"));
137+
}
138+
120139
@Test
121140
public void shouldSerializeStrings() throws Exception {
122141
ClaimsHolder holder = holderFor("name", "Auth0 Inc");

0 commit comments

Comments
 (0)