oauth2.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # This file is part of Radicale Server - Calendar Server
  2. #
  3. # Original from https://gitlab.mim-libre.fr/alphabet/radicale_oauth/
  4. # Copyright © 2021-2022 Bruno Boiget
  5. # Copyright © 2022-2022 Daniel Dehennin
  6. #
  7. # Since migration into upstream
  8. # Copyright © 2025-2025 Peter Bieringer <pb@bieringer.de>
  9. #
  10. # This library is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This library is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  22. """
  23. Authentication backend that checks credentials against an oauth2 server auth endpoint
  24. """
  25. import requests
  26. from radicale import auth
  27. from radicale.log import logger
  28. class Auth(auth.BaseAuth):
  29. def __init__(self, configuration):
  30. super().__init__(configuration)
  31. self._endpoint = configuration.get("auth", "oauth2_token_endpoint")
  32. if not self._endpoint:
  33. logger.error("auth.oauth2_token_endpoint URL missing")
  34. raise RuntimeError("OAuth2 token endpoint URL is required")
  35. logger.info("auth OAuth2 token endpoint: %s" % (self._endpoint))
  36. def _login(self, login, password):
  37. """Validate credentials.
  38. Sends login credentials to oauth token endpoint and checks that a token is returned
  39. """
  40. try:
  41. # authenticate to authentication endpoint and return login if ok, else ""
  42. req_params = {
  43. "username": login,
  44. "password": password,
  45. "grant_type": "password",
  46. "client_id": "radicale",
  47. }
  48. req_headers = {"Content-Type": "application/x-www-form-urlencoded"}
  49. response = requests.post(
  50. self._endpoint, data=req_params, headers=req_headers
  51. )
  52. if (
  53. response.status_code == requests.codes.ok
  54. and "access_token" in response.json()
  55. ):
  56. return login
  57. except OSError as e:
  58. logger.critical("Failed to authenticate against OAuth2 server %s: %s" % (self._endpoint, e))
  59. logger.warning("User failed to authenticate using OAuth2: %r" % login)
  60. return ""