Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw JWTDecodeException when date claim format is invalid #241

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) thro

Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) {
JsonNode node = tree.get(claimName);
if (node == null || node.isNull() || !node.canConvertToLong()) {
if (node == null || node.isNull()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So non Java person here, what is the difference here in checking for null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node is the instance of the JsonNode we're checking. The check is asserting that the instance of this object is null OR that the parsed JSON value is null. This second check wouldn't be possible if the instance is null as that would throw a NPE when calling some method on it.

return null;
}
if (!node.canConvertToLong()) {
throw new JWTDecodeException(String.format("The claim '%s' contained a non-numeric date value.", claimName));
}
final long ms = node.asLong() * 1000;
return new Date(ms);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void shouldGetCustomMapClaim() throws Exception {

@Test
public void shouldGetAvailableClaims() throws Exception {
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ");
DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjEyMzQ1Njc4OTAsImlhdCI6MTIzNDU2Nzg5MCwibmJmIjoxMjM0NTY3ODkwLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.2_0nxDPJwOk64U5V5V9pt8U92jTPJbGsHYQ35HYhbdE");
assertThat(jwt, is(notNullValue()));
assertThat(jwt.getClaims(), is(notNullValue()));
assertThat(jwt.getClaims(), is(instanceOf(Map.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ public void shouldGetNullDateWhenParsingNull() throws Exception {
}

@Test
public void shouldGetNullDateWhenParsingNonNumericNode() throws Exception {
public void shouldThrowWhenParsingNonNumericNode() throws Exception {
exception.expect(JWTDecodeException.class);
exception.expectMessage("The claim 'key' contained a non-numeric date value.");

Map<String, JsonNode> tree = new HashMap<>();
TextNode node = new TextNode("123456789");
tree.put("key", node);

Date date = deserializer.getDateFromSeconds(tree, "key");
assertThat(date, is(nullValue()));
deserializer.getDateFromSeconds(tree, "key");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no assertions as now caught by the exception.expect? if getDateFromSeconds explodes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Because the ExpectedException is "none" for every method but for those in which we override it, when we run tests they expect (or don't) to fail with X exception and Y message.

}

@Test
Expand Down