__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.warning("No user authentication is selected: '[auth] type=none' (insecure)")
  37. if configuration.get("auth", "type") == "denyall":
  38. logger.warning("All access is blocked by: '[auth] type=denyall'")
  39. return utils.load_plugin(INTERNAL_TYPES, "auth", "Auth", BaseAuth,
  40. configuration)
  41. class BaseAuth:
  42. _lc_username: bool
  43. _strip_domain: bool
  44. def __init__(self, configuration: "config.Configuration") -> None:
  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. self._lc_username = configuration.get("auth", "lc_username")
  52. self._strip_domain = configuration.get("auth", "strip_domain")
  53. def get_external_login(self, environ: types.WSGIEnviron) -> Union[
  54. Tuple[()], Tuple[str, str]]:
  55. """Optionally provide the login and password externally.
  56. ``environ`` a dict with the WSGI environment
  57. If ``()`` is returned, Radicale handles HTTP authentication.
  58. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  59. ``login`` must be ``""``.
  60. """
  61. return ()
  62. def _login(self, login: str, password: str) -> str:
  63. """Check credentials and map login to internal user
  64. ``login`` the login name
  65. ``password`` the password
  66. Returns the username or ``""`` for invalid credentials.
  67. """
  68. raise NotImplementedError
  69. def login(self, login: str, password: str) -> str:
  70. if self._lc_username:
  71. login = login.lower()
  72. if self._strip_domain:
  73. login = login.split('@')[0]
  74. return self._login(login, password)