Search in sources :

Example 1 with UserDetailsPasswordService

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));
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserDetailsPasswordService(org.springframework.security.core.userdetails.UserDetailsPasswordService) Test(org.junit.jupiter.api.Test)

Example 2 with UserDetailsPasswordService

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);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) UserDetailsPasswordService(org.springframework.security.core.userdetails.UserDetailsPasswordService) Test(org.junit.jupiter.api.Test)

Example 3 with UserDetailsPasswordService

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);
}
Also used : UserDetails(org.springframework.security.core.userdetails.UserDetails) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserDetailsPasswordService(org.springframework.security.core.userdetails.UserDetailsPasswordService) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 UserDetailsPasswordService (org.springframework.security.core.userdetails.UserDetailsPasswordService)3 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)3 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)3 NoOpPasswordEncoder (org.springframework.security.crypto.password.NoOpPasswordEncoder)3 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)3 Authentication (org.springframework.security.core.Authentication)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1