From 69baa2fc44ed2d8d791ff28af7e4adfbd059846e Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Sun, 1 Dec 2024 22:00:44 +0530 Subject: [PATCH] Add service layer support to manage the user defined local authenticators --- .../pom.xml | 4 ++ .../ApplicationAuthenticatorService.java | 19 +++-- .../AuthenticatorMgtServerException.java | 9 +-- ...uthenticatorMgtServerRuntimeException.java | 2 +- .../AuthenticatorMgtExceptionBuilder.java} | 69 +++++++++++++++++-- ...nedAuthenticatorEndpointConfigManager.java | 30 ++++---- ...serDefinedLocalAuthenticatorValidator.java | 16 ++--- .../resources/system-api-resource.xml | 12 ++++ .../resources/system-api-resource.xml.j2 | 12 ++++ 9 files changed, 122 insertions(+), 51 deletions(-) rename components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/{constant/AuthenticatorMgtErrorConstants.java => util/AuthenticatorMgtExceptionBuilder.java} (52%) diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/pom.xml b/components/application-mgt/org.wso2.carbon.identity.application.common/pom.xml index afebca5c4b47..938336adf514 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/pom.xml +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/pom.xml @@ -91,6 +91,8 @@ org.apache.commons.logging; version="${import.package.version.commons.logging}", org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}", org.apache.commons.collections; version="${commons-collections.wso2.osgi.version.range}", + org.wso2.carbon.database.utils.jdbc; version="${org.wso2.carbon.database.utils.version.range}", + org.wso2.carbon.database.utils.jdbc.exceptions; version="${org.wso2.carbon.database.utils.version.range}", org.apache.axis2.*; version="${axis2.osgi.version.range}", @@ -107,6 +109,8 @@ org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}", org.wso2.carbon.identity.central.log.mgt.*; version="${carbon.identity.package.import.version.range}", org.wso2.carbon.identity.action.management.*; version="${carbon.identity.package.import.version.range}", + org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}", + org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}", com.fasterxml.jackson.annotation; version="${com.fasterxml.jackson.annotation.version.range}" diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java index 2164d7e51099..4fefeab4dfec 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java @@ -20,15 +20,14 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages; import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl; import org.wso2.carbon.identity.application.common.dao.impl.CacheBackedAuthenticatorMgtDAO; -import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException; import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; 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.application.common.util.UserDefinedLocalAuthenticatorValidator; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; @@ -36,7 +35,8 @@ import java.util.ArrayList; import java.util.List; -import static org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildClientException; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildRuntimeServerException; /** * Application authenticator service. @@ -150,8 +150,8 @@ public void addLocalAuthenticator(LocalAuthenticatorConfig authenticator) { if (authenticator != null) { if (authenticator.getDefinedByType() != DefinedByType.SYSTEM) { - throw new AuthenticatorMgtServerRuntimeException( - ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED.getMessage()); + throw buildRuntimeServerException( + AuthenticatorMgtError.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED, null); } localAuthenticators.add(authenticator); } @@ -201,9 +201,8 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator( LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain); if (config != null) { - ErrorMessages error = ErrorMessages.ERROR_AUTHENTICATOR_ALREADY_EXIST; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), authenticatorConfig.getName())); + throw buildClientException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST, + authenticatorConfig.getName()); } authenticatorValidator.validateAuthenticatorName(authenticatorConfig.getName()); authenticatorValidator.validateForBlank("Display name", authenticatorConfig.getDisplayName()); @@ -281,9 +280,7 @@ private UserDefinedLocalAuthenticatorConfig resolveExistingAuthenticator(String getUserDefinedLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain)); if (existingAuthenticatorConfig == null) { - ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), authenticatorName)); + throw buildClientException(AuthenticatorMgtError.ERROR_NOT_FOUND_AUTHENTICATOR, authenticatorName); } return existingAuthenticatorConfig; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerException.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerException.java index f336ec560b11..d70c44ab7b83 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerException.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerException.java @@ -23,9 +23,9 @@ */ public class AuthenticatorMgtServerException extends AuthenticatorMgtException { - public AuthenticatorMgtServerException(String errorCode, String message, Throwable cause) { + public AuthenticatorMgtServerException(String errorCode, String message, String description) { - super(message, errorCode, cause); + super(message, errorCode, description); } public AuthenticatorMgtServerException(String errorCode, String message, String description, @@ -33,9 +33,4 @@ public AuthenticatorMgtServerException(String errorCode, String message, String super(message, description, errorCode, cause); } - - public AuthenticatorMgtServerException(String message) { - - super(message); - } } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java index 5c11ce9d22ee..2f90d762d0ef 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java @@ -26,7 +26,7 @@ public class AuthenticatorMgtServerRuntimeException extends RuntimeException { private final String errorCode; private final String description; - public AuthenticatorMgtServerRuntimeException(String message, String description, String errorCode) { + public AuthenticatorMgtServerRuntimeException(String errorCode, String message, String description) { super(message); this.errorCode = errorCode; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java similarity index 52% rename from components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java rename to components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java index e7053b61d03e..3b2b499cdda3 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/AuthenticatorMgtExceptionBuilder.java @@ -16,17 +16,68 @@ * under the License. */ -package org.wso2.carbon.identity.application.common.constant; +package org.wso2.carbon.identity.application.common.util; + +import org.apache.commons.lang.ArrayUtils; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerException; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerRuntimeException; /** - * Constants for authenticator configuration management service. + * Utility class for building authenticator management exceptions. */ -public class AuthenticatorMgtErrorConstants { +public class AuthenticatorMgtExceptionBuilder { + + private AuthenticatorMgtExceptionBuilder() { + + } + + public static AuthenticatorMgtClientException buildClientException(AuthenticatorMgtError error, String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + + return new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), description); + } + + public static AuthenticatorMgtServerException buildServerException(AuthenticatorMgtError error, String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + + return new AuthenticatorMgtServerException(error.getCode(), error.getMessage(), description); + } + + public static AuthenticatorMgtServerException buildServerException(AuthenticatorMgtError error, Throwable e, + String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + + return new AuthenticatorMgtServerException(error.getCode(), error.getMessage(), description, e); + } + + public static AuthenticatorMgtServerRuntimeException buildRuntimeServerException(AuthenticatorMgtError error, + Throwable e, String... data) { + + String description = error.getDescription(); + if (ArrayUtils.isNotEmpty(data)) { + description = String.format(description, data); + } + + return new AuthenticatorMgtServerRuntimeException(error.getCode(), error.getMessage(), description); + } /** - * Error messages. + * Enum class to represent the rule metadata errors. */ - public enum ErrorMessages { + public enum AuthenticatorMgtError { // Client errors. ERROR_NOT_FOUND_AUTHENTICATOR("60001", "No Authenticator found.", @@ -52,13 +103,17 @@ public enum ErrorMessages { ERROR_CODE_ENDPOINT_CONFIG_MGT("65005", "Error while managing endpoint configurations.", "Error while managing endpoint configurations for the user defined local authenticator %s."), ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED("65006", "Error while adding local authenticator.", - "Only system defined authenticators are allowed to add via this method."); + "Only system defined authenticators are allowed to add via this method."), + ERROR_CODE_NO_AUTHENTICATOR_FOUND("65007", "No authenticator found.", + "No authenticator found by given authenticator name: %s."), + ERROR_CODE_NO_ACTION_ID_FOUND("65008", "No action id found.", + "No action id found for the authenticator: %s."); private final String code; private final String message; private final String description; - ErrorMessages(String code, String message, String description) { + AuthenticatorMgtError(String code, String message, String description) { this.code = code; this.message = message; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java index b0057bdb4af7..b2852b32c17d 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java @@ -26,14 +26,14 @@ import org.wso2.carbon.identity.application.common.model.Property; 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.core.util.IdentityTenantUtil; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import static org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants - .ErrorMessages.ERROR_CODE_ENDPOINT_CONFIG_MGT; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildServerException; /** * This class responsible for managing authenticator endpoint configurations for the user defined Local @@ -63,8 +63,7 @@ public void addEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config endpointProperty.setValue(action.getId()); config.setProperties(new Property[]{endpointProperty}); } catch (ActionMgtException e) { - throw new AuthenticatorMgtServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), - "Error occurred while adding associated action for the authenticator:" + config.getName(), e); + throw buildServerException(AuthenticatorMgtError.ERROR_CODE_ENDPOINT_CONFIG_MGT, e, config.getName()); } } @@ -89,9 +88,8 @@ public void updateEndpointConfigurations(UserDefinedLocalAuthenticatorConfig new IdentityTenantUtil.getTenantDomain(tenantId)); newConfig.setProperties(oldConfig.getProperties()); } catch (ActionMgtException e) { - throw new AuthenticatorMgtServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), - String.format("Error occurred while updating associated action with id %s for the authenticator %s", - actionId, oldConfig.getName()), e); + throw buildServerException(AuthenticatorMgtError.ERROR_CODE_ENDPOINT_CONFIG_MGT, e, + actionId, oldConfig.getName()); } } @@ -106,6 +104,9 @@ public void updateEndpointConfigurations(UserDefinedLocalAuthenticatorConfig new public UserDefinedLocalAuthenticatorConfig resolveEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, int tenantId) throws AuthenticatorMgtServerException { + if (config == null) { + return null; + } String actionId = getActionIdFromProperty(config.getProperties(), config.getName()); try { Action action = ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() @@ -116,9 +117,8 @@ public UserDefinedLocalAuthenticatorConfig resolveEndpointConfigurations(UserDef config.setEndpointConfig(buildUserDefinedAuthenticatorEndpointConfig(action.getEndpoint())); return config; } catch (ActionMgtException e) { - throw new AuthenticatorMgtServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), - String.format("Error occurred retrieving associated action with id %s for the authenticator %s", - actionId, config.getName()), e); + throw buildServerException(AuthenticatorMgtError.ERROR_CODE_ENDPOINT_CONFIG_MGT, e, + actionId, config.getName()); } } @@ -154,9 +154,8 @@ public void deleteEndpointConfigurations(UserDefinedLocalAuthenticatorConfig con actionId, IdentityTenantUtil.getTenantDomain(tenantId)); } catch (ActionMgtException e) { - throw new AuthenticatorMgtServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), - String.format("Error occurred while deleting associated action with id %s for the authenticator %s", - actionId, config.getName()), e); + throw buildServerException(AuthenticatorMgtError.ERROR_CODE_ENDPOINT_CONFIG_MGT, e, + actionId, config.getName()); } } @@ -186,8 +185,7 @@ private String getActionIdFromProperty(Property[] properties, String authenticat .filter(property -> ACTION_ID_PROPERTY.equals(property.getName())) .map(Property::getValue) .findFirst() - .orElseThrow(() -> new AuthenticatorMgtServerException( - "No action Id was found in the properties of the authenticator configurations for" + - " the authenticator: " + authenticatorName)); + .orElseThrow(() -> buildServerException(AuthenticatorMgtError.ERROR_CODE_NO_ACTION_ID_FOUND, + authenticatorName)); } } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java index cc02731ba9d8..a457c1d49f91 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java @@ -19,12 +19,14 @@ package org.wso2.carbon.identity.application.common.util; import org.apache.commons.lang.StringUtils; -import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages; import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException; +import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import java.util.regex.Pattern; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildClientException; + /** * User Defined Local Authenticator Validator class. */ @@ -43,9 +45,7 @@ public class UserDefinedLocalAuthenticatorValidator { public void validateForBlank(String fieldName, String fieldValue) throws AuthenticatorMgtClientException { if (StringUtils.isBlank(fieldValue)) { - ErrorMessages error = ErrorMessages.ERROR_BLANK_FIELD_VALUE; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), fieldName)); + throw buildClientException(AuthenticatorMgtError.ERROR_BLANK_FIELD_VALUE, fieldName); } } @@ -59,9 +59,8 @@ public void validateAuthenticatorName(String name) throws AuthenticatorMgtClient boolean isValidName = authenticatorNameRegexPattern.matcher(name).matches(); if (!isValidName) { - ErrorMessages error = ErrorMessages.ERROR_INVALID_AUTHENTICATOR_NAME; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), name, AUTHENTICATOR_NAME_REGEX)); + throw buildClientException(AuthenticatorMgtError.ERROR_INVALID_AUTHENTICATOR_NAME, + name, AUTHENTICATOR_NAME_REGEX); } } @@ -75,8 +74,7 @@ public void validateDefinedByType(DefinedByType definedByType) throws AuthenticatorMgtClientException { if (definedByType != DefinedByType.USER) { - ErrorMessages error = ErrorMessages.ERROR_OP_ON_SYSTEM_AUTHENTICATOR; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), error.getDescription()); + throw buildClientException(AuthenticatorMgtError.ERROR_OP_ON_SYSTEM_AUTHENTICATOR); } } } diff --git a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml index c62001bd8ce9..0b22a4cd6893 100644 --- a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml +++ b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml @@ -116,6 +116,18 @@ description="Delete actions"/> + + + + + + + diff --git a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 index da9c594f422d..196e82cb7d89 100644 --- a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 +++ b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 @@ -125,6 +125,18 @@ description="Delete actions"/> + + + + + + +