22Tests for token handling
33"""
44import unittest
5+ from time import time
56
6- from django .conf import settings
7- from jwkest import BadSignature , Expired , Invalid , MissingKey , jwk
8- from jwkest .jws import JWS
7+ from jwt .exceptions import ExpiredSignatureError , InvalidSignatureError , MissingRequiredClaimError
98
109from openedx .core .djangolib .testing .utils import skip_unless_lms
11- from openedx .core .lib .jwt import _encode_and_sign , create_jwt , unpack_jwt
10+ from openedx .core .lib .jwt import _encode_and_sign , create_jwt , unpack_jwt , unpack_and_verify
1211
1312
1413test_user_id = 121
1514invalid_test_user_id = 120
16- test_timeout = 60
17- test_now = 1661432902
15+ test_timeout = 1000
16+ test_now = int ( time ())
1817test_claims = {"foo" : "bar" , "baz" : "quux" , "meaning" : 42 }
1918expected_full_token = {
2019 "lms_user_id" : test_user_id ,
21- "iat" : 1661432902 ,
22- "exp" : 1661432902 + 60 ,
20+ "iat" : test_now ,
21+ "exp" : test_now + test_timeout ,
2322 "iss" : "token-test-issuer" , # these lines from test_settings.py
2423 "version" : "1.2.0" , # these lines from test_settings.py
2524}
@@ -34,7 +33,7 @@ class TestSign(unittest.TestCase):
3433 def test_create_jwt (self ):
3534 token = create_jwt (test_user_id , test_timeout , {}, test_now )
3635
37- decoded = _verify_jwt (token )
36+ decoded = unpack_and_verify (token )
3837 self .assertEqual (expected_full_token , decoded )
3938
4039 def test_create_jwt_with_claims (self ):
@@ -43,7 +42,7 @@ def test_create_jwt_with_claims(self):
4342 expected_token_with_claims = expected_full_token .copy ()
4443 expected_token_with_claims .update (test_claims )
4544
46- decoded = _verify_jwt (token )
45+ decoded = unpack_and_verify (token )
4746 self .assertEqual (expected_token_with_claims , decoded )
4847
4948 def test_malformed_token (self ):
@@ -53,19 +52,8 @@ def test_malformed_token(self):
5352 expected_token_with_claims = expected_full_token .copy ()
5453 expected_token_with_claims .update (test_claims )
5554
56- with self .assertRaises (BadSignature ):
57- _verify_jwt (token )
58-
59-
60- def _verify_jwt (jwt_token ):
61- """
62- Helper function which verifies the signature and decodes the token
63- from string back to claims form
64- """
65- keys = jwk .KEYS ()
66- keys .load_jwks (settings .TOKEN_SIGNING ['JWT_PUBLIC_SIGNING_JWK_SET' ])
67- decoded = JWS ().verify_compact (jwt_token .encode ('utf-8' ), keys )
68- return decoded
55+ with self .assertRaises (InvalidSignatureError ):
56+ unpack_and_verify (token )
6957
7058
7159@skip_unless_lms
@@ -97,33 +85,33 @@ def test_malformed_token(self):
9785 expected_token_with_claims = expected_full_token .copy ()
9886 expected_token_with_claims .update (test_claims )
9987
100- with self .assertRaises (BadSignature ):
88+ with self .assertRaises (InvalidSignatureError ):
10189 unpack_jwt (token , test_user_id , test_now )
10290
10391 def test_unpack_token_with_invalid_user (self ):
10492 token = create_jwt (invalid_test_user_id , test_timeout , {}, test_now )
10593
106- with self .assertRaises (Invalid ):
94+ with self .assertRaises (InvalidSignatureError ):
10795 unpack_jwt (token , test_user_id , test_now )
10896
10997 def test_unpack_expired_token (self ):
11098 token = create_jwt (test_user_id , test_timeout , {}, test_now )
11199
112- with self .assertRaises (Expired ):
100+ with self .assertRaises (ExpiredSignatureError ):
113101 unpack_jwt (token , test_user_id , test_now + test_timeout + 1 )
114102
115103 def test_missing_expired_lms_user_id (self ):
116104 payload = expected_full_token .copy ()
117105 del payload ['lms_user_id' ]
118106 token = _encode_and_sign (payload )
119107
120- with self .assertRaises (MissingKey ):
108+ with self .assertRaises (MissingRequiredClaimError ):
121109 unpack_jwt (token , test_user_id , test_now )
122110
123111 def test_missing_expired_key (self ):
124112 payload = expected_full_token .copy ()
125113 del payload ['exp' ]
126114 token = _encode_and_sign (payload )
127115
128- with self .assertRaises (MissingKey ):
116+ with self .assertRaises (MissingRequiredClaimError ):
129117 unpack_jwt (token , test_user_id , test_now )
0 commit comments