spring-projects / spring-projects/spring-security
Allow comma-delimited scopes in OAuth2 access token response
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
Expected Behavior
Should be possible to configure scope delimiter if server sends scopes as comma-delimited string (e.g. GitHub does this).
Current Behavior
Delimiter is hard coded here
https://github.com/spring-projects/spring-security/blob/de104e22b7855172876d7be6dc6ef882755da60a/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/DefaultMapOAuth2AccessTokenResponseConverter.java#L80-L86
The following makes redefining this behaviour inefficient:
getScopesis private static methodDefaultMapOAuth2AccessTokenResponseConverteris final classDefaultAuthorizationCodeTokenResponseClientis final class
Context
GitHub OAuth returns comma delimited scopes instead of space delimited as assumed by spring security.
Redefining default behaviour is quire cumbersome and inefficient as it creates redundant instance of OAuth2AccessTokenResponseClient per application and redundant instance of OAuth2AccessTokenResponse for each login event:
public class AdvancedAccessTokenResponseClient implements OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> {
private OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> delegate =
new DefaultAuthorizationCodeTokenResponseClient();
@Override
public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationGrantRequest) {
OAuth2AccessTokenResponse tokenResponse = delegate.getTokenResponse(authorizationGrantRequest);
Set<String> scopes = tokenResponse.getAccessToken().getScopes();
Set<String> scopesNew = scopes.stream().flatMap(s -> Arrays.stream(s.split(","))).collect(Collectors.toSet());
if (scopesNew.equals(scopes))
return tokenResponse;
return OAuth2AccessTokenResponse.withResponse(tokenResponse).scopes(scopesNew).build();
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/endpoint/DefaultMapOAuth2AccessTokenResponseConverter.java, at the getScopes logic linked in the issue. Trace how the access-token response is converted and determine how a configurable delimiter can support GitHub's comma-delimited scopes without changing existing space-delimited behavior; the work is done when both formats are handled as intended.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- authentication, security
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100