Search in sources :

Example 1 with InvalidClaimException

use of io.jsonwebtoken.InvalidClaimException in project jjwt by jwtk.

the class DefaultJwtParser method validateExpectedClaims.

private void validateExpectedClaims(Header header, Claims claims) {
    for (String expectedClaimName : expectedClaims.keySet()) {
        Object expectedClaimValue = expectedClaims.get(expectedClaimName);
        Object actualClaimValue = claims.get(expectedClaimName);
        if (Claims.ISSUED_AT.equals(expectedClaimName) || Claims.EXPIRATION.equals(expectedClaimName) || Claims.NOT_BEFORE.equals(expectedClaimName)) {
            expectedClaimValue = expectedClaims.get(expectedClaimName, Date.class);
            actualClaimValue = claims.get(expectedClaimName, Date.class);
        } else if (expectedClaimValue instanceof Date && actualClaimValue != null && actualClaimValue instanceof Long) {
            actualClaimValue = new Date((Long) actualClaimValue);
        }
        InvalidClaimException invalidClaimException = null;
        if (actualClaimValue == null) {
            String msg = String.format(ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE, expectedClaimName, expectedClaimValue);
            invalidClaimException = new MissingClaimException(header, claims, msg);
        } else if (!expectedClaimValue.equals(actualClaimValue)) {
            String msg = String.format(ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE, expectedClaimName, expectedClaimValue, actualClaimValue);
            invalidClaimException = new IncorrectClaimException(header, claims, msg);
        }
        if (invalidClaimException != null) {
            invalidClaimException.setClaimName(expectedClaimName);
            invalidClaimException.setClaimValue(expectedClaimValue);
            throw invalidClaimException;
        }
    }
}
Also used : MissingClaimException(io.jsonwebtoken.MissingClaimException) InvalidClaimException(io.jsonwebtoken.InvalidClaimException) IncorrectClaimException(io.jsonwebtoken.IncorrectClaimException) Date(java.util.Date)

Aggregations

IncorrectClaimException (io.jsonwebtoken.IncorrectClaimException)1 InvalidClaimException (io.jsonwebtoken.InvalidClaimException)1 MissingClaimException (io.jsonwebtoken.MissingClaimException)1 Date (java.util.Date)1