Skip to content

Commit

Permalink
Send domain qualified username
Browse files Browse the repository at this point in the history
  • Loading branch information
PasinduYeshan committed Jan 9, 2025
1 parent d8ad51a commit 3ab3936
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.wso2.carbon.identity.password.expiry.util.PasswordPolicyUtils;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.model.UserClaimSearchEntry;

import java.util.Arrays;
Expand Down Expand Up @@ -73,8 +74,11 @@ public boolean doPostGetUserClaimValues(String username, String[] claims, String

try {
String userTenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String domainQualifiedUserName =
((AbstractUserStoreManager) userStoreManager).getUser(
null, username).getDomainQualifiedUsername();
Optional<Long> passwordExpiryTime =
PasswordPolicyUtils.getUserPasswordExpiryTime(userTenantDomain, username);
PasswordPolicyUtils.getUserPasswordExpiryTime(userTenantDomain, domainQualifiedUserName);
passwordExpiryTime.ifPresent(expiryTime -> claimMap.put(PasswordPolicyConstants.PASSWORD_EXPIRY_TIME_CLAIM,
String.valueOf(expiryTime)));
} catch (ExpiredPasswordIdentificationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import org.testng.annotations.Test;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;
import org.wso2.carbon.user.core.common.User;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager;
import org.wso2.carbon.user.core.model.UserClaimSearchEntry;

import java.util.Collections;
Expand All @@ -58,13 +59,15 @@ public class PasswordExpiryEventListenerTest {
@Mock
PrivilegedCarbonContext privilegedCarbonContext;
@Mock
UserStoreManager userStoreManager;
AbstractUserStoreManager userStoreManager;
@Mock
User user;

private MockedStatic<PrivilegedCarbonContext> mockedPrivilegedCarbonContext;
private MockedStatic<PasswordPolicyUtils> mockedPasswordPolicyUtils;

@BeforeMethod
public void setUp() {
public void setUp() throws UserStoreException {

MockitoAnnotations.openMocks(this);
passwordExpiryEventListener = new PasswordExpiryEventListener();
Expand All @@ -73,6 +76,9 @@ public void setUp() {
.thenReturn(privilegedCarbonContext);

when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
when(privilegedCarbonContext.getUsername()).thenReturn("USERNAME");

when(userStoreManager.getUser(any(), anyString())).thenReturn(user);
}

@BeforeClass
Expand Down Expand Up @@ -103,6 +109,8 @@ public void testDoPostGetUserClaimValuesWithPasswordExpiryClaim() throws UserSto
Map<String, String> claimMap = new HashMap<>();
String profileName = "default";

when(user.getDomainQualifiedUsername()).thenReturn(username);

// Case 1: When claims contains PASSWORD_EXPIRY_TIME_CLAIM.
claims = new String[]{PasswordPolicyConstants.PASSWORD_EXPIRY_TIME_CLAIM};

Expand Down Expand Up @@ -132,6 +140,23 @@ public void testDoPostGetUserClaimValuesWithoutPasswordExpiryClaim() throws User
String profileName = "default";
claims = new String[]{"claim1", "claim2"};

when(user.getDomainQualifiedUsername()).thenReturn(username);

passwordExpiryEventListener.doPostGetUserClaimValues(username, claims, profileName, claimMap, userStoreManager);
Assert.assertFalse(claimMap.containsKey(PasswordPolicyConstants.PASSWORD_EXPIRY_TIME_CLAIM));
}

@Test
public void testDoPostGetUserClaimValuesInAuthenticationFlow() throws UserStoreException {

String username = "testUser";
String[] claims;
Map<String, String> claimMap = new HashMap<>();
String profileName = "default";
claims = new String[]{"claim1", "claim2"};

when(privilegedCarbonContext.getUsername()).thenReturn(null);

passwordExpiryEventListener.doPostGetUserClaimValues(username, claims, profileName, claimMap, userStoreManager);
Assert.assertFalse(claimMap.containsKey(PasswordPolicyConstants.PASSWORD_EXPIRY_TIME_CLAIM));
}
Expand Down

0 comments on commit 3ab3936

Please sign in to comment.