8000 Handle async processing of the brute force detection when counting login failures by ahus1 · Pull Request #52429 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,27 @@ public void test() {

assertBruteForce(detection.bruteForceUserStatus(testUser.getId()), 0, 0, false, false);

// Wait for each failure to be processed before sending the next request for the same user.
// DefaultBlockingBruteForceProtector blocks concurrent logins for the same user,
// and the blocked request won't increment the failure counter.
// These waits can be removed once brute force processing is synchronous and login-failures V1 is removed
// https://github.com/keycloak/keycloak/pull/52128
oauthClient.doPasswordGrantRequest(testUser.getUsername(), "invalid");
awaitNumFailures(detection, testUser.getId(), 1);

oauthClient.doPasswordGrantRequest(testUser.getUsername(), "invalid");
awaitNumFailures(detection, testUser.getId(), 2);

// Third attempt: user is now locked (failureFactor=2), won't increment numFailures
oauthClient.doPasswordGrantRequest(testUser.getUsername(), "invalid");

oauthClient.doPasswordGrantRequest(testUser2.getUsername(), "invalid");
awaitNumFailures(detection, testUser2.getId(), 1);

oauthClient.doPasswordGrantRequest(testUser2.getUsername(), "invalid");
oauthClient.doPasswordGrantRequest("nosuchuser", "invalid");

// Check testUser2 to ensure all failure processing is completed
await().atMost(5, TimeUnit.SECONDS)
.pollInterval(100, TimeUnit.MILLISECONDS)
.untilAsserted(() -> {
Map<String, Object> status = detection.bruteForceUserStatus(testUser2.getId());
assertEquals(2, status.get("numFailures"),
"Waiting for testUser2 processing to complete");
});
awaitNumFailures(detection, testUser2.getId(), 2);

assertBruteForce(detection.bruteForceUserStatus(testUser.getId()), 2, 1, true, true);
assertBruteForce(detection.bruteForceUserStatus(testUser2.getId()), 2, 1, true, true);
Expand Down Expand Up @@ -123,6 +128,12 @@ private void assertBruteForce(Map<String, Object> status, Integer expectedNumFai
}
}

private void awaitNumFailures(AttackDetectionResource detection, String userId, int expected) {
await().atMost(5, TimeUnit.SECONDS)
.pollInterval(100, TimeUnit.MILLISECONDS)
.untilAsserted(() -> assertEquals(expected, detection.bruteForceUserStatus(userId).get("numFailures")));
}

private static class AttackDetectionResourceRealmConfig implements RealmConfig {

@Override
Expand Down
Loading
0