From 8b39ea677af264fb5054820567b1819aafc867c2 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Wed, 13 Nov 2024 08:07:28 +0530 Subject: [PATCH] Update APIs to support custom authentication management. --- .../v1/core/ServerIdpManagementService.java | 58 +++++++++++++++---- 1 file changed, 48 insertions(+), 10 deletions(-) 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 f6173e5141..83cf010490 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 @@ -679,7 +679,7 @@ public FederatedAuthenticatorListResponse updateFederatedAuthenticators(String i // Need to create a clone, since modifying the fields of the original object, will modify the cached // IDP object. IdentityProvider idpToUpdate = createIdPClone(idp); - updateFederatedAuthenticatorConfig(idpToUpdate, authenticatorRequest, false); + updateFederatedAuthenticatorConfig(idpToUpdate, authenticatorRequest); IdentityProvider updatedIdp = IdentityProviderServiceHolder.getIdentityProviderManager() .updateIdPByResourceId( idpId, idpToUpdate, ContextLoader.getTenantDomainFromContext()); @@ -1765,8 +1765,7 @@ private MetaOutboundConnector createMetaOutboundConnector(ProvisioningConnectorC } private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedAuthenticatorRequest - federatedAuthenticatorRequest, boolean isNewFederatedAuthenticator) - throws IdentityProviderManagementClientException { + federatedAuthenticatorRequest) throws IdentityProviderManagementClientException { if (federatedAuthenticatorRequest != null) { List federatedAuthenticators = federatedAuthenticatorRequest.getAuthenticators(); @@ -1775,13 +1774,52 @@ private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedA List fedAuthConfigs = new ArrayList<>(); for (FederatedAuthenticator authenticator : federatedAuthenticators) { String authenticatorName = getDecodedAuthenticatorName(authenticator.getAuthenticatorId()); - String definedByType; - if (isNewFederatedAuthenticator) { - definedByType = resolveDefinedByTypeToCreateFederatedAuthenticator( - authenticator.getDefinedBy().toString()).toString(); - } else { - definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString(); + String definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString(); + + if (DefinedByType.SYSTEM.toString().equals(definedByType)) { + validateAuthenticatorProperties(authenticatorName, authenticator.getProperties()); + } + + FederatedAuthenticatorConfigBuilderFactory.Builder builder = + new FederatedAuthenticatorConfigBuilderFactory.Builder(); + builder.authenticatorName(authenticatorName); + builder.definedByType(definedByType); + builder.enabled(authenticator.getIsEnabled()); + builder.displayName(getDisplayNameOfAuthenticator(authenticatorName)); + builder.endpoint(authenticator.getEndpoint()); + List properties = authenticator.getProperties().stream().map(propertyToInternal) + .collect(Collectors.toList()); + builder.properties(properties); + FederatedAuthenticatorConfig authConfig = builder.build(); + + fedAuthConfigs.add(authConfig); + + if (StringUtils.equals(defaultAuthenticator, authenticator.getAuthenticatorId())) { + defaultAuthConfig = authConfig; } + } + + if (StringUtils.isNotBlank(defaultAuthenticator) && defaultAuthConfig == null) { + throw handleException(Response.Status.BAD_REQUEST, + Constants.ErrorMessage.ERROR_CODE_INVALID_DEFAULT_AUTHENTICATOR, null); + } + idp.setFederatedAuthenticatorConfigs(fedAuthConfigs.toArray(new FederatedAuthenticatorConfig[0])); + idp.setDefaultAuthenticatorConfig(defaultAuthConfig); + } + } + + private void createFederatedAuthenticatorConfig(IdentityProvider idp, FederatedAuthenticatorRequest + federatedAuthenticatorRequest) throws IdentityProviderManagementClientException { + + if (federatedAuthenticatorRequest != null) { + List federatedAuthenticators = federatedAuthenticatorRequest.getAuthenticators(); + String defaultAuthenticator = federatedAuthenticatorRequest.getDefaultAuthenticatorId(); + FederatedAuthenticatorConfig defaultAuthConfig = null; + List fedAuthConfigs = new ArrayList<>(); + for (FederatedAuthenticator authenticator : federatedAuthenticators) { + String authenticatorName = getDecodedAuthenticatorName(authenticator.getAuthenticatorId()); + String definedByType = resolveDefinedByTypeToCreateFederatedAuthenticator( + authenticator.getDefinedBy().toString()).toString(); if (DefinedByType.SYSTEM.toString().equals(definedByType)) { validateAuthenticatorProperties(authenticatorName, authenticator.getProperties()); @@ -2123,7 +2161,7 @@ private IdentityProvider createIDP(IdentityProviderPOSTRequest identityProviderP } idp.setFederationHub(identityProviderPOSTRequest.getIsFederationHub()); - updateFederatedAuthenticatorConfig(idp, identityProviderPOSTRequest.getFederatedAuthenticators(), true); + createFederatedAuthenticatorConfig(idp, identityProviderPOSTRequest.getFederatedAuthenticators()); if (identityProviderPOSTRequest.getProvisioning() != null) { updateOutboundConnectorConfig(idp, identityProviderPOSTRequest.getProvisioning().getOutboundConnectors()); updateJIT(idp, identityProviderPOSTRequest.getProvisioning().getJit());