use of io.jans.as.model.jwk.JSONWebKeySet in project jans by JanssenProject.
the class KeyGeneratorService method getKeysFromStorage.
public JSONWebKeySet getKeysFromStorage() {
ExpiredObject expiredObject = persistenceService.getExpiredObject(ExpiredObjectType.JWKS.getValue());
if (expiredObject == null || Strings.isNullOrEmpty(expiredObject.getValue())) {
return null;
}
JSONObject keysInJson = new JSONObject(expiredObject.getValue());
JSONWebKeySet keys = JSONWebKeySet.fromJSONObject(keysInJson);
try {
if (hasKeysExpired(expiredObject)) {
LOG.trace("The keys in storage got expired. Deleting the expired keys from storage.");
deleteKeysFromStorage();
return null;
}
} catch (Exception e) {
LOG.error("Error in reading expiry date or deleting expired keys from storage. Trying to delete the keys from storage.", e);
deleteKeysFromStorage();
return null;
}
return keys;
}
use of io.jans.as.model.jwk.JSONWebKeySet in project jans by JanssenProject.
the class PublicOpKeyService method getPublicKey.
public PublicKey getPublicKey(String jwkSetUrl, String keyId, SignatureAlgorithm signatureAlgorithm, Use use) {
// Get keys from cache if present
Optional<PublicKey> cachedKey = getCachedKey(jwkSetUrl, keyId);
if (cachedKey.isPresent()) {
LOG.debug("Taken public key from cache. jwks_url: {}, kid : {} ", jwkSetUrl, keyId);
return cachedKey.get();
}
// Request jwks from OP
JwkClient jwkClient = opClientFactory.createJwkClient(jwkSetUrl);
jwkClient.setExecutor(new ApacheHttpClient43Engine(httpService.getHttpClient()));
JwkResponse jwkResponse = jwkClient.exec();
if (jwkResponse == null || jwkResponse.getStatus() != 200) {
LOG.error("Failed to fetch public key from OP. Obtained Response : {}", (jwkResponse == null ? jwkResponse : jwkResponse.getStatus()));
throw new RuntimeException("Failed to fetch public key from OP. Obtained Response : " + (jwkResponse == null ? jwkResponse : jwkResponse.getStatus()));
}
if (!Strings.isNullOrEmpty(keyId)) {
PublicKey publicKey = jwkResponse.getPublicKey(keyId);
if (publicKey != null) {
cache.put((new Pair<>(jwkSetUrl, keyId)), publicKey);
return publicKey;
}
} else {
JSONWebKeySet jsonWebKeySet = jwkResponse.getJwks();
List<PublicKey> pks = Lists.newArrayList();
for (JSONWebKey key : jsonWebKeySet.getKeys()) {
if (key.getKty() == null)
continue;
if (signatureAlgorithm.getFamily().toString().equals(key.getKty().toString()) && (use == null || use == key.getUse())) {
pks.add(getPublicKey(key));
}
}
if (pks.size() > 1) {
LOG.error("Multiple matching keys found in issuer's jwks_uri for algorithm : {}. `kid` must be provided in this case.", signatureAlgorithm.getName());
throw new RuntimeException("Multiple matching keys found in issuer's jwks_uri for algorithm : " + signatureAlgorithm.getName() + ". `kid` must be provided in this case.");
}
if (pks.size() == 1) {
if (!Strings.isNullOrEmpty(pks.get(0).getKeyId())) {
cache.put((new Pair<>(jwkSetUrl, pks.get(0).getKeyId())), pks.get(0));
}
return pks.get(0);
}
}
LOG.error("Failed to fetch public key from OP.");
throw new RuntimeException("Failed to fetch public key from OP.");
}
Aggregations