11package com .auth0 .jwt ;
22
33import com .auth0 .jwt .algorithms .Algorithm ;
4+ import org .apache .commons .codec .binary .Base64 ;
45import org .junit .Rule ;
56import org .junit .Test ;
67import org .junit .rules .ExpectedException ;
78
9+ import java .nio .charset .StandardCharsets ;
810import java .util .Date ;
911import java .util .HashMap ;
1012import java .util .Map ;
@@ -28,15 +30,29 @@ public void shouldThrowWhenRequestingSignWithoutAlgorithm() throws Exception {
2830
2931 @ SuppressWarnings ("Convert2Diamond" )
3032 @ Test
31- public void shouldAddHeader () throws Exception {
33+ public void shouldAddHeaderClaim () throws Exception {
3234 Map <String , Object > header = new HashMap <String , Object >();
3335 header .put ("asd" , 123 );
3436 String signed = JWTCreator .init ()
3537 .withHeader (header )
3638 .sign (Algorithm .HMAC256 ("secret" ));
3739
3840 assertThat (signed , is (notNullValue ()));
39- assertThat (TokenUtils .splitToken (signed )[0 ], is ("eyJhbGciOiJIUzI1NiIsImFzZCI6MTIzfQ" ));
41+ String [] parts = signed .split ("\\ ." );
42+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
43+ assertThat (headerJson , JsonMatcher .hasEntry ("asd" , 123 ));
44+ }
45+
46+ @ Test
47+ public void shouldAddKeyId () throws Exception {
48+ String signed = JWTCreator .init ()
49+ .withKeyId ("56a8bd44da435300010000015f5ed" )
50+ .sign (Algorithm .HMAC256 ("secret" ));
51+
52+ assertThat (signed , is (notNullValue ()));
53+ String [] parts = signed .split ("\\ ." );
54+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
55+ assertThat (headerJson , JsonMatcher .hasEntry ("kid" , "56a8bd44da435300010000015f5ed" ));
4056 }
4157
4258 @ Test
@@ -134,7 +150,20 @@ public void shouldSetCorrectAlgorithmInTheHeader() throws Exception {
134150 .sign (Algorithm .HMAC256 ("secret" ));
135151
136152 assertThat (signed , is (notNullValue ()));
137- assertThat (TokenUtils .splitToken (signed )[0 ], is ("eyJhbGciOiJIUzI1NiJ9" ));
153+ String [] parts = signed .split ("\\ ." );
154+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
155+ assertThat (headerJson , JsonMatcher .hasEntry ("alg" , "HS256" ));
156+ }
157+
158+ @ Test
159+ public void shouldSetCorrectTypeInTheHeader () throws Exception {
160+ String signed = JWTCreator .init ()
161+ .sign (Algorithm .HMAC256 ("secret" ));
162+
163+ assertThat (signed , is (notNullValue ()));
164+ String [] parts = signed .split ("\\ ." );
165+ String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
166+ assertThat (headerJson , JsonMatcher .hasEntry ("typ" , "JWT" ));
138167 }
139168
140169 @ Test
@@ -158,43 +187,43 @@ public void shouldAcceptCustomClaimOfTypeString() throws Exception {
158187 String jwt = JWTCreator .init ()
159188 .withClaim ("name" , "value" )
160189 .sign (Algorithm .HMAC256 ("secret" ));
161- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoidmFsdWUifQ.4qDWJcNQHDVDW1iAcIgZNiu-qqJQ0RIq8X3ETijBx5k" ;
162190
163191 assertThat (jwt , is (notNullValue ()));
164- assertThat (jwt , is (token ));
192+ String [] parts = jwt .split ("\\ ." );
193+ assertThat (parts [1 ], is ("eyJuYW1lIjoidmFsdWUifQ" ));
165194 }
166195
167196 @ Test
168197 public void shouldAcceptCustomClaimOfTypeInteger () throws Exception {
169198 String jwt = JWTCreator .init ()
170199 .withClaim ("name" , 123 )
171200 .sign (Algorithm .HMAC256 ("secret" ));
172- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoxMjN9.5i6ga8YMteicIeZrFZgJyW4OnI_2jpMaUXcDt-_jme4" ;
173201
174202 assertThat (jwt , is (notNullValue ()));
175- assertThat (jwt , is (token ));
203+ String [] parts = jwt .split ("\\ ." );
204+ assertThat (parts [1 ], is ("eyJuYW1lIjoxMjN9" ));
176205 }
177206
178207 @ Test
179208 public void shouldAcceptCustomClaimOfTypeDouble () throws Exception {
180209 String jwt = JWTCreator .init ()
181210 .withClaim ("name" , 23.45 )
182211 .sign (Algorithm .HMAC256 ("secret" ));
183- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoyMy40NX0.aFNlMk3WiikukJq1jo4Tf8ztR180wjTfSpqec0xKKqU" ;
184212
185213 assertThat (jwt , is (notNullValue ()));
186- assertThat (jwt , is (token ));
214+ String [] parts = jwt .split ("\\ ." );
215+ assertThat (parts [1 ], is ("eyJuYW1lIjoyMy40NX0" ));
187216 }
188217
189218 @ Test
190219 public void shouldAcceptCustomClaimOfTypeBoolean () throws Exception {
191220 String jwt = JWTCreator .init ()
192221 .withClaim ("name" , true )
193222 .sign (Algorithm .HMAC256 ("secret" ));
194- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjp0cnVlfQ.jseAYuhVmT1boYrHQfn9wXmomWq_tdGfphLtG_2tj_M" ;
195223
196224 assertThat (jwt , is (notNullValue ()));
197- assertThat (jwt , is (token ));
225+ String [] parts = jwt .split ("\\ ." );
226+ assertThat (parts [1 ], is ("eyJuYW1lIjp0cnVlfQ" ));
198227 }
199228
200229 @ Test
@@ -203,31 +232,31 @@ public void shouldAcceptCustomClaimOfTypeDate() throws Exception {
203232 String jwt = JWTCreator .init ()
204233 .withClaim ("name" , date )
205234 .sign (Algorithm .HMAC256 ("secret" ));
206- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoxNDc4ODkxNTIxfQ.ZU1B1pDLYoJZhWD8h3_QsK5dViolxvL5Q43Yz9QIxL4" ;
207235
208236 assertThat (jwt , is (notNullValue ()));
209- assertThat (jwt , is (token ));
237+ String [] parts = jwt .split ("\\ ." );
238+ assertThat (parts [1 ], is ("eyJuYW1lIjoxNDc4ODkxNTIxfQ" ));
210239 }
211240
212241 @ Test
213242 public void shouldAcceptCustomArrayClaimOfTypeString () throws Exception {
214243 String jwt = JWTCreator .init ()
215244 .withArrayClaim ("name" , new String []{"text" , "123" , "true" })
216245 .sign (Algorithm .HMAC256 ("secret" ));
217- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLCIxMjMiLCJ0cnVlIl19.lxM8EcmK1uSZRAPd0HUhXGZJdauRmZmLjoeqz4J9yAA" ;
218246
219247 assertThat (jwt , is (notNullValue ()));
220- assertThat (jwt , is (token ));
248+ String [] parts = jwt .split ("\\ ." );
249+ assertThat (parts [1 ], is ("eyJuYW1lIjpbInRleHQiLCIxMjMiLCJ0cnVlIl19" ));
221250 }
222251
223252 @ Test
224253 public void shouldAcceptCustomArrayClaimOfTypeInteger () throws Exception {
225254 String jwt = JWTCreator .init ()
226255 .withArrayClaim ("name" , new Integer []{1 , 2 , 3 })
227256 .sign (Algorithm .HMAC256 ("secret" ));
228- String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbMSwyLDNdfQ.UEuMKRQYrzKAiPpPLhIVawWkKWA1zj0_GderrWUIyFE" ;
229257
230258 assertThat (jwt , is (notNullValue ()));
231- assertThat (jwt , is (token ));
259+ String [] parts = jwt .split ("\\ ." );
260+ assertThat (parts [1 ], is ("eyJuYW1lIjpbMSwyLDNdfQ" ));
232261 }
233262}
0 commit comments