package com.monkeyk.os.web.controller;

import com.monkeyk.os.web.WebUtils;
import com.monkeyk.os.oauth.OAuthAuthxRequest;
import com.monkeyk.os.oauth.authorize.CodeAuthorizeHandler;
import com.monkeyk.os.oauth.authorize.TokenAuthorizeHandler;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.common.error.OAuthError;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * URL: oauth/authorize
 *
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("oauth/")
public class OauthAuthorizeController {


    private static final Logger LOG = LoggerFactory.getLogger(OauthAuthorizeController.class);


    /**
     * Must handle the grant_type as follow:
     * grant_type="authorization_code" -> response_type="code"
     * ?response_type=code&scope=read,write&client_id=[client_id]&redirect_uri=[redirect_uri]&state=[state]
     * <p/>
     * grant_type="implicit"   -> response_type="token"
     * ?response_type=token&scope=read,write&client_id=[client_id]&client_secret=[client_secret]&redirect_uri=[redirect_uri]
     * <p/>
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @RequestMapping("authorize")
    public void authorize(HttpServletRequest request, HttpServletResponse response) throws Exception {

        try {
            OAuthAuthxRequest oauthRequest = new OAuthAuthxRequest(request);


            if (oauthRequest.isCode()) {
                CodeAuthorizeHandler codeAuthorizeHandler = new CodeAuthorizeHandler(oauthRequest, response);
                LOG.debug("Go to  response_type = 'code' handler: {}", codeAuthorizeHandler);
                codeAuthorizeHandler.handle();

            } else if (oauthRequest.isToken()) {
                TokenAuthorizeHandler tokenAuthorizeHandler = new TokenAuthorizeHandler(oauthRequest, response);
                LOG.debug("Go to response_type = 'token' handler: {}", tokenAuthorizeHandler);
                tokenAuthorizeHandler.handle();

            } else {
                unsupportResponseType(oauthRequest, response);
            }

        } catch (OAuthProblemException e) {
            //exception
            OAuthResponse oAuthResponse = OAuthASResponse
                    .errorResponse(HttpServletResponse.SC_FOUND)
                    .location(e.getRedirectUri())
                    .error(e)
                    .buildJSONMessage();
            WebUtils.writeOAuthJsonResponse(response, oAuthResponse);
        }


    }

//    private boolean checkAndHandleAuthHeaderUsed(HttpServletResponse response, OAuthAuthxRequest oauthRequest) throws OAuthSystemException {
//        if (oauthRequest.isClientAuthHeaderUsed()) {
//            OAuthResponse oAuthResponse = OAuthResponse.status(HttpServletResponse.SC_FOUND)
//                    .location(oauthRequest.getRedirectURI())
//                    .setParam("client_id", oauthRequest.getClientId())
//                    .buildJSONMessage();
//
//            LOG.debug("Auth header used by client: {}, return directly", oauthRequest.getClientId());
//            WebUtils.writeOAuthJsonResponse(response, oAuthResponse);
//            return true;
//        }
//        return false;
//    }

    private void unsupportResponseType(OAuthAuthxRequest oauthRequest, HttpServletResponse response) throws OAuthSystemException {
        final String responseType = oauthRequest.getResponseType();
        LOG.debug("Unsupport response_type '{}' by client_id '{}'", responseType, oauthRequest.getClientId());

        OAuthResponse oAuthResponse = OAuthResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                .setError(OAuthError.CodeResponse.UNSUPPORTED_RESPONSE_TYPE)
                .setErrorDescription("Unsupport response_type '" + responseType + "'")
                .buildJSONMessage();
        WebUtils.writeOAuthJsonResponse(response, oAuthResponse);
    }


    @RequestMapping(value = "oauth_login")
    public String oauthLogin() {
        return "oauth/oauth_login";
    }


    @RequestMapping(value = "oauth_approval")
    public String oauthApproval() {
        return "oauth/oauth_approval";
    }


}
@@ -721,16 +721,55 @@
 pping;%0A%0A
+import javax.servlet.ServletException;%0A
 import j
@@ -802,24 +802,24 @@
 letRequest;%0A
-
 import javax
@@ -856,17 +856,42 @@
 nse;
+%0Aimport java.io.IOException;
 %0A%0A/**%0A
- *%0A
  * U
@@ -1771,16 +1771,58 @@
  throws 
+OAuthSystemException, ServletException, IO
 Exceptio
@@ -3014,738 +3014,15 @@
  %7D%0A%0A
-%0A
     %7D%0A%0A
-//    private boolean checkAndHandleAuthHeaderUsed(HttpServletResponse response, OAuthAuthxRequest oauthRequest) throws OAuthSystemException %7B%0A//        if (oauthRequest.isClientAuthHeaderUsed()) %7B%0A//            OAuthResponse oAuthResponse = OAuthResponse.status(HttpServletResponse.SC_FOUND)%0A//                    .location(oauthRequest.getRedirectURI())%0A//                    .setParam(%22client_id%22, oauthRequest.getClientId())%0A//                    .buildJSONMessage();%0A//%0A//            LOG.debug(%22Auth header used by client: %7B%7D, return directly%22, oauthRequest.getClientId());%0A//            WebUtils.writeOAuthJsonResponse(response, oAuthResponse);%0A//            return true;%0A//        %7D%0A//        return false;%0A//    %7D%0A
 %0A   

Upgrade codes from sonar