-
Notifications
You must be signed in to change notification settings - Fork 921
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
Changes from 7 commits
35b2ea7
9d94214
7e33f20
3f8eccb
c1b8fa3
7156ee1
02c9310
c42476d
937f7d6
bb83717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
* @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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use braces |
||
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; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small rewrite