From 62bf5b7ce03b3283695dc440fed027a2ff25d3a1 Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Thu, 16 Jan 2025 13:46:47 +0530 Subject: [PATCH] Improve authenticator mgt. --- .../ApplicationAuthenticatorService.java | 31 +++++++++++++++++++ .../mgt/ApplicationManagementServiceImpl.java | 12 ++++--- .../mgt/dao/IdentityProviderDAO.java | 2 ++ .../mgt/dao/impl/IdentityProviderDAOImpl.java | 5 +-- .../ApplicationAuthenticationService.java | 2 +- .../config/model/graph/JsGraphBuilder.java | 2 +- .../model/graph/JsNashornGraphBuilder.java | 2 +- .../nashorn/JsOpenJdkNashornGraphBuilder.java | 2 +- .../model/graph/JsGraalGraphBuilderTest.java | 12 +++---- .../graph/JsNashornGraphBuilderTest.java | 12 +++---- 10 files changed, 59 insertions(+), 23 deletions(-) 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 92674282267c..1f176ff93611 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 @@ -67,10 +67,41 @@ public static ApplicationAuthenticatorService getInstance() { return instance; } + /** + * This method is used to get the list of SYSTEM defined local authenticator configurations. + * + * @deprecated It is recommended to use {@link #getAllSystemDefinedLocalAuthenticators()}, + * which returning the SYSTEM defined local application authenticator configurations. + */ + @Deprecated public List getLocalAuthenticators() { return this.localAuthenticators; } + /** + * This returns list of all SYSTEM defined local authenticator configurations. + * + * @return Retrieved LocalAuthenticatorConfig. + */ + public List getAllSystemDefinedLocalAuthenticators() { + + return this.localAuthenticators; + } + + /** + * This returns list of all SYSTEM and USER defined local authenticator configurations. + * + * @param tenantDomain Tenant domain. + * @return Retrieved LocalAuthenticatorConfig. + */ + public List getAllLocalAuthenticators(String tenantDomain) + throws AuthenticatorMgtException { + + List configList = new ArrayList<>(getAllUserDefinedLocalAuthenticators(tenantDomain)); + configList.addAll(localAuthenticators); + return configList; + } + /** * This returns user defined local authenticators. * diff --git a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ApplicationManagementServiceImpl.java b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ApplicationManagementServiceImpl.java index 1917f3f1f28f..b962d59d5baa 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ApplicationManagementServiceImpl.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ApplicationManagementServiceImpl.java @@ -30,11 +30,13 @@ import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException; import org.wso2.carbon.identity.application.common.IdentityApplicationRegistrationFailureException; +import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException; import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo; import org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig; import org.wso2.carbon.identity.application.common.model.AuthenticationStep; @@ -997,8 +999,8 @@ public LocalAuthenticatorConfig[] getAllLocalAuthenticators(String tenantDomain) try { startTenantFlow(tenantDomain); - IdentityProviderDAO idpdao = ApplicationMgtSystemConfig.getInstance().getIdentityProviderDAO(); - List localAuthenticators = idpdao.getAllLocalAuthenticators(); + List localAuthenticators = ApplicationAuthenticatorService.getInstance() + .getAllLocalAuthenticators(tenantDomain); if (localAuthenticators != null) { return localAuthenticators.toArray(new LocalAuthenticatorConfig[localAuthenticators.size()]); } @@ -1506,15 +1508,15 @@ private void validateResourceId(String resourceId, String tenantDomain) int filteredCount = 0; try { startTenantFlow(tenantDomain); - IdentityProviderDAO idpdao = ApplicationMgtSystemConfig.getInstance().getIdentityProviderDAO(); - List localAuthenticators = idpdao.getAllLocalAuthenticators(); + List localAuthenticators = ApplicationAuthenticatorService.getInstance() + .getAllLocalAuthenticators(tenantDomain); if (localAuthenticators != null) { filteredCount = (int) localAuthenticators.stream() .filter(authenticatorConfig -> authenticatorConfig.getName() .equals(authenticatorName)).count(); } - } catch (IdentityApplicationManagementException e) { + } catch (IdentityApplicationManagementException | AuthenticatorMgtException e) { throw new IdentityApplicationManagementException( String.format(IdPManagementConstants.ErrorMessage .ERROR_CODE_GET_CONNECTED_APPS_REQUEST_INVALID.getMessage(), resourceId)); diff --git a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/IdentityProviderDAO.java b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/IdentityProviderDAO.java index eb8a2b22786d..8a35f6cd6b30 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/IdentityProviderDAO.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/IdentityProviderDAO.java @@ -51,7 +51,9 @@ public interface IdentityProviderDAO { * Returns all the local authenticators available on the system. * @return * @throws IdentityApplicationManagementException + * @deprecated use {@link ApplicationAuthenticatorService#getInstance().getAllLocalAuthenticators(String)} instead. */ + @Deprecated List getAllLocalAuthenticators() throws IdentityApplicationManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/impl/IdentityProviderDAOImpl.java b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/impl/IdentityProviderDAOImpl.java index c614f7b2d0a9..7af23c6e1d77 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/impl/IdentityProviderDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/dao/impl/IdentityProviderDAOImpl.java @@ -158,10 +158,11 @@ public List getAllIdentityProviders() return federatedIdentityProviders; } - @Override /** - * + * @deprecated use {@link ApplicationAuthenticatorService#getInstance().getAllLocalAuthenticators(String)} instead. */ + @Override + @Deprecated public List getAllLocalAuthenticators() throws IdentityApplicationManagementException { 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..1dfe59cc66b0 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 @@ -57,7 +57,7 @@ public List getAllAuthenticators() throws ApplicationA return FrameworkServiceComponent.getAuthenticators(); } - public List getLocalAuthenticators() throws ApplicationAuthenticationException { + public List getAllSystemDefinedLocalAuthenticators() { List localAuthenticators = new ArrayList(); 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/graph/JsGraphBuilder.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraphBuilder.java index c2d900f62328..5d552b415109 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraphBuilder.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraphBuilder.java @@ -321,7 +321,7 @@ protected void filterOptions(Map> authenticationOpti if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) { List localAuthenticators = ApplicationAuthenticatorService - .getInstance().getLocalAuthenticators(); + .getInstance().getAllSystemDefinedLocalAuthenticators(); for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) { if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) { if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) && 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/graph/JsNashornGraphBuilder.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilder.java index e10ea7715e2d..bd2143e4a417 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilder.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilder.java @@ -476,7 +476,7 @@ protected void filterOptions(Map> authenticationOpti if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) { List localAuthenticators = ApplicationAuthenticatorService - .getInstance().getLocalAuthenticators(); + .getInstance().getAllSystemDefinedLocalAuthenticators(); for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) { if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) { if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) && 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/graph/openjdk/nashorn/JsOpenJdkNashornGraphBuilder.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/openjdk/nashorn/JsOpenJdkNashornGraphBuilder.java index 84248df1456f..f9167eb470ae 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/openjdk/nashorn/JsOpenJdkNashornGraphBuilder.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/openjdk/nashorn/JsOpenJdkNashornGraphBuilder.java @@ -499,7 +499,7 @@ protected void filterOptions(Map> authenticationOpti if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) { List localAuthenticators = ApplicationAuthenticatorService - .getInstance().getLocalAuthenticators(); + .getInstance().getAllSystemDefinedLocalAuthenticators(); for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) { if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) { if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) && diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraalGraphBuilderTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraalGraphBuilderTest.java index 930f15f9b90d..8992c7c4f766 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraalGraphBuilderTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsGraalGraphBuilderTest.java @@ -158,15 +158,15 @@ public void testFilterOptions(Map> options, StepConf @DataProvider public Object[][] filterOptionsDataProvider() { - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().clear(); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().clear(); LocalAuthenticatorConfig basic = new LocalAuthenticatorConfig(); basic.setName("BasicAuthenticator"); basic.setDisplayName("basic"); LocalAuthenticatorConfig totp = new LocalAuthenticatorConfig(); totp.setName("TOTPAuthenticator"); totp.setDisplayName("totp"); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(basic); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(totp); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(basic); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(totp); IdentityProvider localIdp = new IdentityProvider(); localIdp.setId("LOCAL"); @@ -290,7 +290,7 @@ public void testParamsOptions(Map options, StepConfig stepConfig @DataProvider public Object[][] filterParamsDataProvider() { - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().clear(); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().clear(); LocalAuthenticatorConfig basic = new LocalAuthenticatorConfig(); basic.setName("BasicAuthenticator"); basic.setDisplayName("basic"); @@ -299,8 +299,8 @@ public Object[][] filterParamsDataProvider() { totp.setName("TOTPAuthenticator"); totp.setDisplayName("totp"); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(basic); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(totp); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(basic); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(totp); FederatedAuthenticatorConfig twitterFederated = new FederatedAuthenticatorConfig(); twitterFederated.setDisplayName("twitter"); diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilderTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilderTest.java index 42bc89340e8e..a9ef67a61061 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilderTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/config/model/graph/JsNashornGraphBuilderTest.java @@ -161,15 +161,15 @@ public void testFilterOptions(Map> options, StepConf @DataProvider public Object[][] filterOptionsDataProvider() { - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().clear(); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().clear(); LocalAuthenticatorConfig basic = new LocalAuthenticatorConfig(); basic.setName("BasicAuthenticator"); basic.setDisplayName("basic"); LocalAuthenticatorConfig totp = new LocalAuthenticatorConfig(); totp.setName("TOTPAuthenticator"); totp.setDisplayName("totp"); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(basic); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(totp); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(basic); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(totp); IdentityProvider localIdp = new IdentityProvider(); localIdp.setId("LOCAL"); @@ -296,7 +296,7 @@ public void testParamsOptions(Map options, StepConfig stepConfig @DataProvider public Object[][] filterParamsDataProvider() { - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().clear(); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().clear(); LocalAuthenticatorConfig basic = new LocalAuthenticatorConfig(); basic.setName("BasicAuthenticator"); basic.setDisplayName("basic"); @@ -305,8 +305,8 @@ public Object[][] filterParamsDataProvider() { totp.setName("TOTPAuthenticator"); totp.setDisplayName("totp"); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(basic); - ApplicationAuthenticatorService.getInstance().getLocalAuthenticators().add(totp); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(basic); + ApplicationAuthenticatorService.getInstance().getAllSystemDefinedLocalAuthenticators().add(totp); FederatedAuthenticatorConfig twitterFederated = new FederatedAuthenticatorConfig(); twitterFederated.setDisplayName("twitter");