Search in sources :

Example 6 with BeanContext

use of io.micronaut.context.BeanContext 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)

Example 7 with BeanContext

use of io.micronaut.context.BeanContext in project micronaut-test by micronaut-projects.

the class TestActiveCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    if (context.getComponent() instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) context.getComponent();
        final BeanContext beanContext = context.getBeanContext();
        final Optional<Class<?>> declaringType = definition.getDeclaringType();
        if (beanContext instanceof ApplicationContext) {
            ApplicationContext applicationContext = (ApplicationContext) beanContext;
            final Class activeSpecClazz = applicationContext.get(ACTIVE_SPEC_CLAZZ, Class.class).orElse(null);
            final String activeSpecName = Optional.ofNullable(activeSpecClazz).map(clazz -> clazz.getPackage().getName() + "." + clazz.getSimpleName()).orElse(null);
            if (definition.isAnnotationPresent(MockBean.class) && declaringType.isPresent()) {
                final Class<?> declaringTypeClass = declaringType.get();
                String declaringTypeName = declaringTypeClass.getName();
                if (activeSpecClazz != null) {
                    final String packageName = NameUtils.getPackageName(activeSpecName);
                    final String simpleName = NameUtils.getSimpleName(activeSpecName);
                    final String rootName = packageName + ".$" + simpleName;
                    if (definition.isProxy()) {
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) || declaringTypeName.equals(rootName) || declaringTypeName.startsWith(rootName + "$");
                    } else {
                        return declaringTypeClass.isAssignableFrom(activeSpecClazz) && // Check if super class def is part of the current spec
                        definition.getClass().getName().startsWith(rootName + "$") || activeSpecName.equals(declaringTypeName) || declaringTypeName.startsWith(activeSpecName + "$");
                    }
                } else {
                    context.fail("@MockBean of type " + definition.getBeanType() + " not within scope of parent test.");
                    return false;
                }
            } else {
                if (activeSpecName != null) {
                    boolean beanTypeMatches = activeSpecName.equals(definition.getBeanType().getName());
                    return beanTypeMatches || (declaringType.isPresent() && activeSpecClazz == declaringType.get());
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    } else {
        return true;
    }
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) MockBean(io.micronaut.test.annotation.MockBean) Condition(io.micronaut.context.condition.Condition) BeanContext(io.micronaut.context.BeanContext) Optional(java.util.Optional) BeanDefinition(io.micronaut.inject.BeanDefinition) NameUtils(io.micronaut.core.naming.NameUtils) ConditionContext(io.micronaut.context.condition.ConditionContext) BeanContext(io.micronaut.context.BeanContext) ApplicationContext(io.micronaut.context.ApplicationContext) MockBean(io.micronaut.test.annotation.MockBean) BeanDefinition(io.micronaut.inject.BeanDefinition)

Aggregations

BeanContext (io.micronaut.context.BeanContext)7 ApplicationContext (io.micronaut.context.ApplicationContext)6 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)5 Named (io.micronaut.core.naming.Named)5 ValueResolver (io.micronaut.core.value.ValueResolver)5 OauthClientConfiguration (io.micronaut.security.oauth2.configuration.OauthClientConfiguration)5 Condition (io.micronaut.context.condition.Condition)1 ConditionContext (io.micronaut.context.condition.ConditionContext)1 Introspected (io.micronaut.core.annotation.Introspected)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 NameUtils (io.micronaut.core.naming.NameUtils)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)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