From 5dd95b67553123a8d8d4c62b9137604078675599 Mon Sep 17 00:00:00 2001 From: Micah Silverman Date: Wed, 23 Sep 2015 03:18:10 -0400 Subject: [PATCH] Refactored validateExpectedClaims --- .../jsonwebtoken/impl/DefaultJwtParser.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java b/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java index 228a40c0..5a815c76 100644 --- a/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java +++ b/src/main/java/io/jsonwebtoken/impl/DefaultJwtParser.java @@ -363,18 +363,32 @@ public class DefaultJwtParser implements JwtParser { private void validateExpectedClaims(Header header, Claims claims) { for (String expectedClaimName : expectedClaims.keySet()) { - Object expectedClaimValue; - Object actualClaimValue; - // since issued at is a date, call the specific method - // other methods deal with strings and the more - // general method can be used + // this will be overridden if one of the default claims is used + Object expectedClaimValue = expectedClaims.get(expectedClaimName); + Object actualClaimValue = claims.get(expectedClaimName); + if (Claims.ISSUED_AT.equals(expectedClaimName)) { expectedClaimValue = expectedClaims.getIssuedAt(); actualClaimValue = claims.getIssuedAt(); - } else { - expectedClaimValue = expectedClaims.get(expectedClaimName); - actualClaimValue = claims.get(expectedClaimName); + } else if (Claims.AUDIENCE.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getAudience(); + actualClaimValue = claims.getAudience(); + } else if (Claims.ISSUER.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getIssuer(); + actualClaimValue = claims.getIssuer(); + } else if (Claims.SUBJECT.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getSubject(); + actualClaimValue = claims.getSubject(); + } else if (Claims.EXPIRATION.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getExpiration(); + actualClaimValue = claims.getExpiration(); + } else if (Claims.NOT_BEFORE.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getNotBefore(); + actualClaimValue = claims.getNotBefore(); + } else if (Claims.ID.equals(expectedClaimName)) { + expectedClaimValue = expectedClaims.getId(); + actualClaimValue = claims.getId(); } InvalidClaimException invalidClaimException = null; @@ -385,8 +399,7 @@ public class DefaultJwtParser implements JwtParser { expectedClaimName, expectedClaimValue ); invalidClaimException = new MissingClaimException(header, claims, msg); - } - else if (!expectedClaimValue.equals(actualClaimValue)) { + } else if (!expectedClaimValue.equals(actualClaimValue)) { String msg = String.format( ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE, expectedClaimName, expectedClaimValue, actualClaimValue