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.
 
 
 
 
 
 

109 lines
3.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 ._base import Config, ConfigError
  15. MISSING_JWT = """Missing jwt library. This is required for jwt login.
  16. Install by running:
  17. pip install pyjwt
  18. """
  19. class JWTConfig(Config):
  20. section = "jwt"
  21. def read_config(self, config, **kwargs):
  22. jwt_config = config.get("jwt_config", None)
  23. if jwt_config:
  24. self.jwt_enabled = jwt_config.get("enabled", False)
  25. self.jwt_secret = jwt_config["secret"]
  26. self.jwt_algorithm = jwt_config["algorithm"]
  27. # The issuer and audiences are optional, if provided, it is asserted
  28. # that the claims exist on the JWT.
  29. self.jwt_issuer = jwt_config.get("issuer")
  30. self.jwt_audiences = jwt_config.get("audiences")
  31. try:
  32. import jwt
  33. jwt # To stop unused lint.
  34. except ImportError:
  35. raise ConfigError(MISSING_JWT)
  36. else:
  37. self.jwt_enabled = False
  38. self.jwt_secret = None
  39. self.jwt_algorithm = None
  40. self.jwt_issuer = None
  41. self.jwt_audiences = None
  42. def generate_config_section(self, **kwargs):
  43. return """\
  44. # JSON web token integration. The following settings can be used to make
  45. # Synapse JSON web tokens for authentication, instead of its internal
  46. # password database.
  47. #
  48. # Each JSON Web Token needs to contain a "sub" (subject) claim, which is
  49. # used as the localpart of the mxid.
  50. #
  51. # Additionally, the expiration time ("exp"), not before time ("nbf"),
  52. # and issued at ("iat") claims are validated if present.
  53. #
  54. # Note that this is a non-standard login type and client support is
  55. # expected to be non-existent.
  56. #
  57. # See https://github.com/matrix-org/synapse/blob/master/docs/jwt.md.
  58. #
  59. #jwt_config:
  60. # Uncomment the following to enable authorization using JSON web
  61. # tokens. Defaults to false.
  62. #
  63. #enabled: true
  64. # This is either the private shared secret or the public key used to
  65. # decode the contents of the JSON web token.
  66. #
  67. # Required if 'enabled' is true.
  68. #
  69. #secret: "provided-by-your-issuer"
  70. # The algorithm used to sign the JSON web token.
  71. #
  72. # Supported algorithms are listed at
  73. # https://pyjwt.readthedocs.io/en/latest/algorithms.html
  74. #
  75. # Required if 'enabled' is true.
  76. #
  77. #algorithm: "provided-by-your-issuer"
  78. # The issuer to validate the "iss" claim against.
  79. #
  80. # Optional, if provided the "iss" claim will be required and
  81. # validated for all JSON web tokens.
  82. #
  83. #issuer: "provided-by-your-issuer"
  84. # A list of audiences to validate the "aud" claim against.
  85. #
  86. # Optional, if provided the "aud" claim will be required and
  87. # validated for all JSON web tokens.
  88. #
  89. # Note that if the "aud" claim is included in a JSON web token then
  90. # validation will fail without configuring audiences.
  91. #
  92. #audiences:
  93. # - "provided-by-your-issuer"
  94. """