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 1fcb154de067..9861ab5a1cf1 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 @@ -18,14 +18,13 @@ package org.wso2.carbon.identity.application.authentication.framework.internal.core; -import org.wso2.carbon.identity.action.execution.model.ActionType; -import org.wso2.carbon.identity.action.execution.util.ActionExecutorConfig; import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator; 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.UserDefinedFederatedAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.core.util.IdentityConfigParser; import org.wso2.carbon.idp.mgt.IdentityProviderManager; import java.util.ArrayList; @@ -39,6 +38,9 @@ public class ApplicationAuthenticatorManager { private static final ApplicationAuthenticatorManager instance = new ApplicationAuthenticatorManager(); private final List systemDefinedAuthenticators = new ArrayList<>(); + private static final String AUTHENTICATION_ACTION_ENABLED_PROP = + "Actions.Types.Authentication.Enable"; + public static ApplicationAuthenticatorManager getInstance() { return instance; @@ -100,7 +102,7 @@ public List getAllAuthenticators(String tenantDomain) List allAuthenticators = new ArrayList<>(systemDefinedAuthenticators); - if (!ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION) || + if (!isAuthenticationActionEnabled() || FrameworkServiceDataHolder.getInstance().getUserDefinedAuthenticatorService() == null) { return allAuthenticators; } @@ -143,7 +145,7 @@ public ApplicationAuthenticator getApplicationAuthenticatorByName(String authent } } - if (!ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(ActionType.AUTHENTICATION) || + if (!isAuthenticationActionEnabled() || FrameworkServiceDataHolder.getInstance().getUserDefinedAuthenticatorService() == null) { return null; } @@ -172,4 +174,10 @@ public ApplicationAuthenticator getApplicationAuthenticatorByName(String authent throw new RuntimeException("Error while getting the authenticator for the name: " + authenticatorName, e); } } + + private boolean isAuthenticationActionEnabled() { + + return Boolean.parseBoolean((String) IdentityConfigParser.getInstance() + .getConfiguration().get(AUTHENTICATION_ACTION_ENABLED_PROP)); + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/MockAuthenticator.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/MockAuthenticator.java index 6cc0f0dd26d9..a7886b0c5a73 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/MockAuthenticator.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/MockAuthenticator.java @@ -104,4 +104,19 @@ public String getI18nKey() { return this.name + ".authenticator"; } + + public static class MockLocalAuthenticator extends MockAuthenticator implements LocalApplicationAuthenticator { + + public MockLocalAuthenticator(String name) { + super(name); + } + } + + public static class MockFederatedAuthenticator extends MockAuthenticator + implements FederatedApplicationAuthenticator { + + public MockFederatedAuthenticator(String name) { + super(name); + } + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/request/impl/JITProvisioningPostAuthenticationHandlerTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/request/impl/JITProvisioningPostAuthenticationHandlerTest.java index 9e183c65ce48..32d6d45199f0 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/request/impl/JITProvisioningPostAuthenticationHandlerTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/request/impl/JITProvisioningPostAuthenticationHandlerTest.java @@ -56,6 +56,7 @@ import org.wso2.carbon.identity.application.common.model.JustInTimeProvisioningConfig; import org.wso2.carbon.identity.application.common.model.ServiceProvider; import org.wso2.carbon.identity.core.internal.IdentityCoreServiceComponent; +import org.wso2.carbon.identity.core.util.IdentityConfigParser; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManager; import org.wso2.carbon.identity.user.profile.mgt.association.federation.FederatedAssociationManagerImpl; @@ -69,6 +70,7 @@ import java.io.IOException; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -105,6 +107,9 @@ public class JITProvisioningPostAuthenticationHandlerTest extends AbstractFramew private MockedStatic carbonUtils; private MockedStatic privilegedCarbonContextMockedStatic; + private IdentityConfigParser mockIdentityConfigParser; + private MockedStatic identityConfigParser; + @BeforeClass protected void setupSuite() throws XMLStreamException, IdentityProviderManagementException { @@ -130,6 +135,11 @@ protected void setupSuite() throws XMLStreamException, IdentityProviderManagemen sp = getTestServiceProvider("default-sp-1.xml"); carbonUtils = mockStatic(CarbonUtils.class); privilegedCarbonContextMockedStatic = mockStatic(PrivilegedCarbonContext.class); + + mockIdentityConfigParser = mock(IdentityConfigParser.class); + identityConfigParser = mockStatic(IdentityConfigParser.class); + identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser); + setAuthenticatorActionEnableStatus(false); } @AfterClass @@ -138,6 +148,7 @@ protected void cleanup() { configurationFacade.close(); carbonUtils.close(); privilegedCarbonContextMockedStatic.close(); + identityConfigParser.close(); } @Test(description = "This test case tests the Post JIT provisioning handling flow without an authenticated user") @@ -325,4 +336,11 @@ private void initAuthenticators() { authenticatorManager.addSystemDefinedAuthenticator(new MockAuthenticator("HwkMockAuthenticator")); authenticatorManager.addSystemDefinedAuthenticator(new MockAuthenticator("FptMockAuthenticator")); } + + private void setAuthenticatorActionEnableStatus(boolean isEnabled) { + + Map configMap = new HashMap<>(); + configMap.put("Actions.Types.Authentication.Enable", Boolean.toString(isEnabled)); + when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap); + } } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManagerTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManagerTest.java new file mode 100644 index 000000000000..16c9cf999b36 --- /dev/null +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/internal/core/ApplicationAuthenticatorManagerTest.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.identity.application.authentication.framework.internal.core; + +import org.mockito.MockedStatic; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.application.authentication.framework.AbstractFrameworkTest; +import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator; +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.MockAuthenticator; +import org.wso2.carbon.identity.application.authentication.framework.UserDefinedAuthenticatorService; +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.UserDefinedFederatedAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; +import org.wso2.carbon.identity.core.util.IdentityConfigParser; +import org.wso2.carbon.idp.mgt.IdentityProviderManager; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +@Listeners(MockitoTestNGListener.class) +public class ApplicationAuthenticatorManagerTest extends AbstractFrameworkTest { + + private final ApplicationAuthenticatorManager applicationAuthenticatorService = + ApplicationAuthenticatorManager.getInstance(); + + private static final String SYSTEM_DEFINED_AUTHENTICATOR_NAME = "BasicAuthenticator"; + private static final String USER_DEFINED_LOCAL_AUTHENTICATOR_NAME = "UserDefinedLocalMockAuthenticator"; + private static final String USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME = "UserDefinedFederatedMockAuthenticator"; + private static final String TENANT_DOMAIN = "carbon.super"; + + private static final ApplicationAuthenticator systemDefinedAuthenticator = + new MockAuthenticator(SYSTEM_DEFINED_AUTHENTICATOR_NAME); + private static final LocalApplicationAuthenticator userDefinedLocalAuthenticator = + new MockAuthenticator.MockLocalAuthenticator(USER_DEFINED_LOCAL_AUTHENTICATOR_NAME); + private static final FederatedApplicationAuthenticator userDefinedFederatedAuthenticator = + new MockAuthenticator.MockFederatedAuthenticator(USER_DEFINED_FEDERATED_AUTHENTICATOR_NAME); + + private static UserDefinedAuthenticatorService userDefinedAuthenticatorService = + mock(UserDefinedAuthenticatorService.class); + + private IdentityConfigParser mockIdentityConfigParser; + private MockedStatic identityConfigParser; + + private final MockedStatic mockedAuthenticationService = + mockStatic(ApplicationAuthenticatorService.class); + private final ApplicationAuthenticatorService authenticatorService = mock(ApplicationAuthenticatorService.class); + + private final MockedStatic mockedIdentityProviderManager = + mockStatic(IdentityProviderManager.class); + private final IdentityProviderManager identityProviderManager = mock(IdentityProviderManager.class); + + @BeforeClass + public void setUp() throws Exception { + + mockIdentityConfigParser = mock(IdentityConfigParser.class); + identityConfigParser = mockStatic(IdentityConfigParser.class); + + removeAllSystemDefinedAuthenticators(); + applicationAuthenticatorService.addSystemDefinedAuthenticator(systemDefinedAuthenticator); + when(userDefinedAuthenticatorService.getUserDefinedLocalAuthenticator(any())).thenReturn( + userDefinedLocalAuthenticator); + when(userDefinedAuthenticatorService.getUserDefinedFederatedAuthenticator(any())).thenReturn( + userDefinedFederatedAuthenticator); + FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(userDefinedAuthenticatorService); + + identityConfigParser.when(IdentityConfigParser::getInstance).thenReturn(mockIdentityConfigParser); + + mockedAuthenticationService.when(ApplicationAuthenticatorService::getInstance).thenReturn(authenticatorService); + when(authenticatorService.getAllUserDefinedLocalAuthenticators(TENANT_DOMAIN)).thenReturn( + List.of(new UserDefinedLocalAuthenticatorConfig( + AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION))); + + mockedIdentityProviderManager.when(IdentityProviderManager::getInstance).thenReturn(identityProviderManager); + when(identityProviderManager.getAllFederatedAuthenticators(TENANT_DOMAIN)).thenReturn(new + FederatedAuthenticatorConfig[]{new UserDefinedFederatedAuthenticatorConfig()}); + } + + @AfterClass + public void tearDown() { + + identityConfigParser.close(); + mockedAuthenticationService.close(); + mockedIdentityProviderManager.close(); + } + + @Test + public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNotNullUserDefinedAuthenticatorService() { + + setAuthenticatorActionEnableStatus(true); + List result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN); + assertEquals(3, result.size()); + } + + @Test + public void testGetAllAuthenticatorsWithAuthActionTypeEnabledAndNullUserDefinedAuthenticatorService() { + + FrameworkServiceDataHolder.getInstance().setUserDefinedAuthenticatorService(null); + setAuthenticatorActionEnableStatus(true); + List result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN); + assertEquals(1, result.size()); + } + + @Test + public void testGetAllAuthenticatorsWithAuthenticationActionTypeDisabled() { + + setAuthenticatorActionEnableStatus(false); + List result = applicationAuthenticatorService.getAllAuthenticators(TENANT_DOMAIN); + assertEquals(1, result.size()); + } + + private void setAuthenticatorActionEnableStatus(boolean isEnabled) { + + Map configMap = new HashMap<>(); + configMap.put("Actions.Types.Authentication.Enable", Boolean.toString(isEnabled)); + when(mockIdentityConfigParser.getConfiguration()).thenReturn(configMap); + } +} diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/resources/testng.xml b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/resources/testng.xml index 1df935ca7581..296310505260 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/resources/testng.xml +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/resources/testng.xml @@ -43,6 +43,7 @@ +