__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-2022 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2024 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. Authentication module.
  22. Authentication is based on usernames and passwords. If something more
  23. advanced is needed an external WSGI server or reverse proxy can be used
  24. (see ``remote_user`` or ``http_x_remote_user`` backend).
  25. Take a look at the class ``BaseAuth`` if you want to implement your own.
  26. """
  27. from typing import Sequence, Tuple, Union
  28. from radicale import config, types, utils
  29. from radicale.log import logger
  30. INTERNAL_TYPES: Sequence[str] = ("none", "remote_user", "http_x_remote_user",
  31. "denyall",
  32. "htpasswd")
  33. def load(configuration: "config.Configuration") -> "BaseAuth":
  34. """Load the authentication module chosen in configuration."""
  35. if configuration.get("auth", "type") == "none":
  36. logger.warn("No user authentication is selected (insecure)")
  37. return utils.load_plugin(INTERNAL_TYPES, "auth", "Auth", BaseAuth,
  38. configuration)
  39. class BaseAuth:
  40. _lc_username: bool
  41. def __init__(self, configuration: "config.Configuration") -> None:
  42. """Initialize BaseAuth.
  43. ``configuration`` see ``radicale.config`` module.
  44. The ``configuration`` must not change during the lifetime of
  45. this object, it is kept as an internal reference.
  46. """
  47. self.configuration = configuration
  48. self._lc_username = configuration.get("auth", "lc_username")
  49. def get_external_login(self, environ: types.WSGIEnviron) -> Union[
  50. Tuple[()], Tuple[str, str]]:
  51. """Optionally provide the login and password externally.
  52. ``environ`` a dict with the WSGI environment
  53. If ``()`` is returned, Radicale handles HTTP authentication.
  54. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  55. ``login`` must be ``""``.
  56. """
  57. return ()
  58. def _login(self, login: str, password: str) -> str:
  59. """Check credentials and map login to internal user
  60. ``login`` the login name
  61. ``password`` the password
  62. Returns the username or ``""`` for invalid credentials.
  63. """
  64. raise NotImplementedError
  65. def login(self, login: str, password: str) -> str:
  66. return self._login(login, password).lower() if self._lc_username else self._login(login, password)