Refactored validateExpectedClaims

This commit is contained in:
Micah Silverman 2015-09-23 03:18:10 -04:00
parent 62ccd16748
commit 5dd95b6755
1 changed files with 23 additions and 10 deletions

View File

@ -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