Skip to content

Commit

Permalink
feat(auth): log login failures (#699)
Browse files Browse the repository at this point in the history
close #697
  • Loading branch information
gquintana authored May 15, 2021
1 parent 2cbfe17 commit f691db0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/main/java/org/akhq/modules/BasicAuthAuthenticationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ public class BasicAuthAuthenticationProvider implements AuthenticationProvider {

@Override
public Publisher<AuthenticationResponse> authenticate(@Nullable HttpRequest<?> httpRequest, AuthenticationRequest<?, ?> authenticationRequest) {
for(BasicAuth auth : securityProperties.getBasicAuth()) {
if (authenticationRequest.getIdentity().equals(auth.getUsername()) &&
auth.isValidPassword((String) authenticationRequest.getSecret())) {

UserDetails userDetails = new UserDetails(auth.getUsername(),
userGroupUtils.getUserRoles(auth.getGroups()),
userGroupUtils.getUserAttributes(auth.getGroups()));

return Flowable.just(userDetails);
String username = String.valueOf(authenticationRequest.getIdentity());
for (BasicAuth auth : securityProperties.getBasicAuth()) {
if (!username.equals(auth.getUsername())) {
continue;
}
if (!auth.isValidPassword((String) authenticationRequest.getSecret())) {
return Flowable.just(new AuthenticationFailed(AuthenticationFailureReason.CREDENTIALS_DO_NOT_MATCH));
}
UserDetails userDetails = new UserDetails(username,
userGroupUtils.getUserRoles(auth.getGroups()),
userGroupUtils.getUserAttributes(auth.getGroups()));
return Flowable.just(userDetails);
}

return Flowable.just(new AuthenticationFailed());
return Flowable.just(new AuthenticationFailed(AuthenticationFailureReason.USER_NOT_FOUND));
}
}
30 changes: 30 additions & 0 deletions src/main/java/org/akhq/utils/LoginFailedEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.akhq.utils;

import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.security.authentication.AuthenticationFailed;
import io.micronaut.security.authentication.UserDetails;
import io.micronaut.security.event.LoginFailedEvent;
import lombok.extern.slf4j.Slf4j;

import javax.inject.Singleton;

@Singleton
@Slf4j
public class LoginFailedEventListener implements ApplicationEventListener<LoginFailedEvent> {
@Override
public void onApplicationEvent(LoginFailedEvent event) {
if (event.getSource() instanceof AuthenticationFailed) {
AuthenticationFailed authenticationFailed = (AuthenticationFailed) event.getSource();
log.warn("Login failed reason {}, username {}, message {}",
authenticationFailed.getReason(),
authenticationFailed.getUserDetails().map(UserDetails::getUsername).orElse("unknown"),
authenticationFailed.getMessage().orElse("none")
);
}
}

@Override
public boolean supports(LoginFailedEvent event) {
return true;
}
}

0 comments on commit f691db0

Please sign in to comment.