Search in sources :

Example 1 with ClientCredentialsConfiguration

use of io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsConfiguration 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 ClientCredentialsConfiguration

use of io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsConfiguration 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)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)2 BeanContext (io.micronaut.context.BeanContext)2 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)2 Named (io.micronaut.core.naming.Named)2 ValueResolver (io.micronaut.core.value.ValueResolver)2 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)2 ClientCredentialsConfiguration (io.micronaut.security.oauth2.client.clientcredentials.ClientCredentialsConfiguration)1