Skip to content

Commit

Permalink
Add blank lines to improve readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
chamathns committed Oct 27, 2020
1 parent 7016fcd commit 80e97e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void sendForLogin(HttpServletRequest request, HttpServletResponse respons

OIDCRequestBuilder requestBuilder = new OIDCRequestBuilder(oidcAgentConfig);
String authorizationRequest = requestBuilder.buildAuthorizationRequest(state);

try {
response.sendRedirect(authorizationRequest);
} catch (IOException e) {
Expand All @@ -113,6 +114,7 @@ public AuthenticationInfo handleOIDCCallback(HttpServletRequest request, HttpSer
return authenticationInfo;
}
}

logger.log(Level.ERROR, "Authentication unsuccessful. Clearing the active session and redirecting.");
throw new SSOAgentServerException(SSOAgentConstants.ErrorMessages.AUTHENTICATION_FAILED.getMessage(),
SSOAgentConstants.ErrorMessages.AUTHENTICATION_FAILED.getCode());
Expand All @@ -133,8 +135,10 @@ public void logout(AuthenticationInfo authenticationInfo, HttpServletResponse re
URI callbackURI = oidcAgentConfig.getCallbackUrl();
oidcAgentConfig.setPostLogoutRedirectURI(callbackURI);
}

OIDCRequestBuilder requestBuilder = new OIDCRequestBuilder(oidcAgentConfig);
String logoutRequest = requestBuilder.buildLogoutRequest(authenticationInfo, state);

try {
response.sendRedirect(logoutRequest);
} catch (IOException e) {
Expand All @@ -158,6 +162,7 @@ private boolean handleAuthentication(final HttpServletRequest request, Authentic
handleErrorAuthorizationResponse(authorizationResponse);
return false;
}

successResponse = authorizationResponse.toSuccessResponse();
authorizationCode = successResponse.getAuthorizationCode();
tokenRequest = getTokenRequest(authorizationCode);
Expand All @@ -167,6 +172,7 @@ private boolean handleAuthentication(final HttpServletRequest request, Authentic
handleErrorTokenResponse(tokenRequest, tokenResponse);
return false;
}

handleSuccessTokenResponse(tokenResponse, authenticationInfo);
return true;
} catch (com.nimbusds.oauth2.sdk.ParseException | SSOAgentServerException | IOException e) {
Expand All @@ -182,13 +188,15 @@ private void handleSuccessTokenResponse(TokenResponse tokenResponse, Authenticat
AccessToken accessToken = successResponse.getTokens().getAccessToken();
RefreshToken refreshToken = successResponse.getTokens().getRefreshToken();
String idToken;

try {
idToken = successResponse.getCustomParameters().get(SSOAgentConstants.ID_TOKEN).toString();
} catch (NullPointerException e) {
logger.log(Level.ERROR, "id_token is null.");
throw new SSOAgentServerException(SSOAgentConstants.ErrorMessages.ID_TOKEN_NULL.getMessage(),
SSOAgentConstants.ErrorMessages.ID_TOKEN_NULL.getCode(), e);
}

try {
JWTClaimsSet claimsSet = SignedJWT.parse(idToken).getJWTClaimsSet();
User user = new User(claimsSet.getSubject(), getUserAttributes(idToken));
Expand All @@ -208,6 +216,7 @@ private void handleErrorTokenResponse(TokenRequest tokenRequest, TokenResponse t
TokenErrorResponse errorResponse = tokenResponse.toErrorResponse();
JSONObject requestObject = requestToJson(tokenRequest);
JSONObject responseObject = errorResponse.toJSONObject();

logger.log(Level.INFO, "Request object for the error response: ", requestObject);
logger.log(Level.INFO, "Error response object: ", responseObject);
}
Expand All @@ -216,12 +225,14 @@ private void handleErrorAuthorizationResponse(AuthorizationResponse authzRespons

AuthorizationErrorResponse errorResponse = authzResponse.toErrorResponse();
JSONObject responseObject = errorResponse.getErrorObject().toJSONObject();

logger.log(Level.INFO, "Error response object: ", responseObject);
}

private TokenResponse getTokenResponse(TokenRequest tokenRequest) {

TokenResponse tokenResponse = null;

try {
tokenResponse = TokenResponse.parse(tokenRequest.toHTTPRequest().send());
} catch (com.nimbusds.oauth2.sdk.ParseException | IOException e) {
Expand All @@ -245,6 +256,7 @@ private TokenRequest getTokenRequest(AuthorizationCode authorizationCode) {
private JSONObject requestToJson(AbstractRequest request) {

JSONObject obj = new JSONObject();

obj.appendField("tokenEndpoint", request.toHTTPRequest().getURI().toString());
obj.appendField("request body", request.toHTTPRequest().getQueryParameters());
return obj;
Expand All @@ -253,6 +265,7 @@ private JSONObject requestToJson(AbstractRequest request) {
private Map<String, Object> getUserAttributes(String idToken) throws SSOAgentServerException {

Map<String, Object> userClaimValueMap = new HashMap<>();

try {
JWTClaimsSet claimsSet = SignedJWT.parse(idToken).getJWTClaimsSet();
Map<String, Object> customClaimValueMap = claimsSet.getClaims();
Expand All @@ -277,6 +290,7 @@ private void validateConfig(OIDCAgentConfig oidcAgentConfig) throws SSOAgentClie
private void validateForCode(OIDCAgentConfig oidcAgentConfig) throws SSOAgentClientException {

Scope scope = oidcAgentConfig.getScope();

if (scope.isEmpty() || !scope.contains(SSOAgentConstants.OIDC_OPENID)) {
throw new SSOAgentClientException(SSOAgentConstants.ErrorMessages.AGENT_CONFIG_SCOPE.getMessage(),
SSOAgentConstants.ErrorMessages.AGENT_CONFIG_SCOPE.getCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ private void initConfig(Properties properties) throws SSOAgentClientException {
new Secret(properties.getProperty(SSOAgentConstants.CONSUMER_SECRET)) : null;
String indexPage = properties.getProperty(SSOAgentConstants.INDEX_PAGE);
String logoutURL = properties.getProperty(SSOAgentConstants.LOGOUT_URL);
Issuer issuer = StringUtils.isNotBlank(properties.getProperty(SSOAgentConstants.OIDC_ISSUER)) ?
new Issuer(properties.getProperty(SSOAgentConstants.OIDC_ISSUER)) : null;

try {
URI callbackUrl = StringUtils.isNotBlank(properties.getProperty(SSOAgentConstants.CALL_BACK_URL)) ?
new URI(properties.getProperty(SSOAgentConstants.CALL_BACK_URL)) : null;
Expand All @@ -84,6 +87,7 @@ private void initConfig(Properties properties) throws SSOAgentClientException {
StringUtils.isNotBlank(properties.getProperty(SSOAgentConstants.POST_LOGOUT_REDIRECTION_URI)) ?
new URI(properties.getProperty(SSOAgentConstants.POST_LOGOUT_REDIRECTION_URI)) :
callbackUrl;

oidcAgentConfig.setCallbackUrl(callbackUrl);
oidcAgentConfig.setAuthorizeEndpoint(authorizeEndpoint);
oidcAgentConfig.setLogoutEndpoint(logoutEndpoint);
Expand All @@ -94,8 +98,6 @@ private void initConfig(Properties properties) throws SSOAgentClientException {
throw new SSOAgentClientException("URL not formatted properly.", e);
}

Issuer issuer = StringUtils.isNotBlank(properties.getProperty(SSOAgentConstants.OIDC_ISSUER)) ?
new Issuer(properties.getProperty(SSOAgentConstants.OIDC_ISSUER)) : null;
String scopeString = properties.getProperty(SSOAgentConstants.SCOPE);
if (StringUtils.isNotBlank(scopeString)) {
String[] scopeArray = scopeString.split(",");
Expand All @@ -111,6 +113,7 @@ private void initConfig(Properties properties) throws SSOAgentClientException {
skipURIs.add(skipURI);
}
}

oidcAgentConfig.setConsumerKey(consumerKey);
oidcAgentConfig.setConsumerSecret(consumerSecret);
oidcAgentConfig.setIndexPage(indexPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public String buildAuthorizationRequest(String state) {
URI callBackURI = oidcAgentConfig.getCallbackUrl();
URI authorizationEndpoint = oidcAgentConfig.getAuthorizeEndpoint();
State stateParameter = null;

if (StringUtils.isNotBlank(state)) {
stateParameter = new State(state);
}
Expand Down Expand Up @@ -109,6 +110,7 @@ public String buildLogoutRequest(AuthenticationInfo authenticationInfo, String s
URI redirectionURI = oidcAgentConfig.getPostLogoutRedirectURI();
JWT jwtIdToken = authenticationInfo.getIdToken();
State stateParam = null;

if (StringUtils.isNotBlank(state)) {
stateParam = new State(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public boolean isError() {
public boolean isAuthorizationCodeResponse() {

AuthorizationResponse authorizationResponse;

try {
authorizationResponse = AuthorizationResponse.parse(ServletUtils.createHTTPRequest(request));
} catch (com.nimbusds.oauth2.sdk.ParseException | IOException e) {
Expand Down Expand Up @@ -122,6 +123,7 @@ public boolean isSkipURI() {
public boolean isCallbackResponse() {

String callbackContext = oidcAgentConfig.getCallbackUrl().getPath();

return request.getRequestURI().contains(callbackContext);
}

Expand All @@ -133,6 +135,7 @@ public boolean isCallbackResponse() {
public String getIndexPage() {

String indexPage = oidcAgentConfig.getIndexPage();

if (StringUtils.isNotBlank(indexPage)) {
return indexPage;
}
Expand All @@ -143,6 +146,7 @@ private void logErrorAuthorizationResponse(AuthorizationResponse authzResponse)

AuthorizationErrorResponse errorResponse = authzResponse.toErrorResponse();
JSONObject responseObject = errorResponse.getErrorObject().toJSONObject();

logger.log(Level.INFO, "Error response object: ", responseObject);
}
}

0 comments on commit 80e97e5

Please sign in to comment.