use of io.micronaut.context.BeanContext 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;
}
use of io.micronaut.context.BeanContext 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;
}
use of io.micronaut.context.BeanContext 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;
}
use of io.micronaut.context.BeanContext in project micronaut-starter by micronaut-projects.
the class CodeGenConfig method load.
public static CodeGenConfig load(BeanContext beanContext, File directory, ConsoleOutput consoleOutput) {
File micronautCli = new File(directory, "micronaut-cli.yml");
if (micronautCli.exists()) {
try (InputStream inputStream = Files.newInputStream(micronautCli.toPath())) {
Yaml yaml = new Yaml();
Map<String, Object> map = new LinkedHashMap<>();
Iterable<Object> objects = yaml.loadAll(inputStream);
Iterator<Object> i = objects.iterator();
if (i.hasNext()) {
while (i.hasNext()) {
Object object = i.next();
if (object instanceof Map) {
map.putAll((Map) object);
}
}
}
BeanIntrospection<CodeGenConfig> introspection = BeanIntrospection.getIntrospection(CodeGenConfig.class);
CodeGenConfig codeGenConfig = introspection.instantiate();
introspection.getBeanProperties().forEach(bp -> {
Object value = map.get(bp.getName());
if (value != null) {
bp.convertAndSet(codeGenConfig, value);
}
});
if (map.containsKey("profile")) {
codeGenConfig.legacy = true;
String profile = map.get("profile").toString();
if (profile.equals("service")) {
codeGenConfig.setApplicationType(ApplicationType.DEFAULT);
} else if (profile.equals("cli")) {
codeGenConfig.setApplicationType(ApplicationType.CLI);
} else if (profile.equals("function-aws") || profile.equals("function-aws-alexa")) {
codeGenConfig.setApplicationType(ApplicationType.FUNCTION);
} else if (profile.equals("grpc")) {
codeGenConfig.setApplicationType(ApplicationType.GRPC);
} else if (profile.equals("kafka") || profile.equals("rabbitmq")) {
codeGenConfig.setApplicationType(ApplicationType.MESSAGING);
} else {
return null;
}
AvailableFeatures availableFeatures = beanContext.getBean(AvailableFeatures.class, Qualifiers.byName(codeGenConfig.getApplicationType().getName()));
if (new File(directory, "build.gradle").exists()) {
codeGenConfig.setBuildTool(BuildTool.GRADLE);
} else if (new File(directory, "build.gradle.kts").exists()) {
codeGenConfig.setBuildTool(BuildTool.GRADLE_KOTLIN);
} else if (new File(directory, "pom.xml").exists()) {
codeGenConfig.setBuildTool(BuildTool.MAVEN);
} else {
return null;
}
codeGenConfig.setFeatures(availableFeatures.getAllFeatures().filter(f -> f instanceof DefaultFeature).map(DefaultFeature.class::cast).filter(f -> f.shouldApply(codeGenConfig.getApplicationType(), new Options(codeGenConfig.getSourceLanguage(), codeGenConfig.getTestFramework(), codeGenConfig.getBuildTool(), VersionInfo.getJavaVersion()), new HashSet<>())).map(Feature::getName).collect(Collectors.toList()));
consoleOutput.warning("This project is using Micronaut CLI v2 but is still using the v1 micronaut-cli.yml format");
consoleOutput.warning("To replace the configuration with the new format, run `mn update-cli-config`");
}
return codeGenConfig;
} catch (IOException e) {
}
}
return null;
}
use of io.micronaut.context.BeanContext 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;
}
Aggregations