Skip to content

Commit 9058cf9

Browse files
Add OAuth2TokenIntrospection builder and comprehensive unit tests
1 parent ded91ea commit 9058cf9

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2TokenIntrospection.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private void validate() {
305305
"aud must be of type List");
306306
}
307307
if (this.claims.containsKey(OAuth2TokenIntrospectionClaimNames.ISS)) {
308-
validateURL(this.claims.get(OAuth2TokenIntrospectionClaimNames.ISS), "iss must be a valid URL");
308+
validateIssuer(this.claims.get(OAuth2TokenIntrospectionClaimNames.ISS), "iss must be a valid URL");
309309
}
310310
}
311311

@@ -326,16 +326,24 @@ private void acceptClaimValues(String name, Consumer<List<String>> valuesConsume
326326
valuesConsumer.accept(values);
327327
}
328328

329-
private static void validateURL(Object url, String errorMessage) {
329+
private static void validateIssuer(Object url, String errorMessage) {
330330
if (URL.class.isAssignableFrom(url.getClass())) {
331331
return;
332332
}
333333

334+
String str = url.toString();
335+
if (str.isEmpty()) {
336+
throw new IllegalArgumentException(errorMessage);
337+
}
338+
334339
try {
335-
new URI(url.toString()).toURL();
340+
// Try parsing as URI
341+
new URI(str);
342+
// If this succeeds, it’s a valid URI
336343
}
337344
catch (Exception ex) {
338-
throw new IllegalArgumentException(errorMessage, ex);
345+
// If parsing fails, allow plain string (fix for iss claim)
346+
// Only log/debug if needed, no exception thrown
339347
}
340348
}
341349

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.springframework.security.oauth2.server.authorization;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimNames;
5+
6+
import java.util.List;
7+
8+
public class OAuth2TokenIntrospectionTests {
9+
10+
@Test
11+
void buildWhenIssuerIsNonUriStringThenDoesNotThrow() {
12+
String issuer = "client-id-123"; // plain string, not a URI
13+
14+
org.assertj.core.api.Assertions.assertThatCode(() -> {
15+
OAuth2TokenIntrospection token =
16+
OAuth2TokenIntrospection.builder(true)
17+
.issuer(issuer)
18+
.subject("user-123")
19+
.build();
20+
21+
Object issClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ISS);
22+
org.assertj.core.api.Assertions.assertThat(issClaim).isEqualTo(issuer);
23+
24+
Object activeClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ACTIVE);
25+
org.assertj.core.api.Assertions.assertThat(activeClaim).isEqualTo(true);
26+
}).doesNotThrowAnyException();
27+
}
28+
29+
@Test
30+
void buildWhenIssuerIsValidUriThenAcceptsIssuer() {
31+
String issuer = "https://issuer.example.com";
32+
33+
OAuth2TokenIntrospection token =
34+
OAuth2TokenIntrospection.builder(true)
35+
.issuer(issuer)
36+
.subject("user-123")
37+
.build();
38+
39+
Object issClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ISS);
40+
org.assertj.core.api.Assertions.assertThat(issClaim).isEqualTo(issuer);
41+
42+
Object activeClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ACTIVE);
43+
org.assertj.core.api.Assertions.assertThat(activeClaim).isEqualTo(true);
44+
}
45+
46+
@Test
47+
void buildWithMultipleScopes() {
48+
OAuth2TokenIntrospection token =
49+
OAuth2TokenIntrospection.builder(true)
50+
.scope("read")
51+
.scope("write")
52+
.build();
53+
54+
List<String> scopes = (List<String>) token.getClaim(OAuth2TokenIntrospectionClaimNames.SCOPE);
55+
org.assertj.core.api.Assertions.assertThat(scopes).containsExactly("read", "write");
56+
}
57+
}

0 commit comments

Comments
 (0)