@Test @Ignore public void testValidate() throws Exception { final JsonWebTokenValidator validator = JsonWebTokenValidator.builder() .publicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlivFI8qB4D0y2jy0CfEqFyy46R0o7S8TKpsx5xbHKoU1VWg6QkQm+ntyIv1p4kE1sPEQO73+HY8+Bzs75XwRTYL1BmR1w8J5hmjVWjc6R2BTBGAYRPFRhor3kpM6ni2SPmNNhurEAHw7TaqszP5eUF/F9+KEBWkwVta+PZ37bwqSE4sCb1soZFrVz/UT/LF4tYpuVYt3YbqToZ3pZOZ9AX2o1GCG3xwOjkc4x0W7ezbQZdC9iftPxVHR8irOijJRRjcPDtA6vPKpzLl6CyYnsIYPd99ltwxTHjr3npfv/3Lw50bAkbT4HeLFxTx4flEoZLKO/g0bAoV2uqBhkA9xnQIDAQAB") .build(); final String claims = "{" + " \"sub\":\"Jane Awesome\"," + " \"iss\":\"https: " \"groups\":[\"manager\",\"user\"]," + " \"exp\":2552047942" + "}"; final String token = Tokens.asToken(claims); final JsonWebToken jwt = validator.validate(token); assertEquals("Jane Awesome", jwt.getSubject()); assertEquals("https: assertEquals(2552047942l, jwt.getExpirationTime()); }
public JsonWebToken validate(final String token) throws ParseException { final JWTAuthConfiguration authConfiguration = verificationKey != null ? JWTAuthConfiguration.authConfiguration(verificationKey, issuer, allowNoExpiryClaim) : JWTAuthConfiguration.authConfiguration(verificationKeys, issuer, allowNoExpiryClaim); JWTCallerPrincipal principal; try { final JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRelaxVerificationKeyValidation() .setRequireSubject() .setSkipDefaultAudienceValidation() .setJwsAlgorithmConstraints( new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256, AlgorithmIdentifiers.RSA_USING_SHA384, AlgorithmIdentifiers.RSA_USING_SHA512 )); if (authConfiguration.getIssuer() != null) { builder.setExpectedIssuer(authConfiguration.getIssuer()); } if (authConfiguration.getExpGracePeriodSecs() > 0) { builder.setAllowedClockSkewInSeconds(authConfiguration.getExpGracePeriodSecs()); } else { builder.setEvaluationTime(NumericDate.fromSeconds(0)); } if (authConfiguration.isSingleKey()) { builder.setVerificationKey(authConfiguration.getPublicKey()); } else { builder.setVerificationKeyResolver(new JwksVerificationKeyResolver(authConfiguration.getPublicKeys())); } final JwtConsumer jwtConsumer = builder.build(); final JwtContext jwtContext = jwtConsumer.process(token); final String type = jwtContext.getJoseObjects().get(0).getHeader("typ"); jwtConsumer.processContext(jwtContext); JwtClaims claimsSet = jwtContext.getJwtClaims(); String principalName = claimsSet.getClaimValue("upn", String.class); if (principalName == null) { principalName = claimsSet.getClaimValue("preferred_username", String.class); if (principalName == null) { principalName = claimsSet.getSubject(); } } claimsSet.setClaim(Claims.raw_token.name(), token); principal = new JWTCallerPrincipal(token, type, claimsSet, principalName); } catch (final InvalidJwtException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token", e); } catch (final MalformedClaimException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token claims", e); } return principal; }
JsonWebTokenValidator { public JsonWebToken validate(final String token) throws ParseException { final JWTAuthConfiguration authConfiguration = verificationKey != null ? JWTAuthConfiguration.authConfiguration(verificationKey, issuer, allowNoExpiryClaim) : JWTAuthConfiguration.authConfiguration(verificationKeys, issuer, allowNoExpiryClaim); JWTCallerPrincipal principal; try { final JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRelaxVerificationKeyValidation() .setRequireSubject() .setSkipDefaultAudienceValidation() .setJwsAlgorithmConstraints( new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256, AlgorithmIdentifiers.RSA_USING_SHA384, AlgorithmIdentifiers.RSA_USING_SHA512 )); if (authConfiguration.getIssuer() != null) { builder.setExpectedIssuer(authConfiguration.getIssuer()); } if (authConfiguration.getExpGracePeriodSecs() > 0) { builder.setAllowedClockSkewInSeconds(authConfiguration.getExpGracePeriodSecs()); } else { builder.setEvaluationTime(NumericDate.fromSeconds(0)); } if (authConfiguration.isSingleKey()) { builder.setVerificationKey(authConfiguration.getPublicKey()); } else { builder.setVerificationKeyResolver(new JwksVerificationKeyResolver(authConfiguration.getPublicKeys())); } final JwtConsumer jwtConsumer = builder.build(); final JwtContext jwtContext = jwtConsumer.process(token); final String type = jwtContext.getJoseObjects().get(0).getHeader("typ"); jwtConsumer.processContext(jwtContext); JwtClaims claimsSet = jwtContext.getJwtClaims(); String principalName = claimsSet.getClaimValue("upn", String.class); if (principalName == null) { principalName = claimsSet.getClaimValue("preferred_username", String.class); if (principalName == null) { principalName = claimsSet.getSubject(); } } claimsSet.setClaim(Claims.raw_token.name(), token); principal = new JWTCallerPrincipal(token, type, claimsSet, principalName); } catch (final InvalidJwtException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token", e); } catch (final MalformedClaimException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token claims", e); } return principal; } }
JsonWebTokenValidator { public JsonWebToken validate(final String token) throws ParseException { final JWTAuthConfiguration authConfiguration = verificationKey != null ? JWTAuthConfiguration.authConfiguration(verificationKey, issuer, allowNoExpiryClaim) : JWTAuthConfiguration.authConfiguration(verificationKeys, issuer, allowNoExpiryClaim); JWTCallerPrincipal principal; try { final JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRelaxVerificationKeyValidation() .setRequireSubject() .setSkipDefaultAudienceValidation() .setJwsAlgorithmConstraints( new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256, AlgorithmIdentifiers.RSA_USING_SHA384, AlgorithmIdentifiers.RSA_USING_SHA512 )); if (authConfiguration.getIssuer() != null) { builder.setExpectedIssuer(authConfiguration.getIssuer()); } if (authConfiguration.getExpGracePeriodSecs() > 0) { builder.setAllowedClockSkewInSeconds(authConfiguration.getExpGracePeriodSecs()); } else { builder.setEvaluationTime(NumericDate.fromSeconds(0)); } if (authConfiguration.isSingleKey()) { builder.setVerificationKey(authConfiguration.getPublicKey()); } else { builder.setVerificationKeyResolver(new JwksVerificationKeyResolver(authConfiguration.getPublicKeys())); } final JwtConsumer jwtConsumer = builder.build(); final JwtContext jwtContext = jwtConsumer.process(token); final String type = jwtContext.getJoseObjects().get(0).getHeader("typ"); jwtConsumer.processContext(jwtContext); JwtClaims claimsSet = jwtContext.getJwtClaims(); String principalName = claimsSet.getClaimValue("upn", String.class); if (principalName == null) { principalName = claimsSet.getClaimValue("preferred_username", String.class); if (principalName == null) { principalName = claimsSet.getSubject(); } } claimsSet.setClaim(Claims.raw_token.name(), token); principal = new JWTCallerPrincipal(token, type, claimsSet, principalName); } catch (final InvalidJwtException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token", e); } catch (final MalformedClaimException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token claims", e); } return principal; } JsonWebTokenValidator(final Predicate<JsonWebToken> validation, final Key verificationKey, final String issuer, final Map<String, Key> verificationKeys, final boolean allowNoExpiryClaim); }
JsonWebTokenValidator { public JsonWebToken validate(final String token) throws ParseException { final JWTAuthConfiguration authConfiguration = verificationKey != null ? JWTAuthConfiguration.authConfiguration(verificationKey, issuer, allowNoExpiryClaim) : JWTAuthConfiguration.authConfiguration(verificationKeys, issuer, allowNoExpiryClaim); JWTCallerPrincipal principal; try { final JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRelaxVerificationKeyValidation() .setRequireSubject() .setSkipDefaultAudienceValidation() .setJwsAlgorithmConstraints( new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256, AlgorithmIdentifiers.RSA_USING_SHA384, AlgorithmIdentifiers.RSA_USING_SHA512 )); if (authConfiguration.getIssuer() != null) { builder.setExpectedIssuer(authConfiguration.getIssuer()); } if (authConfiguration.getExpGracePeriodSecs() > 0) { builder.setAllowedClockSkewInSeconds(authConfiguration.getExpGracePeriodSecs()); } else { builder.setEvaluationTime(NumericDate.fromSeconds(0)); } if (authConfiguration.isSingleKey()) { builder.setVerificationKey(authConfiguration.getPublicKey()); } else { builder.setVerificationKeyResolver(new JwksVerificationKeyResolver(authConfiguration.getPublicKeys())); } final JwtConsumer jwtConsumer = builder.build(); final JwtContext jwtContext = jwtConsumer.process(token); final String type = jwtContext.getJoseObjects().get(0).getHeader("typ"); jwtConsumer.processContext(jwtContext); JwtClaims claimsSet = jwtContext.getJwtClaims(); String principalName = claimsSet.getClaimValue("upn", String.class); if (principalName == null) { principalName = claimsSet.getClaimValue("preferred_username", String.class); if (principalName == null) { principalName = claimsSet.getSubject(); } } claimsSet.setClaim(Claims.raw_token.name(), token); principal = new JWTCallerPrincipal(token, type, claimsSet, principalName); } catch (final InvalidJwtException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token", e); } catch (final MalformedClaimException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token claims", e); } return principal; } JsonWebTokenValidator(final Predicate<JsonWebToken> validation, final Key verificationKey, final String issuer, final Map<String, Key> verificationKeys, final boolean allowNoExpiryClaim); JsonWebToken validate(final String token); static Builder builder(); }
JsonWebTokenValidator { public JsonWebToken validate(final String token) throws ParseException { final JWTAuthConfiguration authConfiguration = verificationKey != null ? JWTAuthConfiguration.authConfiguration(verificationKey, issuer, allowNoExpiryClaim) : JWTAuthConfiguration.authConfiguration(verificationKeys, issuer, allowNoExpiryClaim); JWTCallerPrincipal principal; try { final JwtConsumerBuilder builder = new JwtConsumerBuilder() .setRelaxVerificationKeyValidation() .setRequireSubject() .setSkipDefaultAudienceValidation() .setJwsAlgorithmConstraints( new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.WHITELIST, AlgorithmIdentifiers.RSA_USING_SHA256, AlgorithmIdentifiers.RSA_USING_SHA384, AlgorithmIdentifiers.RSA_USING_SHA512 )); if (authConfiguration.getIssuer() != null) { builder.setExpectedIssuer(authConfiguration.getIssuer()); } if (authConfiguration.getExpGracePeriodSecs() > 0) { builder.setAllowedClockSkewInSeconds(authConfiguration.getExpGracePeriodSecs()); } else { builder.setEvaluationTime(NumericDate.fromSeconds(0)); } if (authConfiguration.isSingleKey()) { builder.setVerificationKey(authConfiguration.getPublicKey()); } else { builder.setVerificationKeyResolver(new JwksVerificationKeyResolver(authConfiguration.getPublicKeys())); } final JwtConsumer jwtConsumer = builder.build(); final JwtContext jwtContext = jwtConsumer.process(token); final String type = jwtContext.getJoseObjects().get(0).getHeader("typ"); jwtConsumer.processContext(jwtContext); JwtClaims claimsSet = jwtContext.getJwtClaims(); String principalName = claimsSet.getClaimValue("upn", String.class); if (principalName == null) { principalName = claimsSet.getClaimValue("preferred_username", String.class); if (principalName == null) { principalName = claimsSet.getSubject(); } } claimsSet.setClaim(Claims.raw_token.name(), token); principal = new JWTCallerPrincipal(token, type, claimsSet, principalName); } catch (final InvalidJwtException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token", e); } catch (final MalformedClaimException e) { VALIDATION.warning(e.getMessage()); throw new ParseException("Failed to verify token claims", e); } return principal; } JsonWebTokenValidator(final Predicate<JsonWebToken> validation, final Key verificationKey, final String issuer, final Map<String, Key> verificationKeys, final boolean allowNoExpiryClaim); JsonWebToken validate(final String token); static Builder builder();  }