-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix OAuth2TokenIntrospection issuer validation and add unit tests #2206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ramesh-nair-dev
wants to merge
2
commits into
spring-projects:main
from
ramesh-nair-dev:fix/token-introspection-issuer
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...g/springframework/security/oauth2/server/authorization/OAuth2TokenIntrospectionTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.springframework.security.oauth2.server.authorization; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimNames; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class OAuth2TokenIntrospectionTests { | ||
|
|
||
| @Test | ||
| void buildWhenIssuerIsNonUriStringThenDoesNotThrow() { | ||
| String issuer = "client-id-123"; // plain string, not a URI | ||
|
|
||
| org.assertj.core.api.Assertions.assertThatCode(() -> { | ||
| OAuth2TokenIntrospection token = | ||
| OAuth2TokenIntrospection.builder(true) | ||
| .issuer(issuer) | ||
| .subject("user-123") | ||
| .build(); | ||
|
|
||
| Object issClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ISS); | ||
| org.assertj.core.api.Assertions.assertThat(issClaim).isEqualTo(issuer); | ||
|
|
||
| Object activeClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ACTIVE); | ||
| org.assertj.core.api.Assertions.assertThat(activeClaim).isEqualTo(true); | ||
| }).doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWhenIssuerIsValidUriThenAcceptsIssuer() { | ||
| String issuer = "https://issuer.example.com"; | ||
|
|
||
| OAuth2TokenIntrospection token = | ||
| OAuth2TokenIntrospection.builder(true) | ||
| .issuer(issuer) | ||
| .subject("user-123") | ||
| .build(); | ||
|
|
||
| Object issClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ISS); | ||
| org.assertj.core.api.Assertions.assertThat(issClaim).isEqualTo(issuer); | ||
|
|
||
| Object activeClaim = token.getClaim(OAuth2TokenIntrospectionClaimNames.ACTIVE); | ||
| org.assertj.core.api.Assertions.assertThat(activeClaim).isEqualTo(true); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWithMultipleScopes() { | ||
| OAuth2TokenIntrospection token = | ||
| OAuth2TokenIntrospection.builder(true) | ||
| .scope("read") | ||
| .scope("write") | ||
| .build(); | ||
|
|
||
| List<String> scopes = (List<String>) token.getClaim(OAuth2TokenIntrospectionClaimNames.SCOPE); | ||
| org.assertj.core.api.Assertions.assertThat(scopes).containsExactly("read", "write"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there really a benefit from first trying to parse a URI, and if this fails, still continue successfully? IMHO it would be enough to just check if the string is blank or not. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback You’re right — if we allow non-URI strings (like client IDs), then attempting to parse as a URI may not add much value.
My initial thought behind keeping the URI parsing was to still preserve compatibility with tokens where iss is a proper URI, and not reject them unintentionally. But since the validation already ensures the string isn’t blank, simplifying to just a non-blank check would be sufficient and cleaner.
I can update the implementation to only validate that iss is not blank. Would you prefer that direction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I would prefer simplicity. Compatibility is still preserved with such a simpler approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make the changes