Search in sources :

Example 1 with FormEncodedDataDefinition

use of io.undertow.server.handlers.form.FormEncodedDataDefinition in project undertow by undertow-io.

the class DeploymentManagerImpl method setupSecurityHandlers.

/**
     * sets up the outer security handlers.
     * <p/>
     * the handler that actually performs the access check happens later in the chain, it is not setup here
     *
     * @param initialHandler The handler to wrap with security handlers
     */
private HttpHandler setupSecurityHandlers(HttpHandler initialHandler) {
    final DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    final LoginConfig loginConfig = deploymentInfo.getLoginConfig();
    HttpHandler current = initialHandler;
    current = new SSLInformationAssociationHandler(current);
    final SecurityPathMatches securityPathMatches = buildSecurityConstraints();
    securityPathMatches.logWarningsAboutUncoveredMethods();
    current = new ServletAuthenticationCallHandler(current);
    for (HandlerWrapper wrapper : deploymentInfo.getSecurityWrappers()) {
        current = wrapper.wrap(current);
    }
    if (deploymentInfo.isDisableCachingForSecuredPages()) {
        current = Handlers.predicate(Predicates.authRequired(), Handlers.disableCache(current), current);
    }
    if (!securityPathMatches.isEmpty()) {
        current = new ServletAuthenticationConstraintHandler(current);
    }
    current = new ServletConfidentialityConstraintHandler(deploymentInfo.getConfidentialPortManager(), current);
    if (!securityPathMatches.isEmpty()) {
        current = new ServletSecurityConstraintHandler(securityPathMatches, current);
    }
    HandlerWrapper initialSecurityWrapper = deploymentInfo.getInitialSecurityWrapper();
    String mechName = null;
    if (initialSecurityWrapper == null) {
        final Map<String, AuthenticationMechanismFactory> factoryMap = new HashMap<>(deploymentInfo.getAuthenticationMechanisms());
        final IdentityManager identityManager = deploymentInfo.getIdentityManager();
        if (!factoryMap.containsKey(BASIC_AUTH)) {
            factoryMap.put(BASIC_AUTH, new BasicAuthenticationMechanism.Factory(identityManager));
        }
        if (!factoryMap.containsKey(FORM_AUTH)) {
            factoryMap.put(FORM_AUTH, new ServletFormAuthenticationMechanism.Factory(identityManager));
        }
        if (!factoryMap.containsKey(DIGEST_AUTH)) {
            factoryMap.put(DIGEST_AUTH, new DigestAuthenticationMechanism.Factory(identityManager));
        }
        if (!factoryMap.containsKey(CLIENT_CERT_AUTH)) {
            factoryMap.put(CLIENT_CERT_AUTH, new ClientCertAuthenticationMechanism.Factory(identityManager));
        }
        if (!factoryMap.containsKey(ExternalAuthenticationMechanism.NAME)) {
            factoryMap.put(ExternalAuthenticationMechanism.NAME, new ExternalAuthenticationMechanism.Factory(identityManager));
        }
        if (!factoryMap.containsKey(GenericHeaderAuthenticationMechanism.NAME)) {
            factoryMap.put(GenericHeaderAuthenticationMechanism.NAME, new GenericHeaderAuthenticationMechanism.Factory(identityManager));
        }
        List<AuthenticationMechanism> authenticationMechanisms = new LinkedList<>();
        if (deploymentInfo.isUseCachedAuthenticationMechanism()) {
            authenticationMechanisms.add(new CachedAuthenticatedSessionMechanism(identityManager));
        }
        if (loginConfig != null || deploymentInfo.getJaspiAuthenticationMechanism() != null) {
            //we don't allow multipart requests, and use the default encoding when it's set
            FormEncodedDataDefinition formEncodedDataDefinition = new FormEncodedDataDefinition();
            if (deploymentInfo.getDefaultEncoding() != null) {
                formEncodedDataDefinition.setDefaultEncoding(deploymentInfo.getDefaultEncoding());
            }
            FormParserFactory parser = FormParserFactory.builder(false).addParser(formEncodedDataDefinition).build();
            List<AuthMethodConfig> authMethods = Collections.<AuthMethodConfig>emptyList();
            if (loginConfig != null) {
                authMethods = loginConfig.getAuthMethods();
            }
            for (AuthMethodConfig method : authMethods) {
                AuthenticationMechanismFactory factory = factoryMap.get(method.getName());
                if (factory == null) {
                    throw UndertowServletMessages.MESSAGES.unknownAuthenticationMechanism(method.getName());
                }
                if (mechName == null) {
                    mechName = method.getName();
                }
                final Map<String, String> properties = new HashMap<>();
                properties.put(AuthenticationMechanismFactory.CONTEXT_PATH, deploymentInfo.getContextPath());
                properties.put(AuthenticationMechanismFactory.REALM, loginConfig.getRealmName());
                properties.put(AuthenticationMechanismFactory.ERROR_PAGE, loginConfig.getErrorPage());
                properties.put(AuthenticationMechanismFactory.LOGIN_PAGE, loginConfig.getLoginPage());
                properties.putAll(method.getProperties());
                String name = method.getName().toUpperCase(Locale.US);
                // The mechanism name is passed in from the HttpServletRequest interface as the name reported needs to be
                // comparable using '=='
                name = name.equals(FORM_AUTH) ? FORM_AUTH : name;
                name = name.equals(BASIC_AUTH) ? BASIC_AUTH : name;
                name = name.equals(DIGEST_AUTH) ? DIGEST_AUTH : name;
                name = name.equals(CLIENT_CERT_AUTH) ? CLIENT_CERT_AUTH : name;
                authenticationMechanisms.add(factory.create(name, parser, properties));
            }
        }
        deployment.setAuthenticationMechanisms(authenticationMechanisms);
        //if the JASPI auth mechanism is set then it takes over
        if (deploymentInfo.getJaspiAuthenticationMechanism() == null) {
            current = new AuthenticationMechanismsHandler(current, authenticationMechanisms);
        } else {
            current = new AuthenticationMechanismsHandler(current, Collections.<AuthenticationMechanism>singletonList(deploymentInfo.getJaspiAuthenticationMechanism()));
        }
        current = new CachedAuthenticatedSessionHandler(current, this.deployment.getServletContext());
    }
    List<NotificationReceiver> notificationReceivers = deploymentInfo.getNotificationReceivers();
    if (!notificationReceivers.isEmpty()) {
        current = new NotificationReceiverHandler(current, notificationReceivers);
    }
    if (initialSecurityWrapper == null) {
        // TODO - A switch to constraint driven could be configurable, however before we can support that with servlets we would
        // need additional tracking within sessions if a servlet has specifically requested that authentication occurs.
        SecurityContextFactory contextFactory = deploymentInfo.getSecurityContextFactory();
        if (contextFactory == null) {
            contextFactory = SecurityContextFactoryImpl.INSTANCE;
        }
        current = new SecurityInitialHandler(deploymentInfo.getAuthenticationMode(), deploymentInfo.getIdentityManager(), mechName, contextFactory, current);
    } else {
        current = initialSecurityWrapper.wrap(current);
    }
    return current;
}
Also used : IdentityManager(io.undertow.security.idm.IdentityManager) DigestAuthenticationMechanism(io.undertow.security.impl.DigestAuthenticationMechanism) ExternalAuthenticationMechanism(io.undertow.security.impl.ExternalAuthenticationMechanism) HashMap(java.util.HashMap) SecurityPathMatches(io.undertow.servlet.handlers.security.SecurityPathMatches) ServletSecurityConstraintHandler(io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler) HandlerWrapper(io.undertow.server.HandlerWrapper) CachedAuthenticatedSessionHandler(io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler) ServletAuthenticationConstraintHandler(io.undertow.servlet.handlers.security.ServletAuthenticationConstraintHandler) SecurityInitialHandler(io.undertow.security.handlers.SecurityInitialHandler) ServletAuthenticationCallHandler(io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler) AuthMethodConfig(io.undertow.servlet.api.AuthMethodConfig) LoginConfig(io.undertow.servlet.api.LoginConfig) ServletFormAuthenticationMechanism(io.undertow.servlet.handlers.security.ServletFormAuthenticationMechanism) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) GenericHeaderAuthenticationMechanism(io.undertow.security.impl.GenericHeaderAuthenticationMechanism) HttpHandler(io.undertow.server.HttpHandler) CachedAuthenticatedSessionMechanism(io.undertow.security.impl.CachedAuthenticatedSessionMechanism) ClientCertAuthenticationMechanism(io.undertow.security.impl.ClientCertAuthenticationMechanism) ExternalAuthenticationMechanism(io.undertow.security.impl.ExternalAuthenticationMechanism) ServletFormAuthenticationMechanism(io.undertow.servlet.handlers.security.ServletFormAuthenticationMechanism) BasicAuthenticationMechanism(io.undertow.security.impl.BasicAuthenticationMechanism) DigestAuthenticationMechanism(io.undertow.security.impl.DigestAuthenticationMechanism) AuthenticationMechanism(io.undertow.security.api.AuthenticationMechanism) GenericHeaderAuthenticationMechanism(io.undertow.security.impl.GenericHeaderAuthenticationMechanism) LinkedList(java.util.LinkedList) FormParserFactory(io.undertow.server.handlers.form.FormParserFactory) NotificationReceiverHandler(io.undertow.security.handlers.NotificationReceiverHandler) AuthenticationMechanismsHandler(io.undertow.security.handlers.AuthenticationMechanismsHandler) NotificationReceiver(io.undertow.security.api.NotificationReceiver) ServletConfidentialityConstraintHandler(io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler) ClientCertAuthenticationMechanism(io.undertow.security.impl.ClientCertAuthenticationMechanism) FormEncodedDataDefinition(io.undertow.server.handlers.form.FormEncodedDataDefinition) AuthenticationMechanismFactory(io.undertow.security.api.AuthenticationMechanismFactory) BasicAuthenticationMechanism(io.undertow.security.impl.BasicAuthenticationMechanism) SSLInformationAssociationHandler(io.undertow.servlet.handlers.security.SSLInformationAssociationHandler) SecurityContextFactory(io.undertow.security.api.SecurityContextFactory)

Example 2 with FormEncodedDataDefinition

use of io.undertow.server.handlers.form.FormEncodedDataDefinition in project undertow by undertow-io.

the class ManagedServlet method setupMultipart.

public void setupMultipart(ServletContextImpl servletContext) {
    FormEncodedDataDefinition formDataParser = new FormEncodedDataDefinition().setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
    MultipartConfigElement multipartConfig = servletInfo.getMultipartConfig();
    if (multipartConfig == null) {
        multipartConfig = servletContext.getDeployment().getDeploymentInfo().getDefaultMultipartConfig();
    }
    this.multipartConfig = multipartConfig;
    if (multipartConfig != null) {
        //todo: fileSizeThreshold
        MultipartConfigElement config = multipartConfig;
        if (config.getMaxRequestSize() != -1) {
            maxRequestSize = config.getMaxRequestSize();
        } else {
            maxRequestSize = -1;
        }
        final Path tempDir;
        if (config.getLocation() == null || config.getLocation().isEmpty()) {
            tempDir = servletContext.getDeployment().getDeploymentInfo().getTempPath();
        } else {
            String location = config.getLocation();
            Path locFile = Paths.get(location);
            if (locFile.isAbsolute()) {
                tempDir = locFile;
            } else {
                tempDir = servletContext.getDeployment().getDeploymentInfo().getTempPath().resolve(location);
            }
        }
        MultiPartParserDefinition multiPartParserDefinition = new MultiPartParserDefinition(tempDir);
        if (config.getMaxFileSize() > 0) {
            multiPartParserDefinition.setMaxIndividualFileSize(config.getMaxFileSize());
        }
        multiPartParserDefinition.setDefaultEncoding(servletContext.getDeployment().getDefaultCharset().name());
        formParserFactory = FormParserFactory.builder(false).addParser(formDataParser).addParser(multiPartParserDefinition).build();
    } else {
        //no multipart config we don't allow multipart requests
        formParserFactory = FormParserFactory.builder(false).addParser(formDataParser).build();
        maxRequestSize = -1;
    }
}
Also used : Path(java.nio.file.Path) MultipartConfigElement(javax.servlet.MultipartConfigElement) FormEncodedDataDefinition(io.undertow.server.handlers.form.FormEncodedDataDefinition) MultiPartParserDefinition(io.undertow.server.handlers.form.MultiPartParserDefinition)

Aggregations

FormEncodedDataDefinition (io.undertow.server.handlers.form.FormEncodedDataDefinition)2 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)1 AuthenticationMechanismFactory (io.undertow.security.api.AuthenticationMechanismFactory)1 NotificationReceiver (io.undertow.security.api.NotificationReceiver)1 SecurityContextFactory (io.undertow.security.api.SecurityContextFactory)1 AuthenticationMechanismsHandler (io.undertow.security.handlers.AuthenticationMechanismsHandler)1 NotificationReceiverHandler (io.undertow.security.handlers.NotificationReceiverHandler)1 SecurityInitialHandler (io.undertow.security.handlers.SecurityInitialHandler)1 IdentityManager (io.undertow.security.idm.IdentityManager)1 BasicAuthenticationMechanism (io.undertow.security.impl.BasicAuthenticationMechanism)1 CachedAuthenticatedSessionMechanism (io.undertow.security.impl.CachedAuthenticatedSessionMechanism)1 ClientCertAuthenticationMechanism (io.undertow.security.impl.ClientCertAuthenticationMechanism)1 DigestAuthenticationMechanism (io.undertow.security.impl.DigestAuthenticationMechanism)1 ExternalAuthenticationMechanism (io.undertow.security.impl.ExternalAuthenticationMechanism)1 GenericHeaderAuthenticationMechanism (io.undertow.security.impl.GenericHeaderAuthenticationMechanism)1 HandlerWrapper (io.undertow.server.HandlerWrapper)1 HttpHandler (io.undertow.server.HttpHandler)1 FormParserFactory (io.undertow.server.handlers.form.FormParserFactory)1 MultiPartParserDefinition (io.undertow.server.handlers.form.MultiPartParserDefinition)1 AuthMethodConfig (io.undertow.servlet.api.AuthMethodConfig)1