Search in sources :

Example 1 with JBossWebAsset

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));
    }
}
Also used : JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) AnnotationValue(org.jboss.jandex.AnnotationValue) MethodInfo(org.jboss.jandex.MethodInfo) WARArchive(org.wildfly.swarm.undertow.WARArchive) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with JBossWebAsset

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);
    }
}
Also used : DeploymentProcessor(org.wildfly.swarm.spi.api.DeploymentProcessor) DeploymentScoped(org.wildfly.swarm.spi.runtime.annotations.DeploymentScoped) Logger(org.jboss.logging.Logger) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) Archive(org.jboss.shrinkwrap.api.Archive) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) Inject(javax.inject.Inject) List(java.util.List) Configurable(org.wildfly.swarm.spi.api.annotations.Configurable) Map(java.util.Map) WARArchive(org.wildfly.swarm.undertow.WARArchive) AttributeDocumentation(org.wildfly.swarm.config.runtime.AttributeDocumentation) Optional(java.util.Optional) Collections(java.util.Collections) JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) WebXmlAsset(org.wildfly.swarm.undertow.descriptors.WebXmlAsset) WARArchive(org.wildfly.swarm.undertow.WARArchive) SecurityConstraint(org.wildfly.swarm.undertow.descriptors.SecurityConstraint) List(java.util.List) Map(java.util.Map)

Example 3 with JBossWebAsset

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");
}
Also used : JBossWebAsset(org.wildfly.swarm.undertow.descriptors.JBossWebAsset) Test(org.junit.Test)

Aggregations

JBossWebAsset (org.wildfly.swarm.undertow.descriptors.JBossWebAsset)3 WARArchive (org.wildfly.swarm.undertow.WARArchive)2 WebXmlAsset (org.wildfly.swarm.undertow.descriptors.WebXmlAsset)2 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 AnnotationInstance (org.jboss.jandex.AnnotationInstance)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1 ClassInfo (org.jboss.jandex.ClassInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1 Logger (org.jboss.logging.Logger)1 Archive (org.jboss.shrinkwrap.api.Archive)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1 Test (org.junit.Test)1 AttributeDocumentation (org.wildfly.swarm.config.runtime.AttributeDocumentation)1 DeploymentProcessor (org.wildfly.swarm.spi.api.DeploymentProcessor)1 Configurable (org.wildfly.swarm.spi.api.annotations.Configurable)1 DeploymentScoped (org.wildfly.swarm.spi.runtime.annotations.DeploymentScoped)1