use of org.springframework.security.core.userdetails.UserDetailsPasswordService in project spring-security by spring-projects.
the class DaoAuthenticationProviderTests method authenticateWhenSuccessAndPasswordManagerThenUpdates.
@Test
public void authenticateWhenSuccessAndPasswordManagerThenUpdates() {
String password = "password";
String encodedPassword = "encoded";
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", password);
PasswordEncoder encoder = mock(PasswordEncoder.class);
UserDetailsService userDetailsService = mock(UserDetailsService.class);
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(encoder);
provider.setUserDetailsService(userDetailsService);
provider.setUserDetailsPasswordService(passwordManager);
UserDetails user = PasswordEncodedUser.user();
given(encoder.matches(any(), any())).willReturn(true);
given(encoder.upgradeEncoding(any())).willReturn(true);
given(encoder.encode(any())).willReturn(encodedPassword);
given(userDetailsService.loadUserByUsername(any())).willReturn(user);
given(passwordManager.updatePassword(any(), any())).willReturn(user);
Authentication result = provider.authenticate(token);
verify(encoder).encode(password);
verify(passwordManager).updatePassword(eq(user), eq(encodedPassword));
}
use of org.springframework.security.core.userdetails.UserDetailsPasswordService in project spring-security by spring-projects.
the class DaoAuthenticationProviderTests method authenticateWhenNotUpgradeAndPasswordManagerThenNoUpdate.
@Test
public void authenticateWhenNotUpgradeAndPasswordManagerThenNoUpdate() {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
PasswordEncoder encoder = mock(PasswordEncoder.class);
UserDetailsService userDetailsService = mock(UserDetailsService.class);
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(encoder);
provider.setUserDetailsService(userDetailsService);
provider.setUserDetailsPasswordService(passwordManager);
UserDetails user = PasswordEncodedUser.user();
given(encoder.matches(any(), any())).willReturn(true);
given(encoder.upgradeEncoding(any())).willReturn(false);
given(userDetailsService.loadUserByUsername(any())).willReturn(user);
Authentication result = provider.authenticate(token);
verifyZeroInteractions(passwordManager);
}
use of org.springframework.security.core.userdetails.UserDetailsPasswordService in project spring-security by spring-projects.
the class DaoAuthenticationProviderTests method authenticateWhenBadCredentialsAndPasswordManagerThenNoUpdate.
@Test
public void authenticateWhenBadCredentialsAndPasswordManagerThenNoUpdate() {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password");
PasswordEncoder encoder = mock(PasswordEncoder.class);
UserDetailsService userDetailsService = mock(UserDetailsService.class);
UserDetailsPasswordService passwordManager = mock(UserDetailsPasswordService.class);
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(encoder);
provider.setUserDetailsService(userDetailsService);
provider.setUserDetailsPasswordService(passwordManager);
UserDetails user = PasswordEncodedUser.user();
given(encoder.matches(any(), any())).willReturn(false);
given(userDetailsService.loadUserByUsername(any())).willReturn(user);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> provider.authenticate(token));
verifyZeroInteractions(passwordManager);
}
Aggregations