Search in sources :

Example 1 with FormDataParser

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

the class FormAuthenticationMechanism method runFormAuth.

public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange exchange, final SecurityContext securityContext) {
    final FormDataParser parser = formParserFactory.createParser(exchange);
    if (parser == null) {
        UndertowLogger.SECURITY_LOGGER.debug("Could not authenticate as no form parser is present");
        // TODO - May need a better error signaling mechanism here to prevent repeated attempts.
        return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
    }
    try {
        final FormData data = parser.parseBlocking();
        final FormData.FormValue jUsername = data.getFirst("j_username");
        final FormData.FormValue jPassword = data.getFirst("j_password");
        if (jUsername == null || jPassword == null) {
            UndertowLogger.SECURITY_LOGGER.debugf("Could not authenticate as username or password was not present in the posted result for %s", exchange);
            return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
        }
        final String userName = jUsername.getValue();
        final String password = jPassword.getValue();
        AuthenticationMechanismOutcome outcome = null;
        PasswordCredential credential = new PasswordCredential(password.toCharArray());
        try {
            IdentityManager identityManager = getIdentityManager(securityContext);
            Account account = identityManager.verify(userName, credential);
            if (account != null) {
                securityContext.authenticationComplete(account, name, true);
                UndertowLogger.SECURITY_LOGGER.debugf("Authenticated user %s using for auth for %s", account.getPrincipal().getName(), exchange);
                outcome = AuthenticationMechanismOutcome.AUTHENTICATED;
            } else {
                securityContext.authenticationFailed(MESSAGES.authenticationFailed(userName), name);
            }
        } finally {
            if (outcome == AuthenticationMechanismOutcome.AUTHENTICATED) {
                handleRedirectBack(exchange);
                exchange.endExchange();
            }
            return outcome != null ? outcome : AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : FormData(io.undertow.server.handlers.form.FormData) Account(io.undertow.security.idm.Account) IdentityManager(io.undertow.security.idm.IdentityManager) FormDataParser(io.undertow.server.handlers.form.FormDataParser) PasswordCredential(io.undertow.security.idm.PasswordCredential) IOException(java.io.IOException)

Example 2 with FormDataParser

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

the class HttpServletRequestImpl method parseFormData.

private FormData parseFormData() {
    if (parsedFormData == null) {
        if (readStarted) {
            return null;
        }
        final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getCurrentServlet().getManagedServlet();
        final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange);
        if (parser == null) {
            return null;
        }
        readStarted = true;
        try {
            return parsedFormData = parser.parseBlocking();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return parsedFormData;
}
Also used : ManagedServlet(io.undertow.servlet.core.ManagedServlet) FormDataParser(io.undertow.server.handlers.form.FormDataParser) IOException(java.io.IOException)

Example 3 with FormDataParser

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

the class HttpServletRequestImpl method setCharacterEncoding.

@Override
public void setCharacterEncoding(final String env) throws UnsupportedEncodingException {
    if (readStarted) {
        return;
    }
    try {
        characterEncoding = Charset.forName(env);
        final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalServletPathMatch().getServletChain().getManagedServlet();
        final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange);
        if (parser != null) {
            parser.setCharacterEncoding(env);
        }
    } catch (UnsupportedCharsetException e) {
        throw new UnsupportedEncodingException();
    }
}
Also used : ManagedServlet(io.undertow.servlet.core.ManagedServlet) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FormDataParser(io.undertow.server.handlers.form.FormDataParser) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with FormDataParser

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

the class MCMPHandler method parseFormData.

/**
     * Transform the form data into an intermediate request data which can me used
     * by the web manager
     *
     * @param exchange    the http server exchange
     * @return
     * @throws IOException
     */
RequestData parseFormData(final HttpServerExchange exchange) throws IOException {
    // Read post parameters
    final FormDataParser parser = parserFactory.createParser(exchange);
    final FormData formData = parser.parseBlocking();
    final RequestData data = new RequestData();
    for (String name : formData) {
        final HttpString key = new HttpString(name);
        data.add(key, formData.get(name));
    }
    return data;
}
Also used : FormData(io.undertow.server.handlers.form.FormData) FormDataParser(io.undertow.server.handlers.form.FormDataParser) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Aggregations

FormDataParser (io.undertow.server.handlers.form.FormDataParser)4 FormData (io.undertow.server.handlers.form.FormData)2 ManagedServlet (io.undertow.servlet.core.ManagedServlet)2 IOException (java.io.IOException)2 Account (io.undertow.security.idm.Account)1 IdentityManager (io.undertow.security.idm.IdentityManager)1 PasswordCredential (io.undertow.security.idm.PasswordCredential)1 HttpString (io.undertow.util.HttpString)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1