__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2018 Unrud <unrud@outlook.com>
  6. #
  7. # This library is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. Authentication module.
  21. Authentication is based on usernames and passwords. If something more
  22. advanced is needed an external WSGI server or reverse proxy can be used
  23. (see ``remote_user`` or ``http_x_remote_user`` backend).
  24. Take a look at the class ``BaseAuth`` if you want to implement your own.
  25. """
  26. from importlib import import_module
  27. from radicale.log import logger
  28. INTERNAL_TYPES = ("none", "remote_user", "http_x_remote_user", "htpasswd")
  29. def load(configuration):
  30. """Load the authentication manager chosen in configuration."""
  31. auth_type = configuration.get("auth", "type")
  32. if auth_type in INTERNAL_TYPES:
  33. module = "radicale.auth.%s" % auth_type
  34. else:
  35. module = auth_type
  36. try:
  37. class_ = import_module(module).Auth
  38. except Exception as e:
  39. raise RuntimeError("Failed to load authentication module %r: %s" %
  40. (module, e)) from e
  41. logger.info("Authentication type is %r", auth_type)
  42. return class_(configuration)
  43. class BaseAuth:
  44. def __init__(self, configuration):
  45. """Initialize BaseAuth.
  46. ``configuration`` see ``radicale.config`` module.
  47. The ``configuration`` must not change during the lifetime of
  48. this object, it is kept as an internal reference.
  49. """
  50. self.configuration = configuration
  51. def get_external_login(self, environ):
  52. """Optionally provide the login and password externally.
  53. ``environ`` a dict with the WSGI environment
  54. If ``()`` is returned, Radicale handles HTTP authentication.
  55. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  56. ``login`` must be ``""``.
  57. """
  58. return ()
  59. def login(self, login, password):
  60. """Check credentials and map login to internal user
  61. ``login`` the login name
  62. ``password`` the password
  63. Returns the user name or ``""`` for invalid credentials.
  64. """
  65. raise NotImplementedError