Search in sources :

Example 6 with OidcJsonWebKeyCacheKey

use of org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey in project cas by apereo.

the class OidcJwtAccessTokenCipherExecutor method getJsonWebKeyFor.

private Optional<JsonWebKeySet> getJsonWebKeyFor(final OidcJsonWebKeyUsage usage) {
    val issuer = oidcIssuerService.determineIssuer(Optional.empty());
    LOGGER.trace("Determined issuer [{}] to fetch the JSON web key", issuer);
    return Objects.requireNonNull(defaultJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(issuer, usage)));
}
Also used : lombok.val(lombok.val) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey)

Example 7 with OidcJsonWebKeyCacheKey

use of org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey in project cas by apereo.

the class OidcPrivateKeyJwtAuthenticatorTests method verifyAction.

@Test
public void verifyAction() throws Exception {
    val auth = new OidcPrivateKeyJwtAuthenticator(servicesManager, registeredServiceAccessStrategyEnforcer, ticketRegistry, webApplicationServiceFactory, casProperties, applicationContext);
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val context = new JEEContext(request, response);
    val audience = casProperties.getServer().getPrefix().concat('/' + OidcConstants.BASE_OIDC_URL + '/' + OidcConstants.ACCESS_TOKEN_URL);
    val registeredService = getOidcRegisteredService();
    registeredService.setClientId(UUID.randomUUID().toString());
    val file = File.createTempFile("jwks-service", ".jwks");
    val core = casProperties.getAuthn().getOidc().getJwks().getCore();
    val jsonWebKey = OidcJsonWebKeyStoreUtils.generateJsonWebKey(core.getJwksType(), core.getJwksKeySize(), OidcJsonWebKeyUsage.SIGNING);
    jsonWebKey.setKeyId("cas-kid");
    val jsonWebKeySet = new JsonWebKeySet(jsonWebKey);
    val data = jsonWebKeySet.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
    FileUtils.write(file, data, StandardCharsets.UTF_8);
    registeredService.setJwks("file://" + file.getAbsolutePath());
    servicesManager.save(registeredService);
    val claims = getClaims(registeredService.getClientId(), registeredService.getClientId(), registeredService.getClientId(), audience);
    val webKeys = oidcServiceJsonWebKeystoreCache.get(new OidcJsonWebKeyCacheKey(registeredService, OidcJsonWebKeyUsage.SIGNING)).get();
    val key = (PublicJsonWebKey) webKeys.getJsonWebKeys().get(0);
    val jwt = EncodingUtils.signJwsRSASha512(key.getPrivateKey(), claims.toJson().getBytes(StandardCharsets.UTF_8), Map.of());
    val credentials = getCredential(request, OAuth20Constants.CLIENT_ASSERTION_TYPE_JWT_BEARER, new String(jwt, StandardCharsets.UTF_8), registeredService.getClientId());
    auth.validate(credentials, context, JEESessionStore.INSTANCE);
    assertNotNull(credentials.getUserProfile());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JEEContext(org.pac4j.core.context.JEEContext) JsonWebKeySet(org.jose4j.jwk.JsonWebKeySet) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OidcJsonWebKeyCacheKey(org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey) PublicJsonWebKey(org.jose4j.jwk.PublicJsonWebKey) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)7 OidcJsonWebKeyCacheKey (org.apereo.cas.oidc.jwks.OidcJsonWebKeyCacheKey)7 PublicJsonWebKey (org.jose4j.jwk.PublicJsonWebKey)5 OidcRegisteredService (org.apereo.cas.services.OidcRegisteredService)2 OAuthRegisteredService (org.apereo.cas.support.oauth.services.OAuthRegisteredService)2 JsonWebKey (org.jose4j.jwk.JsonWebKey)2 JsonWebKeySet (org.jose4j.jwk.JsonWebKeySet)2 LoadingCache (com.github.benmanes.caffeine.cache.LoadingCache)1 Serializable (java.io.Serializable)1 Key (java.security.Key)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 OidcIssuerService (org.apereo.cas.oidc.issuer.OidcIssuerService)1 OidcJsonWebKeyUsage (org.apereo.cas.oidc.jwks.OidcJsonWebKeyUsage)1