File tree Expand file tree Collapse file tree 4 files changed +32
-30
lines changed
Expand file tree Collapse file tree 4 files changed +32
-30
lines changed Original file line number Diff line number Diff line change @@ -35,25 +35,13 @@ class Algorithms(object):
3535
3636 KEYS = {}
3737
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- return None
49-
5038 def register_key (self , algorithm , key_class ):
5139 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
40+ if not issubclass (key_class , Key ):
41+ raise TypeError ( "Key class not a subclass of jwk.Key" )
42+ self .KEYS [ algorithm ] = key_class
43+ self . SUPPORTED . add ( algorithm )
44+ return True
45+
5846
5947ALGORITHMS = Algorithms ()
Original file line number Diff line number Diff line change @@ -47,6 +47,18 @@ def base64_to_long(data):
4747 return int_arr_to_long (struct .unpack ('%sB' % len (_d ), _d ))
4848
4949
50+ def get_key (algorithm ):
51+ if algorithm in ALGORITHMS .KEYS :
52+ return ALGORITHMS .KEYS [algorithm ]
53+ elif algorithm in ALGORITHMS .HMAC :
54+ return HMACKey
55+ elif algorithm in ALGORITHMS .RSA :
56+ return RSAKey
57+ elif algorithm in ALGORITHMS .EC :
58+ return ECKey
59+ return None
60+
61+
5062def construct (key_data , algorithm = None ):
5163 """
5264 Construct a Key object for the given algorithm with the given
@@ -60,7 +72,7 @@ def construct(key_data, algorithm=None):
6072 if not algorithm :
6173 raise JWKError ('Unable to find a algorithm for key: %s' % key_data )
6274
63- key_class = ALGORITHMS . get_key (algorithm )
75+ key_class = get_key (algorithm )
6476 if not key_class :
6577 raise JWKError ('Unable to find a algorithm for key: %s' % key_data )
6678 return key_class (key_data , algorithm )
Original file line number Diff line number Diff line change @@ -23,15 +23,10 @@ def test_verify_is_interface(self, alg):
2323
2424class TestAlgorithms :
2525
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-
3326 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
27+ assert ALGORITHMS .register_key ("ALG" , Key )
28+ from jose .jwk import get_key
29+ assert get_key ("ALG" ) == Key
30+
31+ with pytest .raises (TypeError ):
32+ assert ALGORITHMS .register_key ("ALG" , object )
Original file line number Diff line number Diff line change @@ -115,3 +115,10 @@ def test_construct_from_jwk_missing_alg(self):
115115
116116 with pytest .raises (JWKError ):
117117 key = jwk .construct ("key" , algorithm = "NONEXISTENT" )
118+
119+ def test_get_key (self ):
120+ assert jwk .get_key ("HS256" ) == jwk .HMACKey
121+ assert jwk .get_key ("RS256" ) == jwk .RSAKey
122+ assert jwk .get_key ("ES256" ) == jwk .ECKey
123+
124+ assert jwk .get_key ("NONEXISTENT" ) == None
You can’t perform that action at this time.
0 commit comments