http.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012 Ehsanul Hoque
  5. # Copyright © 2013 Guillaume Ayoub
  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. HTTP authentication.
  21. Authentication based on the ``requests`` module.
  22. Post a request to an authentication server with the username/password.
  23. Anything other than a 200/201 response is considered auth failure.
  24. """
  25. import requests
  26. from .. import config, log
  27. AUTH_URL = config.get("auth", "http_url")
  28. USER_PARAM = config.get("auth", "http_user_parameter")
  29. PASSWORD_PARAM = config.get("auth", "http_password_parameter")
  30. def is_authenticated(user, password):
  31. """Check if ``user``/``password`` couple is valid."""
  32. log.LOGGER.debug("HTTP-based auth on %s." % AUTH_URL)
  33. payload = {USER_PARAM: user, PASSWORD_PARAM: password}
  34. return requests.post(AUTH_URL, data=payload).status_code in (200, 201)