Skip to content

Commit

Permalink
Improve api based auth error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
janakamarasena committed Nov 8, 2023
1 parent 58aaa0e commit 4cba805
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public AuthServiceResponse handleAuthentication(AuthServiceRequest authRequest)
try {
commonAuthenticationHandler.doPost(wrappedRequest, wrappedResponse);
} catch (ServletException | IOException e) {
throw new AuthServiceException("Error while handling authentication request.", e);
throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_UNABLE_TO_PROCEED.code(),
AuthServiceConstants.ErrorMessage.ERROR_UNABLE_TO_PROCEED.description(), e);
}

return processCommonAuthResponse(wrappedRequest, wrappedResponse);
Expand Down Expand Up @@ -100,7 +101,9 @@ private AuthServiceResponse processCommonAuthResponse(AuthServiceRequestWrapper
} else if (isAuthFlowIncomplete(request)) {
handleIntermediateAuthResponse(request, response, authServiceResponse);
} else {
throw new AuthServiceException("Unknown authentication flow status: " + request.getAuthFlowStatus());
throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_UNKNOWN_AUTH_FLOW_STATUS.code(),
String.format(AuthServiceConstants.ErrorMessage.ERROR_UNKNOWN_AUTH_FLOW_STATUS.description(),
request.getAuthFlowStatus()));
}

return authServiceResponse;
Expand Down Expand Up @@ -201,7 +204,9 @@ private List<AuthenticatorData> getAuthenticatorBasicData(String authenticatorLi

ApplicationAuthenticator authenticator = FrameworkUtils.getAppAuthenticatorByName(name);
if (authenticator == null) {
throw new AuthServiceException("Authenticator not found for name: " + name);
throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.code(),
String.format(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.description(),
name));
}

if (!authenticator.isAPIBasedAuthenticationSupported()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,71 @@ public enum FlowStatus {
public static final String ERROR_CODE_PARAM = "errorCode";
public static final String ERROR_CODE_UNKNOWN_ERROR = "UNKNOWN_ERROR";
public static final String ERROR_MSG_UNKNOWN_ERROR = "Unknown error occurred.";
public static final String ERROR_CODE_PREFIX = "ABA-";

/**
* Enum for error messages.
*/
public enum ErrorMessage {

// Client errors starting from 600xx.
ERROR_AUTH_REQUEST("60001",
"Invalid authentication request.",
"Received authentication request is invalid."),
ERROR_INVALID_AUTHENTICATOR_ID("60002",
"Invalid authenticatorId.",
"Provided authenticatorId %s is invalid."),

// Server Error starting from 650xx.
ERROR_UNABLE_TO_PROCEED("65001",
"Unable to proceed with authentication.",
"Server encountered an error while processing the authentication request."),
ERROR_AUTHENTICATOR_NOT_FOUND("65002",
"Unable to find authenticator.",
"Authenticator not found for name: %s"),
ERROR_UNKNOWN_AUTH_FLOW_STATUS("65003",
"Unknown authentication flow status.",
"Unknown authentication flow status: %s");
private final String code;
private final String message;
private final String description;

ErrorMessage(String code, String message, String description) {

this.code = code;
this.message = message;
this.description = description;
}

public String code() {

return ERROR_CODE_PREFIX + code;
}

public String message() {

return message;
}

public String description() {

return description;
}

public ErrorMessage fromCode(String code) {

for (ErrorMessage error : ErrorMessage.values()) {
if (error.code.equals(code)) {
return error;
}
}
return null;
}

@Override
public String toString() {

return code + " | " + message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static Map<String, String> extractQueryParams(String url) throws AuthServ
}
}
} catch (URISyntaxException | UnsupportedEncodingException e) {
throw new AuthServiceException("Error while extracting query params from provided url.", e);
throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_UNABLE_TO_PROCEED.code(),
"Error while extracting query params from provided url.", e);
}
return queryParams;
}
Expand Down

0 comments on commit 4cba805

Please sign in to comment.