Skip to content
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

Support Map as an additional claim on JWTClaim payload #439

Open
rafaelmenta opened this issue Jun 18, 2020 · 2 comments
Open

Support Map as an additional claim on JWTClaim payload #439

rafaelmenta opened this issue Jun 18, 2020 · 2 comments
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@rafaelmenta
Copy link
Contributor

Currently JwtClaims only support String objects when passing a map of additional claims. This limitation prevent applications that need more sophisticated JWT payloads to use the library.

According to the RFC 7519 a claim value can be any valid JSON value including numbers, booleans, dates, list and other objects.

An example of this feature being implemented can be found on this commit for the library java-jwt, by auth0.

Code snippet

This is how usage would look like:

Map<String, String> value = ImmutableMap.of("foo", "bar");

Map<String, String> stringClaim = ImmutableMap.of("fooStr", "barStr");
Map<String, Map<String, ?>> mapClaim = 
    Collections.<String, Map<String, ?>>singletonMap("zaz", value);

JwtClaims claims = JwtClaims.newBuilder()
        .setAdditionalClaims(stringClaim)
        .setAdditionalComplexClaims(mapClaim) // [NEW]
        .build();
  1. Note that the introduction of another setter instead of changing setAdditionalClaims signature is to keep backwards compatibility. The newly introduced setAdditionalComplexClaims could support either a Map or a wild card (?) type, with validations happening during the build() call. I am open to both approaches and any recommendation would be welcome.

  2. Also not that because of the above a change on JwtCredentials is also required replacing the following code when generating the JWT payload:

// used to be  payload.putAll(jwtClaims.getAdditionalClaims());
payload.putAll(jwtClaims.getAllAdditionalClaims());
  1. getAllAdditionalClaims would be a map combining both getAdditionalClaims and getAdditionalComplexClaims. This would allow current users who set their claims through the original method to keep their code functional.

External references such as API reference guides used

Any additional information below

Should this issue be accepted I'm willing to move forward with the implementation suggested above and create a Pull Request contributing to the library. Please also advice if there's any recommendation for the item (2) on the list above.

Thanks!

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Jun 19, 2020
@chingor13 chingor13 added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jun 24, 2020
@chingor13
Copy link
Contributor

Thanks for opening this issue!

Would we also need to have a List version? It seems that a claim value can also be a List.

Perhaps we would want to consider an overload like:

// import com.google.api.client.json.GenericData;

GenericData genericData = new GenericData();
genericData.put("key1, "string value");
genericData.put("key2", List.of("value1", "value2"));
GenericData nestedData = new GenericData();
nestedData.put("foo", "bar");
genericData.put("key3", nestedData);

JwtClaims claims = JwtClaims.newBuilder()
        .setAdditionalClaims(genericData)
        .build();

@rafaelmenta
Copy link
Contributor Author

Hi Jeff,

Agreed, if we decide to add List we may as well support all additional types (numbers, booleans, dates, etc).

I will play around a little bit with how it would look like if use GenericData for overloading but my guess is that it would end up on a breaking change on the getter method, do you have any thoughts on that?

The alternative would be the additional setter/getter getAdditionalComplexClaims receiving a GenericData and a getter that merges them both so we can keep functionality on current users, similar to what I originally proposed.

Let me know which way sounds more appealing to you and I will have a PR soon :)

@yoshi-automation yoshi-automation removed triage me I really want to be triaged. 🚨 This issue needs some love. labels Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants