/*
 * Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.wso2.carbon.identity.authenticator.backend.oauth;

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.core.security.AuthenticatorsConfiguration;
import org.wso2.carbon.core.services.authentication.CarbonServerAuthenticator;
import org.wso2.carbon.identity.authenticator.backend.oauth.validator.OAuthValidationResponse;
import org.wso2.carbon.utils.ServerConstants;
import org.wso2.carbon.identity.authenticator.backend.oauth.validator.OAuth2TokenValidator;
import org.wso2.carbon.identity.authenticator.backend.oauth.validator.OAuthValidatorFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.rmi.RemoteException;

/**
 * This is a custom back end authenticator for enable OAuth token authentication for admin services
 */
public class OauthAuthenticator implements CarbonServerAuthenticator {

    private static final Log log = LogFactory.getLog(OauthAuthenticator.class);
    private static final int PRIORITY = 5;
    private static final int ACCESS_TOKEN_INDEX = 1;
    private OAuth2TokenValidator tokenValidator;

    public OauthAuthenticator() {
        try {
            tokenValidator = OAuthValidatorFactory.getValidator();
        } catch (IllegalArgumentException e) {
            log.error("Failed to initialise Authenticator",e);
        }
    }

    /**
     * Checks whether the authentication of the context can be handled using this authenticator.
     *
     * @param messageContext containing the request need to be authenticated.
     * @return boolean indicating whether the request can be authenticated by this Authenticator.
     */
    public boolean isHandle(MessageContext messageContext) {
        HttpServletRequest httpServletRequest = getHttpRequest(messageContext);
        String headerValue = httpServletRequest.getHeader(HTTPConstants.HEADER_AUTHORIZATION);
        if (headerValue != null && !headerValue.trim().isEmpty()) {
            String[] headerPart = headerValue.trim().split(OauthAuthenticatorConstants.SPLITING_CHARACTOR);
            if (OauthAuthenticatorConstants.AUTHORIZATION_HEADER_PREFIX_BEARER.equals(headerPart[0])) {
                return true;
            }
        } else if (httpServletRequest.getParameter(OauthAuthenticatorConstants.BEARER_TOKEN_IDENTIFIER) != null) {
            return true;
        }
        return false;
    }

    /**
     * Authenticates the user using the provided OAuth token and returns the status as a boolean.
     * Sets the tenant domain and tenant friendly username to the session as attributes.
     *
     * @param messageContext containing the request need to be authenticated.
     * @return boolean indicating the authentication status.
     */
    public boolean isAuthenticated(MessageContext messageContext) {
        HttpServletRequest httpServletRequest = getHttpRequest(messageContext);
        String headerValue = httpServletRequest.getHeader(HTTPConstants.HEADER_AUTHORIZATION);
        String[] headerPart = headerValue.trim().split(OauthAuthenticatorConstants.SPLITING_CHARACTOR);
        String accessToken = headerPart[ACCESS_TOKEN_INDEX];
        OAuthValidationResponse response = null;
        try {
            response = tokenValidator.validateToken(accessToken);
        } catch (RemoteException e) {
            log.error("Failed to validate the OAuth token provided.", e);
        }
        if (response != null && response.isValid()) {
            HttpSession session;
            if ((session = httpServletRequest.getSession(false)) != null) {
                session.setAttribute(MultitenantConstants.TENANT_DOMAIN, response.getTenantDomain());
                session.setAttribute(ServerConstants.USER_LOGGED_IN, response.getUserName());
                if (log.isDebugEnabled()) {
                    log.debug("Authentication successful for " + session.getAttribute(ServerConstants.USER_LOGGED_IN));
                }
            }
            return true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Authentication failed.Illegal attempt from session " + httpServletRequest.getSession().getId());
        }
        return false;
    }

    /**
     * this method is currently not implemented.
     *
     * @param messageContext containing the request need to be authenticated.
     * @return boolean
     */
    public boolean authenticateWithRememberMe(MessageContext messageContext) {
        throw new UnsupportedOperationException();
    }

    /**
     * @return string Authenticator name.
     */
    public String getAuthenticatorName() {
        return OauthAuthenticatorConstants.AUTHENTICATOR_NAME;
    }

    /**
     * @return int priority of the authenticator.
     */
    public int getPriority() {
        return PRIORITY;
    }

    /**
     * @return boolean true for enable or otherwise for disable status.
     */
    public boolean isDisabled() {
        return false;
    }

    /**
     * Retrieve HTTP Servlet Request form thr Message Context.
     *
     * @param messageContext Containing the Servlet Request for backend authentication.
     * @return HTTPServletRequest.
     */
    private HttpServletRequest getHttpRequest(MessageContext messageContext) {
        return (HttpServletRequest) messageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
    }

}

@@ -2630,32 +2630,77 @@
 essageContext);%0A
+        if(httpServletRequest != null) %7B%0A    
         String h
@@ -2782,24 +2782,28 @@
 N);%0A        
+    
 if (headerVa
@@ -2854,32 +2854,36 @@
 ) %7B%0A            
+ 
+   
 String%5B%5D headerP
@@ -2970,24 +2970,28 @@
             
+  
+  
 if (OauthAut
@@ -3078,32 +3078,36 @@
                 
+    
 return true;%0A   
@@ -3107,32 +3107,36 @@
 ue;%0A            
+    
 %7D%0A        %7D else
@@ -3129,16 +3129,20 @@
         
+ 
+   
 %7D else i
@@ -3244,32 +3244,36 @@
 ) %7B%0A            
+    
 return true;%0A   
@@ -3261,32 +3261,46 @@
    return true;%0A
+            %7D%0A
         %7D%0A      
@@ -5851,36 +5851,367 @@
 ) %7B%0A        
-return false
+AuthenticatorsConfiguration authenticatorsConfiguration = AuthenticatorsConfiguration.getInstance();%0A        AuthenticatorsConfiguration.AuthenticatorConfig authenticatorConfig = authenticatorsConfiguration.%0A                getAuthenticatorConfig(OauthAuthenticatorConstants.AUTHENTICATOR_NAME);%0A        return authenticatorConfig.isDisabled()
 ;%0A    %7D%0A%0A   

Fix the disable failier of the authenticator