From 5781e578d4fd65858a3e954f57597db203c395f3 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Tue, 17 Dec 2024 16:24:34 +0530 Subject: [PATCH 1/2] Improve error codes for user defined local authenticator mgt. --- .../authenticators/common/Constants.java | 4 ---- .../ServerAuthenticatorManagementService.java | 18 +++++++++++++----- ...LocalAuthenticatorConfigBuilderFactory.java | 5 ++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.common/src/main/java/org/wso2/carbon/identity/api/server/authenticators/common/Constants.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.common/src/main/java/org/wso2/carbon/identity/api/server/authenticators/common/Constants.java index a632624d11..b2c546f4f3 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.common/src/main/java/org/wso2/carbon/identity/api/server/authenticators/common/Constants.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.common/src/main/java/org/wso2/carbon/identity/api/server/authenticators/common/Constants.java @@ -71,10 +71,6 @@ public enum ErrorMessage { "Filter needs to be in the format ++. Eg: tag+eq+2FA"), ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE("60002", "Unsupported filter attribute.", "The filter attribute '%s' is not supported."), - ERROR_CODE_INVALID_ENDPOINT_CONFIG("60003", "Invalid endpoint configuration provided.", - "Invalid endpoint configuration is provided for the authenticator %s."), - ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND("60004", "Authenticator not found.", - "Authenticator not found by the given name: %s."), ERROR_CODE_ERROR_LISTING_AUTHENTICATORS("65001", "Unable to list the existing authenticators.", "Server encountered an error while listing the authenticators."), diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java index 03ada6e804..85a450da36 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java @@ -49,6 +49,7 @@ import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import org.wso2.carbon.identity.base.IdentityException; import org.wso2.carbon.identity.core.model.ExpressionNode; @@ -259,8 +260,10 @@ public Authenticator updateUserDefinedLocalAuthenticator( LocalAuthenticatorConfig existingAuthenticator = getApplicationAuthenticatorService() .getLocalAuthenticatorByName(authenticatorName, tenantDomain); if (existingAuthenticator == null) { - throw handleException(Response.Status.NOT_FOUND, - Constants.ErrorMessage.ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND, authenticatorName); + AuthenticatorMgtError error = AuthenticatorMgtError.ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND; + throw handleAuthenticatorException(new AuthenticatorMgtClientException(error.getCode(), + error.getMessage(), String.format(error.getMessage(), authenticatorName)), + Response.Status.NOT_FOUND); } UserDefinedLocalAuthenticatorConfig updatedConfig = getApplicationAuthenticatorService() .updateUserDefinedLocalAuthenticator( @@ -993,13 +996,16 @@ private APIError handleIdPException(IdentityProviderManagementException e, * @param e IdentityProviderManagementException. * @return APIError. */ - private APIError handleAuthenticatorException(AuthenticatorMgtException e) { + private APIError handleAuthenticatorException(AuthenticatorMgtException e, Response.Status... responseStatus) { ErrorResponse errorResponse = new ErrorResponse.Builder() .withCode(e.getErrorCode()) .withMessage(e.getMessage()) .withDescription(e.getDescription()).build(); - Response.Status status; + Response.Status status = null; + if (responseStatus != null && responseStatus[0] != null) { + status = responseStatus[0]; + } if (e instanceof AuthenticatorMgtClientException) { if (e.getErrorCode() != null) { @@ -1010,7 +1016,9 @@ private APIError handleAuthenticatorException(AuthenticatorMgtException e) { errorResponse.setCode(errorCode); } errorResponse.setDescription(e.getDescription()); - status = Response.Status.BAD_REQUEST; + if (status == null) { + status = Response.Status.BAD_REQUEST; + } } else if (e instanceof AuthenticatorMgtServerException) { if (e.getErrorCode() != null) { String errorCode = e.getErrorCode(); diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java index 0e36abed91..e85e279f2d 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java @@ -18,7 +18,6 @@ package org.wso2.carbon.identity.api.server.authenticators.v1.impl; -import org.wso2.carbon.identity.api.server.authenticators.common.Constants; import org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator; import org.wso2.carbon.identity.api.server.authenticators.v1.model.Endpoint; import org.wso2.carbon.identity.api.server.authenticators.v1.model.UserDefinedLocalAuthenticatorCreation; @@ -28,6 +27,7 @@ import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig; import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; import java.util.Arrays; @@ -36,7 +36,6 @@ import java.util.stream.Collectors; import static org.wso2.carbon.identity.api.server.authenticators.common.Constants.CONFIGS_AUTHENTICATOR_PATH_COMPONENT; -import static org.wso2.carbon.identity.api.server.authenticators.common.Constants.ErrorMessage.ERROR_CODE_INVALID_ENDPOINT_CONFIG; import static org.wso2.carbon.identity.api.server.common.Constants.V1_API_PATH_COMPONENT; import static org.wso2.carbon.identity.api.server.common.Util.base64URLEncode; @@ -128,7 +127,7 @@ private static UserDefinedAuthenticatorEndpointConfig buildEndpointConfig(Endpoi Map.Entry::getKey, entry -> entry.getValue().toString()))); return endpointConfigBuilder.build(); } catch (NoSuchElementException | IllegalArgumentException e) { - Constants.ErrorMessage error = ERROR_CODE_INVALID_ENDPOINT_CONFIG; + AuthenticatorMgtError error = AuthenticatorMgtError.ERROR_CODE_INVALID_ENDPOINT_CONFIG; throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), e.getMessage()); } } From f86ef5e1f9c9a25021e27969151159827ea62cf3 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Wed, 18 Dec 2024 19:35:39 +0530 Subject: [PATCH 2/2] Improve error codes for user defined local authenticator mgt. --- .../v1/core/ServerAuthenticatorManagementService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java index 30dd29f9f5..93b264877f 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java @@ -1018,7 +1018,7 @@ private APIError handleAuthenticatorException(AuthenticatorMgtException e, Respo .withMessage(e.getMessage()) .withDescription(e.getDescription()); Response.Status status = null; - if (responseStatus != null && responseStatus[0] != null) { + if (ArrayUtils.isNotEmpty(responseStatus)) { status = responseStatus[0]; } ErrorResponse errorResponse;