Skip to content

Commit 970fb3c

Browse files
committed
add setter for the headers Key Id value
1 parent 16050be commit 970fb3c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/src/main/java/com/auth0/jwt/JWTCreator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ public Builder withHeader(Map<String, Object> headerClaims) {
7373
return this;
7474
}
7575

76+
/**
77+
* Add a specific Key Id ("kid") claim to the Header.
78+
*
79+
* @param keyId the Key Id value.
80+
* @return this same Builder instance.
81+
*/
82+
public Builder withKeyId(String keyId) {
83+
this.headerClaims.put(PublicClaims.KEY_ID, keyId);
84+
return this;
85+
}
86+
7687
/**
7788
* Add a specific Issuer ("iss") claim to the Payload.
7889
*

lib/src/test/java/com/auth0/jwt/JWTCreatorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public void shouldAddHeader() throws Exception {
3939
assertThat(TokenUtils.splitToken(signed)[0], is("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImFzZCI6MTIzfQ"));
4040
}
4141

42+
@Test
43+
public void shouldAddKeyId() throws Exception {
44+
String signed = JWTCreator.init()
45+
.withKeyId("56a8bd44da435300010000015f5ed")
46+
.sign(Algorithm.HMAC256("secret"));
47+
48+
assertThat(signed, is(notNullValue()));
49+
assertThat(TokenUtils.splitToken(signed)[0], is("eyJraWQiOiI1NmE4YmQ0NGRhNDM1MzAwMDEwMDAwMDE1ZjVlZCIsInR5cCI6IkpXVCIsImFsZyI6IkhTMjU2In0"));
50+
}
51+
4252
@Test
4353
public void shouldAddIssuer() throws Exception {
4454
String signed = JWTCreator.init()

0 commit comments

Comments
 (0)