Skip to content

Commit ed02ef9

Browse files
committed
Add Test for Malformed Scope
Fixes gh-7563
1 parent badb0a0 commit ed02ef9

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/introspection/NimbusOpaqueTokenIntrospectorTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Optional;
2727

28+
import net.minidev.json.JSONArray;
2829
import net.minidev.json.JSONObject;
2930
import okhttp3.mockwebserver.Dispatcher;
3031
import okhttp3.mockwebserver.MockResponse;
@@ -100,10 +101,24 @@ public class NimbusOpaqueTokenIntrospectorTests {
100101
" \"iss\" : \"badissuer\"\n" +
101102
" }";
102103

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+
103117
private static final ResponseEntity<String> ACTIVE = response(ACTIVE_RESPONSE);
104118
private static final ResponseEntity<String> INACTIVE = response(INACTIVE_RESPONSE);
105119
private static final ResponseEntity<String> INVALID = response(INVALID_RESPONSE);
106120
private static final ResponseEntity<String> MALFORMED_ISSUER = response(MALFORMED_ISSUER_RESPONSE);
121+
private static final ResponseEntity<String> MALFORMED_SCOPE = response(MALFORMED_SCOPE_RESPONSE);
107122

108123
@Test
109124
public void introspectWhenActiveTokenThenOk() throws Exception {
@@ -230,6 +245,24 @@ public void introspectWhenIntrospectionTokenReturnsMalformedIssuerResponseThenIn
230245
.isInstanceOf(OAuth2IntrospectionException.class);
231246
}
232247

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+
233266
@Test
234267
public void constructorWhenIntrospectionUriIsNullThenIllegalArgumentException() {
235268
assertThatCode(() -> new NimbusOpaqueTokenIntrospector(null, CLIENT_ID, CLIENT_SECRET))

0 commit comments

Comments
 (0)