-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Validate account status in OneTimeTokenAuthenticationProvider #17656
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,22 @@ | |
|
||
package org.springframework.security.authentication.ott; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.context.MessageSource; | ||
import org.springframework.context.MessageSourceAware; | ||
import org.springframework.context.support.MessageSourceAccessor; | ||
import org.springframework.security.authentication.AccountExpiredException; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.DisabledException; | ||
import org.springframework.security.authentication.LockedException; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.SpringSecurityMessageSource; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsChecker; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.util.Assert; | ||
|
@@ -31,14 +42,21 @@ | |
* {@link UserDetailsService} to fetch user authorities. | ||
* | ||
* @author Marcus da Coregio | ||
* @author Andrey Litvitski | ||
* @since 6.4 | ||
*/ | ||
public final class OneTimeTokenAuthenticationProvider implements AuthenticationProvider { | ||
public final class OneTimeTokenAuthenticationProvider implements AuthenticationProvider, MessageSourceAware { | ||
|
||
private final OneTimeTokenService oneTimeTokenService; | ||
|
||
private final UserDetailsService userDetailsService; | ||
|
||
private final Log logger = LogFactory.getLog(getClass()); | ||
|
||
private UserDetailsChecker authenticationChecks = new DefaultAuthenticationChecks(); | ||
|
||
private MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); | ||
|
||
public OneTimeTokenAuthenticationProvider(OneTimeTokenService oneTimeTokenService, | ||
UserDetailsService userDetailsService) { | ||
Assert.notNull(oneTimeTokenService, "oneTimeTokenService cannot be null"); | ||
|
@@ -56,6 +74,7 @@ public Authentication authenticate(Authentication authentication) throws Authent | |
} | ||
try { | ||
UserDetails user = this.userDetailsService.loadUserByUsername(consumed.getUsername()); | ||
this.authenticationChecks.check(user); | ||
OneTimeTokenAuthenticationToken authenticated = OneTimeTokenAuthenticationToken.authenticated(user, | ||
user.getAuthorities()); | ||
authenticated.setDetails(otpAuthenticationToken.getDetails()); | ||
|
@@ -71,4 +90,39 @@ public boolean supports(Class<?> authentication) { | |
return OneTimeTokenAuthenticationToken.class.isAssignableFrom(authentication); | ||
} | ||
|
||
@Override | ||
public void setMessageSource(MessageSource messageSource) { | ||
this.messages = new MessageSourceAccessor(messageSource); | ||
} | ||
|
||
public void setAuthenticationChecks(UserDetailsChecker authenticationChecks) { | ||
this.authenticationChecks = authenticationChecks; | ||
} | ||
|
||
private class DefaultAuthenticationChecks implements UserDetailsChecker { | ||
|
||
@Override | ||
public void check(UserDetails user) { | ||
if (!user.isAccountNonLocked()) { | ||
OneTimeTokenAuthenticationProvider.this.logger | ||
.debug("Failed to authenticate since user account is locked"); | ||
throw new LockedException(OneTimeTokenAuthenticationProvider.this.messages | ||
.getMessage("AbstractUserDetailsAuthenticationProvider.locked", "User account is locked")); | ||
Comment on lines
+109
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I agree with you that this is a slippery slope. On the other hand, it will work. Is there any other way to do this? Yes, we could hypothetically copy the message text personally for Are there any suggestions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should just throw messages directly. In this.messages.getMessage(“AbstractUserDetailsAuthenticationProvider.badCredentials”, “Bad credentials”) In turn, in |
||
} | ||
if (!user.isEnabled()) { | ||
OneTimeTokenAuthenticationProvider.this.logger | ||
.debug("Failed to authenticate since user account is disabled"); | ||
throw new DisabledException(OneTimeTokenAuthenticationProvider.this.messages | ||
.getMessage("AbstractUserDetailsAuthenticationProvider.disabled", "User is disabled")); | ||
} | ||
if (!user.isAccountNonExpired()) { | ||
OneTimeTokenAuthenticationProvider.this.logger | ||
.debug("Failed to authenticate since user account has expired"); | ||
throw new AccountExpiredException(OneTimeTokenAuthenticationProvider.this.messages | ||
.getMessage("AbstractUserDetailsAuthenticationProvider.expired", "User account has expired")); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the catch can be common something considering your added exceptions - like AuthenticationException
Then you can send specific exception message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you in advance for your comment.
Am I correct in assuming that you are suggesting something like this:
In general, I personally think that this is not a good idea, as we would like the user to know what type of exception is being thrown. Since in
check()
we throw exceptions that are primarily inherited fromAccountStatusException
. We should also understand thatBadCredentialsException
is not related toAccountStatusException
, which I think could confuse the user.You can also see how this is implemented in
AbstractUserDetailsAuthenticationProvider
.