Search in sources :

Example 1 with ValueResolver

use of io.micronaut.core.value.ValueResolver in project micronaut-security by micronaut-projects.

the class ClientCredentialsHeaderPropagatorEnabled 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));
            Optional<ClientCredentialsHeaderTokenPropagatorConfiguration> headerTokenConfiguration = clientConfiguration.getClientCredentials().flatMap(ClientCredentialsConfiguration::getHeaderPropagation);
            if (headerTokenConfiguration.isPresent()) {
                if (headerTokenConfiguration.get().isEnabled()) {
                    return true;
                } else {
                    context.fail("Client credentials header token handler is disabled");
                    return false;
                }
            } else {
                context.fail("Client credentials header token handler disabled due to a lack of configuration");
                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) ClientCredentialsConfiguration(io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsConfiguration) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) OauthClientConfiguration(io.micronaut.security.oauth2.configuration.OauthClientConfiguration)

Example 2 with ValueResolver

use of io.micronaut.core.value.ValueResolver 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 3 with ValueResolver

use of io.micronaut.core.value.ValueResolver in project micronaut-security by micronaut-projects.

the class ClientCredentialsEnabled 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 failureMessage = "Client credentials is disabled for the client [" + name + "]";
            if (clientConfiguration.isEnabled()) {
                Optional<ClientCredentialsConfiguration> clientCredentialsConfiguration = clientConfiguration.getClientCredentials();
                if (!clientCredentialsConfiguration.isPresent() || clientCredentialsConfiguration.get().isEnabled()) {
                    return true;
                } else {
                    context.fail(failureMessage);
                    return false;
                }
            } else {
                context.fail(failureMessage);
                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 4 with ValueResolver

use of io.micronaut.core.value.ValueResolver in project micronaut-security by micronaut-projects.

the class PasswordGrantCondition 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 password grant flow for provider [" + name;
            if (clientConfiguration.isEnabled()) {
                if (clientConfiguration.getGrantType() == GrantType.PASSWORD) {
                    if (clientConfiguration.getToken().isPresent()) {
                        if (beanContext.containsBean(OauthAuthenticationMapper.class, Qualifiers.byName(name))) {
                            return true;
                        } else {
                            context.fail(failureMsgPrefix + "] because no user details mapper could be found");
                        }
                    } else if (clientConfiguration.getOpenid().isPresent()) {
                        boolean hasOpenIdProviderMetadata = beanContext.containsBean(OpenIdProviderMetadata.class, Qualifiers.byName(name));
                        boolean hasTokenResponseValidator = beanContext.containsBean(OpenIdTokenResponseValidator.class);
                        if (hasOpenIdProviderMetadata && hasTokenResponseValidator) {
                            boolean hasAuthenticationMapper = beanContext.containsBean(OpenIdAuthenticationMapper.class, Qualifiers.byName(name));
                            if (!hasAuthenticationMapper) {
                                hasAuthenticationMapper = beanContext.containsBean(DefaultOpenIdAuthenticationMapper.class);
                            }
                            if (hasAuthenticationMapper) {
                                return true;
                            } else {
                                context.fail(failureMsgPrefix + "] because no user details mapper could be found");
                            }
                        } else {
                            context.fail(failureMsgPrefix + "] because no provider metadata and token validator could be found");
                        }
                    } else {
                        context.fail(failureMsgPrefix + "] because no token endpoint or openid configuration was found");
                    }
                } else {
                    context.fail(failureMsgPrefix + "] because the grant type is not 'password'");
                }
            } 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) OpenIdTokenResponseValidator(io.micronaut.security.oauth2.endpoint.token.response.validation.OpenIdTokenResponseValidator) ValueResolver(io.micronaut.core.value.ValueResolver) DefaultOpenIdAuthenticationMapper(io.micronaut.security.oauth2.endpoint.token.response.DefaultOpenIdAuthenticationMapper) OpenIdAuthenticationMapper(io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) OauthClientConfiguration(io.micronaut.security.oauth2.configuration.OauthClientConfiguration) OpenIdProviderMetadata(io.micronaut.security.oauth2.client.OpenIdProviderMetadata)

Example 5 with ValueResolver

use of io.micronaut.core.value.ValueResolver in project micronaut-security by micronaut-projects.

the class OpenIdClientCondition 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));
            OpenIdClientConfiguration openIdClientConfiguration = clientConfiguration.getOpenid().get();
            String failureMessagePrefix = "Skipped OpenID client creation for provider [" + name;
            if (clientConfiguration.isEnabled()) {
                if (openIdClientConfiguration.getIssuer().isPresent() || endpointsManuallyConfigured(openIdClientConfiguration)) {
                    if (clientConfiguration.getGrantType() == GrantType.AUTHORIZATION_CODE) {
                        Optional<AuthorizationEndpointConfiguration> authorization = openIdClientConfiguration.getAuthorization();
                        if (!authorization.isPresent() || authorization.get().getResponseType() == ResponseType.CODE) {
                            return true;
                        } else {
                            context.fail(failureMessagePrefix + "] because the response type is not 'code'");
                        }
                    } else {
                        context.fail(failureMessagePrefix + "] because the grant type is not 'authorization-code'");
                    }
                } else {
                    context.fail(failureMessagePrefix + "] because no issuer is configured");
                }
            } else {
                context.fail(failureMessagePrefix + "] because the configuration is disabled");
            }
            return false;
        }
    }
    return true;
}
Also used : BeanContext(io.micronaut.context.BeanContext) Named(io.micronaut.core.naming.Named) AuthorizationEndpointConfiguration(io.micronaut.security.oauth2.configuration.endpoints.AuthorizationEndpointConfiguration) ApplicationContext(io.micronaut.context.ApplicationContext) ValueResolver(io.micronaut.core.value.ValueResolver) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) OauthClientConfiguration(io.micronaut.security.oauth2.configuration.OauthClientConfiguration) OpenIdClientConfiguration(io.micronaut.security.oauth2.configuration.OpenIdClientConfiguration)

Aggregations

AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)6 Named (io.micronaut.core.naming.Named)6 ValueResolver (io.micronaut.core.value.ValueResolver)6 ApplicationContext (io.micronaut.context.ApplicationContext)5 BeanContext (io.micronaut.context.BeanContext)5 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)5 LdapConfiguration (io.micronaut.security.ldap.configuration.LdapConfiguration)1 OpenIdProviderMetadata (io.micronaut.security.oauth2.client.OpenIdProviderMetadata)1 ClientCredentialsConfiguration (io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsConfiguration)1 OpenIdClientConfiguration (io.micronaut.security.oauth2.configuration.OpenIdClientConfiguration)1 AuthorizationEndpointConfiguration (io.micronaut.security.oauth2.configuration.endpoints.AuthorizationEndpointConfiguration)1 DefaultOpenIdAuthenticationMapper (io.micronaut.security.oauth2.endpoint.token.response.DefaultOpenIdAuthenticationMapper)1 OpenIdAuthenticationMapper (io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper)1 OpenIdTokenResponseValidator (io.micronaut.security.oauth2.endpoint.token.response.validation.OpenIdTokenResponseValidator)1