Skip to content

Commit

Permalink
Fixed wait time bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmrsulja committed Oct 13, 2023
1 parent f3e4c90 commit 74528fb
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ public class PasswordChangeRequestSpamMitigation {

private static final Map<String, Integer> requestFrequency = new HashMap<>();

private static void initializeHistoryRequestDataIfNotExists(String emailAddress) {
private static final long INTERVAL_INCREASE_MINUTES = 1;

private static boolean initializeHistoryRequestDataIfNotExists(String emailAddress) {
if (requestHistory.containsKey(emailAddress)) {
return;
return false;
}

requestHistory.put(emailAddress, LocalDateTime.now());
requestFrequency.put(emailAddress, 0);
return true;
}

public static PasswordChangeRequestSpamMitigationResponse isPasswordResetRequestable(UserAccount userAccount) {
initializeHistoryRequestDataIfNotExists(userAccount.getEmailAddress());
boolean justInitialised = initializeHistoryRequestDataIfNotExists(userAccount.getEmailAddress());

Integer numberOfSuccessiveRequests = requestFrequency.get(userAccount.getEmailAddress());
LocalDateTime momentOfFirstRequest = requestHistory.get(userAccount.getEmailAddress());
LocalDateTime nextRequestAvailableAt = momentOfFirstRequest.plusMinutes(numberOfSuccessiveRequests * 10);
LocalDateTime nextRequestAvailableAt =
momentOfFirstRequest.plusMinutes(numberOfSuccessiveRequests * INTERVAL_INCREASE_MINUTES);

if (nextRequestAvailableAt.isAfter(LocalDateTime.now())) {
String[] dateTimeTokens = nextRequestAvailableAt.toString().split("T");
Expand All @@ -35,6 +39,10 @@ public static PasswordChangeRequestSpamMitigationResponse isPasswordResetRequest
return new PasswordChangeRequestSpamMitigationResponse(false, dateString, timeString);
}

if (numberOfSuccessiveRequests > 0) {
requestHistory.put(userAccount.getEmailAddress(), LocalDateTime.now());
}

return new PasswordChangeRequestSpamMitigationResponse(true);
}

Expand Down

0 comments on commit 74528fb

Please sign in to comment.