use of org.wildfly.swarm.undertow.descriptors.JBossWebAsset in project wildfly-swarm by wildfly-swarm.
the class MPJWTAuthExtensionArchivePreparer method process.
@Override
public void process() throws Exception {
WARArchive war = archive.as(WARArchive.class);
// Check for LoginConfig annotation
Collection<AnnotationInstance> lcAnnotations = index.getAnnotations(LOGIN_CONFIG);
for (AnnotationInstance lc : lcAnnotations) {
AnnotationValue authMethod = lc.value("authMethod");
AnnotationValue realmName = lc.value("realmName");
String realm = realmName != null ? realmName.asString() : "";
// Set the web.xml login-config auth-method and jboss-web.xml security domain
if (authMethod != null) {
WebXmlAsset webXml = war.findWebXmlAsset();
webXml.setLoginConfig(authMethod.asString(), realm);
}
if (realm.length() > 0) {
JBossWebAsset jBossWeb = war.findJbossWebAsset();
jBossWeb.setSecurityDomain(realm);
}
}
// Get the @ApplicationPath setting
WebXmlAsset webXml = war.findWebXmlAsset();
String appPath = "/";
Collection<AnnotationInstance> appPaths = index.getAnnotations(APP_PATH);
if (!appPaths.isEmpty()) {
appPath = appPaths.iterator().next().value().asString();
}
// Process the @RolesAllowed, @PermitAll and @DenyAll annotations
Collection<AnnotationInstance> rolesAnnotations = index.getAnnotations(ROLES_ALLOWED);
for (AnnotationInstance annotation : rolesAnnotations) {
if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
// Process the root resource
String[] roles = annotation.value().asStringArray();
ClassInfo classInfo = annotation.target().asClass();
if (!scannedClasses.contains(classInfo.name())) {
generateSecurityConstraints(webXml, classInfo, roles, appPath);
}
} else if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
// Process the containing root resource if it has not been already
MethodInfo methodInfo = annotation.target().asMethod();
ClassInfo classInfo = methodInfo.declaringClass();
if (!scannedClasses.contains(classInfo.name())) {
String[] roles = {};
generateSecurityConstraints(webXml, classInfo, roles, appPath);
}
}
}
// Handle the verification configuration on the fraction
if (fraction.getTokenIssuer().isPresent()) {
log.debugf("Issuer: %s", fraction.getTokenIssuer().get());
war.addAsManifestResource(new StringAsset(fraction.getTokenIssuer().get()), "MP-JWT-ISSUER");
}
if (fraction.getPublicKey() != null) {
log.debugf("PublicKey: %s", fraction.getPublicKey());
war.addAsManifestResource(new StringAsset(fraction.getPublicKey()), "MP-JWT-SIGNER");
}
if (log.isTraceEnabled()) {
log.trace("war: " + war.toString(true));
}
}
use of org.wildfly.swarm.undertow.descriptors.JBossWebAsset in project wildfly-swarm by wildfly-swarm.
the class HttpSecurityPreparer method process.
@SuppressWarnings("unchecked")
@Override
public void process() {
if (deploymentConfigs == null || deploymentConfigs.isEmpty()) {
return;
}
// find a matching archive declaration
Optional<String> match = deploymentConfigs.keySet().stream().filter(c -> archive.getName().equals(c)).findFirst();
if (!match.isPresent()) {
// no matching archive
return;
}
Map<String, Object> matchingConfig = (Map<String, Object>) deploymentConfigs.get(match.get());
if (!matchingConfig.containsKey("web")) {
// missing web configuration
return;
}
Map<String, Object> deploymentConfig = (Map<String, Object>) matchingConfig.get("web");
WARArchive war = archive.as(WARArchive.class);
WebXmlAsset webXml = war.findWebXmlAsset();
JBossWebAsset jbossWeb = war.findJbossWebAsset();
// login-config
Map<String, Object> loginConfig = (Map<String, Object>) deploymentConfig.get("login-config");
if (loginConfig != null) {
String authMethod = (String) loginConfig.getOrDefault("auth-method", "NONE");
// Setup login-config
webXml.setLoginConfig(authMethod, "ignored");
// security domain
if (loginConfig.containsKey("security-domain")) {
jbossWeb.setSecurityDomain((String) loginConfig.get("security-domain"));
}
// form login
if (loginConfig.containsKey("form-login-config")) {
Map<String, Object> formLoginConfig = (Map<String, Object>) loginConfig.get("form-login-config");
webXml.setFormLoginConfig("Security Realm", (String) formLoginConfig.get("form-login-page"), (String) formLoginConfig.get("form-error-page"));
}
}
// security constraints
List<Map<String, Object>> securityConstraints = (List<Map<String, Object>>) deploymentConfig.getOrDefault("security-constraints", Collections.EMPTY_LIST);
for (Map<String, Object> sc : securityConstraints) {
SecurityConstraint securityConstraint = webXml.protect((String) sc.getOrDefault("url-pattern", "/*"));
((List<String>) sc.getOrDefault("methods", Collections.emptyList())).forEach(securityConstraint::withMethod);
((List<String>) sc.getOrDefault("roles", Collections.emptyList())).forEach(securityConstraint::withRole);
}
}
use of org.wildfly.swarm.undertow.descriptors.JBossWebAsset in project wildfly-swarm by wildfly-swarm.
the class JbossWebAssetTest method testEmpty.
@Test
public void testEmpty() throws Exception {
JBossWebAsset asset = new JBossWebAsset();
assertThat(asset.isRootSet()).isFalse();
assertThat(asset.getContextRoot()).isNull();
asset.setContextRoot("/myRoot");
assertThat(asset.isRootSet()).isTrue();
assertThat(asset.getContextRoot()).isEqualTo("/myRoot");
asset.setContextRoot("/anotherRoot");
assertThat(asset.isRootSet()).isTrue();
assertThat(asset.getContextRoot()).isEqualTo("/anotherRoot");
asset.setSecurityDomain("some-security-domain");
assertThat(asset.getSecurityDomain()).isEqualTo("some-security-domain");
}
Aggregations