imap.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # RadicaleIMAP IMAP authentication plugin for Radicale.
  2. # Copyright © 2017, 2020 Unrud <unrud@outlook.com>
  3. # Copyright © 2025-2025 Peter Bieringer <pb@bieringer.de>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import imaplib
  18. import ssl
  19. from radicale import auth
  20. from radicale.log import logger
  21. class Auth(auth.BaseAuth):
  22. """Authenticate user with IMAP."""
  23. def __init__(self, configuration) -> None:
  24. super().__init__(configuration)
  25. self._host, self._port = self.configuration.get("auth", "imap_host")
  26. logger.info("auth imap host: %r", self._host)
  27. self._security = self.configuration.get("auth", "imap_security")
  28. if self._security == "none":
  29. logger.warning("auth imap security: %s (INSECURE, credentials are transmitted in clear text)", self._security)
  30. else:
  31. logger.info("auth imap security: %s", self._security)
  32. if self._security == "tls":
  33. if self._port is None:
  34. self._port = 993
  35. logger.info("auth imap port (autoselected): %d", self._port)
  36. else:
  37. logger.info("auth imap port: %d", self._port)
  38. else:
  39. if self._port is None:
  40. self._port = 143
  41. logger.info("auth imap port (autoselected): %d", self._port)
  42. else:
  43. logger.info("auth imap port: %d", self._port)
  44. def _login(self, login, password) -> str:
  45. try:
  46. connection: imaplib.IMAP4 | imaplib.IMAP4_SSL
  47. if self._security == "tls":
  48. connection = imaplib.IMAP4_SSL(
  49. host=self._host, port=self._port,
  50. ssl_context=ssl.create_default_context())
  51. else:
  52. connection = imaplib.IMAP4(host=self._host, port=self._port)
  53. if self._security == "starttls":
  54. connection.starttls(ssl.create_default_context())
  55. try:
  56. connection.login(login, password)
  57. except imaplib.IMAP4.error as e:
  58. logger.warning("IMAP authentication failed for user %r: %s", login, e, exc_info=False)
  59. return ""
  60. connection.logout()
  61. return login
  62. except (OSError, imaplib.IMAP4.error) as e:
  63. logger.error("Failed to communicate with IMAP server %r: %s" % ("[%s]:%d" % (self._host, self._port), e))
  64. return ""