Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

49 linhas
1.7 KiB

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