Commit ccee4027 authored by Simo Sorce's avatar Simo Sorce

Minor style fixes

Streamline some of the code by using better python
conventions/style/syntax/constricts

Resolves #61
Resolves #62
Resolves #63
Signed-off-by: 's avatarSimo Sorce <simo@redhat.com>
Reviewed-by: 's avatarChristian Heimes <cheimes@redhat.com>
Closes #64
parent f7edc391
......@@ -40,7 +40,11 @@ def json_decode(string):
return json.loads(string)
class InvalidJWAAlgorithm(Exception):
class JWException(Exception):
pass
class InvalidJWAAlgorithm(JWException):
def __init__(self, message=None):
msg = 'Invalid JWA Algorithm name'
if message:
......@@ -48,7 +52,7 @@ class InvalidJWAAlgorithm(Exception):
super(InvalidJWAAlgorithm, self).__init__(msg)
class InvalidCEKeyLength(Exception):
class InvalidCEKeyLength(JWException):
"""Invalid CEK Key Length.
This exception is raised when a Content Encryption Key does not match
......@@ -60,7 +64,7 @@ class InvalidCEKeyLength(Exception):
super(InvalidCEKeyLength, self).__init__(msg)
class InvalidJWEOperation(Exception):
class InvalidJWEOperation(JWException):
"""Invalid JWS Object.
This exception is raised when a requested operation cannot
......@@ -78,7 +82,7 @@ class InvalidJWEOperation(Exception):
super(InvalidJWEOperation, self).__init__(msg)
class InvalidJWEKeyType(Exception):
class InvalidJWEKeyType(JWException):
"""Invalid JWE Key Type.
This exception is raised when the provided JWK Key does not match
......@@ -90,7 +94,7 @@ class InvalidJWEKeyType(Exception):
super(InvalidJWEKeyType, self).__init__(msg)
class InvalidJWEKeyLength(Exception):
class InvalidJWEKeyLength(JWException):
"""Invalid JWE Key Length.
This exception is raised when the provided JWK Key does not match
......
......@@ -73,16 +73,11 @@ def _randombits(x):
return os.urandom(_inbytes(x))
# Note: l is the number of bits, which should be a multiple of 16
def _encode_int(n, l):
e = hex(n).rstrip("L").lstrip("0x")
elen = len(e)
ilen = ((l + 7) // 8) * 2 # number of bytes rounded up times 2 chars/bytes
if elen > ilen:
e = e[:ilen]
else:
e = '0' * (ilen - elen) + e # pad as necessary
return unhexlify(e)
# Note: the number of bits should be a multiple of 16
def _encode_int(n, bits):
e = '{:x}'.format(n)
ilen = ((bits + 7) // 8) * 2 # number of bytes rounded up times 2 bytes
return unhexlify(e.rjust(ilen, '0')[:ilen])
def _decode_int(n):
......
......@@ -243,11 +243,9 @@ class JWE(object):
self.objects['recipients'] = list()
n = dict()
if 'encrypted_key' in self.objects:
n['encrypted_key'] = self.objects['encrypted_key']
del self.objects['encrypted_key']
n['encrypted_key'] = self.objects.pop('encrypted_key')
if 'header' in self.objects:
n['header'] = self.objects['header']
del self.objects['header']
n['header'] = self.objects.pop('header')
self.objects['recipients'].append(n)
self.objects['recipients'].append(rec)
else:
......
......@@ -219,8 +219,7 @@ class JWK(object):
def generate_key(self, **params):
try:
kty = params['generate']
del params['generate']
kty = params.pop('generate')
gen = getattr(self, '_generate_%s' % kty)
except (KeyError, AttributeError):
raise InvalidJWKType(kty)
......@@ -230,8 +229,7 @@ class JWK(object):
def _get_gen_size(self, params, default_size=None):
size = default_size
if 'size' in params:
size = params['size']
del params['size']
size = params.pop('size')
elif 'alg' in params:
try:
from jwcrypto.jwa import JWA
......@@ -256,8 +254,7 @@ class JWK(object):
pubexp = 65537
size = self._get_gen_size(params, 2048)
if 'public_exponent' in params:
pubexp = params['public_exponent']
del params['public_exponent']
pubexp = params.pop('public_exponent')
key = rsa.generate_private_key(pubexp, size, default_backend())
self._import_pyca_pri_rsa(key, **params)
......@@ -298,13 +295,11 @@ class JWK(object):
def _generate_EC(self, params):
curve = 'P-256'
if 'curve' in params:
curve = params['curve']
del params['curve']
curve = params.pop('curve')
# 'curve' is for backwards compat, if 'crv' is defined it takes
# precedence
if 'crv' in params:
curve = params['crv']
del params['crv']
curve = params.pop('crv')
curve_name = self._get_curve_by_name(curve)
key = ec.generate_private_key(curve_name, default_backend())
self._import_pyca_pri_ec(key, **params)
......
......@@ -405,17 +405,13 @@ class JWS(object):
elif 'signature' in self.objects:
self.objects['signatures'] = list()
n = dict()
n['signature'] = self.objects['signature']
del self.objects['signature']
n['signature'] = self.objects.pop('signature')
if 'protected' in self.objects:
n['protected'] = self.objects['protected']
del self.objects['protected']
n['protected'] = self.objects.pop('protected')
if 'header' in self.objects:
n['header'] = self.objects['header']
del self.objects['header']
n['header'] = self.objects.pop('header')
if 'valid' in self.objects:
n['valid'] = self.objects['valid']
del self.objects['valid']
n['valid'] = self.objects.pop('valid')
self.objects['signatures'].append(n)
self.objects['signatures'].append(o)
else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment