forked from Ildar1/RQCODE_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordLockoutRequirement.java
More file actions
84 lines (66 loc) · 2.53 KB
/
PasswordLockoutRequirement.java
File metadata and controls
84 lines (66 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.HashMap;
import java.util.Map;
public class PasswordLockoutRequirement extends Requirement {
private static final int MAX_FAILED_ATTEMPTS = 5; // Maximum allowed failed attempts
private static final long LOCKOUT_DURATION_MS = 1 * 10 * 1000; // Lockout duration in milliseconds (10 seconds)
private final Map<String, UserLoginStatus> userStatusMap = new HashMap<>();
private final String username;
public PasswordLockoutRequirement(String username) {
this.username = username;
userStatusMap.putIfAbsent(username, new UserLoginStatus());
}
@Override
public Checkable.CheckStatus check() {
UserLoginStatus status = userStatusMap.get(username);
if (status.isLocked()) {
if (System.currentTimeMillis() - status.getLockoutStartTime() > LOCKOUT_DURATION_MS) {
status.resetLockout(); // Unlock after the lockout period
} else {
return Checkable.CheckStatus.FAIL;
}
}
return status.getFailedAttempts() >= MAX_FAILED_ATTEMPTS
? Checkable.CheckStatus.FAIL
: Checkable.CheckStatus.PASS;
}
// public void recordCorrectAttempt() {
// if (this.check() != Checkable.CheckStatus.FAIL)
// userStatusMap.get(username).resetFailedAttempts();
// }
public void recordFailedAttempt() {
userStatusMap.get(username).incrementFailedAttempts();
}
public void resetFailedAttempts() {
userStatusMap.get(username).resetFailedAttempts();
}
@Override
public String toString() {
return "The system must lock a user’s account after " + MAX_FAILED_ATTEMPTS + " consecutive failed login attempts.";
}
private static class UserLoginStatus {
private int failedAttempts = 0;
private long lockoutStartTime = 0;
public int getFailedAttempts() {
return failedAttempts;
}
public void incrementFailedAttempts() {
failedAttempts++;
if (failedAttempts >= MAX_FAILED_ATTEMPTS) {
lockoutStartTime = System.currentTimeMillis();
}
}
public void resetFailedAttempts() {
failedAttempts = 0;
lockoutStartTime = 0;
}
public boolean isLocked() {
return lockoutStartTime > 0;
}
public long getLockoutStartTime() {
return lockoutStartTime;
}
public void resetLockout() {
lockoutStartTime = 0;
}
}
}