__init__.py 3.0 KB

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