Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/jose/jwk.py: 29%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from jose.backends.base import Key
2from jose.constants import ALGORITHMS
3from jose.exceptions import JWKError
5try:
6 from jose.backends import RSAKey # noqa: F401
7except ImportError:
8 pass
10try:
11 from jose.backends import ECKey # noqa: F401
12except ImportError:
13 pass
15try:
16 from jose.backends import AESKey # noqa: F401
17except ImportError:
18 pass
20try:
21 from jose.backends import DIRKey # noqa: F401
22except ImportError:
23 pass
25try:
26 from jose.backends import HMACKey # noqa: F401
27except ImportError:
28 pass
31def get_key(algorithm):
32 if algorithm in ALGORITHMS.KEYS:
33 return ALGORITHMS.KEYS[algorithm]
34 elif algorithm in ALGORITHMS.HMAC: # noqa: F811
35 return HMACKey
36 elif algorithm in ALGORITHMS.RSA:
37 from jose.backends import RSAKey # noqa: F811
39 return RSAKey
40 elif algorithm in ALGORITHMS.EC:
41 from jose.backends import ECKey # noqa: F811
43 return ECKey
44 elif algorithm in ALGORITHMS.AES:
45 from jose.backends import AESKey # noqa: F811
47 return AESKey
48 elif algorithm == ALGORITHMS.DIR:
49 from jose.backends import DIRKey # noqa: F811
51 return DIRKey
52 return None
55def register_key(algorithm, key_class):
56 if not issubclass(key_class, Key):
57 raise TypeError("Key class is not a subclass of jwk.Key")
58 ALGORITHMS.KEYS[algorithm] = key_class
59 ALGORITHMS.SUPPORTED.add(algorithm)
60 return True
63def construct(key_data, algorithm=None):
64 """
65 Construct a Key object for the given algorithm with the given
66 key_data.
67 """
69 # Allow for pulling the algorithm off of the passed in jwk.
70 if not algorithm and isinstance(key_data, dict):
71 algorithm = key_data.get("alg", None)
73 if not algorithm:
74 raise JWKError("Unable to find an algorithm for key")
76 key_class = get_key(algorithm)
77 if not key_class:
78 raise JWKError("Unable to find an algorithm for key")
79 return key_class(key_data, algorithm)