@ -18,17 +18,15 @@ package org.springframework.security.authentication;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.List ;
import java.util.List ;
import java.util.Set ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Test ;
import org.springframework.context.MessageSource ;
import org.springframework.context.MessageSource ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.Authentication ;
import org.springframework.security.core.AuthenticationException ;
import org.springframework.security.core.AuthenticationException ;
import org.springframework.security.core.authority.AuthorityUtils ;
import org.springframework.security.core.GrantedAuthority ;
import org.springframework.security.core.context.SecurityContextHolderStrategy ;
import org.springframework.security.core.context.SecurityContextImpl ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
@ -51,7 +49,7 @@ public class ProviderManagerTests {
@Test
@Test
void authenticationFailsWithUnsupportedToken ( ) {
void authenticationFailsWithUnsupportedToken ( ) {
Authentication token = new AbstractAuthenticationToken ( null ) {
Authentication token = new AbstractAuthenticationToken ( ( Collection < ? extends GrantedAuthority > ) null ) {
@Override
@Override
public Object getCredentials ( ) {
public Object getCredentials ( ) {
return "" ;
return "" ;
@ -82,24 +80,24 @@ public class ProviderManagerTests {
@Test
@Test
void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject ( ) {
void authenticationSucceedsWithSupportedTokenAndReturnsExpectedObject ( ) {
Authentication a = mock ( Authentication . class ) ;
Authentication a = new TestingAuthenticationToken ( "user" , "pass" , "FACTOR" ) ;
ProviderManager mgr = new ProviderManager ( createProviderWhichReturns ( a ) ) ;
ProviderManager mgr = new ProviderManager ( createProviderWhichReturns ( a ) ) ;
AuthenticationEventPublisher publisher = mock ( AuthenticationEventPublisher . class ) ;
AuthenticationEventPublisher publisher = mock ( AuthenticationEventPublisher . class ) ;
mgr . setAuthenticationEventPublisher ( publisher ) ;
mgr . setAuthenticationEventPublisher ( publisher ) ;
Authentication result = mgr . authenticate ( a ) ;
Authentication result = mgr . authenticate ( a ) ;
assertThat ( result ) . isEqualTo ( a ) ;
assertThat ( result . getPrincipal ( ) ) . isEqualTo ( a . getPrincipal ( ) ) ;
verify ( publisher ) . publishAuthenticationSuccess ( result ) ;
verify ( publisher ) . publishAuthenticationSuccess ( result ) ;
}
}
@Test
@Test
void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates ( ) {
void authenticationSucceedsWhenFirstProviderReturnsNullButSecondAuthenticates ( ) {
Authentication a = mock ( Authentication . class ) ;
Authentication a = new TestingAuthenticationToken ( "user" , "pass" , "FACTOR" ) ;
ProviderManager mgr = new ProviderManager (
ProviderManager mgr = new ProviderManager (
Arrays . asList ( createProviderWhichReturns ( null ) , createProviderWhichReturns ( a ) ) ) ;
Arrays . asList ( createProviderWhichReturns ( null ) , createProviderWhichReturns ( a ) ) ) ;
AuthenticationEventPublisher publisher = mock ( AuthenticationEventPublisher . class ) ;
AuthenticationEventPublisher publisher = mock ( AuthenticationEventPublisher . class ) ;
mgr . setAuthenticationEventPublisher ( publisher ) ;
mgr . setAuthenticationEventPublisher ( publisher ) ;
Authentication result = mgr . authenticate ( a ) ;
Authentication result = mgr . authenticate ( a ) ;
assertThat ( result ) . isSameAs ( a ) ;
assertThat ( result . getPrincipal ( ) ) . isEqualTo ( a . getPrincipal ( ) ) ;
verify ( publisher ) . publishAuthenticationSuccess ( result ) ;
verify ( publisher ) . publishAuthenticationSuccess ( result ) ;
}
}
@ -166,11 +164,12 @@ public class ProviderManagerTests {
@Test
@Test
void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates ( ) {
void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates ( ) {
Authentication authReq = mock ( Authentication . class ) ;
Authentication result = new TestingAuthenticationToken ( "user" , "pass" , "FACTOR" ) ;
ProviderManager mgr = new ProviderManager (
ProviderManager mgr = new ProviderManager (
createProviderWhichThrows ( new BadCredentialsException ( "" , new Throwable ( ) ) ) ,
createProviderWhichThrows ( new BadCredentialsException ( "" , new Throwable ( ) ) ) ,
createProviderWhichReturns ( authReq ) ) ;
createProviderWhichReturns ( result ) ) ;
assertThat ( mgr . authenticate ( mock ( Authentication . class ) ) ) . isSameAs ( authReq ) ;
Authentication request = new TestingAuthenticationToken ( "user" , "pass" ) ;
assertThat ( mgr . authenticate ( request ) . getPrincipal ( ) ) . isEqualTo ( result . getPrincipal ( ) ) ;
}
}
@Test
@Test
@ -314,22 +313,6 @@ public class ProviderManagerTests {
verifyNoMoreInteractions ( publisher ) ; // Child should not publish (duplicate event)
verifyNoMoreInteractions ( publisher ) ; // Child should not publish (duplicate event)
}
}
@Test
void authenticateWhenPreviousAuthenticationThenApplies ( ) {
Authentication factorOne = new TestingAuthenticationToken ( "user" , "pass" , "FACTOR_ONE" ) ;
Authentication factorTwo = new TestingAuthenticationToken ( "user" , "pass" , "FACTOR_TWO" ) ;
SecurityContextHolderStrategy securityContextHolderStrategy = mock ( SecurityContextHolderStrategy . class ) ;
given ( securityContextHolderStrategy . getContext ( ) ) . willReturn ( new SecurityContextImpl ( factorOne ) ) ;
AuthenticationProvider provider = mock ( AuthenticationProvider . class ) ;
given ( provider . authenticate ( any ( ) ) ) . willReturn ( factorTwo ) ;
given ( provider . supports ( any ( ) ) ) . willReturn ( true ) ;
ProviderManager manager = new ProviderManager ( provider ) ;
manager . setSecurityContextHolderStrategy ( securityContextHolderStrategy ) ;
Authentication request = new TestingAuthenticationToken ( "user" , "password" ) ;
Set < String > authorities = AuthorityUtils . authorityListToSet ( manager . authenticate ( request ) . getAuthorities ( ) ) ;
assertThat ( authorities ) . containsExactlyInAnyOrder ( "FACTOR_ONE" , "FACTOR_TWO" ) ;
}
private AuthenticationProvider createProviderWhichThrows ( final AuthenticationException ex ) {
private AuthenticationProvider createProviderWhichThrows ( final AuthenticationException ex ) {
AuthenticationProvider provider = mock ( AuthenticationProvider . class ) ;
AuthenticationProvider provider = mock ( AuthenticationProvider . class ) ;
given ( provider . supports ( any ( Class . class ) ) ) . willReturn ( true ) ;
given ( provider . supports ( any ( Class . class ) ) ) . willReturn ( true ) ;