Skip to content

Commit

Permalink
Fix all flake8 issues except for E501 (line too long)
Browse files Browse the repository at this point in the history
  • Loading branch information
blag committed Jul 9, 2018
1 parent 6b3d557 commit 70faf31
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 79 deletions.
8 changes: 4 additions & 4 deletions jose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__copyright__ = 'Copyright 2016 Michael Davis'


from .exceptions import JOSEError
from .exceptions import JWSError
from .exceptions import ExpiredSignatureError
from .exceptions import JWTError
from .exceptions import JOSEError # noqa: F401
from .exceptions import JWSError # noqa: F401
from .exceptions import ExpiredSignatureError # noqa: F401
from .exceptions import JWTError # noqa: F401
10 changes: 5 additions & 5 deletions jose/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

try:
from jose.backends.pycrypto_backend import RSAKey
from jose.backends.pycrypto_backend import RSAKey # noqa: F401
except ImportError:
try:
from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey
from jose.backends.cryptography_backend import CryptographyRSAKey as RSAKey # noqa: F401
except ImportError:
from jose.backends.rsa_backend import RSAKey
from jose.backends.rsa_backend import RSAKey # noqa: F401

try:
from jose.backends.cryptography_backend import CryptographyECKey as ECKey
from jose.backends.cryptography_backend import CryptographyECKey as ECKey # noqa: F401
except ImportError:
from jose.backends.ecdsa_backend import ECDSAECKey as ECKey
from jose.backends.ecdsa_backend import ECDSAECKey as ECKey # noqa: F401
4 changes: 2 additions & 2 deletions jose/backends/cryptography_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def sign(self, msg):
if self.hash_alg.digest_size * 8 > self.prepared_key.curve.key_size:
raise TypeError("this curve (%s) is too short "
"for your digest (%d)" % (self.prepared_key.curve.name,
8*self.hash_alg.digest_size))
8 * self.hash_alg.digest_size))
signature = self.prepared_key.sign(msg, ec.ECDSA(self.hash_alg()))
order = (2 ** self.prepared_key.curve.key_size) - 1
return sigencode_string(*sigdecode_der(signature, order), order=order)
Expand All @@ -104,7 +104,7 @@ def verify(self, msg, sig):
try:
self.prepared_key.verify(signature, msg, ec.ECDSA(self.hash_alg()))
return True
except:
except Exception:
return False

def is_public(self):
Expand Down
2 changes: 1 addition & 1 deletion jose/backends/ecdsa_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def sign(self, msg):
def verify(self, msg, sig):
try:
return self.prepared_key.verify(sig, msg, hashfunc=self.hash_alg, sigdecode=ecdsa.util.sigdecode_string)
except:
except Exception:
return False

def is_public(self):
Expand Down
1 change: 0 additions & 1 deletion jose/backends/pycrypto_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
_RSAKey = RSA.RsaKey
else:
_RSAKey = RSA._RSAobj



class RSAKey(Key):
Expand Down
7 changes: 3 additions & 4 deletions jose/backends/rsa_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
# to enable pure python rsa module to be in compliance with section 6.3.1 of RFC7518
# which requires only private exponent (d) for private key.


def _gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
a, b = b, (a % b)
return a


Expand Down Expand Up @@ -138,7 +139,7 @@ def _process_jwk(self, jwk_dict):
e = base64_to_long(jwk_dict.get('e'))
n = base64_to_long(jwk_dict.get('n'))

if not 'd' in jwk_dict:
if 'd' not in jwk_dict:
return pyrsa.PublicKey(e=e, n=n)
else:
d = base64_to_long(jwk_dict.get('d'))
Expand All @@ -159,8 +160,6 @@ def _process_jwk(self, jwk_dict):
p, q = _rsa_recover_prime_factors(n, e, d)
return pyrsa.PrivateKey(n=n, e=e, d=d, p=p, q=q)



def sign(self, msg):
return pyrsa.sign(msg, self._prepared_key, self.hash_alg)

Expand Down
8 changes: 4 additions & 4 deletions jose/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from jose.backends.base import Key

try:
from jose.backends import RSAKey
from jose.backends import RSAKey # noqa: F401
except ImportError:
pass

try:
from jose.backends import ECKey
from jose.backends import ECKey # noqa: F401
except ImportError:
pass

Expand All @@ -26,10 +26,10 @@ def get_key(algorithm):
elif algorithm in ALGORITHMS.HMAC:
return HMACKey
elif algorithm in ALGORITHMS.RSA:
from jose.backends import RSAKey
from jose.backends import RSAKey # noqa: F811
return RSAKey
elif algorithm in ALGORITHMS.EC:
from jose.backends import ECKey
from jose.backends import ECKey # noqa: F811
return ECKey
return None

Expand Down
28 changes: 14 additions & 14 deletions jose/jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _sig_matches_keys(keys, signing_input, signature, alg):
try:
if key.verify(signing_input, signature):
return True
except:
except Exception:
pass
return False

Expand Down Expand Up @@ -250,18 +250,18 @@ def _get_keys(key):

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

alg = header.get('alg')
if not alg:
raise JWSError('No algorithm was specified in the JWS header.')
alg = header.get('alg')
if not alg:
raise JWSError('No algorithm was specified in the JWS header.')

if algorithms is not None and alg not in algorithms:
raise JWSError('The specified alg value is not allowed')
if algorithms is not None and alg not in algorithms:
raise JWSError('The specified alg value is not allowed')

keys = _get_keys(key)
try:
if not _sig_matches_keys(keys, signing_input, signature, alg):
raise JWSSignatureError()
except JWSSignatureError:
raise JWSError('Signature verification failed.')
except JWSError:
raise JWSError('Invalid or unsupported algorithm: %s' % alg)
keys = _get_keys(key)
try:
if not _sig_matches_keys(keys, signing_input, signature, alg):
raise JWSSignatureError()
except JWSSignatureError:
raise JWSError('Signature verification failed.')
except JWSError:
raise JWSError('Invalid or unsupported algorithm: %s' % alg)
8 changes: 4 additions & 4 deletions jose/jwt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import binascii
import json

from calendar import timegm
Expand Down Expand Up @@ -168,7 +167,7 @@ def get_unverified_header(token):
"""
try:
headers = jws.get_unverified_headers(token)
except:
except Exception:
raise JWTError('Error decoding token headers.')

return headers
Expand Down Expand Up @@ -206,7 +205,7 @@ def get_unverified_claims(token):
"""
try:
claims = jws.get_unverified_claims(token)
except:
except Exception:
raise JWTError('Error decoding token claims.')

try:
Expand Down Expand Up @@ -384,6 +383,7 @@ def _validate_sub(claims, subject=None):
if claims.get('sub') != subject:
raise JWTClaimsError('Invalid subject')


def _validate_jti(claims):
"""Validates that the 'jti' claim is valid.
Expand Down Expand Up @@ -433,7 +433,7 @@ def _validate_at_hash(claims, access_token, algorithm):
except (TypeError, ValueError):
msg = 'Unable to calculate at_hash to verify against token claims.'
raise JWTClaimsError(msg)

if claims['at_hash'] != expected_hash:
raise JWTClaimsError('at_hash claim does not match access_token.')

Expand Down
4 changes: 2 additions & 2 deletions tests/algorithms/test_EC.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def test_verify(self, Backend):
signature = key.sign(msg)
public_key = key.public_key()

assert public_key.verify(msg, signature) == True
assert public_key.verify(msg, b'not a signature') == False
assert bool(public_key.verify(msg, signature))
assert not bool(public_key.verify(msg, b'not a signature'))

def assert_parameters(self, as_dict, private):
assert isinstance(as_dict, dict)
Expand Down
13 changes: 3 additions & 10 deletions tests/algorithms/test_RSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_cryptography_RSA_key_instance(self):
key = rsa.RSAPublicNumbers(
long(65537),
long(26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819),
).public_key(default_backend())
).public_key(default_backend())

pubkey = CryptographyRSAKey(key, ALGORITHMS.RS256)
assert pubkey.is_public()
Expand Down Expand Up @@ -158,12 +158,6 @@ def test_RSA_jwk(self, Backend):
# None of the extra parameters are present, but 'key' is still private.
assert not Backend(key, ALGORITHMS.RS256).is_public()

@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
def test_string_secret(self, Backend):
key = 'secret'
with pytest.raises(JOSEError):
Backend(key, ALGORITHMS.RS256)

@pytest.mark.parametrize("Backend", [RSAKey, CryptographyRSAKey, PurePythonRSAKey])
def test_get_public_key(self, Backend):
key = Backend(private_key, ALGORITHMS.RS256)
Expand Down Expand Up @@ -262,6 +256,5 @@ def test_pycrypto_unencoded_cleartext(self, Backend):
signature = key.sign(msg)
public_key = key.public_key()

assert public_key.verify(msg, signature) == True
assert public_key.verify(msg, 1) == False

assert bool(public_key.verify(msg, signature))
assert not bool(public_key.verify(msg, 1))
1 change: 0 additions & 1 deletion tests/algorithms/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ def test_sign_is_interface(self, alg):
def test_verify_is_interface(self, alg):
with pytest.raises(NotImplementedError):
alg.verify('msg', 'sig')

6 changes: 3 additions & 3 deletions tests/rfc/test_rfc7520.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from jose.jws import verify

import pytest
# Disable flake8 reporting
# flake8: noqa

from jose.jws import verify

expected_payload = b"It\xe2\x80\x99s a dangerous business, Frodo, going out your door. You step onto the road, and if you don't keep your feet, there\xe2\x80\x99s no knowing where you might be swept off to."

Expand Down
12 changes: 6 additions & 6 deletions tests/test_jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from jose.exceptions import JWKError
from jose.backends.base import Key
from jose.backends.pycrypto_backend import RSAKey
from jose.backends.cryptography_backend import CryptographyECKey, CryptographyRSAKey
from jose.backends.cryptography_backend import CryptographyECKey
from jose.backends.ecdsa_backend import ECDSAECKey

import pytest
Expand Down Expand Up @@ -53,7 +53,7 @@ def test_invalid_hash_alg(self):
key = RSAKey(rsa_key, 'HS512')

with pytest.raises(JWKError):
key = ECDSAECKey(ec_key, 'RS512')
key = ECDSAECKey(ec_key, 'RS512') # noqa: F841

def test_invalid_jwk(self):

Expand All @@ -64,7 +64,7 @@ def test_invalid_jwk(self):
key = RSAKey(hmac_key, 'RS256')

with pytest.raises(JWKError):
key = ECDSAECKey(rsa_key, 'ES256')
key = ECDSAECKey(rsa_key, 'ES256') # noqa: F841

def test_RSAKey_errors(self):

Expand All @@ -88,7 +88,7 @@ def test_RSAKey_errors(self):
}

with pytest.raises(JWKError):
key = RSAKey(rsa_key, 'RS256')
key = RSAKey(rsa_key, 'RS256') # noqa: F841

def test_construct_from_jwk(self):

Expand Down Expand Up @@ -122,7 +122,7 @@ def test_construct_from_jwk_missing_alg(self):
key = jwk.construct(hmac_key)

with pytest.raises(JWKError):
key = jwk.construct("key", algorithm="NONEXISTENT")
key = jwk.construct("key", algorithm="NONEXISTENT") # noqa: F841

def test_get_key(self):
hs_key = jwk.get_key("HS256")
Expand All @@ -131,7 +131,7 @@ def test_get_key(self):
assert issubclass(jwk.get_key("RS256"), Key)
assert issubclass(jwk.get_key("ES256"), Key)

assert jwk.get_key("NONEXISTENT") == None
assert jwk.get_key("NONEXISTENT") is None

def test_register_key(self):
assert jwk.register_key("ALG", jwk.Key)
Expand Down
Loading

0 comments on commit 70faf31

Please sign in to comment.