jwt.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 Niklas Riekenbrauck
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from ._base import Config, ConfigError
  16. MISSING_JWT = (
  17. """Missing jwt library. This is required for jwt login.
  18. Install by running:
  19. pip install pyjwt
  20. """
  21. )
  22. class JWTConfig(Config):
  23. def read_config(self, config):
  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. try:
  30. import jwt
  31. jwt # To stop unused lint.
  32. except ImportError:
  33. raise ConfigError(MISSING_JWT)
  34. else:
  35. self.jwt_enabled = False
  36. self.jwt_secret = None
  37. self.jwt_algorithm = None
  38. def default_config(self, **kwargs):
  39. return """\
  40. # The JWT needs to contain a globally unique "sub" (subject) claim.
  41. #
  42. # jwt_config:
  43. # enabled: true
  44. # secret: "a secret"
  45. # algorithm: "HS256"
  46. """