16
16
17
17
package org .springframework .security .authentication .ott ;
18
18
19
- import org .springframework .security .authentication .AuthenticationProvider ;
20
- import org .springframework .security .authentication .BadCredentialsException ;
19
+ import org .apache .commons .logging .Log ;
20
+ import org .apache .commons .logging .LogFactory ;
21
+ import org .springframework .context .MessageSource ;
22
+ import org .springframework .context .MessageSourceAware ;
23
+ import org .springframework .context .support .MessageSourceAccessor ;
24
+ import org .springframework .security .authentication .*;
25
+ import org .springframework .security .authentication .dao .AbstractUserDetailsAuthenticationProvider ;
21
26
import org .springframework .security .core .Authentication ;
22
27
import org .springframework .security .core .AuthenticationException ;
28
+ import org .springframework .security .core .SpringSecurityMessageSource ;
23
29
import org .springframework .security .core .userdetails .UserDetails ;
30
+ import org .springframework .security .core .userdetails .UserDetailsChecker ;
24
31
import org .springframework .security .core .userdetails .UserDetailsService ;
25
32
import org .springframework .security .core .userdetails .UsernameNotFoundException ;
26
33
import org .springframework .util .Assert ;
33
40
* @author Marcus da Coregio
34
41
* @since 6.4
35
42
*/
36
- public final class OneTimeTokenAuthenticationProvider implements AuthenticationProvider {
43
+ public final class OneTimeTokenAuthenticationProvider implements AuthenticationProvider , MessageSourceAware {
44
+
45
+ private final Log logger = LogFactory .getLog (getClass ());
37
46
38
47
private final OneTimeTokenService oneTimeTokenService ;
39
48
40
49
private final UserDetailsService userDetailsService ;
41
50
51
+ private UserDetailsChecker userDetailsChecker = new DefaultPreAuthenticationChecks ();
52
+
53
+ private MessageSourceAccessor messages = SpringSecurityMessageSource .getAccessor ();
54
+
42
55
public OneTimeTokenAuthenticationProvider (OneTimeTokenService oneTimeTokenService ,
43
56
UserDetailsService userDetailsService ) {
44
57
Assert .notNull (oneTimeTokenService , "oneTimeTokenService cannot be null" );
@@ -56,6 +69,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
56
69
}
57
70
try {
58
71
UserDetails user = this .userDetailsService .loadUserByUsername (consumed .getUsername ());
72
+ userDetailsChecker .check (user );
59
73
OneTimeTokenAuthenticationToken authenticated = OneTimeTokenAuthenticationToken .authenticated (user ,
60
74
user .getAuthorities ());
61
75
authenticated .setDetails (otpAuthenticationToken .getDetails ());
@@ -71,4 +85,39 @@ public boolean supports(Class<?> authentication) {
71
85
return OneTimeTokenAuthenticationToken .class .isAssignableFrom (authentication );
72
86
}
73
87
88
+ @ Override
89
+ public void setMessageSource (MessageSource messageSource ) {
90
+ this .messages = new MessageSourceAccessor (messageSource );
91
+ }
92
+
93
+ public void setUserDetailsChecker (UserDetailsChecker userDetailsChecker ) {
94
+ this .userDetailsChecker = userDetailsChecker ;
95
+ }
96
+
97
+ private class DefaultPreAuthenticationChecks implements UserDetailsChecker {
98
+
99
+ @ Override
100
+ public void check (UserDetails user ) {
101
+ if (!user .isAccountNonLocked ()) {
102
+ OneTimeTokenAuthenticationProvider .this .logger
103
+ .debug ("Failed to authenticate since user account is locked" );
104
+ throw new LockedException (OneTimeTokenAuthenticationProvider .this .messages
105
+ .getMessage ("AbstractUserDetailsAuthenticationProvider.locked" , "User account is locked" ));
106
+ }
107
+ if (!user .isEnabled ()) {
108
+ OneTimeTokenAuthenticationProvider .this .logger
109
+ .debug ("Failed to authenticate since user account is disabled" );
110
+ throw new DisabledException (OneTimeTokenAuthenticationProvider .this .messages
111
+ .getMessage ("AbstractUserDetailsAuthenticationProvider.disabled" , "User is disabled" ));
112
+ }
113
+ if (!user .isAccountNonExpired ()) {
114
+ OneTimeTokenAuthenticationProvider .this .logger
115
+ .debug ("Failed to authenticate since user account has expired" );
116
+ throw new AccountExpiredException (OneTimeTokenAuthenticationProvider .this .messages
117
+ .getMessage ("AbstractUserDetailsAuthenticationProvider.expired" , "User account has expired" ));
118
+ }
119
+ }
120
+
121
+ }
122
+
74
123
}
0 commit comments