From fa4caea50a04ec2335135afe2be090e3f13ac43d Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Wed, 13 Nov 2024 08:03:54 +0530 Subject: [PATCH] Update APIs to support custom authentication management. --- .../api/server/idp/common/Constants.java | 10 ++++----- .../v1/core/ServerIdpManagementService.java | 21 +++++++++++-------- ...atedAuthenticatorConfigBuilderFactory.java | 11 +--------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.common/src/main/java/org/wso2/carbon/identity/api/server/idp/common/Constants.java b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.common/src/main/java/org/wso2/carbon/identity/api/server/idp/common/Constants.java index 7b96242696..858226befd 100644 --- a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.common/src/main/java/org/wso2/carbon/identity/api/server/idp/common/Constants.java +++ b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.common/src/main/java/org/wso2/carbon/identity/api/server/idp/common/Constants.java @@ -146,13 +146,13 @@ public enum ErrorMessage { ERROR_CODE_ERROR_LISTING_TRUSTED_TOKEN_ISSUERS("60021", "Unable to list existing trusted token issuers.", "Server encountered an error while listing the trusted token issuers."), - ERROR_CODE_ENDPOINT_PROVIDED_FOR_SYSTEM_AUTH("60039", "No endpoint configuration is allowed " + - "for system defined authenticators.", "No endpoint configuration must be " + + ERROR_CODE_ENDPOINT_PROVIDED_FOR_SYSTEM_AUTH("60039", "Invalid Request.", + "No endpoint configuration must be " + "provided for the system defined federated authenticators %s."), - ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH("60040", "No properties are allowed for " + - "user defined authenticators.", "No properties must be provided for the user defined " + + ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH("60040", "Invalid Request.", + "No properties must be provided for the user defined " + "federated authenticators %s."), - ERROR_CODE_NO_ENDPOINT_PROVIDED("60041", "No endpoint provided.", "Endpoint " + + ERROR_CODE_NO_ENDPOINT_PROVIDED("60041", "Invalid Request.", "Endpoint " + "configuration must be provided for the user defined federated authenticators %s."), ERROR_CODE_NON_DECODABLE_AUTH_ID("60042", "Non-decodable authenticator ID.", "Unable to decode the provided authenticator ID %s."), diff --git a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/core/ServerIdpManagementService.java b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/core/ServerIdpManagementService.java index 34602068d7..f6a5bb19cd 100644 --- a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/core/ServerIdpManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/core/ServerIdpManagementService.java @@ -146,6 +146,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -1777,10 +1778,10 @@ private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedA String authenticatorName = getDecodedAuthenticatorName(authenticator.getAuthenticatorId()); String definedByType; if (isNewFederatedAuthenticator) { - definedByType = resolveDefinedByTypeForCreateFederatedAuthenticator( + definedByType = resolveDefinedByTypeToCreateFederatedAuthenticator( authenticator.getDefinedBy().toString()).toString(); } else { - definedByType = resolveDefinedByTypeForUpdateFederatedAuthenticator(authenticatorName).toString(); + definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString(); } if (DefinedByType.SYSTEM.toString().equals(definedByType)) { @@ -1794,8 +1795,9 @@ private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedA builder.enabled(authenticator.getIsEnabled()); builder.displayName(getDisplayNameOfAuthenticator(authenticatorName)); builder.endpoint(authenticator.getEndpoint()); - List properties = authenticator.getProperties().stream().map(propertyToInternal) - .collect(Collectors.toList()); + List properties = Optional.ofNullable(authenticator.getProperties()) + .map(props -> props.stream().map(propertyToInternal).collect(Collectors.toList())) + .orElse(null); builder.properties(properties); FederatedAuthenticatorConfig authConfig = builder.build(); @@ -2848,7 +2850,7 @@ private FederatedAuthenticatorConfig updateFederatedAuthenticatorConfig(String f FederatedAuthenticatorPUTRequest authenticator) throws IdentityProviderManagementClientException { String authenticatorName = getDecodedAuthenticatorName(federatedAuthenticatorId); - String definedByType = resolveDefinedByTypeForUpdateFederatedAuthenticator(authenticatorName).toString(); + String definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString(); if (DefinedByType.SYSTEM.toString().equals(definedByType)) { validateAuthenticatorProperties(authenticatorName, authenticator.getProperties()); } @@ -2860,14 +2862,15 @@ private FederatedAuthenticatorConfig updateFederatedAuthenticatorConfig(String f builder.enabled(authenticator.getIsEnabled()); builder.displayName(getDisplayNameOfAuthenticator(authenticatorName)); builder.endpoint(authenticator.getEndpoint()); - List properties = authenticator.getProperties().stream().map(propertyToInternal) - .collect(Collectors.toList()); + List properties = Optional.ofNullable(authenticator.getProperties()) + .map(props -> props.stream().map(propertyToInternal).collect(Collectors.toList())) + .orElse(null); builder.properties(properties); return builder.build(); } - private DefinedByType resolveDefinedByTypeForCreateFederatedAuthenticator(String definedByType) { + private DefinedByType resolveDefinedByTypeToCreateFederatedAuthenticator(String definedByType) { /* For new federated authenticators: If 'definedByType' is not null, use the value provided in the request payload. If not, default to SYSTEM. */ @@ -2877,7 +2880,7 @@ private DefinedByType resolveDefinedByTypeForCreateFederatedAuthenticator(String return DefinedByType.SYSTEM; } - private DefinedByType resolveDefinedByTypeForUpdateFederatedAuthenticator(String authenticatorName) { + private DefinedByType resolveDefinedByTypeToUpdateFederatedAuthenticator(String authenticatorName) { /* For existing federated authenticators, disregard any value provided in the request payload. Instead, resolve and retrieve the 'definedBy' type of the corresponding existing authenticator. diff --git a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/impl/FederatedAuthenticatorConfigBuilderFactory.java b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/impl/FederatedAuthenticatorConfigBuilderFactory.java index f769c1e31b..3abb38b961 100644 --- a/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/impl/FederatedAuthenticatorConfigBuilderFactory.java +++ b/components/org.wso2.carbon.identity.api.server.idp/org.wso2.carbon.identity.api.server.idp.v1/src/main/java/org/wso2/carbon/identity/api/server/idp/v1/impl/FederatedAuthenticatorConfigBuilderFactory.java @@ -20,7 +20,6 @@ import org.wso2.carbon.identity.api.server.idp.common.Constants; import org.wso2.carbon.identity.api.server.idp.v1.model.Endpoint; -import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.Property; import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig; @@ -74,14 +73,6 @@ private static void validateSystemDefinedFederatedAuthenticatorModel(Builder bui throw new IdentityProviderManagementClientException(error.getCode(), String.format(error.getDescription(), builder.authenticatorName)); } - - // Check if there is an authenticator registered in the system for the given authenticator ID. - if (ApplicationAuthenticatorService.getInstance() - .getFederatedAuthenticatorByName(builder.authenticatorName) == null) { - Constants.ErrorMessage error = Constants.ErrorMessage.ERROR_CODE_NO_SYSTEM_AUTHENTICATOR_FOUND; - throw new IdentityProviderManagementClientException(error.getCode(), - String.format(error.getDescription(), builder.authenticatorName)); - } } private static UserDefinedFederatedAuthenticatorConfig createUserDefinedFederatedAuthenticator(Builder builder) @@ -106,7 +97,7 @@ private static void validateUserDefinedFederatedAuthenticatorModel(Builder build throws IdentityProviderManagementClientException { // The User-defined authenticator configs must not have properties configurations; throw an error if they do. - if (builder.properties == null || !builder.properties.isEmpty()) { + if (builder.properties != null) { Constants.ErrorMessage error = Constants.ErrorMessage.ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH; throw new IdentityProviderManagementClientException(error.getCode(), String.format(error.getDescription(), builder.authenticatorName));