|
| 1 | +""" |
| 2 | +Tests for token handling |
| 3 | +""" |
| 4 | +import unittest |
| 5 | + |
| 6 | +from django.conf import settings |
| 7 | +from jwkest import BadSignature, Expired, Invalid, MissingKey, jwk |
| 8 | +from jwkest.jws import JWS |
| 9 | + |
| 10 | +from openedx.core.djangolib.testing.utils import skip_unless_lms |
| 11 | +from openedx.core.lib.jwt import _encode_and_sign, create_jwt, unpack_jwt |
| 12 | + |
| 13 | + |
| 14 | +test_user_id = 121 |
| 15 | +invalid_test_user_id = 120 |
| 16 | +test_timeout = 60 |
| 17 | +test_now = 1661432902 |
| 18 | +test_claims = {"foo": "bar", "baz": "quux", "meaning": 42} |
| 19 | +expected_full_token = { |
| 20 | + "lms_user_id": test_user_id, |
| 21 | + "iat": 1661432902, |
| 22 | + "exp": 1661432902 + 60, |
| 23 | + "iss": "token-test-issuer", # these lines from test_settings.py |
| 24 | + "version": "1.2.0", # these lines from test_settings.py |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +@skip_unless_lms |
| 29 | +class TestSign(unittest.TestCase): |
| 30 | + """ |
| 31 | + Tests for JWT creation and signing. |
| 32 | + """ |
| 33 | + |
| 34 | + def test_create_jwt(self): |
| 35 | + token = create_jwt(test_user_id, test_timeout, {}, test_now) |
| 36 | + |
| 37 | + decoded = _verify_jwt(token) |
| 38 | + self.assertEqual(expected_full_token, decoded) |
| 39 | + |
| 40 | + def test_create_jwt_with_claims(self): |
| 41 | + token = create_jwt(test_user_id, test_timeout, test_claims, test_now) |
| 42 | + |
| 43 | + expected_token_with_claims = expected_full_token.copy() |
| 44 | + expected_token_with_claims.update(test_claims) |
| 45 | + |
| 46 | + decoded = _verify_jwt(token) |
| 47 | + self.assertEqual(expected_token_with_claims, decoded) |
| 48 | + |
| 49 | + def test_malformed_token(self): |
| 50 | + token = create_jwt(test_user_id, test_timeout, test_claims, test_now) |
| 51 | + token = token + "a" |
| 52 | + |
| 53 | + expected_token_with_claims = expected_full_token.copy() |
| 54 | + expected_token_with_claims.update(test_claims) |
| 55 | + |
| 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 |
| 69 | + |
| 70 | + |
| 71 | +@skip_unless_lms |
| 72 | +class TestUnpack(unittest.TestCase): |
| 73 | + """ |
| 74 | + Tests for JWT unpacking. |
| 75 | + """ |
| 76 | + |
| 77 | + def test_unpack_jwt(self): |
| 78 | + token = create_jwt(test_user_id, test_timeout, {}, test_now) |
| 79 | + decoded = unpack_jwt(token, test_user_id, test_now) |
| 80 | + |
| 81 | + self.assertEqual(expected_full_token, decoded) |
| 82 | + |
| 83 | + def test_unpack_jwt_with_claims(self): |
| 84 | + token = create_jwt(test_user_id, test_timeout, test_claims, test_now) |
| 85 | + |
| 86 | + expected_token_with_claims = expected_full_token.copy() |
| 87 | + expected_token_with_claims.update(test_claims) |
| 88 | + |
| 89 | + decoded = unpack_jwt(token, test_user_id, test_now) |
| 90 | + |
| 91 | + self.assertEqual(expected_token_with_claims, decoded) |
| 92 | + |
| 93 | + def test_malformed_token(self): |
| 94 | + token = create_jwt(test_user_id, test_timeout, test_claims, test_now) |
| 95 | + token = token + "a" |
| 96 | + |
| 97 | + expected_token_with_claims = expected_full_token.copy() |
| 98 | + expected_token_with_claims.update(test_claims) |
| 99 | + |
| 100 | + with self.assertRaises(BadSignature): |
| 101 | + unpack_jwt(token, test_user_id, test_now) |
| 102 | + |
| 103 | + def test_unpack_token_with_invalid_user(self): |
| 104 | + token = create_jwt(invalid_test_user_id, test_timeout, {}, test_now) |
| 105 | + |
| 106 | + with self.assertRaises(Invalid): |
| 107 | + unpack_jwt(token, test_user_id, test_now) |
| 108 | + |
| 109 | + def test_unpack_expired_token(self): |
| 110 | + token = create_jwt(test_user_id, test_timeout, {}, test_now) |
| 111 | + |
| 112 | + with self.assertRaises(Expired): |
| 113 | + unpack_jwt(token, test_user_id, test_now + test_timeout + 1) |
| 114 | + |
| 115 | + def test_missing_expired_lms_user_id(self): |
| 116 | + payload = expected_full_token.copy() |
| 117 | + del payload['lms_user_id'] |
| 118 | + token = _encode_and_sign(payload) |
| 119 | + |
| 120 | + with self.assertRaises(MissingKey): |
| 121 | + unpack_jwt(token, test_user_id, test_now) |
| 122 | + |
| 123 | + def test_missing_expired_key(self): |
| 124 | + payload = expected_full_token.copy() |
| 125 | + del payload['exp'] |
| 126 | + token = _encode_and_sign(payload) |
| 127 | + |
| 128 | + with self.assertRaises(MissingKey): |
| 129 | + unpack_jwt(token, test_user_id, test_now) |
0 commit comments