@@ -83,6 +83,35 @@ def test_encode_access_token(self):
83
83
self .assertLessEqual (exp_seconds , 60 * 5 )
84
84
self .assertGreater (exp_seconds , 60 * 4 )
85
85
86
+ def test_encode_access_token__no_user_claims (self ):
87
+ '''
88
+ To make JWT shorter, do not add `user_claims` if empty.
89
+ '''
90
+ secret = 'super-totally-secret-key'
91
+ algorithm = 'HS256'
92
+ token_expire_delta = timedelta (minutes = 5 )
93
+ identity_claim = 'sub'
94
+
95
+ # `user_claims` is empty dict
96
+ with self .app .test_request_context ():
97
+ identity = 'user1'
98
+ token = encode_access_token (identity , secret , algorithm , token_expire_delta ,
99
+ fresh = False , user_claims = {}, csrf = False ,
100
+ identity_claim = identity_claim )
101
+
102
+ data = jwt .decode (token , secret , algorithms = [algorithm ])
103
+ self .assertNotIn ('user_claims' , data )
104
+
105
+ # `user_claims` is None
106
+ with self .app .test_request_context ():
107
+ identity = 'user1'
108
+ token = encode_access_token (identity , secret , algorithm , token_expire_delta ,
109
+ fresh = False , user_claims = None , csrf = False ,
110
+ identity_claim = identity_claim )
111
+
112
+ data = jwt .decode (token , secret , algorithms = [algorithm ])
113
+ self .assertNotIn ('user_claims' , data )
114
+
86
115
def test_encode_invalid_access_token (self ):
87
116
# Check with non-serializable json
88
117
with self .app .test_request_context ():
@@ -212,6 +241,29 @@ def test_decode_jwt(self):
212
241
self .assertEqual (data [identity_claim ], 'banana' )
213
242
self .assertEqual (data ['type' ], 'refresh' )
214
243
244
+ def test_decode_access_token__no_user_claims (self ):
245
+ '''
246
+ Test decoding a valid access token without `user_claims`.
247
+ '''
248
+ identity_claim = 'sub'
249
+ with self .app .test_request_context ():
250
+ now = datetime .utcnow ()
251
+ token_data = {
252
+ 'exp' : now + timedelta (minutes = 5 ),
253
+ 'iat' : now ,
254
+ 'nbf' : now ,
255
+ 'jti' : 'banana' ,
256
+ identity_claim : 'banana' ,
257
+ 'fresh' : True ,
258
+ 'type' : 'access' ,
259
+ }
260
+ encoded_token = jwt .encode (token_data , 'secret' , 'HS256' ).decode ('utf-8' )
261
+ data = decode_jwt (encoded_token , 'secret' , 'HS256' ,
262
+ csrf = False , identity_claim = identity_claim )
263
+
264
+ self .assertIn ('user_claims' , data )
265
+ self .assertEqual (data ['user_claims' ], {})
266
+
215
267
def test_decode_invalid_jwt (self ):
216
268
with self .app .test_request_context ():
217
269
identity_claim = 'identity'
@@ -284,19 +336,6 @@ def test_decode_invalid_jwt(self):
284
336
decode_jwt (encoded_token , 'secret' , 'HS256' ,
285
337
csrf = False , identity_claim = identity_claim )
286
338
287
- # Missing user claims in access token
288
- with self .assertRaises (JWTDecodeError ):
289
- token_data = {
290
- 'jti' : 'banana' ,
291
- identity_claim : 'banana' ,
292
- 'exp' : datetime .utcnow () + timedelta (minutes = 5 ),
293
- 'type' : 'access' ,
294
- 'fresh' : True
295
- }
296
- encoded_token = jwt .encode (token_data , 'secret' , 'HS256' ).decode ('utf-8' )
297
- decode_jwt (encoded_token , 'secret' , 'HS256' ,
298
- csrf = False , identity_claim = identity_claim )
299
-
300
339
# Bad token type
301
340
with self .assertRaises (JWTDecodeError ):
302
341
token_data = {
0 commit comments