From 5fe52b8bfcfb5be5f4c85a9a496b4fa15c4652f0 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Tue, 10 Dec 2024 17:50:30 +0530 Subject: [PATCH 1/3] Add tags in the idp authenticator responses. --- .../v1/core/ServerIdpManagementService.java | 57 ++-------------- ...atedAuthenticatorConfigBuilderFactory.java | 66 ++++++++++++++++--- 2 files changed, 63 insertions(+), 60 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 4a08eb348c..846711ec5f 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 @@ -588,33 +588,10 @@ public FederatedAuthenticatorListResponse getFederatedAuthenticators(String idpI listResponse = new FederatedAuthenticatorListResponse(); FederatedAuthenticatorConfig[] fedAuthConfigs = idP.getFederatedAuthenticatorConfigs(); if (fedAuthConfigs != null) { - List fedAuthList = new ArrayList<>(); - String defaultAuthenticator = null; - for (FederatedAuthenticatorConfig config : fedAuthConfigs) { - String fedAuthId = base64URLEncode(config.getName()); - FederatedAuthenticatorListItem listItem = new FederatedAuthenticatorListItem(); - listItem.setAuthenticatorId(fedAuthId); - listItem.setName(config.getName()); - listItem.setIsEnabled(config.isEnabled()); - listItem.setDefinedBy( - FederatedAuthenticatorListItem.DefinedByEnum.valueOf(config.getDefinedByType().toString())); - FederatedAuthenticatorConfig federatedAuthenticatorConfig = - ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName( - config.getName()); - if (federatedAuthenticatorConfig != null) { - String[] tags = federatedAuthenticatorConfig.getTags(); - if (ArrayUtils.isNotEmpty(tags)) { - listItem.setTags(Arrays.asList(tags)); - } - } - listItem.setSelf( - ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT + - "/%s/federated-authenticators/%s", idpId, fedAuthId)).toString()); - fedAuthList.add(listItem); - if (idP.getDefaultAuthenticatorConfig() != null) { - defaultAuthenticator = base64URLEncode(idP.getDefaultAuthenticatorConfig().getName()); - } - } + List fedAuthList = FederatedAuthenticatorConfigBuilderFactory.build( + fedAuthConfigs, idP.getResourceId()); + String defaultAuthenticator = (idP.getDefaultAuthenticatorConfig() != null ? base64URLEncode(idP + .getDefaultAuthenticatorConfig().getName()) : null); listResponse.setDefaultAuthenticatorId(defaultAuthenticator); listResponse.setAuthenticators(fedAuthList); } @@ -2428,30 +2405,8 @@ private FederatedAuthenticatorListResponse createFederatedAuthenticatorResponse( FederatedAuthenticatorConfig[] fedAuthConfigs = idp.getFederatedAuthenticatorConfigs(); FederatedAuthenticatorListResponse fedAuthIDPResponse = new FederatedAuthenticatorListResponse(); - List authenticators = new ArrayList<>(); - for (FederatedAuthenticatorConfig fedAuthConfig : fedAuthConfigs) { - FederatedAuthenticatorListItem fedAuthListItem = new FederatedAuthenticatorListItem(); - fedAuthListItem.setAuthenticatorId(base64URLEncode(fedAuthConfig.getName())); - fedAuthListItem.setName(fedAuthConfig.getName()); - fedAuthListItem.setIsEnabled(fedAuthConfig.isEnabled()); - fedAuthListItem.setDefinedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf( - fedAuthConfig.getDefinedByType().toString())); - FederatedAuthenticatorConfig federatedAuthenticatorConfig = - ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName( - fedAuthConfig.getName()); - if (federatedAuthenticatorConfig != null) { - String[] tags = federatedAuthenticatorConfig.getTags(); - if (ArrayUtils.isNotEmpty(tags)) { - fedAuthListItem.setTags(Arrays.asList(tags)); - } - } - fedAuthListItem.setSelf( - ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT + - "/%s/federated-authenticators/%s", idp.getResourceId(), - base64URLEncode(fedAuthConfig.getName()))) - .toString()); - authenticators.add(fedAuthListItem); - } + List authenticators = + FederatedAuthenticatorConfigBuilderFactory.build(fedAuthConfigs, idp.getResourceId()); fedAuthIDPResponse.setDefaultAuthenticatorId(idp.getDefaultAuthenticatorConfig() != null ? base64URLEncode(idp .getDefaultAuthenticatorConfig().getName()) : null); fedAuthIDPResponse.setAuthenticators(authenticators); 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 2223458692..ef6c99e585 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,11 +20,13 @@ import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.identity.api.server.common.ContextLoader; import org.wso2.carbon.identity.api.server.idp.common.Constants; import org.wso2.carbon.identity.api.server.idp.common.IdentityProviderServiceHolder; import org.wso2.carbon.identity.api.server.idp.v1.model.AuthenticationType; import org.wso2.carbon.identity.api.server.idp.v1.model.Endpoint; import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticator; +import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListItem; import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorPUTRequest; import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; @@ -38,6 +40,7 @@ import org.wso2.carbon.idp.mgt.IdentityProviderManagementServerException; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; import java.util.List; @@ -47,7 +50,10 @@ import java.util.function.Function; import java.util.stream.Collectors; +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; import static org.wso2.carbon.identity.api.server.idp.common.Constants.GOOGLE_PRIVATE_KEY; +import static org.wso2.carbon.identity.api.server.idp.common.Constants.IDP_PATH_COMPONENT; /** * The factory class for building federated authenticator configuration related models. @@ -118,15 +124,9 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config) federatedAuthenticator.setName(config.getName()); federatedAuthenticator.setIsEnabled(config.isEnabled()); - - FederatedAuthenticatorConfig federatedAuthenticatorConfig = - ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName( - config.getName()); - if (federatedAuthenticatorConfig != null) { - String[] tags = federatedAuthenticatorConfig.getTags(); - if (ArrayUtils.isNotEmpty(tags)) { - federatedAuthenticator.setTags(Arrays.asList(tags)); - } + String[] tags = resolveAuthenticatorTags(config); + if (ArrayUtils.isNotEmpty(tags)) { + federatedAuthenticator.setTags(Arrays.asList(tags)); } if (DefinedByType.SYSTEM == config.getDefinedByType()) { @@ -141,6 +141,37 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config) return federatedAuthenticator; } + + /** + * Builds a list of FederatedAuthenticatorListItem instances based on the given array of + * FederatedAuthenticatorConfig. + * + * @param fedAuthConfigs Array of FederatedAuthenticatorConfig instances. + * @return List of FederatedAuthenticatorListItem instances. + */ + public static List build(FederatedAuthenticatorConfig[] fedAuthConfigs, + String idpResourceId) { + + List authenticators = new ArrayList<>(); + for (FederatedAuthenticatorConfig config : fedAuthConfigs) { + FederatedAuthenticatorListItem authenticatorListItem = new FederatedAuthenticatorListItem(); + authenticatorListItem.setAuthenticatorId(base64URLEncode(config.getName())); + authenticatorListItem.setName(config.getName()); + authenticatorListItem.setIsEnabled(config.isEnabled()); + authenticatorListItem.definedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf( + config.getDefinedByType().toString())); + String[] tags = resolveAuthenticatorTags(config); + if (ArrayUtils.isNotEmpty(tags)) { + authenticatorListItem.setTags(Arrays.asList(tags)); + } + authenticatorListItem.setSelf(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + + IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s", idpResourceId, + base64URLEncode(config.getName()))).toString()); + authenticators.add(authenticatorListItem); + } + + return authenticators; + } private static FederatedAuthenticatorConfig createFederatedAuthenticatorConfig(Config config) throws IdentityProviderManagementClientException { @@ -435,6 +466,23 @@ private static void resolveEndpointConfiguration(FederatedAuthenticator authenti } } + private static String[] resolveAuthenticatorTags(FederatedAuthenticatorConfig config) { + + /* If the authenticator is defined by the user, return the tags of the authenticator config. Otherwise, return + the tags of the system registered federated authenticator template. + */ + if (DefinedByType.USER == config.getDefinedByType()) { + return config.getTags(); + + } + FederatedAuthenticatorConfig federatedAuthenticatorConfig = + ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName()); + if (federatedAuthenticatorConfig != null) { + return federatedAuthenticatorConfig.getTags(); + } + return new String[0]; + } + /** * Config class to build FederatedAuthenticatorConfig. */ From 97b733472ae101946928210f25037b568a46dab5 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Tue, 17 Dec 2024 21:51:45 +0530 Subject: [PATCH 2/3] Address comments. --- ...atedAuthenticatorConfigBuilderFactory.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 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/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 ef6c99e585..2b891823e2 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 @@ -18,7 +18,6 @@ package org.wso2.carbon.identity.api.server.idp.v1.impl; -import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.identity.api.server.common.ContextLoader; import org.wso2.carbon.identity.api.server.idp.common.Constants; @@ -124,9 +123,9 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config) federatedAuthenticator.setName(config.getName()); federatedAuthenticator.setIsEnabled(config.isEnabled()); - String[] tags = resolveAuthenticatorTags(config); - if (ArrayUtils.isNotEmpty(tags)) { - federatedAuthenticator.setTags(Arrays.asList(tags)); + List tags = resolveAuthenticatorTags(config); + if (tags.isEmpty()) { + federatedAuthenticator.setTags(tags); } if (DefinedByType.SYSTEM == config.getDefinedByType()) { @@ -147,6 +146,7 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config) * FederatedAuthenticatorConfig. * * @param fedAuthConfigs Array of FederatedAuthenticatorConfig instances. + * @param idpResourceId Identity provider resource ID. * @return List of FederatedAuthenticatorListItem instances. */ public static List build(FederatedAuthenticatorConfig[] fedAuthConfigs, @@ -160,9 +160,9 @@ public static List build(FederatedAuthenticatorC authenticatorListItem.setIsEnabled(config.isEnabled()); authenticatorListItem.definedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf( config.getDefinedByType().toString())); - String[] tags = resolveAuthenticatorTags(config); - if (ArrayUtils.isNotEmpty(tags)) { - authenticatorListItem.setTags(Arrays.asList(tags)); + List tags = resolveAuthenticatorTags(config); + if (tags.isEmpty()) { + authenticatorListItem.setTags(tags); } authenticatorListItem.setSelf(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s", idpResourceId, @@ -466,21 +466,19 @@ private static void resolveEndpointConfiguration(FederatedAuthenticator authenti } } - private static String[] resolveAuthenticatorTags(FederatedAuthenticatorConfig config) { + private static List resolveAuthenticatorTags(FederatedAuthenticatorConfig config) { /* If the authenticator is defined by the user, return the tags of the authenticator config. Otherwise, return the tags of the system registered federated authenticator template. */ if (DefinedByType.USER == config.getDefinedByType()) { - return config.getTags(); - + return Arrays.asList(config.getTags()); } + FederatedAuthenticatorConfig federatedAuthenticatorConfig = ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName()); - if (federatedAuthenticatorConfig != null) { - return federatedAuthenticatorConfig.getTags(); - } - return new String[0]; + return federatedAuthenticatorConfig != null ? Arrays.asList(federatedAuthenticatorConfig.getTags()) + : new ArrayList<>(); } /** From bfca516a6e8495b5ac3f5d9d99f3f5adbc10210d Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Wed, 18 Dec 2024 08:52:08 +0530 Subject: [PATCH 3/3] Addressed comments. --- ...atedAuthenticatorConfigBuilderFactory.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 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/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 2b891823e2..3b31190a81 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 @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.api.server.idp.v1.impl; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.identity.api.server.common.ContextLoader; import org.wso2.carbon.identity.api.server.idp.common.Constants; @@ -123,9 +124,9 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config) federatedAuthenticator.setName(config.getName()); federatedAuthenticator.setIsEnabled(config.isEnabled()); - List tags = resolveAuthenticatorTags(config); - if (tags.isEmpty()) { - federatedAuthenticator.setTags(tags); + String[] tags = resolveAuthenticatorTags(config); + if (ArrayUtils.isNotEmpty(tags)) { + federatedAuthenticator.setTags(Arrays.asList(tags)); } if (DefinedByType.SYSTEM == config.getDefinedByType()) { @@ -160,9 +161,9 @@ public static List build(FederatedAuthenticatorC authenticatorListItem.setIsEnabled(config.isEnabled()); authenticatorListItem.definedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf( config.getDefinedByType().toString())); - List tags = resolveAuthenticatorTags(config); - if (tags.isEmpty()) { - authenticatorListItem.setTags(tags); + String[] tags = resolveAuthenticatorTags(config); + if (ArrayUtils.isNotEmpty(tags)) { + authenticatorListItem.setTags(Arrays.asList(tags)); } authenticatorListItem.setSelf(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s", idpResourceId, @@ -466,19 +467,18 @@ private static void resolveEndpointConfiguration(FederatedAuthenticator authenti } } - private static List resolveAuthenticatorTags(FederatedAuthenticatorConfig config) { + private static String[] resolveAuthenticatorTags(FederatedAuthenticatorConfig config) { /* If the authenticator is defined by the user, return the tags of the authenticator config. Otherwise, return the tags of the system registered federated authenticator template. */ if (DefinedByType.USER == config.getDefinedByType()) { - return Arrays.asList(config.getTags()); + return config.getTags(); } - FederatedAuthenticatorConfig federatedAuthenticatorConfig = ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName()); - return federatedAuthenticatorConfig != null ? Arrays.asList(federatedAuthenticatorConfig.getTags()) - : new ArrayList<>(); + return federatedAuthenticatorConfig != null ? federatedAuthenticatorConfig.getTags() + : new String[0]; } /**