__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. _lc_username: bool
  36. def __init__(self, configuration: "config.Configuration") -> None:
  37. """Initialize BaseAuth.
  38. ``configuration`` see ``radicale.config`` module.
  39. The ``configuration`` must not change during the lifetime of
  40. this object, it is kept as an internal reference.
  41. """
  42. self.configuration = configuration
  43. self._lc_username = configuration.get("auth", "lc_username")
  44. def get_external_login(self, environ: types.WSGIEnviron) -> Union[
  45. Tuple[()], Tuple[str, str]]:
  46. """Optionally provide the login and password externally.
  47. ``environ`` a dict with the WSGI environment
  48. If ``()`` is returned, Radicale handles HTTP authentication.
  49. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  50. ``login`` must be ``""``.
  51. """
  52. return ()
  53. def _login(self, login: str, password: str) -> str:
  54. """Check credentials and map login to internal user
  55. ``login`` the login name
  56. ``password`` the password
  57. Returns the username or ``""`` for invalid credentials.
  58. """
  59. raise NotImplementedError
  60. def login(self, login: str, password: str) -> str:
  61. return self._login(login, password).lower() if self._lc_username else self._login(login, password)