com.auth0.jwk.Jwk Java Examples

The following examples show how to use com.auth0.jwk.Jwk. 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: CachingOpenIdMetadata.java    From botbuilder-java with MIT License 7 votes vote down vote up
private void refreshCache() {
    keyCache.clear();

    try {
        URL openIdUrl = new URL(this.url);
        HashMap<String, String> openIdConf =
            this.mapper.readValue(openIdUrl, new TypeReference<HashMap<String, Object>>() {
            });
        URL keysUrl = new URL(openIdConf.get("jwks_uri"));
        lastUpdated = System.currentTimeMillis();
        UrlJwkProvider provider = new UrlJwkProvider(keysUrl);
        keyCache = provider.getAll().stream().collect(Collectors.toMap(Jwk::getId, jwk -> jwk));
    } catch (IOException e) {
        LOGGER.error(String.format("Failed to load openID config: %s", e.getMessage()));
        lastUpdated = 0;
    } catch (SigningKeyNotFoundException keyexception) {
        LOGGER.error("refreshCache", keyexception);
        lastUpdated = 0;
    }
}
 
Example #2
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 #3
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 #4
Source File: KeycloakSigningKeyResolverTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldReturnPublicKey() throws Exception {
  final String kid = "123";
  final Jwk jwk = mock(Jwk.class);
  final Map<String, Object> param = new HashMap<>();
  param.put("kid", kid);
  final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  kpg.initialize(1024);
  final KeyPair keyPair = kpg.generateKeyPair();

  when(jwk.getPublicKey()).thenReturn(keyPair.getPublic());
  when(jwkProvider.get(eq(kid))).thenReturn(jwk);

  Key actual = signingKeyResolver.resolveSigningKey(new DefaultJwsHeader(param), "plaintext");
  assertEquals(actual, keyPair.getPublic());
}
 
Example #5
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 #6
Source File: SignatureVerifierTest.java    From auth0-java-mvc-common with MIT License 5 votes vote down vote up
private JwkProvider getRSProvider(String rsaPath) throws Exception {
    JwkProvider jwkProvider = mock(JwkProvider.class);
    Jwk jwk = mock(Jwk.class);
    when(jwkProvider.get("abc123")).thenReturn(jwk);
    RSAPublicKey key = readPublicKeyFromFile(rsaPath);
    when(jwk.getPublicKey()).thenReturn(key);
    return jwkProvider;
}