|
1 | 1 | import base64 |
2 | 2 | import json |
3 | | -from datetime import UTC, datetime, timedelta |
| 3 | + |
| 4 | +try: |
| 5 | + from datetime import UTC, datetime, timedelta |
| 6 | + utc_now = datetime.now(UTC) # Preferred in Python 3.13+ |
| 7 | +except ImportError: |
| 8 | + from datetime import datetime, timedelta, timezone |
| 9 | + utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below |
| 10 | + UTC = timezone.utc |
| 11 | + |
4 | 12 |
|
5 | 13 | import pytest |
6 | 14 |
|
@@ -84,7 +92,9 @@ def return_encoded_array(token, key, algorithms, verify=True): |
84 | 92 |
|
85 | 93 | jws.verify = return_encoded_array |
86 | 94 |
|
87 | | - with pytest.raises(JWTError, match="Invalid payload string: must be a json object"): |
| 95 | + with pytest.raises( |
| 96 | + JWTError, match="Invalid payload string: must be a json object" |
| 97 | + ): |
88 | 98 | jwt.decode(token, "secret", ["HS256"]) |
89 | 99 | finally: |
90 | 100 | jws.verify = old_jws_verify |
@@ -144,20 +154,35 @@ def test_deterministic_headers(self): |
144 | 154 |
|
145 | 155 | # manually decode header to compare it to known good |
146 | 156 | decoded_headers1 = base64url_decode(encoded_headers1.encode("utf-8")) |
147 | | - assert decoded_headers1 == b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}""" |
| 157 | + assert ( |
| 158 | + decoded_headers1 |
| 159 | + == b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}""" |
| 160 | + ) |
148 | 161 |
|
149 | 162 | def test_encode(self, claims, key): |
150 | 163 | expected = ( |
151 | | - ("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" ".eyJhIjoiYiJ9" ".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw"), |
152 | | - ("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"), |
| 164 | + ( |
| 165 | + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" |
| 166 | + ".eyJhIjoiYiJ9" |
| 167 | + ".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw" |
| 168 | + ), |
| 169 | + ( |
| 170 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" |
| 171 | + ".eyJhIjoiYiJ9" |
| 172 | + ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8" |
| 173 | + ), |
153 | 174 | ) |
154 | 175 |
|
155 | 176 | encoded = jwt.encode(claims, key) |
156 | 177 |
|
157 | 178 | assert encoded in expected |
158 | 179 |
|
159 | 180 | def test_decode(self, claims, key): |
160 | | - token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8" |
| 181 | + token = ( |
| 182 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" |
| 183 | + ".eyJhIjoiYiJ9" |
| 184 | + ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8" |
| 185 | + ) |
161 | 186 |
|
162 | 187 | decoded = jwt.decode(token, key) |
163 | 188 |
|
@@ -504,8 +529,8 @@ def test_unverified_claims_object(self, claims, key): |
504 | 529 | [ |
505 | 530 | ("aud", "aud"), |
506 | 531 | ("ait", "ait"), |
507 | | - ("exp", datetime.now(UTC) + timedelta(seconds=3600)), |
508 | | - ("nbf", datetime.now(UTC) - timedelta(seconds=5)), |
| 532 | + ("exp", utc_now + timedelta(seconds=3600)), |
| 533 | + ("nbf", utc_now - timedelta(seconds=5)), |
509 | 534 | ("iss", "iss"), |
510 | 535 | ("sub", "sub"), |
511 | 536 | ("jti", "jti"), |
|
0 commit comments