Skip to content

Commit 98e0bea

Browse files
author
Marko Mrdjenovic
committed
make algorithms extendable
1 parent 8556fc2 commit 98e0bea

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

jose/constants.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22

3-
class ALGORITHMS(object):
3+
4+
class Algorithms(object):
45
NONE = 'none'
56
HS256 = 'HS256'
67
HS384 = 'HS384'
@@ -12,13 +13,13 @@ class ALGORITHMS(object):
1213
ES384 = 'ES384'
1314
ES512 = 'ES512'
1415

15-
HMAC = (HS256, HS384, HS512)
16-
RSA = (RS256, RS384, RS512)
17-
EC = (ES256, ES384, ES512)
16+
HMAC = set([HS256, HS384, HS512])
17+
RSA = set([RS256, RS384, RS512])
18+
EC = set([ES256, ES384, ES512])
1819

19-
SUPPORTED = HMAC + RSA + EC
20+
SUPPORTED = HMAC.union(RSA).union(EC)
2021

21-
ALL = SUPPORTED + (NONE, )
22+
ALL = SUPPORTED.union([NONE])
2223

2324
HASHES = {
2425
HS256: hashlib.sha256,
@@ -31,3 +32,22 @@ class ALGORITHMS(object):
3132
ES384: hashlib.sha384,
3233
ES512: hashlib.sha512,
3334
}
35+
36+
KEYS = {}
37+
38+
def get_key(self, algorithm):
39+
from jose.jwk import HMACKey, RSAKey, ECKey
40+
if algorithm in self.KEYS:
41+
return self.KEYS[algorithm]
42+
elif algorithm in self.HMAC:
43+
return HMACKey
44+
elif algorithm in self.RSA:
45+
return RSAKey
46+
elif algorithm in self.EC:
47+
return ECKey
48+
49+
def register_key(self, algorithm, key_class):
50+
self.KEYS[algorithm] = key_class
51+
self.SUPPORTED.add(algorithm)
52+
53+
ALGORITHMS = Algorithms()

jose/jwk.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,10 @@ def construct(key_data, algorithm=None):
6060
if not algorithm:
6161
raise JWKError('Unable to find a algorithm for key: %s' % key_data)
6262

63-
if algorithm in ALGORITHMS.HMAC:
64-
return HMACKey(key_data, algorithm)
65-
66-
if algorithm in ALGORITHMS.RSA:
67-
return RSAKey(key_data, algorithm)
68-
69-
if algorithm in ALGORITHMS.EC:
70-
return ECKey(key_data, algorithm)
63+
key_class = ALGORITHMS.get_key(algorithm)
64+
if not key_class:
65+
raise JWKError('Unable to find a algorithm for key: %s' % key_data)
66+
return key_class(key_data, algorithm)
7167

7268

7369
def get_algorithm_object(algorithm):
@@ -112,13 +108,12 @@ class HMACKey(Key):
112108
SHA256 = hashlib.sha256
113109
SHA384 = hashlib.sha384
114110
SHA512 = hashlib.sha512
115-
valid_hash_algs = ALGORITHMS.HMAC
116111

117112
prepared_key = None
118113
hash_alg = None
119114

120115
def __init__(self, key, algorithm):
121-
if algorithm not in self.valid_hash_algs:
116+
if algorithm not in ALGORITHMS.HMAC:
122117
raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm)
123118
self.hash_alg = get_algorithm_object(algorithm)
124119

@@ -174,14 +169,13 @@ class RSAKey(Key):
174169
SHA256 = Crypto.Hash.SHA256
175170
SHA384 = Crypto.Hash.SHA384
176171
SHA512 = Crypto.Hash.SHA512
177-
valid_hash_algs = ALGORITHMS.RSA
178172

179173
prepared_key = None
180174
hash_alg = None
181175

182176
def __init__(self, key, algorithm):
183177

184-
if algorithm not in self.valid_hash_algs:
178+
if algorithm not in ALGORITHMS.RSA:
185179
raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm)
186180
self.hash_alg = get_algorithm_object(algorithm)
187181

@@ -257,7 +251,6 @@ class ECKey(Key):
257251
SHA256 = hashlib.sha256
258252
SHA384 = hashlib.sha384
259253
SHA512 = hashlib.sha512
260-
valid_hash_algs = ALGORITHMS.EC
261254

262255
curve_map = {
263256
SHA256: ecdsa.curves.NIST256p,
@@ -270,7 +263,7 @@ class ECKey(Key):
270263
curve = None
271264

272265
def __init__(self, key, algorithm):
273-
if algorithm not in self.valid_hash_algs:
266+
if algorithm not in ALGORITHMS.EC:
274267
raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm)
275268
self.hash_alg = get_algorithm_object(algorithm)
276269

0 commit comments

Comments
 (0)