// create OAuth client that uses custom http client under the hood
@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);
        }
    }
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;
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);
}
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);