3
3
import com .auth0 .jwt .algorithms .Algorithm ;
4
4
import com .auth0 .jwt .interfaces .ECDSAKeyProvider ;
5
5
import com .auth0 .jwt .interfaces .RSAKeyProvider ;
6
+ import com .fasterxml .jackson .core .JsonProcessingException ;
6
7
import com .fasterxml .jackson .databind .ObjectMapper ;
7
8
import org .junit .Rule ;
8
9
import org .junit .Test ;
@@ -82,13 +83,48 @@ public void shouldAddHeaderClaim() {
82
83
83
84
@ Test
84
85
public void shouldReturnBuilderIfNullMapIsProvided () {
86
+ Map <String , Object > nullMap = null ;
87
+ String nullString = null ;
85
88
String signed = JWTCreator .init ()
86
- .withHeader (null )
89
+ .withHeader (nullMap )
90
+ .withHeader (nullString )
87
91
.sign (Algorithm .HMAC256 ("secret" ));
88
92
89
93
assertThat (signed , is (notNullValue ()));
90
94
}
91
95
96
+ @ Test
97
+ public void shouldSupportJsonValueHeaderWithNestedDataStructure () {
98
+ String stringClaim = "someClaim" ;
99
+ Integer intClaim = 1 ;
100
+ List <String > nestedListClaims = Arrays .asList ("1" , "2" );
101
+ String claimsJson = "{\" stringClaim\" : \" someClaim\" , \" intClaim\" : 1, \" nestedClaim\" : { \" listClaim\" : [ \" 1\" , \" 2\" ]}}" ;
102
+
103
+ String jwt = JWTCreator .init ()
104
+ .withHeader (claimsJson )
105
+ .sign (Algorithm .HMAC256 ("secret" ));
106
+
107
+ assertThat (jwt , is (notNullValue ()));
108
+ String [] parts = jwt .split ("\\ ." );
109
+ String headerJson = new String (Base64 .getUrlDecoder ().decode (parts [0 ]), StandardCharsets .UTF_8 );
110
+
111
+ assertThat (headerJson , JsonMatcher .hasEntry ("stringClaim" , stringClaim ));
112
+ assertThat (headerJson , JsonMatcher .hasEntry ("intClaim" , intClaim ));
113
+ assertThat (headerJson , JsonMatcher .hasEntry ("listClaim" , nestedListClaims ));
114
+ }
115
+
116
+ @ Test
117
+ public void shouldFailWithIllegalArgumentExceptionForInvalidJsonForHeaderClaims () {
118
+ String invalidJson = "{ invalidJson }" ;
119
+
120
+ exception .expect (IllegalArgumentException .class );
121
+ exception .expectMessage ("Invalid header JSON" );
122
+
123
+ JWTCreator .init ()
124
+ .withHeader (invalidJson )
125
+ .sign (Algorithm .HMAC256 ("secret" ));
126
+ }
127
+
92
128
@ Test
93
129
public void shouldOverwriteExistingHeaderIfHeaderMapContainsTheSameKey () {
94
130
Map <String , Object > header = new HashMap <>();
@@ -105,6 +141,7 @@ public void shouldOverwriteExistingHeaderIfHeaderMapContainsTheSameKey() {
105
141
assertThat (headerJson , JsonMatcher .hasEntry (HeaderParams .KEY_ID , "xyz" ));
106
142
}
107
143
144
+
108
145
@ Test
109
146
public void shouldOverwriteExistingHeadersWhenSettingSameHeaderKey () {
110
147
Map <String , Object > header = new HashMap <>();
@@ -715,8 +752,11 @@ public void withPayloadShouldAddBasicClaim() {
715
752
716
753
@ Test
717
754
public void withPayloadShouldCreateJwtWithEmptyBodyIfPayloadNull () {
755
+ Map <String , Object > nullMap = null ;
756
+ String nullString = null ;
718
757
String jwt = JWTCreator .init ()
719
- .withPayload (null )
758
+ .withPayload (nullMap )
759
+ .withPayload (nullString )
720
760
.sign (Algorithm .HMAC256 ("secret" ));
721
761
722
762
assertThat (jwt , is (notNullValue ()));
@@ -921,10 +961,42 @@ public void withPayloadShouldSupportNullValuesEverywhere() {
921
961
assertThat (headerJson , JsonMatcher .hasEntry ("objClaim" , objClaim ));
922
962
}
923
963
964
+ @ Test
965
+ public void withPayloadShouldSupportJsonValueWithNestedDataStructure () {
966
+ String stringClaim = "someClaim" ;
967
+ Integer intClaim = 1 ;
968
+ List <String > nestedListClaims = Arrays .asList ("1" , "2" );
969
+ String claimsJson = "{\" stringClaim\" : \" someClaim\" , \" intClaim\" : 1, \" nestedClaim\" : { \" listClaim\" : [ \" 1\" , \" 2\" ]}}" ;
970
+
971
+ String jwt = JWTCreator .init ()
972
+ .withPayload (claimsJson )
973
+ .sign (Algorithm .HMAC256 ("secret" ));
974
+
975
+ assertThat (jwt , is (notNullValue ()));
976
+ String [] parts = jwt .split ("\\ ." );
977
+ String payloadJson = new String (Base64 .getUrlDecoder ().decode (parts [1 ]), StandardCharsets .UTF_8 );
978
+
979
+ assertThat (payloadJson , JsonMatcher .hasEntry ("stringClaim" , stringClaim ));
980
+ assertThat (payloadJson , JsonMatcher .hasEntry ("intClaim" , intClaim ));
981
+ assertThat (payloadJson , JsonMatcher .hasEntry ("listClaim" , nestedListClaims ));
982
+ }
983
+
984
+ @ Test
985
+ public void shouldFailWithIllegalArgumentExceptionForInvalidJsonForPayloadClaims () {
986
+ String invalidJson = "{ invalidJson }" ;
987
+
988
+ exception .expect (IllegalArgumentException .class );
989
+ exception .expectMessage ("Invalid payload JSON" );
990
+
991
+ JWTCreator .init ()
992
+ .withPayload (invalidJson )
993
+ .sign (Algorithm .HMAC256 ("secret" ));
994
+ }
995
+
924
996
@ Test
925
997
public void shouldCreatePayloadWithNullForMap () {
926
998
String jwt = JWTCreator .init ()
927
- .withClaim ("name" , (Map <String ,?>) null )
999
+ .withClaim ("name" , (Map <String , ?>) null )
928
1000
.sign (Algorithm .HMAC256 ("secret" ));
929
1001
assertThat (jwt , is (notNullValue ()));
930
1002
assertTrue (JWT .decode (jwt ).getClaim ("name" ).isNull ());
0 commit comments