Search in sources :

Example 1 with EndpointConfiguration

use of io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration in project micronaut-security by micronaut-projects.

the class OauthClientCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    AnnotationMetadataProvider component = context.getComponent();
    BeanContext beanContext = context.getBeanContext();
    if (beanContext instanceof ApplicationContext && component instanceof ValueResolver) {
        Optional<String> optional = ((ValueResolver) component).get(Named.class.getName(), String.class);
        if (optional.isPresent()) {
            String name = optional.get();
            OauthClientConfiguration clientConfiguration = beanContext.getBean(OauthClientConfiguration.class, Qualifiers.byName(name));
            String failureMsgPrefix = "Skipped client creation for provider [" + name;
            if (clientConfiguration.isEnabled()) {
                if (clientConfiguration.getAuthorization().flatMap(EndpointConfiguration::getUrl).isPresent()) {
                    if (clientConfiguration.getToken().flatMap(EndpointConfiguration::getUrl).isPresent()) {
                        if (clientConfiguration.getGrantType() == GrantType.AUTHORIZATION_CODE) {
                            return true;
                        } else {
                            context.fail(failureMsgPrefix + "] because grant type is not authorization code");
                        }
                    } else {
                        context.fail(failureMsgPrefix + "] because no token endpoint is configured");
                    }
                } else {
                    context.fail(failureMsgPrefix + "] because no authorization endpoint is configured");
                }
            } else {
                context.fail(failureMsgPrefix + "] because the configuration is disabled");
            }
            return false;
        }
    }
    return true;
}
Also used : BeanContext(io.micronaut.context.BeanContext) Named(io.micronaut.core.naming.Named) ApplicationContext(io.micronaut.context.ApplicationContext) ValueResolver(io.micronaut.core.value.ValueResolver) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) OauthClientConfiguration(io.micronaut.security.oauth2.configuration.OauthClientConfiguration)

Example 2 with EndpointConfiguration

use of io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration in project micronaut-security by micronaut-projects.

the class DefaultOauthClient method authorizationRedirect.

@Override
public Publisher<MutableHttpResponse<?>> authorizationRedirect(HttpRequest<?> originating) {
    AuthorizationRequest authorizationRequest = beanContext.createBean(OauthAuthorizationRequest.class, originating, clientConfiguration);
    String authorizationEndpoint = clientConfiguration.getAuthorization().flatMap(EndpointConfiguration::getUrl).orElseThrow(() -> new ConfigurationException("Oauth client requires the authorization URL to be set in configuration"));
    if (LOG.isTraceEnabled()) {
        LOG.trace("Starting authorization code grant flow to provider [{}]. Redirecting to [{}]", getName(), authorizationEndpoint);
    }
    return Flux.just(redirectHandler.redirect(authorizationRequest, authorizationEndpoint));
}
Also used : OauthAuthorizationRequest(io.micronaut.security.oauth2.endpoint.authorization.request.OauthAuthorizationRequest) AuthorizationRequest(io.micronaut.security.oauth2.endpoint.authorization.request.AuthorizationRequest) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException)

Example 3 with EndpointConfiguration

use of io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration in project micronaut-security by micronaut-projects.

the class OauthPasswordAuthenticationProvider method getTokenEndpoint.

/**
 * Builds the secure endpoint from the client configuration.
 *
 * @param clientConfiguration The client configuration
 * @return The token endpoint
 */
protected SecureEndpoint getTokenEndpoint(OauthClientConfiguration clientConfiguration) {
    SecureEndpointConfiguration endpointConfiguration = clientConfiguration.getToken().orElseThrow(() -> new IllegalArgumentException("Token endpoint configuration is missing for provider [" + clientConfiguration.getName() + "]"));
    List<AuthenticationMethod> authMethodsSupported = Collections.singletonList(endpointConfiguration.getAuthMethod().orElse(AuthenticationMethod.CLIENT_SECRET_BASIC));
    String url = endpointConfiguration.getUrl().orElseThrow(() -> new IllegalArgumentException("Token endpoint URL is null for provider [" + clientConfiguration.getName() + "]"));
    return new DefaultSecureEndpoint(url, authMethodsSupported);
}
Also used : SecureEndpointConfiguration(io.micronaut.security.oauth2.configuration.endpoints.SecureEndpointConfiguration) DefaultSecureEndpoint(io.micronaut.security.oauth2.endpoint.DefaultSecureEndpoint) AuthenticationMethod(io.micronaut.security.oauth2.endpoint.AuthenticationMethod)

Example 4 with EndpointConfiguration

use of io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration in project micronaut-security by micronaut-projects.

the class OpenIdClientFactory method overrideFromConfig.

private void overrideFromConfig(DefaultOpenIdProviderMetadata configuration, OpenIdClientConfiguration openIdClientConfiguration, OauthClientConfiguration oauthClientConfiguration) {
    openIdClientConfiguration.getJwksUri().ifPresent(configuration::setJwksUri);
    oauthClientConfiguration.getIntrospection().ifPresent(introspection -> {
        introspection.getUrl().ifPresent(configuration::setIntrospectionEndpoint);
        introspection.getAuthMethod().ifPresent(authMethod -> configuration.setIntrospectionEndpointAuthMethodsSupported(Collections.singletonList(authMethod.toString())));
    });
    oauthClientConfiguration.getRevocation().ifPresent(revocation -> {
        revocation.getUrl().ifPresent(configuration::setRevocationEndpoint);
        revocation.getAuthMethod().ifPresent(authMethod -> configuration.setRevocationEndpointAuthMethodsSupported(Collections.singletonList(authMethod.toString())));
    });
    openIdClientConfiguration.getRegistration().flatMap(EndpointConfiguration::getUrl).ifPresent(configuration::setRegistrationEndpoint);
    openIdClientConfiguration.getUserInfo().flatMap(EndpointConfiguration::getUrl).ifPresent(configuration::setUserinfoEndpoint);
    openIdClientConfiguration.getAuthorization().flatMap(EndpointConfiguration::getUrl).ifPresent(configuration::setAuthorizationEndpoint);
    openIdClientConfiguration.getToken().ifPresent(token -> {
        token.getUrl().ifPresent(configuration::setTokenEndpoint);
        token.getAuthMethod().ifPresent(authMethod -> configuration.setTokenEndpointAuthMethodsSupported(Collections.singletonList(authMethod.toString())));
    });
    EndSessionEndpointConfiguration endSession = openIdClientConfiguration.getEndSession();
    if (endSession.isEnabled()) {
        endSession.getUrl().ifPresent(configuration::setEndSessionEndpoint);
    }
}
Also used : EndSessionEndpointConfiguration(io.micronaut.security.oauth2.configuration.endpoints.EndSessionEndpointConfiguration)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)1 BeanContext (io.micronaut.context.BeanContext)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)1 Named (io.micronaut.core.naming.Named)1 ValueResolver (io.micronaut.core.value.ValueResolver)1 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)1 EndSessionEndpointConfiguration (io.micronaut.security.oauth2.configuration.endpoints.EndSessionEndpointConfiguration)1 SecureEndpointConfiguration (io.micronaut.security.oauth2.configuration.endpoints.SecureEndpointConfiguration)1 AuthenticationMethod (io.micronaut.security.oauth2.endpoint.AuthenticationMethod)1 DefaultSecureEndpoint (io.micronaut.security.oauth2.endpoint.DefaultSecureEndpoint)1 AuthorizationRequest (io.micronaut.security.oauth2.endpoint.authorization.request.AuthorizationRequest)1 OauthAuthorizationRequest (io.micronaut.security.oauth2.endpoint.authorization.request.OauthAuthorizationRequest)1