This class represents a JSON Web Key Set.
| 2 | |
| 3 | |
| 4 | class KeySet: |
| 5 | """This class represents a JSON Web Key Set.""" |
| 6 | |
| 7 | def __init__(self, keys): |
| 8 | self.keys = keys |
| 9 | |
| 10 | def as_dict(self, is_private=False, **params): |
| 11 | """Represent this key as a dict of the JSON Web Key Set.""" |
| 12 | return {"keys": [k.as_dict(is_private, **params) for k in self.keys]} |
| 13 | |
| 14 | def as_json(self, is_private=False, **params): |
| 15 | """Represent this key set as a JSON string.""" |
| 16 | obj = self.as_dict(is_private, **params) |
| 17 | return json_dumps(obj) |
| 18 | |
| 19 | def find_by_kid(self, kid, **params): |
| 20 | """Find the key matches the given kid value. |
| 21 | |
| 22 | :param kid: A string of kid |
| 23 | :return: Key instance |
| 24 | :raise: ValueError |
| 25 | """ |
| 26 | # Proposed fix, feel free to do something else but the idea is that we take the only key |
| 27 | # of the set if no kid is specified |
| 28 | if kid is None and len(self.keys) == 1: |
| 29 | return self.keys[0] |
| 30 | |
| 31 | keys = [key for key in self.keys if key.kid == kid] |
| 32 | if params: |
| 33 | keys = list(_filter_keys_by_params(keys, **params)) |
| 34 | |
| 35 | if keys: |
| 36 | return keys[0] |
| 37 | raise ValueError("Key not found") |
| 38 | |
| 39 | |
| 40 | def _filter_keys_by_params(keys, **params): |
no outgoing calls
searching dependent graphs…