From c096f8d77d87dc6eabe6448718f1e0976455977b Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Mon, 11 Nov 2024 09:03:11 +0530 Subject: [PATCH] Add service layer support for the custom local auth extensions. --- .../ApplicationAuthenticatorService.java | 62 +++--- .../AuthenticatorMgtErrorConstants.java | 4 +- .../ApplicationCommonServiceComponent.java | 49 +++++ .../ApplicationCommonServiceDataHolder.java | 32 +++ .../VerificationAuthenticatorConfig.java | 49 ----- ...nedAuthenticatorEndpointConfigManager.java | 193 ++++++++++++++++++ ...serDefinedLocalAuthenticatorValidator.java | 9 +- 7 files changed, 306 insertions(+), 92 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceComponent.java create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceDataHolder.java delete mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/VerificationAuthenticatorConfig.java create mode 100644 components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java 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 021a881b70cd..f99eed76be3a 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 @@ -28,6 +28,7 @@ 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.UserDefinedLocalAuthenticatorValidator; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; @@ -63,28 +64,19 @@ public static ApplicationAuthenticatorService getInstance() { return instance; } - /** - * This returns only SYSTEM defined local authenticators. - * - * @return Retrieved LocalAuthenticatorConfig. - */ - @Deprecated public List getLocalAuthenticators() { return this.localAuthenticators; } /** - * This returns both SYSTEM and USER defined local authenticators. + * This returns User defined local authenticators. * * @return Retrieved LocalAuthenticatorConfig. */ - public List getLocalAuthenticators(String tenantDomain) + public List getUserDefinedLocalAuthenticators(String tenantDomain) throws AuthenticatorMgtException { - List userDefinedAuthenticators = - CACHE_BACKED_DAO.getAllUserDefinedLocalAuthenticator(IdentityTenantUtil.getTenantId(tenantDomain)); - userDefinedAuthenticators.addAll(localAuthenticators); - return userDefinedAuthenticators; + return CACHE_BACKED_DAO.getAllUserDefinedLocalAuthenticator(IdentityTenantUtil.getTenantId(tenantDomain)); } public List getFederatedAuthenticators() { @@ -99,8 +91,10 @@ public List getRequestPathAuthenticators() { * This returns only SYSTEM defined local authenticator by name. * * @param name The name of the Local Application Authenticator configuration. - * * @return Retrieved LocalAuthenticatorConfig. + * + * @deprecated It is recommended to use {@link #getLocalAuthenticatorByName(String, String)}, + * which supports retrieving both USER and SYSTEM defined Local Application Authenticator configuration by name. */ @Deprecated public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name) { @@ -117,7 +111,6 @@ public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name) { * * @param name The name of the Local Application Authenticator configuration. * @param tenantDomain Tenant domain. - * * @return Retrieved LocalAuthenticatorConfig. * @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration by name. */ @@ -194,12 +187,12 @@ public void removeRequestPathAuthenticator(RequestPathAuthenticatorConfig authen * @param authenticatorConfig The Local Application Authenticator configuration. * @param type Authentication type of the authenticator. * @param tenantDomain Tenant domain. - * * @return Updated LocalAuthenticatorConfig. * @throws AuthenticatorMgtException If an error occurs while creating the authenticator configuration. */ - public LocalAuthenticatorConfig createUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, - AuthenticationType type, String tenantDomain) throws AuthenticatorMgtException { + public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator( + UserDefinedLocalAuthenticatorConfig authenticatorConfig, AuthenticationType type, String tenantDomain) + throws AuthenticatorMgtException { LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain); if (config != null) { @@ -220,14 +213,14 @@ public LocalAuthenticatorConfig createUserDefinedLocalAuthenticator(LocalAuthent * * @param authenticatorConfig The Local Application Authenticator configuration. * @param tenantDomain Tenant Domain. - * - * @return Updated LocalAuthenticatorConfig. + * @return Updated UserDefinedLocalAuthenticatorConfig. * @throws AuthenticatorMgtException If an error occurs while updating the authenticator configuration. */ - public LocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, - String tenantDomain) throws AuthenticatorMgtException { + public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator( + UserDefinedLocalAuthenticatorConfig authenticatorConfig, String tenantDomain) + throws AuthenticatorMgtException { - LocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator( + UserDefinedLocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator( authenticatorConfig.getName(), tenantDomain); authenticatorValidator.validateDefinedByType(existingConfig.getDefinedByType()); authenticatorValidator.validateForBlank("Display name", authenticatorConfig.getDisplayName()); @@ -241,16 +234,16 @@ public LocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(LocalAuthent * * @param authenticatorName Name of Local Application Authenticator configuration to be deleted. * @param tenantDomain Tenant domain. - * * @throws AuthenticatorMgtException If an error occurs while deleting the authenticator configuration. */ public void deleteUserDefinedLocalAuthenticator(String authenticatorName, String tenantDomain) throws AuthenticatorMgtException { - LocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator(authenticatorName, tenantDomain); + UserDefinedLocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator( + authenticatorName, tenantDomain); authenticatorValidator.validateDefinedByType(existingConfig.getDefinedByType()); - CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName, + CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName, existingConfig, IdentityTenantUtil.getTenantId(tenantDomain)); } @@ -259,15 +252,14 @@ public void deleteUserDefinedLocalAuthenticator(String authenticatorName, String * * @param authenticatorName Name of Local Application Authenticator configuration to be deleted. * @param tenantDomain Tenant domain. - * - * @return Retrieved LocalAuthenticatorConfig. + * @return Retrieved UserDefinedLocalAuthenticatorConfig. * @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration. */ - public LocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenticatorName, String tenantDomain) - throws AuthenticatorMgtException { + public UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenticatorName, + String tenantDomain) throws AuthenticatorMgtException { - LocalAuthenticatorConfig config = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator(authenticatorName, - IdentityTenantUtil.getTenantId(tenantDomain)); + UserDefinedLocalAuthenticatorConfig config = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator( + authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain)); if (config != null && !config.getDefinedByType().equals(DefinedByType.USER)) { return null; @@ -277,11 +269,11 @@ public LocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenti } - private LocalAuthenticatorConfig resolveExistingAuthenticator(String authenticatorName, String tenantDomain) - throws AuthenticatorMgtException { + private UserDefinedLocalAuthenticatorConfig resolveExistingAuthenticator(String authenticatorName, + String tenantDomain) throws AuthenticatorMgtException { - LocalAuthenticatorConfig existingAuthenticatorConfig = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator( - authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain)); + UserDefinedLocalAuthenticatorConfig existingAuthenticatorConfig = CACHE_BACKED_DAO. + getUserDefinedLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain)); if (existingAuthenticatorConfig == null) { ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR; 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/constant/AuthenticatorMgtErrorConstants.java index 02ecc9e52d00..0e5fa90c883d 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/constant/AuthenticatorMgtErrorConstants.java @@ -48,7 +48,9 @@ public enum ErrorMessages { ERROR_WHILE_RETRIEVING_AUTHENTICATOR_BY_NAME("65003", "Error while retrieving authenticator.", "Error while retrieving authenticator from the system."), ERROR_WHILE_DELETING_AUTHENTICATOR("65004", "Error while deleting authenticator.", - "Error while deleting authenticator from the system."),; + "Error while deleting authenticator from the system."), + ERROR_CODE_ENDPOINT_CONFIG_MGT("65005", "Error while managing endpoint configurations.", + "Error while managing endpoint configurations for the user defined local authenticator %s."),; private final String code; private final String message; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceComponent.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceComponent.java new file mode 100644 index 000000000000..99151fe58567 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceComponent.java @@ -0,0 +1,49 @@ +package org.wso2.carbon.identity.application.common.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.identity.action.management.ActionManagementService; + +/** + * OSGI service component for the Application Common Service Component. + */ +@Component( + name = "application.common.service.component", + immediate = true +) +public class ApplicationCommonServiceComponent { + + private static final Log LOG = LogFactory.getLog(ApplicationCommonServiceComponent.class); + + @Reference( + name = "action.management.service", + service = ActionManagementService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetActionManagementService" + ) + protected void setActionManagementService(ActionManagementService actionManagementService) { + + if (LOG.isDebugEnabled()) { + LOG.debug( + "Registering a reference for ActionManagementService in the ApplicationCommonServiceComponent."); + } + ApplicationCommonServiceDataHolder.getInstance().setActionManagementService(actionManagementService); + } + + protected void unsetActionManagementService(ActionManagementService actionManagementService) { + + if (LOG.isDebugEnabled()) { + LOG.debug("Unregistering the reference for ActionManagementService in the " + + "ApplicationCommonServiceComponent."); + } + if (ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() + .equals(actionManagementService)) { + ApplicationCommonServiceDataHolder.getInstance().setActionManagementService(null); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceDataHolder.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceDataHolder.java new file mode 100644 index 000000000000..65ce99834a5b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/internal/ApplicationCommonServiceDataHolder.java @@ -0,0 +1,32 @@ +package org.wso2.carbon.identity.application.common.internal; + +import org.wso2.carbon.identity.action.management.ActionManagementService; + +/** + * The data holder for the Application Common Service Component. + */ +public class ApplicationCommonServiceDataHolder { + + private static final ApplicationCommonServiceDataHolder INSTANCE = new ApplicationCommonServiceDataHolder(); + + private ActionManagementService actionManagementService; + + private ApplicationCommonServiceDataHolder() { + + } + + public static ApplicationCommonServiceDataHolder getInstance() { + + return INSTANCE; + } + + public ActionManagementService getActionManagementService() { + + return actionManagementService; + } + + public void setActionManagementService(ActionManagementService actionManagementService) { + + this.actionManagementService = actionManagementService; + } +} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/VerificationAuthenticatorConfig.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/VerificationAuthenticatorConfig.java deleted file mode 100644 index 97efab22cc93..000000000000 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/model/VerificationAuthenticatorConfig.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.identity.application.common.model; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * Verification authenticator configuration. - */ -public class VerificationAuthenticatorConfig extends LocalAuthenticatorConfig { - - private static final String TAG_2FA = "2FA"; - - public VerificationAuthenticatorConfig() { - - setTags(new String[0]); - } - - @Override - public void setTags(String[] tagList) { - - // Check if "2FA" is in the tag list; if not, add it. - List tagsAsList = new ArrayList<>(Arrays.asList()); - if (tagsAsList.contains(TAG_2FA)) { - tags = tagList; - } - - tagsAsList.add(TAG_2FA); - tags = tagsAsList.toArray(new String[0]); - } -} 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 new file mode 100644 index 000000000000..b0057bdb4af7 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedAuthenticatorEndpointConfigManager.java @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.common.util; + +import org.wso2.carbon.identity.action.management.exception.ActionMgtException; +import org.wso2.carbon.identity.action.management.model.Action; +import org.wso2.carbon.identity.action.management.model.EndpointConfig; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerException; +import org.wso2.carbon.identity.application.common.internal.ApplicationCommonServiceDataHolder; +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.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; + +/** + * This class responsible for managing authenticator endpoint configurations for the user defined Local + * authenticators. + */ +public class UserDefinedAuthenticatorEndpointConfigManager { + + private static final String ACTION_ID_PROPERTY = "actionId"; + + /** + * Create a new action for given endpoint configurations of the user defined authenticator. + * + * @param config The Local application authenticator configuration. + * @param tenantId The id of Tenant domain. + * @throws AuthenticatorMgtServerException If an error occurs while adding the action. + */ + public void addEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, int tenantId) + throws AuthenticatorMgtServerException { + + try { + Action action = ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() + .addAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), + buildActionToCreate(config.getName(), config.getEndpointConfig().getEndpointConfig()), + IdentityTenantUtil.getTenantDomain(tenantId)); + Property endpointProperty = new Property(); + endpointProperty.setName(ACTION_ID_PROPERTY); + 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); + } + } + + /** + * Updated associated action for given updated endpoint configurations of the user defined authenticator. + * + * @param newConfig The Local application authenticator configuration to be updated. + * @param oldConfig The current Local application authenticator configuration. + * @param tenantId The id of Tenant domain. + * @throws AuthenticatorMgtServerException If an error occurs while updating associated action. + */ + public void updateEndpointConfigurations(UserDefinedLocalAuthenticatorConfig newConfig, + UserDefinedLocalAuthenticatorConfig oldConfig, int tenantId) + throws AuthenticatorMgtServerException { + + String actionId = getActionIdFromProperty(oldConfig.getProperties(), oldConfig.getName()); + try { + ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() + .updateAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), + actionId, + buildActionToUpdate(newConfig.getEndpointConfig().getEndpointConfig()), + 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); + } + } + + /** + * Retrieve associated action of the user defined authenticator. + * + * @param config The Local application authenticator configuration. + * @param tenantId The id of Tenant domain. + * @return Local authenticator with endpoint configurations resolved. + * @throws AuthenticatorMgtServerException If an error occurs retrieving updating associated action. + */ + public UserDefinedLocalAuthenticatorConfig resolveEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, + int tenantId) throws AuthenticatorMgtServerException { + + String actionId = getActionIdFromProperty(config.getProperties(), config.getName()); + try { + Action action = ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() + .getActionByActionId(Action.ActionTypes.AUTHENTICATION.getPathParam(), + actionId, + IdentityTenantUtil.getTenantDomain(tenantId)); + + 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); + } + } + + private UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig( + EndpointConfig endpointConfig) { + + UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = + new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder(); + endpointConfigBuilder.uri(endpointConfig.getUri()); + endpointConfigBuilder.authenticationType(endpointConfig.getAuthentication().getType().getName()); + Map propMap = new HashMap<>(); + endpointConfig.getAuthentication().getProperties() + .forEach(prop -> propMap.put(prop.getName(), prop.getValue())); + endpointConfigBuilder.authenticationProperties(propMap); + return endpointConfigBuilder.build(); + } + + /** + * Delete associated action of the user defined authenticator. + * + * @param config The Local application authenticator configuration. + * @param tenantId The id of Tenant domain. + * + * @throws AuthenticatorMgtServerException If an error occurs while deleting associated action. + */ + public void deleteEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, int tenantId) throws + AuthenticatorMgtServerException { + + String actionId = getActionIdFromProperty(config.getProperties(), config.getName()); + try { + ApplicationCommonServiceDataHolder.getInstance().getActionManagementService() + .deleteAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), + 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); + } + } + + private Action buildActionToCreate(String authenticatorName, EndpointConfig endpointConfig) { + + Action.ActionRequestBuilder actionRequestBuilder = new Action.ActionRequestBuilder(); + actionRequestBuilder.name(authenticatorName); + actionRequestBuilder.description(String.format("This is the action associated to the user defined Local" + + "authenticator %s.", authenticatorName)); + actionRequestBuilder.endpoint(endpointConfig); + + return actionRequestBuilder.build(); + } + + private Action buildActionToUpdate(EndpointConfig endpointConfig) { + + Action.ActionRequestBuilder actionRequestBuilder = new Action.ActionRequestBuilder(); + actionRequestBuilder.endpoint(endpointConfig); + + return actionRequestBuilder.build(); + } + + private String getActionIdFromProperty(Property[] properties, String authenticatorName) + throws AuthenticatorMgtServerException { + + return Arrays.stream(properties) + .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)); + } +} 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 1ff69af14d20..cc02731ba9d8 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 @@ -18,11 +18,9 @@ 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.model.LocalAuthenticatorConfig; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import java.util.regex.Pattern; @@ -55,7 +53,6 @@ public void validateForBlank(String fieldName, String fieldValue) throws Authent * Validate the user defined local authenticator name. * * @param name The authenticator name. - * * @throws AuthenticatorMgtClientException if the authenticator name is not valid. */ public void validateAuthenticatorName(String name) throws AuthenticatorMgtClientException { @@ -71,8 +68,7 @@ public void validateAuthenticatorName(String name) throws AuthenticatorMgtClient /** * Validate the authenticator is a user defined by authenticator. * - * @param authenticatorConfig The authenticator config. - * + * @param definedByType The defined by type of the authenticator config. * @throws AuthenticatorMgtClientException if the authenticator is not a user defined authenticator. */ public void validateDefinedByType(DefinedByType definedByType) @@ -80,8 +76,7 @@ public void validateDefinedByType(DefinedByType definedByType) if (definedByType != DefinedByType.USER) { ErrorMessages error = ErrorMessages.ERROR_OP_ON_SYSTEM_AUTHENTICATOR; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), authenticatorConfig.getName())); + throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), error.getDescription()); } } }