forked from jpadilla/pyjwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_jwk.py
More file actions
72 lines (52 loc) · 1.83 KB
/
api_jwk.py
File metadata and controls
72 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
from .algorithms import get_default_algorithms
from .exceptions import PyJWKError, PyJWKSetError
class PyJWK(object):
def __init__(self, jwk_data, algorithm=None):
self._algorithms = get_default_algorithms()
self._jwk_data = jwk_data
if not algorithm and isinstance(self._jwk_data, dict):
algorithm = self._jwk_data.get("alg", None)
if not algorithm:
raise PyJWKError(
"Unable to find a algorithm for key: %s" % self._jwk_data
)
self.Algorithm = self._algorithms.get(algorithm)
if not self.Algorithm:
raise PyJWKError(
"Unable to find a algorithm for key: %s" % self._jwk_data
)
self.key = self.Algorithm.from_jwk(self._jwk_data)
@staticmethod
def from_dict(obj, algorithm=None):
return PyJWK(obj, algorithm)
@staticmethod
def from_json(data, algorithm=None):
obj = json.loads(data)
return PyJWK.from_dict(obj, algorithm)
@property
def key_type(self):
return self._jwk_data.get("kty", None)
@property
def key_id(self):
return self._jwk_data.get("kid", None)
@property
def public_key_use(self):
return self._jwk_data.get("use", None)
class PyJWKSet(object):
def __init__(self, keys):
self.keys = []
if not keys or not isinstance(keys, list):
raise PyJWKSetError("Invalid JWK Set value")
if len(keys) == 0:
raise PyJWKSetError("The JWK Set did not contain any keys")
for key in keys:
self.keys.append(PyJWK(key))
@staticmethod
def from_dict(obj):
keys = obj.get("keys", [])
return PyJWKSet(keys)
@staticmethod
def from_json(data):
obj = json.loads(data)
return PyJWKSet.from_dict(obj)