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

Fixing JwtCreator builder when setting headers as a map #320

Merged
merged 10 commits into from
Nov 6, 2019

Conversation

maxbalan
Copy link
Contributor

currently when using JwtCreator when setting headers as a map it is overwriting existing headers map, which forces to use withHeader(Map<String, Object> headerClaims) first then any other headers.

e.g.

  1. the key id is missing if I do JWT.create().withKeyId(ID).withHeader(map)
  2. the key id is not missing if I do JWT.create().withHeader(map).withKeyId(ID)

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this.
If you call Map#putAll with a null value it will throw NPE. I think it's missing the null check and the "remove claim" scenario. Please add that and the corresponding test cases as follow:

  • when calling withHeader with null value it should not throw NPE but return the builder instance anyway.
  • when calling withHeader with a map with a claim that was already set, it should overwrite its value.
  • when calling withHeader with a map with a claim with null value, it should not add it and remove it if present in the headerClaims field (this is the same behavior we have when adding body-level claims)

@maxbalan
Copy link
Contributor Author

@lbalmaceda thank you for a quick reply, this makes sense, I will update it and commit a new change

@maxbalan
Copy link
Contributor Author

@lbalmaceda could you please have another look at this change? I will really appreciate if we can have this fixed in the next version. thanks.

@maxbalan maxbalan requested a review from a team September 17, 2019 08:45
@joshcanhelp joshcanhelp removed the request for review from a team September 19, 2019 16:08
@lbalmaceda
Copy link
Contributor

@maxbalan 😐Sorry I missed this. I've set up a reminder and will review it tomorrow.

@lbalmaceda lbalmaceda self-assigned this Sep 19, 2019
@lbalmaceda lbalmaceda added this to the v3-Next milestone Sep 19, 2019
Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you got confused with the tests and the withClaim() method. I left you some comments.

@@ -66,12 +66,24 @@ private JWTCreator(Algorithm algorithm, Map<String, Object> headerClaims, Map<St

/**
* Add specific Claims to set as the Header.
* If provided map is null then nothing is changed
* If provided map contains a header with null value then that header will be removed from the header claims
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small rewrite

Suggested change
* If provided map contains a header with null value then that header will be removed from the header claims
* If provided map contains a claim with null value then that claim will be removed from the header

*
* @param headerClaims the values to use as Claims in the token's Header.
* @return this same Builder instance.
*/
public Builder withHeader(Map<String, Object> headerClaims) {
this.headerClaims = new HashMap<>(headerClaims);
if (headerClaims == null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use braces {} for this even if it's a one liner, to follow the project code style

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java Outdated Show resolved Hide resolved
lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java Outdated Show resolved Hide resolved
@Test
public void shouldRemoveHeaderIfTheValueIsNull() throws Exception {
Map<String, Object> header = new HashMap<String, Object>();
header.put("test", null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The claim must first be set in order to be unset. Use a claim that you know is being set to the header.

@maxbalan maxbalan requested a review from a team November 5, 2019 15:12
@maxbalan
Copy link
Contributor Author

maxbalan commented Nov 5, 2019

@lbalmaceda Hi, I fixed the pr as per your suggestions I hope this removes some of the ambiguity and adds more to tests readability, please have another look

@lbalmaceda lbalmaceda removed the request for review from a team November 5, 2019 16:16
lbalmaceda
lbalmaceda previously approved these changes Nov 5, 2019
Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxbalan thanks. All good!

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java Outdated Show resolved Hide resolved
@maxbalan
Copy link
Contributor Author

maxbalan commented Nov 5, 2019

@lbalmaceda looks like when I applied your suggestions for test name change it dismissed the pr approve

Copy link
Contributor

@lbalmaceda lbalmaceda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried kicking the codecov checks but they are still stuck. I'll reach someone so we can merge this PR. Thanks for the contribution

@damieng damieng merged commit 2a0c022 into auth0:master Nov 6, 2019
@jimmyjames jimmyjames removed this from the v3-Next milestone Jan 2, 2020
@jimmyjames jimmyjames added this to the 3.9.0 milestone Jan 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants