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

Allow bulk adding of claims #196

Closed
roberterdin opened this issue Dec 19, 2016 · 3 comments
Closed

Allow bulk adding of claims #196

roberterdin opened this issue Dec 19, 2016 · 3 comments
Milestone

Comments

@roberterdin
Copy link
Contributor

Can you introduce a method in the JwtBuilder that allows for bulk insertation of claims but does not overwrite previously set ones?

Jwts.builder()
     .setClaims(someClaims) 
    .setSubject(someSubject)
    .signWith(SignatureAlgorithm.HS256, key)
    .compact();
// --> all good
Jwts.builder()
    .setSubject(someSubject)
    .setClaims(someClaims) 
    .signWith(SignatureAlgorithm.HS256, key)
    .compact();
// --> subject gets overwritten :(

While it is easy to avoid the overwriting it just makes the code prone to errors when the order of the builder functions matters.

Proposed solution.
Change the JwtBuilder and the Claims interfaces and implementations such that they allow an addClaims functions like this one in the DefaultJwtBuilder implementation.

    @Override
    public JwtBuilder addClaims(Map<String, Object> claims) {
        this.claims.putAll(claims)
        return this;
    }

I'm happy to submit a PR.

Cheers

@lhazlewood
Copy link
Contributor

lhazlewood commented Dec 19, 2016

Thanks for the issue! A PR would be appreciated! Please add an @since 0.8.0 JavaDoc tag to the method before submission.

@petergphillips
Copy link

I've just been caught out by the setClaims method too, but for a different reason. The setClaims method takes a Map and then modifies the passed in map by adding the other claims to the map. This means that a immutable map (e.g. ImmutableMap or Collections.unmodifiableMap) can't be used in the builder. Modification of input parameters by an API is also not pleasant.

Would it be worth therefore deprecating the setClaims method altogether and moving to addClaims instead?

lhazlewood added a commit that referenced this issue May 16, 2017
Added addClaims function to JwtBuilder as described in Issue #196.
@lhazlewood
Copy link
Contributor

PR merged - thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants