1+ package com .auth0 .jwt ;
2+
3+ import static com .auth0 .jwt .TimeUtil .generateRandomExpDateInFuture ;
4+ import static com .auth0 .jwt .TimeUtil .generateRandomIatDateInPast ;
5+ import com .auth0 .jwt .algorithms .Algorithm ;
6+ import com .auth0 .jwt .exceptions .InvalidClaimException ;
7+ import com .auth0 .jwt .exceptions .TokenExpiredException ;
8+ import com .auth0 .jwt .interfaces .Claim ;
9+ import com .auth0 .jwt .interfaces .DecodedJWT ;
10+ import com .auth0 .jwt .interfaces .Verification ;
11+ import static org .junit .Assert .assertTrue ;
12+ import org .junit .Rule ;
13+ import org .junit .Test ;
14+ import org .junit .rules .ExpectedException ;
15+
16+ import java .text .SimpleDateFormat ;
17+ import java .util .*;
18+
19+ public class FbJwtCreatorTest {
20+
21+ @ Rule
22+ public ExpectedException thrown = ExpectedException .none ();
23+ private static final Date exp = generateRandomExpDateInFuture ();
24+ private static final Date iat = generateRandomIatDateInPast ();
25+ private static final String USER_ID = "userId" ;
26+ private static final String APP_ID = "appId" ;
27+
28+ @ Test
29+ public void testFbJwtCreatorAllStandardClaimsMustBeRequired () throws Exception {
30+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
31+ String token = FbJwtCreator .build ()
32+ .withExp (exp )
33+ .withIat (iat )
34+ .withUserId (USER_ID )
35+ .withAppId (APP_ID )
36+ .sign (algorithm );
37+ Verification verification = FbJWT .require (algorithm );
38+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
39+ DecodedJWT jwt = verifier .decode (token );
40+ Map <String , Claim > claims = jwt .getClaims ();
41+ verifyClaims (claims );
42+ }
43+
44+ @ Test
45+ public void testFbJwtCreatorInvalidUserId () throws Exception {
46+ thrown .expect (InvalidClaimException .class );
47+ thrown .expectMessage ("The Claim 'userId' value doesn't match the required one." );
48+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
49+ String token = FbJwtCreator .build ()
50+ .withExp (exp )
51+ .withIat (iat )
52+ .withUserId ("invalid" )
53+ .withAppId (APP_ID )
54+ .sign (algorithm );
55+ Verification verification = FbJWT .require (algorithm );
56+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
57+ DecodedJWT jwt = verifier .decode (token );
58+ }
59+
60+ @ Test
61+ public void testFbJwtCreatorInvalidAppId () throws Exception {
62+ thrown .expect (InvalidClaimException .class );
63+ thrown .expectMessage ("The Claim 'appId' value doesn't match the required one." );
64+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
65+ String token = FbJwtCreator .build ()
66+ .withExp (exp )
67+ .withIat (iat )
68+ .withUserId (USER_ID )
69+ .withAppId ("invalid" )
70+ .sign (algorithm );
71+ Verification verification = FbJWT .require (algorithm );
72+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
73+ DecodedJWT jwt = verifier .decode (token );
74+ }
75+
76+ @ Test
77+ public void testFbJwtCreatorUserIdNotProvided () throws Exception {
78+ thrown .expect (Exception .class );
79+ thrown .expectMessage ("Standard claim: UserId has not been set" );
80+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
81+ String token = FbJwtCreator .build ()
82+ .withExp (exp )
83+ .withIat (iat )
84+ .withAppId (APP_ID )
85+ .sign (algorithm );
86+ Verification verification = FbJWT .require (algorithm );
87+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
88+ DecodedJWT jwt = verifier .decode (token );
89+ }
90+
91+ @ Test
92+ public void testFbJwtCreatorNoneAlgorithmNotAllowed () throws Exception {
93+ thrown .expect (IllegalAccessException .class );
94+ thrown .expectMessage ("None algorithm isn't allowed" );
95+
96+ Algorithm algorithm = Algorithm .none ();
97+ String token = FbJwtCreator .build ()
98+ .withExp (exp )
99+ .withIat (iat )
100+ .withUserId (USER_ID )
101+ .withAppId (APP_ID )
102+ .setIsNoneAlgorithmAllowed (false )
103+ .sign (algorithm );
104+ Verification verification = FbJWT .require (algorithm );
105+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
106+ DecodedJWT jwt = verifier .decode (token );
107+ }
108+
109+ @ Test
110+ public void testFbJwtCreatorNoneAlgorithmNotSpecifiedButStillNotAllowed () throws Exception {
111+ thrown .expect (IllegalAccessException .class );
112+ thrown .expectMessage ("None algorithm isn't allowed" );
113+
114+ Algorithm algorithm = Algorithm .none ();
115+ String token = FbJwtCreator .build ()
116+ .withExp (exp )
117+ .withIat (iat )
118+ .withUserId (USER_ID )
119+ .withAppId (APP_ID )
120+ .sign (algorithm );
121+ Verification verification = FbJWT .require (algorithm );
122+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
123+ DecodedJWT jwt = verifier .decode (token );
124+ }
125+
126+ @ Test
127+ public void testFbJwtCreatorNoneAlgorithmAllowed () throws Exception {
128+ Algorithm algorithm = Algorithm .none ();
129+ String token = FbJwtCreator .build ()
130+ .withExp (exp )
131+ .withIat (iat )
132+ .withUserId (USER_ID )
133+ .withAppId (APP_ID )
134+ .setIsNoneAlgorithmAllowed (true )
135+ .sign (algorithm );
136+ Verification verification = FbJWT .require (algorithm );
137+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
138+ DecodedJWT jwt = verifier .decode (token );
139+ Map <String , Claim > claims = jwt .getClaims ();
140+ verifyClaims (claims );
141+ }
142+
143+ @ Test
144+ public void testFbJwtCreatorArrayClaim () throws Exception {
145+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
146+ String token = FbJwtCreator .build ()
147+ .withExp (exp )
148+ .withIat (iat )
149+ .withUserId (USER_ID )
150+ .withAppId (APP_ID )
151+ .setIsNoneAlgorithmAllowed (true )
152+ .withArrayClaim ("arrayKey" , "arrayValue1" , "arrayValue2" )
153+ .sign (algorithm );
154+ Verification verification = FbJWT .require (algorithm );
155+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
156+ DecodedJWT jwt = verifier .decode (token );
157+ Map <String , Claim > claims = jwt .getClaims ();
158+ verifyClaims (claims );
159+ }
160+
161+ @ Test
162+ public void testFbJwtCreatorNonStandardClaimStringValue () throws Exception {
163+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
164+ String token = FbJwtCreator .build ()
165+ .withExp (exp )
166+ .withIat (iat )
167+ .withUserId (USER_ID )
168+ .withAppId (APP_ID )
169+ .setIsNoneAlgorithmAllowed (true )
170+ .withNonStandardClaim ("nonStandardClaim" , "nonStandardClaimValue" )
171+ .sign (algorithm );
172+ Verification verification = FbJWT .require (algorithm );
173+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
174+ DecodedJWT jwt = verifier .decode (token );
175+ Map <String , Claim > claims = jwt .getClaims ();
176+ verifyClaims (claims );
177+ }
178+
179+ @ Test
180+ public void testFbJwtCreatorNonStandardClaimIntegerValue () throws Exception {
181+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
182+ String token = FbJwtCreator .build ()
183+ .withExp (exp )
184+ .withIat (iat )
185+ .withUserId (USER_ID )
186+ .withAppId (APP_ID )
187+ .setIsNoneAlgorithmAllowed (true )
188+ .withNonStandardClaim ("nonStandardClaim" , 999 )
189+ .sign (algorithm );
190+ Verification verification = FbJWT .require (algorithm );
191+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
192+ DecodedJWT jwt = verifier .decode (token );
193+ Map <String , Claim > claims = jwt .getClaims ();
194+ verifyClaims (claims );
195+ }
196+
197+ @ Test
198+ public void testFbJwtCreatorNonStandardClaimLongValue () throws Exception {
199+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
200+ String token = FbJwtCreator .build ()
201+ .withExp (exp )
202+ .withIat (iat )
203+ .withUserId (USER_ID )
204+ .withAppId (APP_ID )
205+ .setIsNoneAlgorithmAllowed (true )
206+ .withNonStandardClaim ("nonStandardClaim" , 999L )
207+ .sign (algorithm );
208+ Verification verification = FbJWT .require (algorithm );
209+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
210+ DecodedJWT jwt = verifier .decode (token );
211+ Map <String , Claim > claims = jwt .getClaims ();
212+ verifyClaims (claims );
213+ }
214+
215+ @ Test
216+ public void testFbJwtCreatorNonStandardClaimDoubleValue () throws Exception {
217+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
218+ String token = FbJwtCreator .build ()
219+ .withExp (exp )
220+ .withIat (iat )
221+ .withUserId (USER_ID )
222+ .withAppId (APP_ID )
223+ .setIsNoneAlgorithmAllowed (true )
224+ .withNonStandardClaim ("nonStandardClaim" , 9.99 )
225+ .sign (algorithm );
226+ Verification verification = FbJWT .require (algorithm );
227+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
228+ DecodedJWT jwt = verifier .decode (token );
229+ Map <String , Claim > claims = jwt .getClaims ();
230+ verifyClaims (claims );
231+ }
232+
233+ @ Test
234+ public void testFbJwtCreatorNonStandardClaimBooleanValue () throws Exception {
235+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
236+ String token = FbJwtCreator .build ()
237+ .withExp (exp )
238+ .withIat (iat )
239+ .withUserId (USER_ID )
240+ .withAppId (APP_ID )
241+ .setIsNoneAlgorithmAllowed (true )
242+ .withNonStandardClaim ("nonStandardClaim" , true )
243+ .sign (algorithm );
244+ Verification verification = FbJWT .require (algorithm );
245+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
246+ DecodedJWT jwt = verifier .decode (token );
247+ Map <String , Claim > claims = jwt .getClaims ();
248+ verifyClaims (claims );
249+ }
250+
251+ @ Test
252+ public void testFbJwtCreatorNonStandardClaimDateValue () throws Exception {
253+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
254+ String token = FbJwtCreator .build ()
255+ .withExp (exp )
256+ .withIat (iat )
257+ .withUserId (USER_ID )
258+ .withAppId (APP_ID )
259+ .setIsNoneAlgorithmAllowed (true )
260+ .withNonStandardClaim ("nonStandardClaim" , new Date ())
261+ .sign (algorithm );
262+ Verification verification = FbJWT .require (algorithm );
263+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
264+ DecodedJWT jwt = verifier .decode (token );
265+ Map <String , Claim > claims = jwt .getClaims ();
266+ verifyClaims (claims );
267+ }
268+ @ Test
269+ public void testFbJwtCreatorExpTimeHasPassed () throws Exception {
270+ thrown .expect (TokenExpiredException .class );
271+ thrown .expectMessage ("The Token has expired on Wed Oct 29 00:00:00 PDT 2014." );
272+
273+ String myDate = "2014/10/29" ;
274+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
275+ Date date = sdf .parse (myDate );
276+ long expLong = date .getTime ();
277+ Date expDate = new Date (expLong );
278+
279+ Algorithm algorithm = Algorithm .HMAC256 ("secret" );
280+ String token = FbJwtCreator .build ()
281+ .withExp (expDate )
282+ .withIat (iat )
283+ .withUserId (USER_ID )
284+ .withAppId (APP_ID )
285+ .setIsNoneAlgorithmAllowed (true )
286+ .withNonStandardClaim ("nonStandardClaim" , new Date ())
287+ .sign (algorithm );
288+ Verification verification = FbJWT .require (algorithm );
289+ JWT verifier = verification .createVerifierForFb (USER_ID , APP_ID ).build ();
290+ DecodedJWT jwt = verifier .decode (token );
291+ Map <String , Claim > claims = jwt .getClaims ();
292+ verifyClaims (claims );
293+ }
294+
295+
296+ private static void verifyClaims (Map <String ,Claim > claims ) {
297+ assertTrue (claims .get (USER_ID ).asString ().equals (USER_ID ));
298+ assertTrue (claims .get (APP_ID ).asString ().equals (APP_ID ));
299+ }
300+ }
0 commit comments