Skip to content

Commit 8339a19

Browse files
authored
Merge pull request #101 from blag/flake8-fixes
Fix all flake8 issues except for E501 (line too long)
2 parents 7ff8122 + 70faf31 commit 8339a19

File tree

16 files changed

+157
-79
lines changed

16 files changed

+157
-79
lines changed

jose/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
__copyright__ = 'Copyright 2016 Michael Davis'
66

77

8-
from .exceptions import JOSEError
9-
from .exceptions import JWSError
10-
from .exceptions import ExpiredSignatureError
11-
from .exceptions import JWTError
8+
from .exceptions import JOSEError # noqa: F401
9+
from .exceptions import JWSError # noqa: F401
10+
from .exceptions import ExpiredSignatureError # noqa: F401
11+
from .exceptions import JWTError # noqa: F401

jose/backends/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

22
try:
3-
from jose.backends.pycrypto_backend import RSAKey
3+
from jose.backends.pycrypto_backend import RSAKey # noqa: F401
44
except ImportError:
55
try:
6-
from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey
6+
from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey # noqa: F401
77
except ImportError:
8-
from jose.backends.rsa_backend import RSAKey
8+
from jose.backends.rsa_backend import RSAKey # noqa: F401
99

1010
try:
11-
from jose.backends.cryptography_backend import CryptographyECKey as ECKey
11+
from jose.backends.cryptography_backend import CryptographyECKey as ECKey # noqa: F401
1212
except ImportError:
13-
from jose.backends.ecdsa_backend import ECDSAECKey as ECKey
13+
from jose.backends.ecdsa_backend import ECDSAECKey as ECKey # noqa: F401

jose/backends/cryptography_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def sign(self, msg):
9393
if self.hash_alg.digest_size * 8 > self.prepared_key.curve.key_size:
9494
raise TypeError("this curve (%s) is too short "
9595
"for your digest (%d)" % (self.prepared_key.curve.name,
96-
8*self.hash_alg.digest_size))
96+
8 * self.hash_alg.digest_size))
9797
signature = self.prepared_key.sign(msg, ec.ECDSA(self.hash_alg()))
9898
order = (2 ** self.prepared_key.curve.key_size) - 1
9999
return sigencode_string(*sigdecode_der(signature, order), order=order)
@@ -104,7 +104,7 @@ def verify(self, msg, sig):
104104
try:
105105
self.prepared_key.verify(signature, msg, ec.ECDSA(self.hash_alg()))
106106
return True
107-
except:
107+
except Exception:
108108
return False
109109

110110
def is_public(self):

jose/backends/ecdsa_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def sign(self, msg):
9696
def verify(self, msg, sig):
9797
try:
9898
return self.prepared_key.verify(sig, msg, hashfunc=self.hash_alg, sigdecode=ecdsa.util.sigdecode_string)
99-
except:
99+
except Exception:
100100
return False
101101

102102
def is_public(self):

jose/backends/pycrypto_backend.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
_RSAKey = RSA.RsaKey
2424
else:
2525
_RSAKey = RSA._RSAobj
26-
2726

2827

2928
class RSAKey(Key):

jose/backends/rsa_backend.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
# to enable pure python rsa module to be in compliance with section 6.3.1 of RFC7518
1818
# which requires only private exponent (d) for private key.
1919

20+
2021
def _gcd(a, b):
2122
"""Calculate the Greatest Common Divisor of a and b.
2223
2324
Unless b==0, the result will have the same sign as b (so that when
2425
b is divided by it, the result comes out positive).
2526
"""
2627
while b:
27-
a, b = b, a%b
28+
a, b = b, (a % b)
2829
return a
2930

3031

@@ -138,7 +139,7 @@ def _process_jwk(self, jwk_dict):
138139
e = base64_to_long(jwk_dict.get('e'))
139140
n = base64_to_long(jwk_dict.get('n'))
140141

141-
if not 'd' in jwk_dict:
142+
if 'd' not in jwk_dict:
142143
return pyrsa.PublicKey(e=e, n=n)
143144
else:
144145
d = base64_to_long(jwk_dict.get('d'))
@@ -159,8 +160,6 @@ def _process_jwk(self, jwk_dict):
159160
p, q = _rsa_recover_prime_factors(n, e, d)
160161
return pyrsa.PrivateKey(n=n, e=e, d=d, p=p, q=q)
161162

162-
163-
164163
def sign(self, msg):
165164
return pyrsa.sign(msg, self._prepared_key, self.hash_alg)
166165

jose/jwk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from jose.backends.base import Key
1111

1212
try:
13-
from jose.backends import RSAKey
13+
from jose.backends import RSAKey # noqa: F401
1414
except ImportError:
1515
pass
1616

1717
try:
18-
from jose.backends import ECKey
18+
from jose.backends import ECKey # noqa: F401
1919
except ImportError:
2020
pass
2121

@@ -26,10 +26,10 @@ def get_key(algorithm):
2626
elif algorithm in ALGORITHMS.HMAC:
2727
return HMACKey
2828
elif algorithm in ALGORITHMS.RSA:
29-
from jose.backends import RSAKey
29+
from jose.backends import RSAKey # noqa: F811
3030
return RSAKey
3131
elif algorithm in ALGORITHMS.EC:
32-
from jose.backends import ECKey
32+
from jose.backends import ECKey # noqa: F811
3333
return ECKey
3434
return None
3535

jose/jws.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _sig_matches_keys(keys, signing_input, signature, alg):
213213
try:
214214
if key.verify(signing_input, signature):
215215
return True
216-
except:
216+
except Exception:
217217
pass
218218
return False
219219

@@ -252,18 +252,18 @@ def _get_keys(key):
252252

253253
def _verify_signature(signing_input, header, signature, key='', algorithms=None):
254254

255-
alg = header.get('alg')
256-
if not alg:
257-
raise JWSError('No algorithm was specified in the JWS header.')
255+
alg = header.get('alg')
256+
if not alg:
257+
raise JWSError('No algorithm was specified in the JWS header.')
258258

259-
if algorithms is not None and alg not in algorithms:
260-
raise JWSError('The specified alg value is not allowed')
259+
if algorithms is not None and alg not in algorithms:
260+
raise JWSError('The specified alg value is not allowed')
261261

262-
keys = _get_keys(key)
263-
try:
264-
if not _sig_matches_keys(keys, signing_input, signature, alg):
265-
raise JWSSignatureError()
266-
except JWSSignatureError:
267-
raise JWSError('Signature verification failed.')
268-
except JWSError:
269-
raise JWSError('Invalid or unsupported algorithm: %s' % alg)
262+
keys = _get_keys(key)
263+
try:
264+
if not _sig_matches_keys(keys, signing_input, signature, alg):
265+
raise JWSSignatureError()
266+
except JWSSignatureError:
267+
raise JWSError('Signature verification failed.')
268+
except JWSError:
269+
raise JWSError('Invalid or unsupported algorithm: %s' % alg)

jose/jwt.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
import binascii
32
import json
43

54
from calendar import timegm
@@ -170,7 +169,7 @@ def get_unverified_header(token):
170169
"""
171170
try:
172171
headers = jws.get_unverified_headers(token)
173-
except:
172+
except Exception:
174173
raise JWTError('Error decoding token headers.')
175174

176175
return headers
@@ -208,7 +207,7 @@ def get_unverified_claims(token):
208207
"""
209208
try:
210209
claims = jws.get_unverified_claims(token)
211-
except:
210+
except Exception:
212211
raise JWTError('Error decoding token claims.')
213212

214213
try:
@@ -386,6 +385,7 @@ def _validate_sub(claims, subject=None):
386385
if claims.get('sub') != subject:
387386
raise JWTClaimsError('Invalid subject')
388387

388+
389389
def _validate_jti(claims):
390390
"""Validates that the 'jti' claim is valid.
391391
@@ -435,7 +435,7 @@ def _validate_at_hash(claims, access_token, algorithm):
435435
except (TypeError, ValueError):
436436
msg = 'Unable to calculate at_hash to verify against token claims.'
437437
raise JWTClaimsError(msg)
438-
438+
439439
if claims['at_hash'] != expected_hash:
440440
raise JWTClaimsError('at_hash claim does not match access_token.')
441441

tests/algorithms/test_EC.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def test_verify(self, Backend):
107107
signature = key.sign(msg)
108108
public_key = key.public_key()
109109

110-
assert public_key.verify(msg, signature) == True
111-
assert public_key.verify(msg, b'not a signature') == False
110+
assert bool(public_key.verify(msg, signature))
111+
assert not bool(public_key.verify(msg, b'not a signature'))
112112

113113
def assert_parameters(self, as_dict, private):
114114
assert isinstance(as_dict, dict)

0 commit comments

Comments
 (0)