33
33
import org .springframework .http .MediaType ;
34
34
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
35
35
import org .springframework .security .oauth2 .client .registration .ClientRegistration .ProviderDetails ;
36
+ import org .springframework .security .oauth2 .client .registration .ClientRegistration .ProviderDetails .UserInfoEndpoint ;
36
37
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
37
38
import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
38
39
import org .springframework .security .oauth2 .core .oidc .IdTokenClaimNames ;
@@ -61,21 +62,9 @@ public void cleanup() throws Exception {
61
62
@ Test
62
63
public void getClientRegistrationsWhenUsingDefinedProviderShouldAdapt () {
63
64
OAuth2ClientProperties properties = new OAuth2ClientProperties ();
64
- Provider provider = new Provider ();
65
- provider .setAuthorizationUri ("http://example.com/auth" );
66
- provider .setTokenUri ("http://example.com/token" );
67
- provider .setUserInfoUri ("http://example.com/info" );
65
+ Provider provider = createProvider ();
68
66
provider .setUserInfoAuthenticationMethod ("form" );
69
- provider .setUserNameAttribute ("sub" );
70
- provider .setJwkSetUri ("http://example.com/jwk" );
71
- OAuth2ClientProperties .Registration registration = new OAuth2ClientProperties .Registration ();
72
- registration .setProvider ("provider" );
73
- registration .setClientId ("clientId" );
74
- registration .setClientSecret ("clientSecret" );
75
- registration .setClientAuthenticationMethod ("post" );
76
- registration .setAuthorizationGrantType ("authorization_code" );
77
- registration .setRedirectUri ("http://example.com/redirect" );
78
- registration .setScope (Collections .singleton ("scope" ));
67
+ OAuth2ClientProperties .Registration registration = createRegistration ("provider" );
79
68
registration .setClientName ("clientName" );
80
69
properties .getRegistration ().put ("registration" , registration );
81
70
properties .getProvider ().put ("provider" , provider );
@@ -86,13 +75,11 @@ public void getClientRegistrationsWhenUsingDefinedProviderShouldAdapt() {
86
75
assertThat (adaptedProvider .getAuthorizationUri ())
87
76
.isEqualTo ("http://example.com/auth" );
88
77
assertThat (adaptedProvider .getTokenUri ()).isEqualTo ("http://example.com/token" );
89
- assertThat (adaptedProvider .getUserInfoEndpoint ().getUri ())
90
- .isEqualTo ("http://example.com/info" );
91
- assertThat (adaptedProvider .getUserInfoEndpoint ().getAuthenticationMethod ())
92
- .isEqualTo (
93
- org .springframework .security .oauth2 .core .AuthenticationMethod .FORM );
94
- assertThat (adaptedProvider .getUserInfoEndpoint ().getUserNameAttributeName ())
95
- .isEqualTo ("sub" );
78
+ UserInfoEndpoint userInfoEndpoint = adaptedProvider .getUserInfoEndpoint ();
79
+ assertThat (userInfoEndpoint .getUri ()).isEqualTo ("http://example.com/info" );
80
+ assertThat (userInfoEndpoint .getAuthenticationMethod ()).isEqualTo (
81
+ org .springframework .security .oauth2 .core .AuthenticationMethod .FORM );
82
+ assertThat (userInfoEndpoint .getUserNameAttributeName ()).isEqualTo ("sub" );
96
83
assertThat (adaptedProvider .getJwkSetUri ()).isEqualTo ("http://example.com/jwk" );
97
84
assertThat (adapted .getRegistrationId ()).isEqualTo ("registration" );
98
85
assertThat (adapted .getClientId ()).isEqualTo ("clientId" );
@@ -103,7 +90,7 @@ public void getClientRegistrationsWhenUsingDefinedProviderShouldAdapt() {
103
90
org .springframework .security .oauth2 .core .AuthorizationGrantType .AUTHORIZATION_CODE );
104
91
assertThat (adapted .getRedirectUriTemplate ())
105
92
.isEqualTo ("http://example.com/redirect" );
106
- assertThat (adapted .getScopes ()).containsExactly ("scope " );
93
+ assertThat (adapted .getScopes ()).containsExactly ("user " );
107
94
assertThat (adapted .getClientName ()).isEqualTo ("clientName" );
108
95
}
109
96
@@ -123,9 +110,10 @@ public void getClientRegistrationsWhenUsingCommonProviderShouldAdapt() {
123
110
.isEqualTo ("https://accounts.google.com/o/oauth2/v2/auth" );
124
111
assertThat (adaptedProvider .getTokenUri ())
125
112
.isEqualTo ("https://www.googleapis.com/oauth2/v4/token" );
126
- assertThat (adaptedProvider .getUserInfoEndpoint ().getUri ())
113
+ UserInfoEndpoint userInfoEndpoint = adaptedProvider .getUserInfoEndpoint ();
114
+ assertThat (userInfoEndpoint .getUri ())
127
115
.isEqualTo ("https://www.googleapis.com/oauth2/v3/userinfo" );
128
- assertThat (adaptedProvider . getUserInfoEndpoint () .getUserNameAttributeName ())
116
+ assertThat (userInfoEndpoint .getUserNameAttributeName ())
129
117
.isEqualTo (IdTokenClaimNames .SUB );
130
118
assertThat (adaptedProvider .getJwkSetUri ())
131
119
.isEqualTo ("https://www.googleapis.com/oauth2/v3/certs" );
@@ -145,14 +133,7 @@ public void getClientRegistrationsWhenUsingCommonProviderShouldAdapt() {
145
133
@ Test
146
134
public void getClientRegistrationsWhenUsingCommonProviderWithOverrideShouldAdapt () {
147
135
OAuth2ClientProperties properties = new OAuth2ClientProperties ();
148
- OAuth2ClientProperties .Registration registration = new OAuth2ClientProperties .Registration ();
149
- registration .setProvider ("google" );
150
- registration .setClientId ("clientId" );
151
- registration .setClientSecret ("clientSecret" );
152
- registration .setClientAuthenticationMethod ("post" );
153
- registration .setAuthorizationGrantType ("authorization_code" );
154
- registration .setRedirectUri ("http://example.com/redirect" );
155
- registration .setScope (Collections .singleton ("scope" ));
136
+ OAuth2ClientProperties .Registration registration = createRegistration ("google" );
156
137
registration .setClientName ("clientName" );
157
138
properties .getRegistration ().put ("registration" , registration );
158
139
Map <String , ClientRegistration > registrations = OAuth2ClientPropertiesRegistrationAdapter
@@ -163,13 +144,13 @@ public void getClientRegistrationsWhenUsingCommonProviderWithOverrideShouldAdapt
163
144
.isEqualTo ("https://accounts.google.com/o/oauth2/v2/auth" );
164
145
assertThat (adaptedProvider .getTokenUri ())
165
146
.isEqualTo ("https://www.googleapis.com/oauth2/v4/token" );
166
- assertThat (adaptedProvider .getUserInfoEndpoint ().getUri ())
147
+ UserInfoEndpoint userInfoEndpoint = adaptedProvider .getUserInfoEndpoint ();
148
+ assertThat (userInfoEndpoint .getUri ())
167
149
.isEqualTo ("https://www.googleapis.com/oauth2/v3/userinfo" );
168
- assertThat (adaptedProvider . getUserInfoEndpoint () .getUserNameAttributeName ())
150
+ assertThat (userInfoEndpoint .getUserNameAttributeName ())
169
151
.isEqualTo (IdTokenClaimNames .SUB );
170
- assertThat (adaptedProvider .getUserInfoEndpoint ().getAuthenticationMethod ())
171
- .isEqualTo (
172
- org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
152
+ assertThat (userInfoEndpoint .getAuthenticationMethod ()).isEqualTo (
153
+ org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
173
154
assertThat (adaptedProvider .getJwkSetUri ())
174
155
.isEqualTo ("https://www.googleapis.com/oauth2/v3/certs" );
175
156
assertThat (adapted .getRegistrationId ()).isEqualTo ("registration" );
@@ -181,7 +162,7 @@ public void getClientRegistrationsWhenUsingCommonProviderWithOverrideShouldAdapt
181
162
org .springframework .security .oauth2 .core .AuthorizationGrantType .AUTHORIZATION_CODE );
182
163
assertThat (adapted .getRedirectUriTemplate ())
183
164
.isEqualTo ("http://example.com/redirect" );
184
- assertThat (adapted .getScopes ()).containsExactly ("scope " );
165
+ assertThat (adapted .getScopes ()).containsExactly ("user " );
185
166
assertThat (adapted .getClientName ()).isEqualTo ("clientName" );
186
167
}
187
168
@@ -212,11 +193,11 @@ public void getClientRegistrationsWhenProviderNotSpecifiedShouldUseRegistrationI
212
193
.isEqualTo ("https://accounts.google.com/o/oauth2/v2/auth" );
213
194
assertThat (adaptedProvider .getTokenUri ())
214
195
.isEqualTo ("https://www.googleapis.com/oauth2/v4/token" );
215
- assertThat (adaptedProvider .getUserInfoEndpoint ().getUri ())
196
+ UserInfoEndpoint userInfoEndpoint = adaptedProvider .getUserInfoEndpoint ();
197
+ assertThat (userInfoEndpoint .getUri ())
216
198
.isEqualTo ("https://www.googleapis.com/oauth2/v3/userinfo" );
217
- assertThat (adaptedProvider .getUserInfoEndpoint ().getAuthenticationMethod ())
218
- .isEqualTo (
219
- org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
199
+ assertThat (userInfoEndpoint .getAuthenticationMethod ()).isEqualTo (
200
+ org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
220
201
assertThat (adaptedProvider .getJwkSetUri ())
221
202
.isEqualTo ("https://www.googleapis.com/oauth2/v3/certs" );
222
203
assertThat (adapted .getRegistrationId ()).isEqualTo ("google" );
@@ -270,20 +251,10 @@ public void oidcProviderConfigurationWithCustomConfigurationOverridesProviderDef
270
251
this .server .start ();
271
252
String issuer = this .server .url ("" ).toString ();
272
253
setupMockResponse (issuer );
273
- OAuth2ClientProperties .Registration registration = new OAuth2ClientProperties .Registration ();
274
- registration .setProvider ("okta-oidc" );
275
- registration .setClientId ("clientId" );
276
- registration .setClientSecret ("clientSecret" );
277
- registration .setClientAuthenticationMethod ("post" );
278
- registration .setRedirectUri ("http://example.com/redirect" );
279
- registration .setScope (Collections .singleton ("user" ));
280
- Provider provider = new Provider ();
254
+ OAuth2ClientProperties .Registration registration = createRegistration (
255
+ "okta-oidc" );
256
+ Provider provider = createProvider ();
281
257
provider .setIssuerUri (issuer );
282
- provider .setAuthorizationUri ("http://example.com/auth" );
283
- provider .setTokenUri ("http://example.com/token" );
284
- provider .setUserInfoUri ("http://example.com/info" );
285
- provider .setUserNameAttribute ("sub" );
286
- provider .setJwkSetUri ("http://example.com/jwk" );
287
258
OAuth2ClientProperties properties = new OAuth2ClientProperties ();
288
259
properties .getProvider ().put ("okta-oidc" , provider );
289
260
properties .getRegistration ().put ("okta" , registration );
@@ -304,10 +275,31 @@ public void oidcProviderConfigurationWithCustomConfigurationOverridesProviderDef
304
275
.isEqualTo ("http://example.com/auth" );
305
276
assertThat (providerDetails .getTokenUri ()).isEqualTo ("http://example.com/token" );
306
277
assertThat (providerDetails .getJwkSetUri ()).isEqualTo ("http://example.com/jwk" );
307
- assertThat (providerDetails .getUserInfoEndpoint ().getUri ())
308
- .isEqualTo ("http://example.com/info" );
309
- assertThat (providerDetails .getUserInfoEndpoint ().getUserNameAttributeName ())
310
- .isEqualTo ("sub" );
278
+ UserInfoEndpoint userInfoEndpoint = providerDetails .getUserInfoEndpoint ();
279
+ assertThat (userInfoEndpoint .getUri ()).isEqualTo ("http://example.com/info" );
280
+ assertThat (userInfoEndpoint .getUserNameAttributeName ()).isEqualTo ("sub" );
281
+ }
282
+
283
+ private Provider createProvider () {
284
+ Provider provider = new Provider ();
285
+ provider .setAuthorizationUri ("http://example.com/auth" );
286
+ provider .setTokenUri ("http://example.com/token" );
287
+ provider .setUserInfoUri ("http://example.com/info" );
288
+ provider .setUserNameAttribute ("sub" );
289
+ provider .setJwkSetUri ("http://example.com/jwk" );
290
+ return provider ;
291
+ }
292
+
293
+ private OAuth2ClientProperties .Registration createRegistration (String provider ) {
294
+ OAuth2ClientProperties .Registration registration = new OAuth2ClientProperties .Registration ();
295
+ registration .setProvider (provider );
296
+ registration .setClientId ("clientId" );
297
+ registration .setClientSecret ("clientSecret" );
298
+ registration .setClientAuthenticationMethod ("post" );
299
+ registration .setRedirectUri ("http://example.com/redirect" );
300
+ registration .setScope (Collections .singleton ("user" ));
301
+ registration .setAuthorizationGrantType ("authorization_code" );
302
+ return registration ;
311
303
}
312
304
313
305
private void testOidcConfiguration (OAuth2ClientProperties .Registration registration ,
@@ -338,18 +330,11 @@ private void testOidcConfiguration(OAuth2ClientProperties.Registration registrat
338
330
.isEqualTo ("https://example.com/oauth2/v4/token" );
339
331
assertThat (providerDetails .getJwkSetUri ())
340
332
.isEqualTo ("https://example.com/oauth2/v3/certs" );
341
- assertThat (providerDetails .getUserInfoEndpoint ().getUri ())
333
+ UserInfoEndpoint userInfoEndpoint = providerDetails .getUserInfoEndpoint ();
334
+ assertThat (userInfoEndpoint .getUri ())
342
335
.isEqualTo ("https://example.com/oauth2/v3/userinfo" );
343
- assertThat (providerDetails .getUserInfoEndpoint ().getAuthenticationMethod ())
344
- .isEqualTo (
345
- org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
346
- }
347
-
348
- private String cleanIssuerPath (String issuer ) {
349
- if (issuer .endsWith ("/" )) {
350
- return issuer .substring (0 , issuer .length () - 1 );
351
- }
352
- return issuer ;
336
+ assertThat (userInfoEndpoint .getAuthenticationMethod ()).isEqualTo (
337
+ org .springframework .security .oauth2 .core .AuthenticationMethod .HEADER );
353
338
}
354
339
355
340
private void setupMockResponse (String issuer ) throws Exception {
0 commit comments