Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): log login failures #699

Merged
merged 1 commit into from
May 15, 2021
Merged
Show file tree
Hide file tree
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
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;
}
}