com.auth0.jwk.JwkException Java Examples

The following examples show how to use com.auth0.jwk.JwkException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JWTVerifierFactory.java    From spring-jwt-gateway with Apache License 2.0 6 votes vote down vote up
@Bean
@Qualifier("jwk")
public JWTVerifier create(@Value("${jwt.issuer}") String issuer, @Value("${jwt.audience}") String audience)
        throws JwkException, IOException {

    UrlJwkProvider urlJwkProvider = new UrlJwkProvider(issuer);
    RestTemplate restTemplate = new RestTemplate();

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = objectMapper.readTree(restTemplate.getForObject(issuer + "/.well-known/jwks.json", String.class));
    String kid = jsonNode.get("keys").get(0).get("kid").asText();

    Jwk jwk = urlJwkProvider.get(kid);

    return JWT.require(Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null))
            .withIssuer(issuer)
            .withAudience(audience)
            .build();
}
 
Example #2
Source File: CachingOpenIdMetadata.java    From botbuilder-java with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private OpenIdMetadataKey findKey(String keyId) {
    if (!keyCache.containsKey(keyId)) {
        LOGGER.warn("findKey: keyId " + keyId + " doesn't exist.");
        return null;
    }

    try {
        Jwk jwk = keyCache.get(keyId);
        OpenIdMetadataKey key = new OpenIdMetadataKey();
        key.key = (RSAPublicKey) jwk.getPublicKey();
        key.endorsements = (List<String>) jwk.getAdditionalAttributes().get("endorsements");
        key.certificateChain = jwk.getCertificateChain();
        return key;
    } catch (JwkException e) {
        String errorDescription = String.format("Failed to load keys: %s", e.getMessage());
        LOGGER.warn(errorDescription);
    }
    return null;
}
 
Example #3
Source File: AsymmetricSignatureVerifier.java    From auth0-java-mvc-common with MIT License 5 votes vote down vote up
private static JWTVerifier createJWTVerifier(final JwkProvider jwkProvider) {
    Algorithm alg = Algorithm.RSA256(new RSAKeyProvider() {
        @Override
        public RSAPublicKey getPublicKeyById(String keyId) {
            try {
                Jwk jwk = jwkProvider.get(keyId);
                return (RSAPublicKey) jwk.getPublicKey();
            } catch (JwkException ignored) {
                // JwkException handled by Algorithm verify implementation from java-jwt
            }
            return null;
        }

        @Override
        public RSAPrivateKey getPrivateKey() {
            //NO-OP
            return null;
        }

        @Override
        public String getPrivateKeyId() {
            //NO-OP
            return null;
        }
    });
    return JWT.require(alg)
            .ignoreIssuedAt()
            .build();
}
 
Example #4
Source File: SignatureVerifierTest.java    From auth0-java-mvc-common with MIT License 5 votes vote down vote up
@Test
public void failsWhenErrorGettingJwk() throws Exception {
    JwkProvider  jwkProvider = mock(JwkProvider.class);
    when(jwkProvider.get("abc123")).thenThrow(JwkException.class);

    exception.expect(TokenValidationException.class);
    exception.expectMessage("Invalid token signature");
    SignatureVerifier verifier = new AsymmetricSignatureVerifier(jwkProvider);
    verifier.verifySignature(RS_JWT);
}
 
Example #5
Source File: KeycloakSigningKeyResolver.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private synchronized PublicKey getJwtPublicKey(JwsHeader<?> header) {
  String kid = header.getKeyId();
  if (header.getKeyId() == null) {
    LOG.warn(
        "'kid' is missing in the JWT token header. This is not possible to validate the token with OIDC provider keys");
    throw new JwtException("'kid' is missing in the JWT token header.");
  }
  try {
    return jwkProvider.get(kid).getPublicKey();
  } catch (JwkException e) {
    throw new JwtException(
        "Error during the retrieval of the public key during JWT token validation", e);
  }
}