Skip to content

Commit bc3853a

Browse files
committed
More specific Exception class
Make sure that all necessary parameters have values when initiating a Key instance.
1 parent f6682ac commit bc3853a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/jwkest/ecc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from math import ceil
1919

2020

21+
class ECCException(Exception):
22+
pass
23+
24+
2125
# Make the EC interface more OO
2226
class NISTEllipticCurve(object):
2327
def __init__(self, bits):
@@ -34,7 +38,7 @@ def by_name(name):
3438
if name == "P-521" or name == b'P-521':
3539
return NISTEllipticCurve(521)
3640
else:
37-
raise Exception("Unknown curve {0}".format(name))
41+
raise ECCException("Unknown curve {0}".format(name))
3842

3943
# Get the name of this curve
4044
# XXX This only works because we only support prime curves right now

src/jwkest/jwk.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,22 +242,22 @@ def __init__(self, kty="", alg="", use="", kid="", key=None, x5c=None,
242242
if isinstance(kty, six.string_types):
243243
self.kty = kty
244244
else:
245-
self.kty = kty.decode("utf8")
245+
self.kty = as_unicode(kty)
246246

247247
if isinstance(alg, six.string_types):
248248
self.alg = alg
249249
else:
250-
self.alg = alg.decode("utf8")
250+
self.alg = as_unicode(alg)
251251

252252
if isinstance(use, six.string_types):
253253
self.use = use
254254
else:
255-
self.use = use.decode("utf8")
255+
self.use = as_unicode(use)
256256

257257
if isinstance(kid, six.string_types):
258258
self.kid = kid
259259
else:
260-
self.kid = kid.decode("utf8")
260+
self.kid = as_unicode(kid)
261261

262262
self.x5c = x5c or []
263263
self.x5t = x5t
@@ -412,6 +412,8 @@ def __init__(self, kty="RSA", alg="", use="", kid="", key=None,
412412
self.deserialize()
413413
elif self.key and not (self.n and self.e):
414414
self._split()
415+
elif not self.key and not(self.n and self.e):
416+
raise DeSerializationNotPossible('Missing required parameter')
415417

416418
def deserialize(self):
417419
if self.n and self.e:
@@ -550,8 +552,12 @@ def __init__(self, kty="EC", alg="", use="", kid="", key=None,
550552
if self.crv and not self.curve:
551553
self.verify()
552554
self.deserialize()
553-
elif self.key and (not self.crv and not self.curve):
554-
self.load_key(key)
555+
elif self.key:
556+
if not self.crv and not self.curve:
557+
self.load_key(key)
558+
else:
559+
if not (self.x and self.y and self.crv):
560+
raise DeSerializationNotPossible('Missing required parameter')
555561

556562
def deserialize(self):
557563
"""

0 commit comments

Comments
 (0)