Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

58 řádky
1.9 KiB

  1. # Copyright 2015 Niklas Riekenbrauck
  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 typing import Any
  15. from synapse.types import JsonDict
  16. from ._base import Config, ConfigError
  17. MISSING_JWT = """Missing jwt library. This is required for jwt login.
  18. Install by running:
  19. pip install pyjwt
  20. """
  21. class JWTConfig(Config):
  22. section = "jwt"
  23. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  24. jwt_config = config.get("jwt_config", None)
  25. if jwt_config:
  26. self.jwt_enabled = jwt_config.get("enabled", False)
  27. self.jwt_secret = jwt_config["secret"]
  28. self.jwt_algorithm = jwt_config["algorithm"]
  29. self.jwt_subject_claim = jwt_config.get("subject_claim", "sub")
  30. # The issuer and audiences are optional, if provided, it is asserted
  31. # that the claims exist on the JWT.
  32. self.jwt_issuer = jwt_config.get("issuer")
  33. self.jwt_audiences = jwt_config.get("audiences")
  34. try:
  35. import jwt
  36. jwt # To stop unused lint.
  37. except ImportError:
  38. raise ConfigError(MISSING_JWT)
  39. else:
  40. self.jwt_enabled = False
  41. self.jwt_secret = None
  42. self.jwt_algorithm = None
  43. self.jwt_subject_claim = None
  44. self.jwt_issuer = None
  45. self.jwt_audiences = None