From 495adff60bd5b555335593439517209f225aeab8 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Sat, 25 Jan 2025 22:22:58 +0530 Subject: [PATCH] Add new method to get federated authenticator by name. --- .../ApplicationAuthenticationService.java | 21 ++++--- .../framework/AuthenticationService.java | 34 ++++++----- .../cache/AuthenticationContextLoader.java | 6 +- .../FileBasedConfigurationBuilder.java | 5 +- .../loader/UIBasedConfigurationLoader.java | 56 +++---------------- .../model/OptimizedAuthenticatorConfig.java | 5 +- .../core/ApplicationAuthenticatorManager.java | 42 ++++++++++++++ .../framework/AbstractFrameworkTest.java | 15 +++++ ...GraphBasedSequenceHandlerAbstractTest.java | 2 +- .../framework/util/FrameworkUtilsTest.java | 12 +++- 10 files changed, 119 insertions(+), 79 deletions(-) diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/ApplicationAuthenticationService.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/ApplicationAuthenticationService.java index 2175d265017a..0109d1fa21da 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/ApplicationAuthenticationService.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/ApplicationAuthenticationService.java @@ -21,14 +21,17 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.identity.application.authentication.framework.exception.ApplicationAuthenticationException; -import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import java.util.ArrayList; import java.util.List; /** - * Application authentication service. + * Application authentication service. This server only return the system defined authenticators. + * This server is exposed to external and currently only being used for API based authenticator which is only support + * for system defined authenticators. */ +@Deprecated public class ApplicationAuthenticationService { private static final Log log = LogFactory.getLog(ApplicationAuthenticationService.class); @@ -43,7 +46,8 @@ public ApplicationAuthenticator getAuthenticator(String name) throws Application ApplicationAuthenticator appAuthenticator = null; - for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) { + for (ApplicationAuthenticator authenticator : + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) { if (authenticator.getName().equals(name)) { appAuthenticator = authenticator; @@ -54,14 +58,15 @@ public ApplicationAuthenticator getAuthenticator(String name) throws Application } public List getAllAuthenticators() throws ApplicationAuthenticationException { - return FrameworkServiceComponent.getAuthenticators(); + return ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators(); } public List getLocalAuthenticators() throws ApplicationAuthenticationException { List localAuthenticators = new ArrayList(); - for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) { + for (ApplicationAuthenticator authenticator : + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) { if (authenticator instanceof LocalApplicationAuthenticator) { localAuthenticators.add(authenticator); @@ -75,7 +80,8 @@ public List getFederatedAuthenticators() throws Applic List federatedAuthenticators = new ArrayList(); - for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) { + for (ApplicationAuthenticator authenticator : + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) { if (authenticator instanceof FederatedApplicationAuthenticator) { federatedAuthenticators.add(authenticator); @@ -89,7 +95,8 @@ public List getRequestPathAuthenticators() throws Appl List reqPathAuthenticators = new ArrayList(); - for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) { + for (ApplicationAuthenticator authenticator : + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) { if (authenticator instanceof RequestPathApplicationAuthenticator) { reqPathAuthenticators.add(authenticator); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java index 5a3abe96f378..366fa560594e 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/AuthenticationService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationResultCacheEntry; import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceClientException; import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceException; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult; import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorData; import org.wso2.carbon.identity.application.authentication.framework.model.auth.service.AuthServiceErrorInfo; @@ -140,7 +141,7 @@ private void handleIntermediateAuthResponse(AuthServiceRequestWrapper request, A List authenticatorDataList; if (isMultiOptionsResponse) { responseData.setAuthenticatorSelectionRequired(true); - authenticatorDataList = getAuthenticatorBasicData(response.getAuthenticators(), + authenticatorDataList = getAuthenticatorBasicData(request, response.getAuthenticators(), request.getAuthInitiationData()); } else { authenticatorDataList = request.getAuthInitiationData(); @@ -274,9 +275,8 @@ private String getErrorMessage(AuthServiceResponseWrapper response) throws AuthS return queryParams.get(AuthServiceConstants.AUTH_FAILURE_MSG_PARAM); } - private List getAuthenticatorBasicData(String authenticatorList, - List authInitiationData) - throws AuthServiceException { + private List getAuthenticatorBasicData(AuthServiceRequestWrapper request, + String authenticatorList, List authInitiationData) throws AuthServiceException { List authenticatorDataList = new ArrayList<>(); String[] authenticatorAndIdpsArr = StringUtils.split(authenticatorList, @@ -293,7 +293,8 @@ private List getAuthenticatorBasicData(String authenticatorLi continue; } - ApplicationAuthenticator authenticator = FrameworkUtils.getAppAuthenticatorByName(name); + ApplicationAuthenticator authenticator = ApplicationAuthenticatorManager.getInstance() + .getAppAuthenticatorByName(name, getTenantDomain((HttpServletRequest) request.getRequest())); if (authenticator == null) { throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.code(), String.format(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.description(), @@ -413,7 +414,7 @@ private void validateRequest(AuthServiceRequest authServiceRequest) throws AuthS } // Validate all configured authenticators support API based authentication. - Set authenticators = getConfiguredAuthenticators(serviceProvider); + Set authenticators = getConfiguredAuthenticators(serviceProvider, tenantDomain); for (ApplicationAuthenticator authenticator : authenticators) { if (!authenticator.isAPIBasedAuthenticationSupported()) { throw new AuthServiceClientException( @@ -425,7 +426,8 @@ private void validateRequest(AuthServiceRequest authServiceRequest) throws AuthS } - private Set getConfiguredAuthenticators(ServiceProvider serviceProvider) { + private Set getConfiguredAuthenticators(ServiceProvider serviceProvider, + String tenantDomain) { LocalAndOutboundAuthenticationConfig authenticationConfig = serviceProvider .getLocalAndOutBoundAuthenticationConfig(); @@ -435,40 +437,42 @@ private Set getConfiguredAuthenticators(ServiceProvide Set authenticators = new HashSet<>(); for (AuthenticationStep authenticationStep : authenticationConfig.getAuthenticationSteps()) { - processLocalAuthenticators(authenticationStep, authenticators); - processFederatedAuthenticators(authenticationStep, authenticators); + processLocalAuthenticators(authenticationStep, authenticators, tenantDomain); + processFederatedAuthenticators(authenticationStep, authenticators, tenantDomain); } return authenticators; } private void processLocalAuthenticators(AuthenticationStep authenticationStep, - Set authenticators) { + Set authenticators, String tenantDomain) { if (authenticationStep.getLocalAuthenticatorConfigs() != null) { for (LocalAuthenticatorConfig localAuthenticatorConfig : authenticationStep.getLocalAuthenticatorConfigs()) { - addAuthenticator(authenticators, localAuthenticatorConfig.getName()); + addAuthenticator(authenticators, localAuthenticatorConfig.getName(), tenantDomain); } } } private void processFederatedAuthenticators(AuthenticationStep authenticationStep, - Set authenticators) { + Set authenticators, String tenantDomain) { if (authenticationStep.getFederatedIdentityProviders() != null) { for (IdentityProvider federatedIdP : authenticationStep.getFederatedIdentityProviders()) { FederatedAuthenticatorConfig fedAuthenticatorConfig = federatedIdP.getDefaultAuthenticatorConfig(); if (fedAuthenticatorConfig != null) { - addAuthenticator(authenticators, fedAuthenticatorConfig.getName()); + addAuthenticator(authenticators, fedAuthenticatorConfig.getName(), tenantDomain); } } } } - private void addAuthenticator(Set authenticators, String authenticatorName) { + private void addAuthenticator(Set authenticators, String authenticatorName, + String tenantDomain) { - ApplicationAuthenticator authenticator = FrameworkUtils.getAppAuthenticatorByName(authenticatorName); + ApplicationAuthenticator authenticator = ApplicationAuthenticatorManager.getInstance() + .getAppAuthenticatorByName(authenticatorName, tenantDomain); if (authenticator != null) { authenticators.add(authenticator); } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationContextLoader.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationContextLoader.java index fd4426586df6..36f527f8f8d5 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationContextLoader.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationContextLoader.java @@ -32,7 +32,7 @@ import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationException; import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationServerException; import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; -import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException; @@ -175,8 +175,8 @@ private void loadAuthenticatorConfig(AuthenticationContext context) StepConfig stepConfig = entry.getValue(); for (AuthenticatorConfig authenticatorConfig : stepConfig.getAuthenticatorList()) { if (authenticatorConfig.getApplicationAuthenticator() == null) { - authenticatorConfig.setApplicationAuthenticator(FrameworkUtils. - getAppAuthenticatorByName(authenticatorConfig.getName())); + authenticatorConfig.setApplicationAuthenticator(ApplicationAuthenticatorManager.getInstance() + .getAppAuthenticatorByName(authenticatorConfig.getName(), context.getTenantDomain())); } if (authenticatorConfig.getIdps() == null && authenticatorConfig.getIdpNames() == null) { authenticatorConfig.setIdPs(Collections.emptyMap()); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/builder/FileBasedConfigurationBuilder.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/builder/FileBasedConfigurationBuilder.java index f4defebefce1..4063c4dc2e35 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/builder/FileBasedConfigurationBuilder.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/builder/FileBasedConfigurationBuilder.java @@ -28,8 +28,8 @@ import org.wso2.carbon.identity.application.authentication.framework.config.model.ExternalIdPConfig; import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig; import org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants; -import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants; import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil; @@ -1047,7 +1047,8 @@ private AuthenticatorConfig processAuthenticatorConfigElement(OMElement authenti } AuthenticatorConfig authenticatorConfig = new AuthenticatorConfig(authenticatorName, enabled, parameterMap); - authenticatorConfig.setApplicationAuthenticator(FrameworkUtils.getAppAuthenticatorByName(authenticatorName)); + authenticatorConfig.setApplicationAuthenticator(ApplicationAuthenticatorManager.getInstance() + .getSystemDefinedAuthenticatorByName(authenticatorName)); return authenticatorConfig; } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/loader/UIBasedConfigurationLoader.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/loader/UIBasedConfigurationLoader.java index e4556a87159c..3c6bee6b4853 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/loader/UIBasedConfigurationLoader.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/loader/UIBasedConfigurationLoader.java @@ -18,7 +18,6 @@ package org.wso2.carbon.identity.application.authentication.framework.config.loader; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator; @@ -31,12 +30,10 @@ import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGenericGraphBuilderFactory; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException; -import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent; import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants; import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; -import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; -import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException; import org.wso2.carbon.identity.application.common.model.AuthenticationStep; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.IdentityProvider; @@ -44,8 +41,6 @@ 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.ServiceProvider; -import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig; -import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig; import org.wso2.carbon.identity.application.mgt.ApplicationConstants; import org.wso2.carbon.idp.mgt.IdentityProviderManagementException; @@ -227,8 +222,10 @@ protected void loadRequestPathAuthenticators(SequenceConfig sequenceConfig, Serv authConfig.setName(authenticatorName); authConfig.setEnabled(true); - // iterate through each system authentication config - for (ApplicationAuthenticator appAuthenticator : FrameworkServiceComponent.getAuthenticators()) { + /* iterate through each only system authentication config as this is to load request path + authenticators. */ + for (ApplicationAuthenticator appAuthenticator : + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) { if (authenticatorName.equalsIgnoreCase(appAuthenticator.getName())) { authConfig.setApplicationAuthenticator(appAuthenticator); @@ -307,16 +304,10 @@ private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp, authenticatorConfig = new AuthenticatorConfig(); authenticatorConfig.setName(authenticatorName); - ApplicationAuthenticator appAuthenticatorForConfig = null; - for (ApplicationAuthenticator appAuthenticator : FrameworkServiceComponent.getAuthenticators()) { - - if (authenticatorName.equalsIgnoreCase(appAuthenticator.getName())) { - appAuthenticatorForConfig = appAuthenticator; - break; - } - } + ApplicationAuthenticator appAuthenticatorForConfig = ApplicationAuthenticatorManager.getInstance() + .getAppAuthenticatorByName(authenticatorName, tenantDomain); if (appAuthenticatorForConfig == null) { - appAuthenticatorForConfig = resolveUserDefinedAuthenticator(authenticatorName, idp, tenantDomain); + throw new FrameworkException("No authenticator found by the name: " + authenticatorName); } authenticatorConfig.setApplicationAuthenticator(appAuthenticatorForConfig); stepConfig.getAuthenticatorList().add(authenticatorConfig); @@ -332,35 +323,4 @@ private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp, stepConfig.setMultiOption(true); } } - - private ApplicationAuthenticator resolveUserDefinedAuthenticator( - String authenticatorName, IdentityProvider idp, String tenantDomain) throws FrameworkException { - - try { - if (StringUtils.equals(idp.getIdentityProviderName(), FrameworkConstants.LOCAL_IDP_NAME)) { - - UserDefinedLocalAuthenticatorConfig config = ApplicationAuthenticatorService.getInstance() - .getUserDefinedLocalAuthenticator(authenticatorName, tenantDomain); - if (config != null) { - return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService() - .getLocalAuthenticatorAdapter(config); - } - } else { - UserDefinedFederatedAuthenticatorConfig config = (UserDefinedFederatedAuthenticatorConfig) - IdentityProviderManager.getInstance().getIdPByName(idp.getIdentityProviderName(), tenantDomain) - .getDefaultAuthenticatorConfig(); - - if (config != null) { - return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService() - .getFederatedAuthenticatorAdapter(config); - } - } - - throw new FrameworkException(String.format( - "No user defined authenticator config found by the given name: %s.", authenticatorName)); - } catch (AuthenticatorMgtException | IdentityProviderManagementException e) { - throw new FrameworkException(String.format( - "An error occurred when retrieving user defined authenticator: %s", authenticatorName), e); - } - } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/OptimizedAuthenticatorConfig.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/OptimizedAuthenticatorConfig.java index 95adf0590bfb..31e42716420f 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/OptimizedAuthenticatorConfig.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/OptimizedAuthenticatorConfig.java @@ -26,7 +26,7 @@ import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationException; import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationServerException; import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; -import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException; import org.wso2.carbon.idp.mgt.IdentityProviderManagementException; @@ -80,7 +80,8 @@ public AuthenticatorConfig getAuthenticatorConfig() throws AuthenticatorConfig authenticatorConfig = new AuthenticatorConfig(); authenticatorConfig.setName(this.name); authenticatorConfig.setEnabled(this.enabled); - authenticatorConfig.setApplicationAuthenticator(FrameworkUtils.getAppAuthenticatorByName(this.name)); + authenticatorConfig.setApplicationAuthenticator(ApplicationAuthenticatorManager.getInstance() + .getAppAuthenticatorByName(this.name, tenantDomain)); authenticatorConfig.setAuthenticatorStateInfo(this.authenticatorStateInfo); authenticatorConfig.setParameterMap(this.parameterMap); Map idps = new HashMap<>(); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManager.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManager.java index db58bee8b0e4..6237d48860cd 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManager.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManager.java @@ -22,8 +22,10 @@ import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator; import org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator; import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; +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.LocalAuthenticatorConfig; +import org.wso2.carbon.idp.mgt.IdentityProviderManager; import java.util.ArrayList; import java.util.List; @@ -110,4 +112,44 @@ public LocalApplicationAuthenticator getLocalAuthenticatorAdapter(LocalAuthentic return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService() .getLocalAuthenticatorAdapter(config); } + + /** + * Get the ApplicationAuthenticator for the given authenticator name. + * + * @param authenticatorName Authenticator name. + * @param tenantDomain Tenant domain. + * @return ApplicationAuthenticator instance. + */ + public ApplicationAuthenticator getAppAuthenticatorByName(String authenticatorName, String tenantDomain) { + + // Check whether the authenticator is in the system defined authenticator. + for (ApplicationAuthenticator authenticator : systemDefinedAuthenticators) { + if (authenticator.getName().equals(authenticatorName)) { + return authenticator; + } + } + + // Check whether the authenticator config is the user defined local authenticator config, if so resolve it. + try { + LocalAuthenticatorConfig localConfig = ApplicationAuthenticatorService.getInstance() + .getUserDefinedLocalAuthenticator(tenantDomain, authenticatorName); + if (localConfig != null) { + return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService() + .getLocalAuthenticatorAdapter(localConfig); + } + + // Check whether the authenticator config is the user defined fed authenticator config, if so resolve it. + FederatedAuthenticatorConfig[] fedConfig = IdentityProviderManager.getInstance() + .getAllFederatedAuthenticators(tenantDomain); + for (FederatedAuthenticatorConfig fedAuth : fedConfig) { + if (fedAuth.getName().equals(authenticatorName)) { + return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService() + .getFederatedAuthenticatorAdapter(fedAuth); + } + } + return null; + } catch (Exception e) { + throw new RuntimeException("Error while getting the authenticator for the name: " + authenticatorName, e); + } + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/AbstractFrameworkTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/AbstractFrameworkTest.java index 033deaf922bd..b6240239afb8 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/AbstractFrameworkTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/AbstractFrameworkTest.java @@ -21,10 +21,13 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; +import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager; import org.wso2.carbon.identity.application.common.model.IdentityProvider; import org.wso2.carbon.identity.application.common.model.ServiceProvider; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import javax.xml.stream.XMLStreamException; @@ -60,4 +63,16 @@ protected IdentityProvider getTestIdentityProvider(String idpFileName) throws XM return IdentityProvider.build(documentElement); } + + /** + * Remove all system defined authenticators. + */ + protected void removeAllSystemDefinedAuthenticators() { + + List authenticatorList = new ArrayList<>( + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()); + for (ApplicationAuthenticator authenticator : authenticatorList) { + ApplicationAuthenticatorManager.getInstance().removeSystemDefinedAuthenticator(authenticator); + } + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/GraphBasedSequenceHandlerAbstractTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/GraphBasedSequenceHandlerAbstractTest.java index c85ed7d6561a..e75087ad4a38 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/GraphBasedSequenceHandlerAbstractTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/GraphBasedSequenceHandlerAbstractTest.java @@ -152,7 +152,7 @@ protected void setUp() throws UserStoreException, NoSuchFieldException, IllegalA protected void resetAuthenticators() { ApplicationAuthenticatorManager authenticatorManager = ApplicationAuthenticatorManager.getInstance(); - //Delete + removeAllSystemDefinedAuthenticators(); authenticatorManager.addSystemDefinedAuthenticator( new MockAuthenticator("BasicMockAuthenticator", new MockSubjectCallback())); authenticatorManager.addSystemDefinedAuthenticator(new MockAuthenticator("HwkMockAuthenticator")); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java index 4863e7acd861..1d61480412a5 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java @@ -76,6 +76,7 @@ import org.wso2.carbon.user.core.UserCoreConstants; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -165,7 +166,7 @@ public class FrameworkUtilsTest extends IdentityBaseTest { @BeforeClass public void setFrameworkServiceComponent() { - ApplicationAuthenticatorManager.getInstance().removeSystemDefinedAuthenticator(); + removeAllSystemDefinedAuthenticators(); ApplicationAuthenticatorManager.getInstance().addSystemDefinedAuthenticator( new MockAuthenticator("BasicAuthenticator")); ApplicationAuthenticatorManager.getInstance().addSystemDefinedAuthenticator( @@ -994,4 +995,13 @@ public void testGetUsernameFieldAutofillWithSubjectAttrConfig() { assertFalse(FrameworkUtils.isUsernameFieldAutofillWithSubjectAttr()); } } + + private void removeAllSystemDefinedAuthenticators() { + + List authenticatorList = new ArrayList<>( + ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()); + for (ApplicationAuthenticator authenticator : authenticatorList) { + ApplicationAuthenticatorManager.getInstance().removeSystemDefinedAuthenticator(authenticator); + } + } }