41
41
import org .springframework .security .oauth2 .core .oidc .user .OidcUserAuthority ;
42
42
43
43
import java .time .Instant ;
44
- import java .util .Arrays ;
44
+ import java .util .Collections ;
45
45
import java .util .HashMap ;
46
- import java .util .LinkedHashSet ;
47
46
import java .util .Map ;
48
- import java .util .Set ;
49
47
import java .util .concurrent .TimeUnit ;
50
48
import java .util .function .Function ;
51
49
@@ -116,6 +114,17 @@ public void setClaimTypeConverterFactoryWhenNullThenThrowIllegalArgumentExceptio
116
114
.isInstanceOf (IllegalArgumentException .class );
117
115
}
118
116
117
+ @ Test
118
+ public void setAccessibleScopesWhenNullThenThrowIllegalArgumentException () {
119
+ assertThatThrownBy (() -> this .userService .setAccessibleScopes (null ))
120
+ .isInstanceOf (IllegalArgumentException .class );
121
+ }
122
+
123
+ @ Test
124
+ public void setAccessibleScopesWhenEmptyThenSet () {
125
+ this .userService .setAccessibleScopes (Collections .emptySet ());
126
+ }
127
+
119
128
@ Test
120
129
public void loadUserWhenUserRequestIsNullThenThrowIllegalArgumentException () {
121
130
this .exception .expect (IllegalArgumentException .class );
@@ -130,20 +139,91 @@ public void loadUserWhenUserInfoUriIsNullThenUserInfoEndpointNotRequested() {
130
139
}
131
140
132
141
@ Test
133
- public void loadUserWhenAuthorizedScopesDoesNotContainUserInfoScopesThenUserInfoEndpointNotRequested () {
142
+ public void loadUserWhenNonStandardScopesAuthorizedThenUserInfoEndpointNotRequested () {
134
143
ClientRegistration clientRegistration = this .clientRegistrationBuilder
135
144
.userInfoUri ("https://provider.com/user" ).build ();
136
-
137
- Set <String > authorizedScopes = new LinkedHashSet <>(Arrays .asList ("scope1" , "scope2" ));
138
- OAuth2AccessToken accessToken = new OAuth2AccessToken (
139
- OAuth2AccessToken .TokenType .BEARER , "access-token" ,
140
- Instant .MIN , Instant .MAX , authorizedScopes );
145
+ this .accessToken = scopes ("scope1" , "scope2" );
141
146
142
147
OidcUser user = this .userService .loadUser (
143
- new OidcUserRequest (clientRegistration , accessToken , this .idToken ));
148
+ new OidcUserRequest (clientRegistration , this . accessToken , this .idToken ));
144
149
assertThat (user .getUserInfo ()).isNull ();
145
150
}
146
151
152
+ // gh-6886
153
+ @ Test
154
+ public void loadUserWhenNonStandardScopesAuthorizedAndAccessibleScopesMatchThenUserInfoEndpointRequested () {
155
+ String userInfoResponse = "{\n " +
156
+ " \" sub\" : \" subject1\" ,\n " +
157
+ " \" name\" : \" first last\" ,\n " +
158
+ " \" given_name\" : \" first\" ,\n " +
159
+ " \" family_name\" : \" last\" ,\n " +
160
+ " \" preferred_username\" : \" user1\" ,\n " +
161
+ " \" email\" : \" [email protected] \" \n " +
162
+ "}\n " ;
163
+ this .server .enqueue (jsonResponse (userInfoResponse ));
164
+
165
+ String userInfoUri = this .server .url ("/user" ).toString ();
166
+
167
+ ClientRegistration clientRegistration = this .clientRegistrationBuilder
168
+ .userInfoUri (userInfoUri ).build ();
169
+
170
+ this .accessToken = scopes ("scope1" , "scope2" );
171
+ this .userService .setAccessibleScopes (Collections .singleton ("scope2" ));
172
+
173
+ OidcUser user = this .userService .loadUser (
174
+ new OidcUserRequest (clientRegistration , this .accessToken , this .idToken ));
175
+ assertThat (user .getUserInfo ()).isNotNull ();
176
+ }
177
+
178
+ // gh-6886
179
+ @ Test
180
+ public void loadUserWhenNonStandardScopesAuthorizedAndAccessibleScopesEmptyThenUserInfoEndpointRequested () {
181
+ String userInfoResponse = "{\n " +
182
+ " \" sub\" : \" subject1\" ,\n " +
183
+ " \" name\" : \" first last\" ,\n " +
184
+ " \" given_name\" : \" first\" ,\n " +
185
+ " \" family_name\" : \" last\" ,\n " +
186
+ " \" preferred_username\" : \" user1\" ,\n " +
187
+ " \" email\" : \" [email protected] \" \n " +
188
+ "}\n " ;
189
+ this .server .enqueue (jsonResponse (userInfoResponse ));
190
+
191
+ String userInfoUri = this .server .url ("/user" ).toString ();
192
+
193
+ ClientRegistration clientRegistration = this .clientRegistrationBuilder
194
+ .userInfoUri (userInfoUri ).build ();
195
+
196
+ this .accessToken = scopes ("scope1" , "scope2" );
197
+ this .userService .setAccessibleScopes (Collections .emptySet ());
198
+
199
+ OidcUser user = this .userService .loadUser (
200
+ new OidcUserRequest (clientRegistration , this .accessToken , this .idToken ));
201
+ assertThat (user .getUserInfo ()).isNotNull ();
202
+ }
203
+
204
+ // gh-6886
205
+ @ Test
206
+ public void loadUserWhenStandardScopesAuthorizedThenUserInfoEndpointRequested () {
207
+ String userInfoResponse = "{\n " +
208
+ " \" sub\" : \" subject1\" ,\n " +
209
+ " \" name\" : \" first last\" ,\n " +
210
+ " \" given_name\" : \" first\" ,\n " +
211
+ " \" family_name\" : \" last\" ,\n " +
212
+ " \" preferred_username\" : \" user1\" ,\n " +
213
+ " \" email\" : \" [email protected] \" \n " +
214
+ "}\n " ;
215
+ this .server .enqueue (jsonResponse (userInfoResponse ));
216
+
217
+ String userInfoUri = this .server .url ("/user" ).toString ();
218
+
219
+ ClientRegistration clientRegistration = this .clientRegistrationBuilder
220
+ .userInfoUri (userInfoUri ).build ();
221
+
222
+ OidcUser user = this .userService .loadUser (
223
+ new OidcUserRequest (clientRegistration , this .accessToken , this .idToken ));
224
+ assertThat (user .getUserInfo ()).isNotNull ();
225
+ }
226
+
147
227
@ Test
148
228
public void loadUserWhenUserInfoSuccessResponseThenReturnUser () {
149
229
String userInfoResponse = "{\n " +
0 commit comments