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;
}
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;
}
}
Aggregations