Skip to content

Commit 072cf8b

Browse files
author
Marko Mrdjenovic
committed
cleaned jwk, added class checking on key, added tests
1 parent 98e0bea commit 072cf8b

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

jose/constants.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ def get_key(self, algorithm):
4545
return RSAKey
4646
elif algorithm in self.EC:
4747
return ECKey
48+
return None
4849

4950
def register_key(self, algorithm, key_class):
50-
self.KEYS[algorithm] = key_class
51-
self.SUPPORTED.add(algorithm)
51+
from jose.jwk import Key
52+
if issubclass(key_class, Key):
53+
self.KEYS[algorithm] = key_class
54+
self.SUPPORTED.add(algorithm)
55+
return True
56+
else:
57+
return False
5258

5359
ALGORITHMS = Algorithms()

jose/jwk.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ class Key(object):
8787
"""
8888
A simple interface for implementing JWK keys.
8989
"""
90-
prepared_key = None
91-
hash_alg = None
92-
93-
def _process_jwk(self, jwk_dict):
94-
raise NotImplementedError()
90+
def __init__(self, key, algorithm):
91+
pass
9592

9693
def sign(self, msg):
9794
raise NotImplementedError()
@@ -109,9 +106,6 @@ class HMACKey(Key):
109106
SHA384 = hashlib.sha384
110107
SHA512 = hashlib.sha512
111108

112-
prepared_key = None
113-
hash_alg = None
114-
115109
def __init__(self, key, algorithm):
116110
if algorithm not in ALGORITHMS.HMAC:
117111
raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm)
@@ -170,9 +164,6 @@ class RSAKey(Key):
170164
SHA384 = Crypto.Hash.SHA384
171165
SHA512 = Crypto.Hash.SHA512
172166

173-
prepared_key = None
174-
hash_alg = None
175-
176167
def __init__(self, key, algorithm):
177168

178169
if algorithm not in ALGORITHMS.RSA:
@@ -236,7 +227,7 @@ def verify(self, msg, sig):
236227
try:
237228
return PKCS1_v1_5.new(self.prepared_key).verify(self.hash_alg.new(msg), sig)
238229
except Exception as e:
239-
raise JWKError(e)
230+
return False
240231

241232

242233
class ECKey(Key):
@@ -252,22 +243,18 @@ class ECKey(Key):
252243
SHA384 = hashlib.sha384
253244
SHA512 = hashlib.sha512
254245

255-
curve_map = {
246+
CURVE_MAP = {
256247
SHA256: ecdsa.curves.NIST256p,
257248
SHA384: ecdsa.curves.NIST384p,
258249
SHA512: ecdsa.curves.NIST521p,
259250
}
260251

261-
prepared_key = None
262-
hash_alg = None
263-
curve = None
264-
265252
def __init__(self, key, algorithm):
266253
if algorithm not in ALGORITHMS.EC:
267254
raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm)
268255
self.hash_alg = get_algorithm_object(algorithm)
269256

270-
self.curve = self.curve_map.get(self.hash_alg)
257+
self.curve = self.CURVE_MAP.get(self.hash_alg)
271258

272259
if isinstance(key, (ecdsa.SigningKey, ecdsa.VerifyingKey)):
273260
self.prepared_key = key

tests/algorithms/test_base.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11

2-
# from jose.jwk import Key
3-
# from jose.exceptions import JOSEError
2+
from jose.jwk import Key, HMACKey, RSAKey, ECKey
3+
from jose.constants import ALGORITHMS
44

5-
# import pytest
5+
import pytest
66

77

8-
# @pytest.fixture
9-
# def alg():
10-
# return Key()
8+
@pytest.fixture
9+
def alg():
10+
return Key("key", "ALG")
1111

1212

13-
# class TestBaseAlgorithm:
13+
class TestBaseAlgorithm:
1414

15-
# def test_prepare_key_is_interface(self, alg):
16-
# with pytest.raises(JOSEError):
17-
# alg.prepare_key('secret')
15+
def test_sign_is_interface(self, alg):
16+
with pytest.raises(NotImplementedError):
17+
alg.sign('msg')
1818

19-
# def test_sign_is_interface(self, alg):
20-
# with pytest.raises(JOSEError):
21-
# alg.sign('msg', 'secret')
19+
def test_verify_is_interface(self, alg):
20+
with pytest.raises(NotImplementedError):
21+
alg.verify('msg', 'sig')
2222

23-
# def test_verify_is_interface(self, alg):
24-
# with pytest.raises(JOSEError):
25-
# alg.verify('msg', 'secret', 'sig')
23+
24+
class TestAlgorithms:
25+
26+
def test_get_key(self):
27+
assert ALGORITHMS.get_key("HS256") == HMACKey
28+
assert ALGORITHMS.get_key("RS256") == RSAKey
29+
assert ALGORITHMS.get_key("ES256") == ECKey
30+
31+
assert ALGORITHMS.get_key("NONEXISTENT") == None
32+
33+
def test_register_key(self):
34+
assert ALGORITHMS.register_key("ALG", Key) == True
35+
assert ALGORITHMS.get_key("ALG") == Key
36+
37+
assert ALGORITHMS.register_key("ALG", object) == False

tests/test_jwk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ class TestJWK:
3535

3636
def test_interface(self):
3737

38-
key = jwk.Key()
39-
40-
with pytest.raises(NotImplementedError):
41-
key._process_jwk(None)
38+
key = jwk.Key("key", "ALG")
4239

4340
with pytest.raises(NotImplementedError):
4441
key.sign('')
@@ -115,3 +112,6 @@ def test_construct_from_jwk_missing_alg(self):
115112

116113
with pytest.raises(JWKError):
117114
key = jwk.construct(hmac_key)
115+
116+
with pytest.raises(JWKError):
117+
key = jwk.construct("key", algorithm="NONEXISTENT")

0 commit comments

Comments
 (0)