You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

59 lines
1.9 KiB

  1. # Copyright 2017 Gehirn Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections import UserList
  15. from typing import TYPE_CHECKING
  16. from .jwk import (
  17. AbstractJWKBase,
  18. jwk_from_dict,
  19. )
  20. if TYPE_CHECKING:
  21. UserListBase = UserList[AbstractJWKBase]
  22. else:
  23. UserListBase = UserList
  24. class JWKSet(UserListBase):
  25. def filter_keys(self, kid=None, kty=None):
  26. # When "kid" values are used within a JWK Set, different
  27. # keys within the JWK Set SHOULD use distinct "kid" values. (One
  28. # example in which different keys might use the same "kid" value is if
  29. # they have different "kty" (key type) values but are considered to be
  30. # equivalent alternatives by the application using them.)
  31. if kid and kty:
  32. return [
  33. key
  34. for key in self.data
  35. if key.get_kty() == kty and key.get_kid() == kid
  36. ]
  37. if kid:
  38. return [key for key in self.data if key.get_kid() == kid]
  39. if kty:
  40. return [key for key in self.data if key.get_kty() == kty]
  41. return self.data.copy()
  42. def to_dict(self, public_only=True):
  43. keys = [key.to_dict(public_only=public_only) for key in self.data]
  44. return {"keys": keys}
  45. @classmethod
  46. def from_dict(cls, dct):
  47. keys = [jwk_from_dict(key_dct) for key_dct in dct.get("keys", [])]
  48. return cls(keys)