spring-projects / spring-projects/spring-security

Add claim path delimiter configuration for JwtGrantedAuthoritiesConverter

Open
#16,603 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: oauth2 type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior
JwtGrantedAuthoritiesConverter should provide an ability to access authorities as nested claim.
As well as it should be included into OAuth2ResourceServerJwtConfiguration.JwtConverterConfiguration.


private Collection<String> getAuthorities(Jwt jwt) {
    String claimName = getAuthoritiesClaimName(jwt);
    ...
    Object authorities = getClaim(jwt, claimName);
    ...
}


private static Object getClaim(Jwt jwt, String claimName) {
    if (this.authoritiesClaimNamePathDelimiter != null) {
        String[] path = claimName.split(this.authoritiesClaimNamePathDelimiter);
        if (path.length > 1) {
            return getNestedClaim(jwt.getClaims(), path);
        }
    }
    return jwt.getClaim(claimName);
}

private static Object getNestedClaim(Map<String, Object> map, String[] path) {
    Object current = map;
    for (String key : path) {
        if (current instanceof Map) {
            current = map.get(key);
        } else {
            return null;
        }
    }
    return current;
}

Current Behavior
Does not provide ability to access nested claims.

private Collection<String> getAuthorities(Jwt jwt) {
    String claimName = getAuthoritiesClaimName(jwt);
    ...
    Object authorities = jwt.getClaim(claimName);
    ...
}

Context
What are you trying to accomplish?
I want to have an ability to access authorities stored in the custom property:

{
  "realm_access": {
    "roles": [
      "admin",
      "manager",
      "user"
    ]
  }
}

What other alternatives have you considered? Are you aware of any workarounds?
Obviously, I can create a custom converter or store claims in the root level of jwt.
However, I find it useful to have it included into the framework.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JwtGrantedAuthoritiesConverter and OAuth2ResourceServerJwtConfiguration.JwtConverterConfiguration, tracing how the authorities claim is selected and read. Add configurable nested-claim path handling and expose it through the resource-server converter configuration; done means authorities can be read from claims such as realm_access.roles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
authorization, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.