19
19
import org .junit .Test ;
20
20
import org .mockito .ArgumentCaptor ;
21
21
import org .springframework .security .authentication .TestingAuthenticationToken ;
22
+ import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
22
23
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
23
24
import org .springframework .security .oauth2 .core .OAuth2ErrorCodes ;
24
25
import org .springframework .security .oauth2 .jose .JoseHeaderNames ;
49
50
* @author Joe Grandja
50
51
*/
51
52
public class OAuth2ClientCredentialsAuthenticationProviderTests {
52
- private RegisteredClient registeredClient ;
53
53
private OAuth2AuthorizationService authorizationService ;
54
54
private JwtEncoder jwtEncoder ;
55
55
private OAuth2ClientCredentialsAuthenticationProvider authenticationProvider ;
56
56
57
57
@ Before
58
58
public void setUp () {
59
- this .registeredClient = TestRegisteredClients .registeredClient ().build ();
60
59
this .authorizationService = mock (OAuth2AuthorizationService .class );
61
60
this .jwtEncoder = mock (JwtEncoder .class );
62
61
this .authenticationProvider = new OAuth2ClientCredentialsAuthenticationProvider (
@@ -89,8 +88,9 @@ public void supportsWhenUnsupportedAuthenticationThenFalse() {
89
88
90
89
@ Test
91
90
public void authenticateWhenClientPrincipalNotOAuth2ClientAuthenticationTokenThenThrowOAuth2AuthenticationException () {
91
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
92
92
TestingAuthenticationToken clientPrincipal = new TestingAuthenticationToken (
93
- this . registeredClient .getClientId (), this . registeredClient .getClientSecret ());
93
+ registeredClient .getClientId (), registeredClient .getClientSecret ());
94
94
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken (clientPrincipal );
95
95
96
96
assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
@@ -102,8 +102,9 @@ public void authenticateWhenClientPrincipalNotOAuth2ClientAuthenticationTokenThe
102
102
103
103
@ Test
104
104
public void authenticateWhenClientPrincipalNotAuthenticatedThenThrowOAuth2AuthenticationException () {
105
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
105
106
OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (
106
- this . registeredClient .getClientId (), this . registeredClient .getClientSecret (), null );
107
+ registeredClient .getClientId (), registeredClient .getClientSecret (), null );
107
108
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken (clientPrincipal );
108
109
109
110
assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
@@ -113,9 +114,25 @@ public void authenticateWhenClientPrincipalNotAuthenticatedThenThrowOAuth2Authen
113
114
.isEqualTo (OAuth2ErrorCodes .INVALID_CLIENT );
114
115
}
115
116
117
+ @ Test
118
+ public void authenticateWhenClientNotAuthorizedToRequestTokenThenThrowOAuth2AuthenticationException () {
119
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ()
120
+ .authorizationGrantTypes (grantTypes -> grantTypes .remove (AuthorizationGrantType .CLIENT_CREDENTIALS ))
121
+ .build ();
122
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (registeredClient );
123
+ OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken (clientPrincipal );
124
+
125
+ assertThatThrownBy (() -> this .authenticationProvider .authenticate (authentication ))
126
+ .isInstanceOf (OAuth2AuthenticationException .class )
127
+ .extracting (ex -> ((OAuth2AuthenticationException ) ex ).getError ())
128
+ .extracting ("errorCode" )
129
+ .isEqualTo (OAuth2ErrorCodes .UNAUTHORIZED_CLIENT );
130
+ }
131
+
116
132
@ Test
117
133
public void authenticateWhenInvalidScopeThenThrowOAuth2AuthenticationException () {
118
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (this .registeredClient );
134
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
135
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (registeredClient );
119
136
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken (
120
137
clientPrincipal , Collections .singleton ("invalid-scope" ));
121
138
@@ -128,7 +145,8 @@ public void authenticateWhenInvalidScopeThenThrowOAuth2AuthenticationException()
128
145
129
146
@ Test
130
147
public void authenticateWhenScopeRequestedThenAccessTokenContainsScope () {
131
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (this .registeredClient );
148
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
149
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (registeredClient );
132
150
Set <String > requestedScope = Collections .singleton ("openid" );
133
151
OAuth2ClientCredentialsAuthenticationToken authentication =
134
152
new OAuth2ClientCredentialsAuthenticationToken (clientPrincipal , requestedScope );
@@ -142,7 +160,8 @@ public void authenticateWhenScopeRequestedThenAccessTokenContainsScope() {
142
160
143
161
@ Test
144
162
public void authenticateWhenValidAuthenticationThenReturnAccessToken () {
145
- OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (this .registeredClient );
163
+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
164
+ OAuth2ClientAuthenticationToken clientPrincipal = new OAuth2ClientAuthenticationToken (registeredClient );
146
165
OAuth2ClientCredentialsAuthenticationToken authentication = new OAuth2ClientCredentialsAuthenticationToken (clientPrincipal );
147
166
148
167
when (this .jwtEncoder .encode (any (), any ())).thenReturn (createJwt ());
0 commit comments