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
14 changes: 13 additions & 1 deletion lib/src/main/java/com/auth0/jwt/JWTCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,24 @@ public static class Builder {

/**
* 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

return this;

for (Map.Entry<String, Object> entry : headerClaims.entrySet()) {
if (entry.getValue() == null) {
this.headerClaims.remove(entry.getKey());
} else {
this.headerClaims.put(entry.getKey(), entry.getValue());
}
}

return this;
}

Expand Down
43 changes: 43 additions & 0 deletions lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ public void shouldAddHeaderClaim() throws Exception {
assertThat(headerJson, JsonMatcher.hasEntry("asd", 123));
}

@Test
public void shouldReturnBuilderIfNullMapIsProvided() throws Exception {
String signed = JWTCreator.init()
.withHeader(null)
.sign(Algorithm.HMAC256("secret"));

assertThat(signed, is(notNullValue()));
}

@Test
public void shouldOverwriteExistingIfHeadersMapContainsTheSameKey() throws Exception {
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
maxbalan marked this conversation as resolved.
Show resolved Hide resolved
Map<String, Object> header = new HashMap<String, Object>();
header.put("test", 456);

String signed = JWTCreator.init()
.withClaim("test", 123)
.withHeader(header)
.sign(Algorithm.HMAC256("secret"));

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("test", 456));
}

@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.

header.put("test2", "isSet");

String signed = JWTCreator.init()
.withClaim("test", 123)
.withHeader(header)
.sign(Algorithm.HMAC256("secret"));

assertThat(signed, is(notNullValue()));
String[] parts = signed.split("\\.");
String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
assertThat(headerJson, JsonMatcher.hasEntry("test", null));
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
assertThat(headerJson, JsonMatcher.hasEntry("test2", "isSet"));
}

@Test
public void shouldAddKeyId() throws Exception {
String signed = JWTCreator.init()
Expand Down