Skip to content

Commit

Permalink
Return current time when lastPasswordUpdateTime is null.
Browse files Browse the repository at this point in the history
  • Loading branch information
PasinduYeshan committed Dec 2, 2024
1 parent 76bdf04 commit feab5d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,11 @@ public static Optional<Long> getUserPasswordExpiryTime(String tenantDomain, Stri
String lastPasswordUpdatedTime =
getLastPasswordUpdatedTime(tenantAwareUsername, userStoreManager, userRealm);

// If last password update time is not available, it will be considered as expired.
if (StringUtils.isBlank(lastPasswordUpdatedTime)) {
return Optional.of(System.currentTimeMillis());
long lastPasswordUpdatedTimeInMillis = 0L;
boolean isLastPasswordUpdatedTimeBlank = StringUtils.isBlank(lastPasswordUpdatedTime);
if (!isLastPasswordUpdatedTimeBlank) {
lastPasswordUpdatedTimeInMillis = getLastPasswordUpdatedTimeInMillis(lastPasswordUpdatedTime);
}

long lastPasswordUpdatedTimeInMillis = getLastPasswordUpdatedTimeInMillis(lastPasswordUpdatedTime);
int defaultPasswordExpiryInDays = getPasswordExpiryInDays(tenantDomain);
boolean skipIfNoApplicableRules = isSkipIfNoApplicableRulesEnabled(tenantDomain);

Expand All @@ -334,6 +333,10 @@ public static Optional<Long> getUserPasswordExpiryTime(String tenantDomain, Stri
// If no rules are defined, use the default expiry time if "skipIfNoApplicableRules" is disabled.
if (CollectionUtils.isEmpty(passwordExpiryRules)) {
if (skipIfNoApplicableRules) return Optional.empty();
// If lastPasswordUpdatedTime is blank, set expiry time to now.
if (isLastPasswordUpdatedTimeBlank) {
return Optional.of(System.currentTimeMillis());
}
return Optional.of(
lastPasswordUpdatedTimeInMillis + getDaysTimeInMillis(defaultPasswordExpiryInDays));
}
Expand All @@ -356,13 +359,19 @@ public static Optional<Long> getUserPasswordExpiryTime(String tenantDomain, Stri
if (PasswordExpiryRuleOperatorEnum.NE.equals(rule.getOperator())) {
return Optional.empty();
}
if (isLastPasswordUpdatedTimeBlank) {
return Optional.of(System.currentTimeMillis());
}
int expiryDays =
rule.getExpiryDays() > 0 ? rule.getExpiryDays() : getPasswordExpiryInDays(tenantDomain);
return Optional.of(lastPasswordUpdatedTimeInMillis + getDaysTimeInMillis(expiryDays));
}
}

if (skipIfNoApplicableRules) return Optional.empty();
if (isLastPasswordUpdatedTimeBlank) {
return Optional.of(System.currentTimeMillis());
}
return Optional.of(
lastPasswordUpdatedTimeInMillis + getDaysTimeInMillis(defaultPasswordExpiryInDays));
} catch (UserStoreException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,10 @@ public void testGetUserPasswordExpiryTime(Integer daysAgo, String[] roles, Strin
if (expiryDays == null) {
Assert.assertFalse(expiryTime.isPresent(), description);
} else if (expiryDays == 0) {
Assert.assertNotNull(expiryTime);
Assert.assertTrue(expiryTime.isPresent());
Assert.assertTrue(expiryTime.get() >= testStartTime && expiryTime.get() <= testEndTime);
} else {
Assert.assertNotNull(expiryTime);
Assert.assertTrue(expiryTime.isPresent());
Assert.assertNotNull(updateTime);
long expectedExpiryTime = updateTime + getDaysTimeInMillis(expiryDays);
Assert.assertTrue(Math.abs(expiryTime.get() - expectedExpiryTime) <= TIME_TOLERANCE_MS);
Expand Down

0 comments on commit feab5d9

Please sign in to comment.