__init__.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # This file is part of Radicale - CalDAV and CardDAV 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 typing import Sequence, Tuple, Union
  27. from radicale import config, types, utils
  28. INTERNAL_TYPES: Sequence[str] = ("none", "remote_user", "http_x_remote_user",
  29. "htpasswd")
  30. def load(configuration: "config.Configuration") -> "BaseAuth":
  31. """Load the authentication module chosen in configuration."""
  32. return utils.load_plugin(INTERNAL_TYPES, "auth", "Auth", BaseAuth,
  33. configuration)
  34. class BaseAuth:
  35. def __init__(self, configuration: "config.Configuration") -> None:
  36. """Initialize BaseAuth.
  37. ``configuration`` see ``radicale.config`` module.
  38. The ``configuration`` must not change during the lifetime of
  39. this object, it is kept as an internal reference.
  40. """
  41. self.configuration = configuration
  42. def get_external_login(self, environ: types.WSGIEnviron) -> Union[
  43. Tuple[()], Tuple[str, str]]:
  44. """Optionally provide the login and password externally.
  45. ``environ`` a dict with the WSGI environment
  46. If ``()`` is returned, Radicale handles HTTP authentication.
  47. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  48. ``login`` must be ``""``.
  49. """
  50. return ()
  51. def login(self, login: str, password: str) -> str:
  52. """Check credentials and map login to internal user
  53. ``login`` the login name
  54. ``password`` the password
  55. Returns the username or ``""`` for invalid credentials.
  56. """
  57. raise NotImplementedError