|
25 | 25 | import java.util.Map;
|
26 | 26 | import java.util.Optional;
|
27 | 27 |
|
| 28 | +import net.minidev.json.JSONArray; |
28 | 29 | import net.minidev.json.JSONObject;
|
29 | 30 | import okhttp3.mockwebserver.Dispatcher;
|
30 | 31 | import okhttp3.mockwebserver.MockResponse;
|
@@ -100,10 +101,24 @@ public class NimbusOpaqueTokenIntrospectorTests {
|
100 | 101 | " \"iss\" : \"badissuer\"\n" +
|
101 | 102 | " }";
|
102 | 103 |
|
| 104 | + private static final String MALFORMED_SCOPE_RESPONSE = "{\n" + |
| 105 | + " \"active\": true,\n" + |
| 106 | + " \"client_id\": \"l238j323ds-23ij4\",\n" + |
| 107 | + " \"username\": \"jdoe\",\n" + |
| 108 | + " \"scope\": [ \"read\", \"write\", \"dolphin\" ],\n" + |
| 109 | + " \"sub\": \"Z5O3upPC88QrAjx00dis\",\n" + |
| 110 | + " \"aud\": \"https://protected.example.net/resource\",\n" + |
| 111 | + " \"iss\": \"https://server.example.com/\",\n" + |
| 112 | + " \"exp\": 1419356238,\n" + |
| 113 | + " \"iat\": 1419350238,\n" + |
| 114 | + " \"extension_field\": \"twenty-seven\"\n" + |
| 115 | + " }"; |
| 116 | + |
103 | 117 | private static final ResponseEntity<String> ACTIVE = response(ACTIVE_RESPONSE);
|
104 | 118 | private static final ResponseEntity<String> INACTIVE = response(INACTIVE_RESPONSE);
|
105 | 119 | private static final ResponseEntity<String> INVALID = response(INVALID_RESPONSE);
|
106 | 120 | private static final ResponseEntity<String> MALFORMED_ISSUER = response(MALFORMED_ISSUER_RESPONSE);
|
| 121 | + private static final ResponseEntity<String> MALFORMED_SCOPE = response(MALFORMED_SCOPE_RESPONSE); |
107 | 122 |
|
108 | 123 | @Test
|
109 | 124 | public void introspectWhenActiveTokenThenOk() throws Exception {
|
@@ -230,6 +245,24 @@ public void introspectWhenIntrospectionTokenReturnsMalformedIssuerResponseThenIn
|
230 | 245 | .isInstanceOf(OAuth2IntrospectionException.class);
|
231 | 246 | }
|
232 | 247 |
|
| 248 | + // gh-7563 |
| 249 | + @Test |
| 250 | + public void introspectWhenIntrospectionTokenReturnsMalformedScopeThenEmptyAuthorities() { |
| 251 | + RestOperations restOperations = mock(RestOperations.class); |
| 252 | + OpaqueTokenIntrospector introspectionClient = |
| 253 | + new NimbusOpaqueTokenIntrospector(INTROSPECTION_URL, restOperations); |
| 254 | + when(restOperations.exchange(any(RequestEntity.class), eq(String.class))) |
| 255 | + .thenReturn(MALFORMED_SCOPE); |
| 256 | + |
| 257 | + OAuth2AuthenticatedPrincipal principal = introspectionClient.introspect("token"); |
| 258 | + assertThat(principal.getAuthorities()).isEmpty(); |
| 259 | + assertThat((Object) principal.getAttribute("scope")) |
| 260 | + .isNotNull() |
| 261 | + .isInstanceOf(JSONArray.class); |
| 262 | + JSONArray scope = principal.getAttribute("scope"); |
| 263 | + assertThat(scope).containsExactly("read", "write", "dolphin"); |
| 264 | + } |
| 265 | + |
233 | 266 | @Test
|
234 | 267 | public void constructorWhenIntrospectionUriIsNullThenIllegalArgumentException() {
|
235 | 268 | assertThatCode(() -> new NimbusOpaqueTokenIntrospector(null, CLIENT_ID, CLIENT_SECRET))
|
|
0 commit comments