__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "ldap")
  34. def load(configuration: "config.Configuration") -> "BaseAuth":
  35. """Load the authentication module chosen in configuration."""
  36. if configuration.get("auth", "type") == "none":
  37. logger.warning("No user authentication is selected: '[auth] type=none' (insecure)")
  38. if configuration.get("auth", "type") == "denyall":
  39. logger.warning("All access is blocked by: '[auth] type=denyall'")
  40. return utils.load_plugin(INTERNAL_TYPES, "auth", "Auth", BaseAuth,
  41. configuration)
  42. class BaseAuth:
  43. _ldap_groups: set
  44. _lc_username: bool
  45. _strip_domain: bool
  46. def __init__(self, configuration: "config.Configuration") -> None:
  47. """Initialize BaseAuth.
  48. ``configuration`` see ``radicale.config`` module.
  49. The ``configuration`` must not change during the lifetime of
  50. this object, it is kept as an internal reference.
  51. """
  52. self.configuration = configuration
  53. self._lc_username = configuration.get("auth", "lc_username")
  54. self._strip_domain = configuration.get("auth", "strip_domain")
  55. def get_external_login(self, environ: types.WSGIEnviron) -> Union[
  56. Tuple[()], Tuple[str, str]]:
  57. """Optionally provide the login and password externally.
  58. ``environ`` a dict with the WSGI environment
  59. If ``()`` is returned, Radicale handles HTTP authentication.
  60. Otherwise, returns a tuple ``(login, password)``. For anonymous users
  61. ``login`` must be ``""``.
  62. """
  63. return ()
  64. def _login(self, login: str, password: str) -> str:
  65. """Check credentials and map login to internal user
  66. ``login`` the login name
  67. ``password`` the password
  68. Returns the username or ``""`` for invalid credentials.
  69. """
  70. raise NotImplementedError
  71. def login(self, login: str, password: str) -> str:
  72. if self._lc_username:
  73. login = login.lower()
  74. if self._strip_domain:
  75. login = login.split('@')[0]
  76. return self._login(login, password)