1616
1717package  org .springframework .security .authentication .ott ;
1818
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 ;
2126import  org .springframework .security .core .Authentication ;
2227import  org .springframework .security .core .AuthenticationException ;
28+ import  org .springframework .security .core .SpringSecurityMessageSource ;
2329import  org .springframework .security .core .userdetails .UserDetails ;
30+ import  org .springframework .security .core .userdetails .UserDetailsChecker ;
2431import  org .springframework .security .core .userdetails .UserDetailsService ;
2532import  org .springframework .security .core .userdetails .UsernameNotFoundException ;
2633import  org .springframework .util .Assert ;
3340 * @author Marcus da Coregio 
3441 * @since 6.4 
3542 */ 
36- public  final  class  OneTimeTokenAuthenticationProvider  implements  AuthenticationProvider  {
43+ public  final  class  OneTimeTokenAuthenticationProvider  implements  AuthenticationProvider , MessageSourceAware  {
44+ 
45+ 	private  final  Log  logger  = LogFactory .getLog (getClass ());
3746
3847	private  final  OneTimeTokenService  oneTimeTokenService ;
3948
4049	private  final  UserDetailsService  userDetailsService ;
4150
51+ 	private  UserDetailsChecker  userDetailsChecker  = new  DefaultPreAuthenticationChecks ();
52+ 
53+ 	private  MessageSourceAccessor  messages  = SpringSecurityMessageSource .getAccessor ();
54+ 
4255	public  OneTimeTokenAuthenticationProvider (OneTimeTokenService  oneTimeTokenService ,
4356			UserDetailsService  userDetailsService ) {
4457		Assert .notNull (oneTimeTokenService , "oneTimeTokenService cannot be null" );
@@ -56,6 +69,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
5669		}
5770		try  {
5871			UserDetails  user  = this .userDetailsService .loadUserByUsername (consumed .getUsername ());
72+ 			userDetailsChecker .check (user );
5973			OneTimeTokenAuthenticationToken  authenticated  = OneTimeTokenAuthenticationToken .authenticated (user ,
6074					user .getAuthorities ());
6175			authenticated .setDetails (otpAuthenticationToken .getDetails ());
@@ -71,4 +85,39 @@ public boolean supports(Class<?> authentication) {
7185		return  OneTimeTokenAuthenticationToken .class .isAssignableFrom (authentication );
7286	}
7387
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+ 
74123}
0 commit comments