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

com.networknt.oauth.code.handler.Oauth2CodePostHandler Maven / Gradle / Ivy

package com.networknt.oauth.code.handler;

import com.networknt.client.oauth.ClientCredentialsRequest;
import com.networknt.client.oauth.OauthHelper;
import com.networknt.client.oauth.TokenRequest;
import com.networknt.client.oauth.TokenResponse;
import com.networknt.config.JsonMapper;
import com.networknt.handler.LightHttpHandler;
import com.networknt.monad.Result;
import com.networknt.oauth.cache.ClientUtil;
import com.networknt.utility.Util;
import io.undertow.security.api.SecurityContext;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormData;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.util.Headers;
import io.undertow.util.StatusCodes;
import net.lightapi.portal.HybridQueryClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Oauth2CodePostHandler extends CodeAuditHandler implements LightHttpHandler {
    static final Logger logger = LoggerFactory.getLogger(Oauth2CodePostHandler.class);
    static final String CLIENT_NOT_FOUND = "ERR12014";

    static final String DEFAULT_AUTHENTICATE_CLASS = "com.networknt.oauth.code.auth.FormAuthentication";
    @SuppressWarnings("unchecked")
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");

        // get the form from the exchange
        final FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);

        final FormData.FormValue jClientId = data.getFirst("client_id");
        final FormData.FormValue jRedirectUri = data.getFirst("redirect_uri");
        final FormData.FormValue jState = data.getFirst("state");
        final FormData.FormValue jRemember = data.getFirst("remember");
        final FormData.FormValue jHost = data.getFirst("host");
        final String clientId = jClientId.getValue();
        final String remember = jRemember == null ? null : jRemember.getValue();  // should be 'Y' or 'N' if not null.
        String redirectUri = jRedirectUri == null ? null : jRedirectUri.getValue();
        final String state = jState == null ? null : jState.getValue();
        if(logger.isDebugEnabled()) {
            logger.debug("client_id = " + clientId + " state = " + state + " redirectUri = " + redirectUri + " remember = " + remember);
        }
        // check if the client_id is valid
        Result resultClient = ClientUtil.getClientById(clientId);
        if(resultClient.isFailure()) {
            logger.error("failed to get the client: " + resultClient.getError());
            setExchangeStatus(exchange, resultClient.getError());
            processAudit(exchange);
            return;
        }
        String client = resultClient.getResult();
        if(client == null) {
            if(logger.isDebugEnabled()) logger.debug("client is not found for clientId = " + clientId);
            setExchangeStatus(exchange, CLIENT_NOT_FOUND, clientId);
            processAudit(exchange);
        } else {
            final SecurityContext context = exchange.getSecurityContext();
            String userId = context.getAuthenticatedAccount().getPrincipal().getName();
            if(logger.isDebugEnabled()) logger.debug("userId = " + userId);
            if("error".equals(userId)) {
                exchange.setStatusCode(StatusCodes.BAD_REQUEST);
                exchange.getResponseSender().send(context.getAuthenticatedAccount().getRoles().iterator().next());
                processAudit(exchange);
            } else {
                Set roles = context.getAuthenticatedAccount().getRoles();
                Map codeMap = new HashMap<>();
                codeMap.put("userId", userId);
                if(roles != null && !roles.isEmpty()) {
                    codeMap.put("roles", String.join(" ", roles));
                }
                // generate auth code
                String code = Util.getUUID();
                codeMap.put("authCode", code);

                Map clientMap = JsonMapper.string2Map(client);
                codeMap.put("host", clientMap.get("host"));
                if(redirectUri == null) {
                    redirectUri = (String)clientMap.get("redirectUri");
                    if(logger.isDebugEnabled()) logger.debug("Get redirectUri from the client " + redirectUri);
                }
                codeMap.put("redirectUri", redirectUri);
                codeMap.put("remember", remember != null ? remember : "N");
                Result resultAuthCode = ClientUtil.createAuthCode(codeMap);
                if(resultAuthCode.isFailure()) logger.error(resultAuthCode.getError().toString());
                redirectUri = redirectUri + "?code=" + code;
                if(state != null) {
                    redirectUri = redirectUri + "&state=" + state;
                }
                if(logger.isDebugEnabled()) logger.debug("redirectUri = " + redirectUri);
                // now redirect here.
                exchange.setStatusCode(StatusCodes.FOUND);
                exchange.getResponseHeaders().put(Headers.LOCATION, redirectUri);
                exchange.endExchange();
                processAudit(exchange);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy