use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.
the class OidcJsonWebKeystoreGeneratorService method generate.
/**
* Generate.
*/
@PostConstruct
@SneakyThrows
public void generate() {
final File file = oidcProperties.getJwksFile().getFile();
if (!file.exists()) {
final RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(rsaJsonWebKey);
final String data = jsonWebKeySet.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
FileUtils.write(file, data, StandardCharsets.UTF_8);
LOGGER.debug("Generated JSON web keystore at [{}]", file);
} else {
LOGGER.debug("Located JSON web keystore at [{}]", file);
}
}
use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.
the class OidcServiceJsonWebKeystoreCacheLoader method buildJsonWebKeySet.
private Optional<JsonWebKeySet> buildJsonWebKeySet(final OidcRegisteredService service) {
try {
LOGGER.debug("Loading JSON web key from [{}]", service.getJwks());
final Resource resource = this.resourceLoader.getResource(service.getJwks());
final JsonWebKeySet jsonWebKeySet = buildJsonWebKeySet(resource);
if (jsonWebKeySet == null || jsonWebKeySet.getJsonWebKeys().isEmpty()) {
LOGGER.warn("No JSON web keys could be found for [{}]", service);
return Optional.empty();
}
final long badKeysCount = jsonWebKeySet.getJsonWebKeys().stream().filter(k -> StringUtils.isBlank(k.getAlgorithm()) && StringUtils.isBlank(k.getKeyId()) && StringUtils.isBlank(k.getKeyType())).count();
if (badKeysCount == jsonWebKeySet.getJsonWebKeys().size()) {
LOGGER.warn("No valid JSON web keys could be found for [{}]", service);
return Optional.empty();
}
final RsaJsonWebKey webKey = getJsonSigningWebKeyFromJwks(jsonWebKeySet);
if (webKey.getPublicKey() == null) {
LOGGER.warn("JSON web key retrieved [{}] has no associated public key", webKey.getKeyId());
return Optional.empty();
}
return Optional.of(jsonWebKeySet);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return Optional.empty();
}
use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.
the class OidcJwksEndpointController method handleRequestInternal.
/**
* Handle request for jwk set.
*
* @param request the request
* @param response the response
* @param model the model
* @return the jwk set
*/
@GetMapping(value = '/' + OidcConstants.BASE_OIDC_URL + '/' + OidcConstants.JWKS_URL, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response, final Model model) {
try {
final String jsonJwks = IOUtils.toString(this.jwksFile.getInputStream(), StandardCharsets.UTF_8);
final JsonWebKeySet jsonWebKeySet = new JsonWebKeySet(jsonJwks);
this.servicesManager.getAllServices().stream().filter(s -> s instanceof OidcRegisteredService && StringUtils.isNotBlank(((OidcRegisteredService) s).getJwks())).forEach(Unchecked.consumer(s -> {
final OidcRegisteredService service = (OidcRegisteredService) s;
final Resource resource = this.resourceLoader.getResource(service.getJwks());
final JsonWebKeySet set = new JsonWebKeySet(IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8));
set.getJsonWebKeys().forEach(jsonWebKeySet::addJsonWebKey);
}));
final String body = jsonWebKeySet.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
return new ResponseEntity<>(body, HttpStatus.OK);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.
the class OidcRegisteredServiceJwtAccessTokenCipherExecutorNoCacheTests method verifyCipherOperation.
@Test
public void verifyCipherOperation() {
val id = UUID.randomUUID().toString();
val defaultCache = mock(LoadingCache.class);
when(defaultCache.get(any())).thenReturn(Optional.empty());
val serviceCache = mock(LoadingCache.class);
when(serviceCache.get(any())).thenReturn(Optional.empty());
val cipher = new OidcRegisteredServiceJwtAccessTokenCipherExecutor(defaultCache, serviceCache, OidcIssuerService.immutable(id));
val service = getOidcRegisteredService("whatever");
val exec = cipher.createCipherExecutorInstance(null, null, service, BaseStringCipherExecutor.CipherOperationsStrategyType.ENCRYPT_AND_SIGN);
assertEquals("value", exec.decode("value", new Object[] { service }));
when(serviceCache.get(any())).thenReturn(Optional.of(new JsonWebKeySet(mock(PublicJsonWebKey.class))));
assertEquals("value", exec.decode("value", new Object[] { service }));
}
use of org.jose4j.jwk.JsonWebKeySet in project cas by apereo.
the class OidcGroovyJsonWebKeystoreGeneratorServiceTests method verifyOperation.
@Test
public void verifyOperation() throws Exception {
val resource = oidcJsonWebKeystoreGeneratorService.generate();
assertTrue(resource.exists());
assertTrue(oidcJsonWebKeystoreGeneratorService.find().isPresent());
assertDoesNotThrow(new Executable() {
@Override
public void execute() throws Throwable {
val results = new String(IOUtils.toByteArray(resource.getInputStream()), StandardCharsets.UTF_8);
new JsonWebKeySet(results);
}
});
}
Aggregations