Skip to content

Commit 838e53a

Browse files
committed
Fixed linting errors
1 parent 1e5d144 commit 838e53a

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

jose/backends/cryptography_backend.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
from ..constants import ALGORITHMS
1818
from ..exceptions import JWEError, JWKError
19-
from ..utils import base64_to_long, base64url_decode, base64url_encode, ensure_binary, long_to_base64, is_pem_format, is_ssh_key
19+
from ..utils import (
20+
base64_to_long,
21+
base64url_decode,
22+
base64url_encode,
23+
ensure_binary,
24+
is_pem_format,
25+
is_ssh_key,
26+
long_to_base64,
27+
)
2028
from .base import Key
2129

2230
_binding = None

jose/utils.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def ensure_binary(s):
110110

111111
# The following was copied from PyJWT:
112112
# https://github.com/jpadilla/pyjwt/commit/9c528670c455b8d948aff95ed50e22940d1ad3fc
113-
# Based on https://github.com/hynek/pem/blob/7ad94db26b0bc21d10953f5dbad3acfdfacf57aa/src/pem/_core.py#L224-L252
113+
# Based on:
114+
# https://github.com/hynek/pem/blob/7ad94db26b0bc21d10953f5dbad3acfdfacf57aa/src/pem/_core.py#L224-L252
114115
_PEMS = {
115116
b"CERTIFICATE",
116117
b"TRUSTED CERTIFICATE",
@@ -130,15 +131,19 @@ def ensure_binary(s):
130131
b"X509 CRL",
131132
}
132133
_PEM_RE = re.compile(
133-
b"----[- ]BEGIN ("
134-
+ b"|".join(re.escape(pem) for pem in _PEMS)
135-
+ b")[- ]----",
134+
b"----[- ]BEGIN (" + b"|".join(re.escape(pem) for pem in _PEMS) + b")[- ]----",
136135
)
136+
137+
137138
def is_pem_format(key: bytes) -> bool:
138139
return bool(_PEM_RE.search(key))
139-
# Based on https://github.com/pyca/cryptography/blob/bcb70852d577b3f490f015378c75cba74986297b/src/cryptography/hazmat/primitives/serialization/ssh.py#L40-L46
140+
141+
142+
# Based on
143+
# https://github.com/pyca/cryptography/blob/bcb70852d577b3f490f015378c75cba74986297b
144+
# /src/cryptography/hazmat/primitives/serialization/ssh.py#L40-L46
140145
_CERT_SUFFIX = b"[email protected]"
141-
_SSH_PUBKEY_RC = re.compile(br"\A(\S+)[ \t]+(\S+)")
146+
_SSH_PUBKEY_RC = re.compile(rb"\A(\S+)[ \t]+(\S+)")
142147
_SSH_KEY_FORMATS = [
143148
b"ssh-ed25519",
144149
b"ssh-rsa",
@@ -147,6 +152,8 @@ def is_pem_format(key: bytes) -> bool:
147152
b"ecdsa-sha2-nistp384",
148153
b"ecdsa-sha2-nistp521",
149154
]
155+
156+
150157
def is_ssh_key(key: bytes) -> bool:
151158
if any(string_value in key for string_value in _SSH_KEY_FORMATS):
152159
return True

tests/algorithms/test_EC.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import base64
12
import json
23
import re
34

5+
from jose import jwt
46
from jose.backends import ECKey
57
from jose.constants import ALGORITHMS
68
from jose.exceptions import JOSEError, JWKError
@@ -14,9 +16,11 @@
1416

1517
try:
1618
from cryptography.hazmat.backends import default_backend as CryptographyBackend
19+
from cryptography.hazmat.primitives import hashes, hmac, serialization
1720
from cryptography.hazmat.primitives.asymmetric import ec as CryptographyEc
1821

1922
from jose.backends.cryptography_backend import CryptographyECKey
23+
2024
except ImportError:
2125
CryptographyECKey = CryptographyEc = CryptographyBackend = None
2226

@@ -228,26 +232,21 @@ def test_to_dict(self):
228232
@pytest.mark.cryptography
229233
@pytest.mark.skipif(CryptographyECKey is None, reason="pyca/cryptography backend not available")
230234
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-
236235
def b64(x):
237-
return base64.urlsafe_b64encode(x).replace(b'=', b'')
236+
return base64.urlsafe_b64encode(x).replace(b"=", b"")
238237

239238
KEY = CryptographyEc.generate_private_key(CryptographyEc.SECP256R1)
240239
PUBKEY = KEY.public_key().public_bytes(
241240
encoding=serialization.Encoding.OpenSSH,
242-
format=serialization.PublicFormat.OpenSSH
241+
format=serialization.PublicFormat.OpenSSH,
243242
)
244243

245244
# Create and sign the payload using a public key, but specify the "alg" in
246245
# 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}')
248247
hasher = hmac.HMAC(PUBKEY, hashes.SHA256())
249248
hasher.update(payload)
250-
evil_token = payload + b'.' + b64(hasher.finalize())
249+
evil_token = payload + b"." + b64(hasher.finalize())
251250

252251
# Verify and decode the token using the public key. The custom algorithm
253252
# field is left unspecified. Decoding using a public key should be

0 commit comments

Comments
 (0)