@Override
    protected void processAuthenticationResponse(HttpServletRequest request,
                                                 HttpServletResponse response, AuthenticationContext context)
            throws AuthenticationFailedException {
        try {
            Map<String, String> authenticatorProperties = context.getAuthenticatorProperties();
            String clientId = authenticatorProperties.get(OIDCAuthenticatorConstants.CLIENT_ID);
            String clientSecret = authenticatorProperties.get(OIDCAuthenticatorConstants.CLIENT_SECRET);
            String tokenEndPoint = getTokenEndpoint(authenticatorProperties);
            if (tokenEndPoint == null) {
                tokenEndPoint = authenticatorProperties.get(OIDCAuthenticatorConstants.OAUTH2_TOKEN_URL);
            }
            String callBackUrl = authenticatorProperties.get(GoogleOAuth2AuthenticationConstant.CALLBACK_URL);
            log.debug("callBackUrl : " + callBackUrl);
            if (callBackUrl == null) {
                callBackUrl = CarbonUIUtil.getAdminConsoleURL(request);
                callBackUrl = callBackUrl.replace("commonauth/carbon/", "commonauth");
            }
            @SuppressWarnings({"unchecked"})
            Map<String, String> paramValueMap = (Map<String, String>) context.getProperty("oidc:param.map");
            if (paramValueMap != null
                && paramValueMap.containsKey("redirect_uri")) {
                callBackUrl = paramValueMap.get("redirect_uri");
            }
            OAuthAuthzResponse authzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
            String code = authzResponse.getCode();
            OAuthClientRequest accessRequest;
            try {
                accessRequest = OAuthClientRequest.tokenLocation(tokenEndPoint)
                        .setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(clientId).setClientSecret(clientSecret)
                        .setRedirectURI(callBackUrl).setCode(code).buildBodyMessage();
            } catch (OAuthSystemException e) {
                throw new AuthenticationFailedException("Exception while building request for request access token", e);
            }
            // create OAuth client that uses custom http client under the hood
            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
            OAuthClientResponse oAuthResponse;
            try {
                oAuthResponse = oAuthClient.accessToken(accessRequest);
            } catch (OAuthSystemException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Exception while requesting access token", e);
                }
                throw new AuthenticationFailedException("Exception while requesting access token", e);
            }
            // TODO : return access token and id token to framework
            String accessToken = oAuthResponse.getParam(OIDCAuthenticatorConstants.ACCESS_TOKEN);
            String idToken = oAuthResponse.getParam(OIDCAuthenticatorConstants.ID_TOKEN);
            if (accessToken != null && (idToken != null || !requiredIDToken(authenticatorProperties))) {
                context.setProperty(OIDCAuthenticatorConstants.ACCESS_TOKEN, accessToken);
                if (idToken != null) {
                    context.setProperty(OIDCAuthenticatorConstants.ID_TOKEN, idToken);
                    String base64Body = idToken.split("\\.")[1];
                    byte[] decoded = Base64.decodeBase64(base64Body.getBytes());
                    String json = new String(decoded);
                    if (log.isDebugEnabled()) {
                        log.debug("Id token json string : " + json);
                    }
                    Map<String, Object> jsonObject = JSONUtils.parseJSON(json);
                    if (jsonObject != null) {
                        Map<ClaimMapping, String> claims = getSubjectAttributes(oAuthResponse);
                        String authenticatedUser = (String) jsonObject.get(OIDCAuthenticatorConstants.Claim.EMAIL);
                        AuthenticatedUser authenticatedUserObj = AuthenticatedUser
                                .createFederateAuthenticatedUserFromSubjectIdentifier(authenticatedUser);
                        authenticatedUserObj.setUserAttributes(claims);
                        context.setSubject(authenticatedUserObj);
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("Decoded json object is null");
                        }
                        throw new AuthenticationFailedException("Decoded json object is null");
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Authentication Failed");
                    }
                    throw new AuthenticationFailedException("Authentication Failed");
                }
            } else {
                throw new AuthenticationFailedException("Authentication Failed");
            }
        } catch (OAuthProblemException e) {
            throw new AuthenticationFailedException("Error occurred while acquiring access token", e);
        } catch (JSONException e) {
            throw new AuthenticationFailedException("Error occurred while parsing json object", e);
        }
    }
@Override
    protected void processAuthenticationResponse(HttpServletRequest request,
                                                 HttpServletResponse response, AuthenticationContext context)
            throws AuthenticationFailedException {
        try {
            Map<String, String> authenticatorProperties = context.getAuthenticatorProperties();
            String clientId = authenticatorProperties.get(OIDCAuthenticatorConstants.CLIENT_ID);
            String clientSecret = authenticatorProperties.get(OIDCAuthenticatorConstants.CLIENT_SECRET);
            String tokenEndPoint = getTokenEndpoint(authenticatorProperties);
            if (tokenEndPoint == null) {
                tokenEndPoint = authenticatorProperties.get(OIDCAuthenticatorConstants.OAUTH2_TOKEN_URL);
            }
            String callBackUrl = authenticatorProperties.get(GoogleOAuth2AuthenticationConstant.CALLBACK_URL);
            log.debug("callBackUrl : " + callBackUrl);
            if (callBackUrl == null) {
                callBackUrl = CarbonUIUtil.getAdminConsoleURL(request);
                callBackUrl = callBackUrl.replace("commonauth/carbon/", "commonauth");
            }
            @SuppressWarnings({"unchecked"})
            Map<String, String> paramValueMap = (Map<String, String>) context.getProperty("oidc:param.map");
            if (paramValueMap != null
                && paramValueMap.containsKey("redirect_uri")) {
                callBackUrl = paramValueMap.get("redirect_uri");
            }
            OAuthAuthzResponse authzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
            String code = authzResponse.getCode();
            OAuthClientRequest accessRequest;
            try {
                accessRequest = OAuthClientRequest.tokenLocation(tokenEndPoint)
                        .setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(clientId).setClientSecret(clientSecret)
                        .setRedirectURI(callBackUrl).setCode(code).buildBodyMessage();
            } catch (OAuthSystemException e) {
                throw new AuthenticationFailedException("Exception while building request for request access token", e);
            }
           
            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
            OAuthClientResponse oAuthResponse;
            try {
                oAuthResponse = oAuthClient.accessToken(accessRequest);
            } catch (OAuthSystemException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Exception while requesting access token", e);
                }
                throw new AuthenticationFailedException("Exception while requesting access token", e);
            }
           
            String accessToken = oAuthResponse.getParam(OIDCAuthenticatorConstants.ACCESS_TOKEN);
            String idToken = oAuthResponse.getParam(OIDCAuthenticatorConstants.ID_TOKEN);
            if (accessToken != null && (idToken != null || !requiredIDToken(authenticatorProperties))) {
                context.setProperty(OIDCAuthenticatorConstants.ACCESS_TOKEN, accessToken);
                if (idToken != null) {
                    context.setProperty(OIDCAuthenticatorConstants.ID_TOKEN, idToken);
                    String base64Body = idToken.split("\\.")[1];
                    byte[] decoded = Base64.decodeBase64(base64Body.getBytes());
                    String json = new String(decoded);
                    if (log.isDebugEnabled()) {
                        log.debug("Id token json string : " + json);
                    }
                    Map<String, Object> jsonObject = JSONUtils.parseJSON(json);
                    if (jsonObject != null) {
                        Map<ClaimMapping, String> claims = getSubjectAttributes(oAuthResponse);
                        String authenticatedUser = (String) jsonObject.get(OIDCAuthenticatorConstants.Claim.EMAIL);
                        AuthenticatedUser authenticatedUserObj = AuthenticatedUser
                                .createFederateAuthenticatedUserFromSubjectIdentifier(authenticatedUser);
                        authenticatedUserObj.setUserAttributes(claims);
                        context.setSubject(authenticatedUserObj);
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("Decoded json object is null");
                        }
                        throw new AuthenticationFailedException("Decoded json object is null");
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Authentication Failed");
                    }
                    throw new AuthenticationFailedException("Authentication Failed");
                }
            } else {
                throw new AuthenticationFailedException("Authentication Failed");
            }
        } catch (OAuthProblemException e) {
            throw new AuthenticationFailedException("Error occurred while acquiring access token", e);
        } catch (JSONException e) {
            throw new AuthenticationFailedException("Error occurred while parsing json object", e);
        }
    }
@override protected void processauthenticationresponse(httpservletrequest request, httpservletresponse response, authenticationcontext context) throws authenticationfailedexception { try { map<string, string> authenticatorproperties = context.getauthenticatorproperties(); string clientid = authenticatorproperties.get(oidcauthenticatorconstants.client_id); string clientsecret = authenticatorproperties.get(oidcauthenticatorconstants.client_secret); string tokenendpoint = gettokenendpoint(authenticatorproperties); if (tokenendpoint == null) { tokenendpoint = authenticatorproperties.get(oidcauthenticatorconstants.oauth2_token_url); } string callbackurl = authenticatorproperties.get(googleoauth2authenticationconstant.callback_url); log.debug("callbackurl : " + callbackurl); if (callbackurl == null) { callbackurl = carbonuiutil.getadminconsoleurl(request); callbackurl = callbackurl.replace("commonauth/carbon/", "commonauth"); } @suppresswarnings({"unchecked"}) map<string, string> paramvaluemap = (map<string, string>) context.getproperty("oidc:param.map"); if (paramvaluemap != null && paramvaluemap.containskey("redirect_uri")) { callbackurl = paramvaluemap.get("redirect_uri"); } oauthauthzresponse authzresponse = oauthauthzresponse.oauthcodeauthzresponse(request); string code = authzresponse.getcode(); oauthclientrequest accessrequest; try { accessrequest = oauthclientrequest.tokenlocation(tokenendpoint) .setgranttype(granttype.authorization_code).setclientid(clientid).setclientsecret(clientsecret) .setredirecturi(callbackurl).setcode(code).buildbodymessage(); } catch (oauthsystemexception e) { throw new authenticationfailedexception("exception while building request for request access token", e); } oauthclient oauthclient = new oauthclient(new urlconnectionclient()); oauthclientresponse oauthresponse; try { oauthresponse = oauthclient.accesstoken(accessrequest); } catch (oauthsystemexception e) { if (log.isdebugenabled()) { log.debug("exception while requesting access token", e); } throw new authenticationfailedexception("exception while requesting access token", e); } string accesstoken = oauthresponse.getparam(oidcauthenticatorconstants.access_token); string idtoken = oauthresponse.getparam(oidcauthenticatorconstants.id_token); if (accesstoken != null && (idtoken != null || !requiredidtoken(authenticatorproperties))) { context.setproperty(oidcauthenticatorconstants.access_token, accesstoken); if (idtoken != null) { context.setproperty(oidcauthenticatorconstants.id_token, idtoken); string base64body = idtoken.split("\\.")[1]; byte[] decoded = base64.decodebase64(base64body.getbytes()); string json = new string(decoded); if (log.isdebugenabled()) { log.debug("id token json string : " + json); } map<string, object> jsonobject = jsonutils.parsejson(json); if (jsonobject != null) { map<claimmapping, string> claims = getsubjectattributes(oauthresponse); string authenticateduser = (string) jsonobject.get(oidcauthenticatorconstants.claim.email); authenticateduser authenticateduserobj = authenticateduser .createfederateauthenticateduserfromsubjectidentifier(authenticateduser); authenticateduserobj.setuserattributes(claims); context.setsubject(authenticateduserobj); } else { if (log.isdebugenabled()) { log.debug("decoded json object is null"); } throw new authenticationfailedexception("decoded json object is null"); } } else { if (log.isdebugenabled()) { log.debug("authentication failed"); } throw new authenticationfailedexception("authentication failed"); } } else { throw new authenticationfailedexception("authentication failed"); } } catch (oauthproblemexception e) { throw new authenticationfailedexception("error occurred while acquiring access token", e); } catch (jsonexception e) { throw new authenticationfailedexception("error occurred while parsing json object", e); } }