implemented locking-user-on-login-fail, needs to be tested

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-10-18 00:11:40 +02:00
parent f5976a7dc3
commit a4200f43aa
8 changed files with 127 additions and 27 deletions

View File

@@ -140,6 +140,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
return this;
}
@Override
public Set<User> find(String key) {
if (!json.has(USERS)) return Set.of();
@@ -175,8 +176,14 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
}
@Override
public Optional<User> load(String user, String password) {
public Optional<User> login(String user, String password) {
if (!json.has(USERS)) return empty();
var optLock = getLock(user);
if (optLock.isPresent()) {
var lock = optLock.get();
LOG.log(WARNING, "{} is locked after {} failed logins. Lock will be released at {}", user, lock.attempts(), lock.releaseTime());
return empty();
}
try {
var users = json.getJSONObject(USERS);
for (String userId : users.keySet()) {
@@ -184,8 +191,13 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
if (KEYS.stream().map(userData::getString).noneMatch(val -> val.equals(user))) continue;
var loadedUser = User.of(userData, userId).filter(u -> passwordMatches(password, u));
if (loadedUser.isPresent()) return loadedUser;
if (loadedUser.isPresent()) {
unlock(user);
return loadedUser;
}
lock(userId);
}
lock(user);
return empty();
} catch (Exception e) {
return empty();
@@ -210,6 +222,7 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
return save();
}
@Override
public FileStore updatePassword(User user, String plaintextPassword) {
return save(user.hashedPassword(passwordHasher.hash(plaintextPassword, uuid())));