|
| 1 | +import base64 |
1 | 2 | import json |
2 | 3 | import re |
3 | 4 |
|
| 5 | +from jose import jwt |
4 | 6 | from jose.backends import ECKey |
5 | 7 | from jose.constants import ALGORITHMS |
6 | 8 | from jose.exceptions import JOSEError, JWKError |
|
14 | 16 |
|
15 | 17 | try: |
16 | 18 | from cryptography.hazmat.backends import default_backend as CryptographyBackend |
| 19 | + from cryptography.hazmat.primitives import hashes, hmac, serialization |
17 | 20 | from cryptography.hazmat.primitives.asymmetric import ec as CryptographyEc |
18 | 21 |
|
19 | 22 | from jose.backends.cryptography_backend import CryptographyECKey |
| 23 | + |
20 | 24 | except ImportError: |
21 | 25 | CryptographyECKey = CryptographyEc = CryptographyBackend = None |
22 | 26 |
|
@@ -228,26 +232,21 @@ def test_to_dict(self): |
228 | 232 | @pytest.mark.cryptography |
229 | 233 | @pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available") |
230 | 234 | def test_incorrect_public_key_hmac_signing(): |
231 | | - import base64 |
232 | | - from cryptography.hazmat.primitives import hashes, hmac, serialization |
233 | | - |
234 | | - from jose import jwt |
235 | | - |
236 | 235 | def b64(x): |
237 | | - return base64.urlsafe_b64encode(x).replace(b'=', b'') |
| 236 | + return base64.urlsafe_b64encode(x).replace(b"=", b"") |
238 | 237 |
|
239 | 238 | KEY = CryptographyEc.generate_private_key(CryptographyEc.SECP256R1) |
240 | 239 | PUBKEY = KEY.public_key().public_bytes( |
241 | 240 | encoding=serialization.Encoding.OpenSSH, |
242 | | - format=serialization.PublicFormat.OpenSSH |
| 241 | + format=serialization.PublicFormat.OpenSSH, |
243 | 242 | ) |
244 | 243 |
|
245 | 244 | # Create and sign the payload using a public key, but specify the "alg" in |
246 | 245 | # the claims that a symmetric key was used. |
247 | | - payload = b64(b'{"alg":"HS256"}') + b'.' + b64(b'{"pwned":true}') |
| 246 | + payload = b64(b'{"alg":"HS256"}') + b"." + b64(b'{"pwned":true}') |
248 | 247 | hasher = hmac.HMAC(PUBKEY, hashes.SHA256()) |
249 | 248 | hasher.update(payload) |
250 | | - evil_token = payload + b'.' + b64(hasher.finalize()) |
| 249 | + evil_token = payload + b"." + b64(hasher.finalize()) |
251 | 250 |
|
252 | 251 | # Verify and decode the token using the public key. The custom algorithm |
253 | 252 | # field is left unspecified. Decoding using a public key should be |
|
0 commit comments