Search in sources :

Example 1 with OpenIdAuthenticationMapper

use of io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper in project micronaut-security by micronaut-projects.

the class DefaultOpenIdAuthorizationResponseHandler method validateOpenIdTokenResponse.

/**
 * @param nonce Nonce
 * @param clientConfiguration The client configuration
 * @param openIdProviderMetadata The provider metadata
 * @param openIdTokenResponse OpenID token response
 * @param authenticationMapper The user details mapper
 * @param state State
 * @return An Authentication response if the open id token could  be validated
 * @throws ParseException If the payload of the JWT doesn't represent a valid JSON object and a JWT claims set.
 */
private Optional<AuthenticationResponse> validateOpenIdTokenResponse(String nonce, OauthClientConfiguration clientConfiguration, OpenIdProviderMetadata openIdProviderMetadata, OpenIdTokenResponse openIdTokenResponse, @Nullable OpenIdAuthenticationMapper authenticationMapper, @Nullable State state) throws ParseException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Token endpoint returned a success response. Validating the JWT");
    }
    Optional<JWT> jwt = tokenResponseValidator.validate(clientConfiguration, openIdProviderMetadata, openIdTokenResponse, nonce);
    if (jwt.isPresent()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Token validation succeeded. Creating a user details");
        }
        OpenIdClaims claims = new JWTOpenIdClaims(jwt.get().getJWTClaimsSet());
        OpenIdAuthenticationMapper openIdAuthenticationMapper = authenticationMapper != null ? authenticationMapper : defaultAuthenticationMapper;
        return Optional.of(openIdAuthenticationMapper.createAuthenticationResponse(clientConfiguration.getName(), openIdTokenResponse, claims, state));
    }
    return Optional.empty();
}
Also used : JWT(com.nimbusds.jwt.JWT) JWTOpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.JWTOpenIdClaims) OpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.OpenIdClaims) JWTOpenIdClaims(io.micronaut.security.oauth2.endpoint.token.response.JWTOpenIdClaims) DefaultOpenIdAuthenticationMapper(io.micronaut.security.oauth2.endpoint.token.response.DefaultOpenIdAuthenticationMapper) OpenIdAuthenticationMapper(io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper)

Example 2 with OpenIdAuthenticationMapper

use of io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper in project micronaut-security by micronaut-projects.

the class OpenIdClientFactory method openIdClient.

/**
 * Creates an {@link OpenIdClient} from the provided parameters.
 *
 * @param openIdClientConfiguration The openid client configuration
 * @param clientConfiguration The client configuration
 * @param openIdProviderMetadata The open id provider metadata
 * @param authenticationMapper The user details mapper
 * @param redirectUrlBuilder The redirect URL builder
 * @param authorizationResponseHandler The authorization response handler
 * @param endSessionEndpointResolver The end session resolver
 * @param endSessionCallbackUrlBuilder The end session callback URL builder
 * @return The OpenID client, or null if the client configuration does not allow it
 */
@EachBean(OpenIdClientConfiguration.class)
@Requires(condition = OpenIdClientCondition.class)
@SuppressWarnings("java:S107")
DefaultOpenIdClient openIdClient(@Parameter OpenIdClientConfiguration openIdClientConfiguration, @Parameter OauthClientConfiguration clientConfiguration, @Parameter BeanProvider<DefaultOpenIdProviderMetadata> openIdProviderMetadata, @Parameter @Nullable OpenIdAuthenticationMapper authenticationMapper, AuthorizationRedirectHandler redirectUrlBuilder, OpenIdAuthorizationResponseHandler authorizationResponseHandler, EndSessionEndpointResolver endSessionEndpointResolver, EndSessionCallbackUrlBuilder endSessionCallbackUrlBuilder) {
    Supplier<OpenIdProviderMetadata> metadataSupplier = SupplierUtil.memoized(openIdProviderMetadata::get);
    EndSessionEndpoint endSessionEndpoint = null;
    if (openIdClientConfiguration.getEndSession().isEnabled()) {
        endSessionEndpoint = endSessionEndpointResolver.resolve(clientConfiguration, metadataSupplier, endSessionCallbackUrlBuilder).orElse(null);
    }
    return new DefaultOpenIdClient(clientConfiguration, metadataSupplier, authenticationMapper, redirectUrlBuilder, authorizationResponseHandler, beanContext, endSessionEndpoint);
}
Also used : EndSessionEndpoint(io.micronaut.security.oauth2.endpoint.endsession.request.EndSessionEndpoint) Requires(io.micronaut.context.annotation.Requires) EachBean(io.micronaut.context.annotation.EachBean)

Aggregations

JWT (com.nimbusds.jwt.JWT)1 EachBean (io.micronaut.context.annotation.EachBean)1 Requires (io.micronaut.context.annotation.Requires)1 EndSessionEndpoint (io.micronaut.security.oauth2.endpoint.endsession.request.EndSessionEndpoint)1 DefaultOpenIdAuthenticationMapper (io.micronaut.security.oauth2.endpoint.token.response.DefaultOpenIdAuthenticationMapper)1 JWTOpenIdClaims (io.micronaut.security.oauth2.endpoint.token.response.JWTOpenIdClaims)1 OpenIdAuthenticationMapper (io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper)1 OpenIdClaims (io.micronaut.security.oauth2.endpoint.token.response.OpenIdClaims)1