codekingpro/portable-devtools
114k
1from authlib.common.encoding import json_dumps2 3 4class KeySet:5 """This class represents a JSON Web Key Set."""6 7 def __init__(self, keys):8 self.keys = keys9 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):20 """Find the key matches the given kid value.21 22 :param kid: A string of kid23 :return: Key instance24 :raise: ValueError25 """26 for k in self.keys:27 if k.kid == kid:28 return k29 raise ValueError('Invalid JSON Web Key Set')30 