@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2020 the original author or authors .
* Copyright 2020 - 2021 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -21,6 +21,8 @@ import java.util.Map;
@@ -21,6 +21,8 @@ import java.util.Map;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.security.crypto.password.NoOpPasswordEncoder ;
import org.springframework.security.crypto.password.PasswordEncoder ;
import org.springframework.security.oauth2.core.AuthorizationGrantType ;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod ;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException ;
@ -37,8 +39,11 @@ import org.springframework.security.oauth2.server.authorization.client.TestRegis
@@ -37,8 +39,11 @@ import org.springframework.security.oauth2.server.authorization.client.TestRegis
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatThrownBy ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.eq ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.spy ;
import static org.mockito.Mockito.verify ;
import static org.mockito.Mockito.when ;
/ * *
@ -64,6 +69,7 @@ public class OAuth2ClientAuthenticationProviderTests {
@@ -64,6 +69,7 @@ public class OAuth2ClientAuthenticationProviderTests {
private RegisteredClientRepository registeredClientRepository ;
private OAuth2AuthorizationService authorizationService ;
private OAuth2ClientAuthenticationProvider authenticationProvider ;
private PasswordEncoder passwordEncoder ;
@Before
public void setUp ( ) {
@ -71,6 +77,18 @@ public class OAuth2ClientAuthenticationProviderTests {
@@ -71,6 +77,18 @@ public class OAuth2ClientAuthenticationProviderTests {
this . authorizationService = mock ( OAuth2AuthorizationService . class ) ;
this . authenticationProvider = new OAuth2ClientAuthenticationProvider (
this . registeredClientRepository , this . authorizationService ) ;
this . passwordEncoder = spy ( new PasswordEncoder ( ) {
@Override
public String encode ( CharSequence rawPassword ) {
return NoOpPasswordEncoder . getInstance ( ) . encode ( rawPassword ) ;
}
@Override
public boolean matches ( CharSequence rawPassword , String encodedPassword ) {
return NoOpPasswordEncoder . getInstance ( ) . matches ( rawPassword , encodedPassword ) ;
}
} ) ;
this . authenticationProvider . setPasswordEncoder ( this . passwordEncoder ) ;
}
@Test
@ -88,8 +106,8 @@ public class OAuth2ClientAuthenticationProviderTests {
@@ -88,8 +106,8 @@ public class OAuth2ClientAuthenticationProviderTests {
}
@Test
public void constructorWhenPasswordEncoder NullThenThrowIllegalArgumentException( ) {
assertThatThrownBy ( ( ) - > authenticationProvider . setPasswordEncoder ( null ) )
public void setPasswordEncoderWhen NullThenThrowIllegalArgumentException( ) {
assertThatThrownBy ( ( ) - > this . authenticationProvider . setPasswordEncoder ( null ) )
. isInstanceOf ( IllegalArgumentException . class )
. hasMessage ( "passwordEncoder cannot be null" ) ;
}
@ -127,6 +145,7 @@ public class OAuth2ClientAuthenticationProviderTests {
@@ -127,6 +145,7 @@ public class OAuth2ClientAuthenticationProviderTests {
. extracting ( ex - > ( ( OAuth2AuthenticationException ) ex ) . getError ( ) )
. extracting ( "errorCode" )
. isEqualTo ( OAuth2ErrorCodes . INVALID_CLIENT ) ;
verify ( this . passwordEncoder ) . matches ( any ( ) , any ( ) ) ;
}
@Test
@ -151,9 +170,11 @@ public class OAuth2ClientAuthenticationProviderTests {
@@ -151,9 +170,11 @@ public class OAuth2ClientAuthenticationProviderTests {
. thenReturn ( registeredClient ) ;
OAuth2ClientAuthenticationToken authentication = new OAuth2ClientAuthenticationToken (
registeredClient . getClientId ( ) , TestRegisteredClients . CLIENT_SECRET , ClientAuthenticationMethod . BASIC , null ) ;
registeredClient . getClientId ( ) , registeredClient . getClientSecret ( ) , ClientAuthenticationMethod . BASIC , null ) ;
OAuth2ClientAuthenticationToken authenticationResult =
( OAuth2ClientAuthenticationToken ) this . authenticationProvider . authenticate ( authentication ) ;
verify ( this . passwordEncoder ) . matches ( any ( ) , any ( ) ) ;
assertThat ( authenticationResult . isAuthenticated ( ) ) . isTrue ( ) ;
assertThat ( authenticationResult . getPrincipal ( ) . toString ( ) ) . isEqualTo ( registeredClient . getClientId ( ) ) ;
assertThat ( authenticationResult . getCredentials ( ) ) . isNull ( ) ;