-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from dhaura/DP-add-login_hint-param
Add Support for "login_hint" for Param in Organization Discovery
- Loading branch information
Showing
3 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,13 @@ | |
|
||
package org.wso2.carbon.identity.application.authenticator.organization.login; | ||
|
||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import org.wso2.carbon.base.CarbonBaseConstants; | ||
import org.wso2.carbon.context.PrivilegedCarbonContext; | ||
|
@@ -43,7 +45,10 @@ | |
import org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl; | ||
import org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO; | ||
import org.wso2.carbon.identity.organization.config.service.OrganizationConfigManager; | ||
import org.wso2.carbon.identity.organization.config.service.model.ConfigProperty; | ||
import org.wso2.carbon.identity.organization.config.service.model.DiscoveryConfig; | ||
import org.wso2.carbon.identity.organization.discovery.service.AttributeBasedOrganizationDiscoveryHandler; | ||
import org.wso2.carbon.identity.organization.discovery.service.OrganizationDiscoveryManager; | ||
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager; | ||
import org.wso2.carbon.identity.organization.management.service.OrganizationManager; | ||
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException; | ||
|
@@ -69,11 +74,15 @@ | |
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.AUTHENTICATOR_FRIENDLY_NAME; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.AUTHENTICATOR_NAME; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ENABLE_CONFIG; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.INBOUND_AUTH_TYPE_OAUTH; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.LOGIN_HINT_PARAMETER; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.OIDC_CLAIM_DIALECT_URL; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORGANIZATION_DISCOVERY_TYPE; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_ID_PARAMETER; | ||
import static org.wso2.carbon.identity.application.authenticator.organization.login.constant.AuthenticatorConstants.ORG_PARAMETER; | ||
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_APPLICATION_NOT_SHARED; | ||
|
@@ -100,6 +109,11 @@ public class OrganizationAuthenticatorTest { | |
private static final String clientId = "3_TCRZ93rTQtPL8k02_trEYTfVca"; | ||
private static final String secretKey = "uW4q6dYgSaHJIv11Llqi1nvOQBUa"; | ||
|
||
private static final String emailDomainDiscoveryType = "emailDomain"; | ||
private static final String invalidDiscoveryType = "invalidDiscoveryType"; | ||
private static final String userEmailWithValidDomain = "[email protected]"; | ||
private static final String userEmailWithInvalidDomain = "[email protected]"; | ||
|
||
private static Map<String, String> authenticatorParamProperties; | ||
private static Map<String, String> authenticatorProperties; | ||
private static Map<String, Object> mockContextParam; | ||
|
@@ -126,6 +140,9 @@ public class OrganizationAuthenticatorTest { | |
private DiscoveryConfig mockDiscoveryConfig; | ||
private MockedStatic<IdentityTenantUtil> mockedUtilities; | ||
|
||
@Mock | ||
private OrganizationDiscoveryManager mockOrganizationDiscoveryManager; | ||
|
||
@BeforeClass | ||
public void setUp() { | ||
|
||
|
@@ -136,6 +153,7 @@ public void setUp() { | |
@BeforeMethod | ||
public void init() throws UserStoreException { | ||
|
||
initMocks(this); | ||
mockServletRequest = mock(HttpServletRequest.class); | ||
mockServletResponse = mock(HttpServletResponse.class); | ||
mockAuthenticationContext = mock(AuthenticationContext.class); | ||
|
@@ -167,6 +185,7 @@ public void init() throws UserStoreException { | |
authenticatorDataHolder.setApplicationManagementService(mockApplicationManagementService); | ||
authenticatorDataHolder.setClaimMetadataManagementService(mockClaimMetadataManagementService); | ||
authenticatorDataHolder.setOrganizationConfigManager(mockOrganizationConfigManager); | ||
authenticatorDataHolder.setOrganizationDiscoveryManager(mockOrganizationDiscoveryManager); | ||
Tenant tenant = mock(Tenant.class); | ||
TenantManager mockTenantManager = mock(TenantManager.class); | ||
when(mockRealmService.getTenantManager()).thenReturn(mockTenantManager); | ||
|
@@ -444,6 +463,60 @@ public void testInitiateAuthenticationRequestInvalidSharedApp() throws Exception | |
mockAuthenticationContext); | ||
} | ||
|
||
@DataProvider(name = "invalidOrgDiscoveryParams") | ||
public Object[][] getInvalidOrgDiscoveryParams() { | ||
|
||
return new Object[][]{ | ||
// When the given discovery type is not valid. | ||
{userEmailWithValidDomain, invalidDiscoveryType, true}, | ||
// When the given discovery type is valid but not enabled. | ||
{userEmailWithValidDomain, emailDomainDiscoveryType, false}, | ||
// When the given email domain of the user email is invalid. | ||
{userEmailWithInvalidDomain, emailDomainDiscoveryType, true} | ||
}; | ||
} | ||
|
||
@Test(dataProvider = "invalidOrgDiscoveryParams") | ||
public void testProcessWithInvalidOrgDiscoveryParam(String userEmail, String discoveryType, | ||
boolean isEmailDomainDiscoveryEnabled) throws Exception { | ||
|
||
Map<String, String[]> mockParamMap = new HashMap<>(); | ||
mockParamMap.put(LOGIN_HINT_PARAMETER, new String[]{userEmail}); | ||
mockParamMap.put(ORGANIZATION_DISCOVERY_TYPE, new String[]{discoveryType}); | ||
when(mockServletRequest.getParameterMap()).thenReturn(mockParamMap); | ||
when(mockServletRequest.getParameter(LOGIN_HINT_PARAMETER)).thenReturn(userEmail); | ||
when(mockServletRequest.getParameter(ORGANIZATION_DISCOVERY_TYPE)).thenReturn(discoveryType); | ||
|
||
when(authenticatorDataHolder.getOrganizationConfigManager().getDiscoveryConfiguration()) | ||
.thenReturn(mockDiscoveryConfig); | ||
List<ConfigProperty> configProperties = new ArrayList<>(); | ||
configProperties.add(new ConfigProperty(emailDomainDiscoveryType + ENABLE_CONFIG, | ||
String.valueOf(isEmailDomainDiscoveryEnabled))); | ||
when(mockDiscoveryConfig.getConfigProperties()).thenReturn(configProperties); | ||
|
||
Map<String, AttributeBasedOrganizationDiscoveryHandler> discoveryHandlers = new HashMap<>(); | ||
AttributeBasedOrganizationDiscoveryHandler discoveryHandler = | ||
mock(AttributeBasedOrganizationDiscoveryHandler.class); | ||
discoveryHandlers.put(emailDomainDiscoveryType, discoveryHandler); | ||
when(authenticatorDataHolder.getOrganizationDiscoveryManager().getAttributeBasedOrganizationDiscoveryHandlers()) | ||
.thenReturn(discoveryHandlers); | ||
|
||
when(mockAuthenticationContext.getLoginTenantDomain()).thenReturn(saasAppOwnedTenant); | ||
when(authenticatorDataHolder.getOrganizationManager().resolveOrganizationId(saasAppOwnedTenant)).thenReturn( | ||
saasAppOwnedOrgId); | ||
when(authenticatorDataHolder.getOrganizationDiscoveryManager() | ||
.getOrganizationIdByDiscoveryAttribute(discoveryType, userEmail, saasAppOwnedOrgId)).thenReturn(null); | ||
|
||
when(mockAuthenticationContext.getContextIdentifier()).thenReturn(contextIdentifier); | ||
when(mockAuthenticationContext.getExternalIdP()).thenReturn(mockExternalIdPConfig); | ||
when(mockAuthenticationContext.getServiceProviderResourceId()).thenReturn(saasAppResourceId); | ||
when(mockExternalIdPConfig.getName()).thenReturn(AUTHENTICATOR_FRIENDLY_NAME); | ||
|
||
AuthenticatorFlowStatus status = organizationAuthenticator.process(mockServletRequest, mockServletResponse, | ||
mockAuthenticationContext); | ||
Assert.assertEquals(status, AuthenticatorFlowStatus.INCOMPLETE); | ||
} | ||
|
||
private void setMockContextParamForValidOrganization() { | ||
|
||
mockContextParam.put(ORG_PARAMETER, org); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters