From 4cba805c4840c6100783a5a3d79300003d7d1875 Mon Sep 17 00:00:00 2001 From: Janak Amarasena Date: Wed, 8 Nov 2023 13:00:04 +0530 Subject: [PATCH] Improve api based auth error handling --- .../framework/AuthenticationService.java | 11 +++- .../auth/service/AuthServiceConstants.java | 66 +++++++++++++++++++ .../util/auth/service/AuthServiceUtils.java | 3 +- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java index a2f11bdc3565..4f6f8c220c17 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java @@ -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); @@ -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; @@ -201,7 +204,9 @@ private List 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()) { diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceConstants.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceConstants.java index 3a2957126926..bd0ec0405397 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceConstants.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceConstants.java @@ -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; + } + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceUtils.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceUtils.java index 0d923bcd64c0..44b103c13241 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceUtils.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/auth/service/AuthServiceUtils.java @@ -62,7 +62,8 @@ public static Map 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; }