All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mangoo.utils.RequestUtils Maven / Gradle / Ivy

The newest version!
package io.mangoo.utils;

import com.google.common.net.MediaType;
import com.google.re2j.Pattern;
import io.mangoo.constants.Header;
import io.mangoo.constants.NotNull;
import io.mangoo.core.Application;
import io.mangoo.core.Config;
import io.mangoo.exceptions.MangooTokenException;
import io.mangoo.models.Identity;
import io.mangoo.routing.Attachment;
import io.mangoo.utils.token.TokenParser;
import io.undertow.security.api.AuthenticationMode;
import io.undertow.security.handlers.AuthenticationCallHandler;
import io.undertow.security.handlers.AuthenticationConstraintHandler;
import io.undertow.security.handlers.AuthenticationMechanismsHandler;
import io.undertow.security.handlers.SecurityInitialHandler;
import io.undertow.security.impl.BasicAuthenticationMechanism;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.AttachmentKey;
import io.undertow.util.Methods;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

public final class RequestUtils {
    private static final Logger LOG = LogManager.getLogger(RequestUtils.class);
    private static final Pattern PATTERN = Pattern.compile("\"");
    private static AttachmentKey attachmentKey;

    private RequestUtils() {
    }
    
    public static AttachmentKey getAttachmentKey() {
        if (attachmentKey == null) {
            attachmentKey = AttachmentKey.create(Attachment.class);
        }
        
        return attachmentKey;
    }
    
    /**
     * Converts request and query parameter into a single map
     *
     * @param exchange The Undertow HttpServerExchange
     * @return A single map contain both request and query parameter
     */
    public static Map getRequestParameters(HttpServerExchange exchange) {
        Objects.requireNonNull(exchange, NotNull.HTTP_SERVER_EXCHANGE);

        final Map requestParameter = new HashMap<>();
        final Map> queryParameters = exchange.getQueryParameters();
        queryParameters.putAll(exchange.getPathParameters());
        queryParameters.forEach((key, value) -> requestParameter.put(key, value.element()));

        return requestParameter;
    }

    /**
     * Checks if the request is a POST, PUT or PATCH request
     *
     * @param exchange The Undertow HttpServerExchange
     * @return True if the request is a POST, PUT or PATCH request, false otherwise
     */
    public static boolean isPostPutPatch(HttpServerExchange exchange) {
        Objects.requireNonNull(exchange, NotNull.HTTP_SERVER_EXCHANGE);

        return (Methods.POST).equals(exchange.getRequestMethod()) || (Methods.PUT).equals(exchange.getRequestMethod()) || (Methods.PATCH).equals(exchange.getRequestMethod());
    }
    
    /**
     * Checks if the requests content-type contains application/json
     *
     * @param exchange The Undertow HttpServerExchange
     * @return True if the request content-type contains application/json, false otherwise
     */
    public static boolean isJsonRequest(HttpServerExchange exchange) {
        Objects.requireNonNull(exchange, NotNull.HTTP_SERVER_EXCHANGE);

        var headerMap = exchange.getRequestHeaders();
        return headerMap != null && headerMap.get(Header.CONTENT_TYPE) != null &&
               headerMap.get(Header.CONTENT_TYPE).element().toLowerCase(Locale.ENGLISH).contains(MediaType.JSON_UTF_8.withoutParameters().toString());
    }

    /**
     * Checks if the given header contains a valid authentication
     *
     * @param cookie The cookie to parse
     * @return True if the cookie contains a valid authentication, false otherwise
     */
    public static boolean hasValidAuthentication(String cookie) {
        var valid = false;
        if (StringUtils.isNotBlank(cookie)) {
            var config = Application.getInstance(Config.class);

            String value = null;
            String [] contents = cookie.split(";");
            for (String content : contents) {
                if (StringUtils.isNotBlank(content) && content.startsWith(config.getAuthenticationCookieName())) {
                    value = StringUtils.substringAfter(content, config.getAuthenticationCookieName() + "=");
                    value = PATTERN.matcher(value).replaceAll("");
                }
            }
            
            if (StringUtils.isNotBlank(value)) {
                try {
                    TokenParser.create()
                        .withSharedSecret(config.getAuthenticationCookieSecret())
                        .withCookieValue(value)
                        .parse();
                    
                    valid = true;
                } catch (MangooTokenException e) {
                    LOG.error("Failed to parse authentication cookie", e);
                }
            }
        }
        
        return valid;
    }

    /**
     * Adds a Wrapper to the handler when the request requires authentication
     * 
     * @param httpHandler The Handler to wrap
     * @param username The username to use
     * @param password The password to use
     * @return An HttpHandler wrapped through BasicAuthentication
     */
    public static HttpHandler wrapBasicAuthentication(HttpHandler httpHandler, String username, String password) {
        Objects.requireNonNull(httpHandler, NotNull.HTTP_HANDLER);
        Objects.requireNonNull(username, NotNull.USERNAME);
        Objects.requireNonNull(password, NotNull.PASSWORD);
        
        HttpHandler wrap = new AuthenticationCallHandler(httpHandler);
        wrap = new AuthenticationConstraintHandler(wrap);
        wrap = new AuthenticationMechanismsHandler(wrap, Collections.singletonList(new BasicAuthenticationMechanism("Authentication required")));
        
        return new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, new Identity(username, password), wrap);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy