All unit tests and live tests passing.
+=======
+ 1.0.0
+=======
+
+XXX Removed kwargs support from constructor - replaced by ssl_context
+
======
0.13
======
except ImportError:
from configparser import SafeConfigParser, NoOptionError
+from backports import ssl
+
import imapclient
from .six.moves.urllib.request import urlopen
from .six.moves.urllib.parse import urlencode
+from .tls import create_default_context
try:
import json
return token
def create_client_from_config(conf):
+ # FIXME: temporary until the config files are expanded to support TLS options.
+ ssl_context = None
+ if conf.ssl:
+ ssl_context = create_default_context()
+ ssl_context.check_hostname = False
+ ssl_context.verify_mode = ssl.CERT_NONE
+
client = imapclient.IMAPClient(conf.host, port=conf.port,
- ssl=conf.ssl, stream=conf.stream)
+ ssl=conf.ssl,
+ ssl_context=ssl_context,
+ stream=conf.stream)
if conf.starttls:
client.starttls()
-# Copyright (c) 2014, Menno Smits
+# Copyright (c) 2015, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
If *ssl* is ``True`` an SSL connection will be made (defaults to
``False``).
+ If *ssl* is ``True`` the optional *ssl_context* argument can be
+ used to provide a ``backports.ssl.SSLContext`` instance used to
+ control SSL/TLS connection parameters. If this is not provided a
+ sensible default context will be used.
+
If *stream* is ``True`` then *host* is used as the command to run
to establish a connection to the IMAP server (defaults to
``False``). This is useful for exotic connection or authentication
setups.
- Additional keyword arguments are passed through to the constructor
- of the :py:class:`imaplib.IMAP4` class, or
- :py:class:`imaplib.IMAP4_SSL` when *ssl* is ``True`` (these are
- used by ``IMAPClient`` internally). This allows passing SSL
- related parameters such as *keyfile*, *certfile* and *ssl_context*
- (Python version dependent). For details, see the :py:mod:`imaplib`
- documentation in the standard library reference.
-
- .. note::
-
- Support for passthrough keyword arguments may be removed in
- some future version of IMAPClient. Backwards compatibility is
- not guaranteed for this feature.
-
The *normalise_times* attribute specifies whether datetimes
returned by ``fetch()`` are normalised to the local system time
and include no timezone information (native), or are datetimes
ReadOnlyError = imaplib.IMAP4.readonly
def __init__(self, host, port=None, use_uid=True, ssl=False, stream=False,
- **kwargs):
+ ssl_context=None):
if stream:
if port is not None:
raise ValueError("can't set 'port' when 'stream' True")
self.host = host
self.port = port
self.ssl = ssl
+ self.ssl_context = ssl_context
self.stream = stream
self.use_uid = use_uid
self.folder_encode = True
self._starttls_done = False
self._cached_capabilities = None
- self._imap = self._create_IMAP4(**kwargs)
+ self._imap = self._create_IMAP4()
self._imap._mesg = self._log # patch in custom debug log method
self._idle_tag = None
- def _create_IMAP4(self, **kwargs):
- # Create the IMAP instance in a separate method to make unit tests easier
+ def _create_IMAP4(self):
if self.stream:
return imaplib.IMAP4_stream(self.host)
- ImapClass = self.ssl and imaplib.IMAP4_SSL or imaplib.IMAP4
- return ImapClass(self.host, self.port, **kwargs)
+
+ if self.ssl:
+ return tls.IMAP4_TLS(self.host, self.port, self.ssl_context)
+
+ return imaplib.IMAP4(self.host, self.port)
def starttls(self, ssl_context=None):
"""Switch to an SSL encrypted connection by sending a STARTTLS command.
-# Copyright (c) 2014, Menno Smits
+# Copyright (c) 2015, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
from imapclient.imapclient import IMAPClient
from imapclient.test.util import unittest
+
class TestInit(unittest.TestCase):
def setUp(self):
- self.patcher = patch('imapclient.imapclient.imaplib')
- self.imaplib = self.patcher.start()
+ patcher = patch('imapclient.imapclient.imaplib')
+ self.imaplib = patcher.start()
+ self.addCleanup(patcher.stop)
- def tearDown(self):
- self.patcher.stop()
+ patcher = patch('imapclient.imapclient.tls')
+ self.tls = patcher.start()
+ self.addCleanup(patcher.stop)
def test_plain(self):
self.imaplib.IMAP4.return_value = sentinel.IMAP4
self.assertEqual(imap.host, '1.2.3.4')
self.assertEqual(imap.port, 143)
self.assertEqual(imap.ssl, False)
+ self.assertEqual(imap.ssl_context, None)
self.assertEqual(imap.stream, False)
def test_SSL(self):
- self.imaplib.IMAP4_SSL.return_value = sentinel.IMAP4_SSL
+ self.tls.IMAP4_TLS.return_value = sentinel.IMAP4_TLS
- imap = IMAPClient('1.2.3.4', ssl=True)
+ imap = IMAPClient('1.2.3.4', ssl=True, ssl_context=sentinel.context)
- self.assertEqual(imap._imap, sentinel.IMAP4_SSL)
- self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4', 993)
+ self.assertEqual(imap._imap, sentinel.IMAP4_TLS)
+ self.tls.IMAP4_TLS.assert_called_with('1.2.3.4', 993, sentinel.context)
self.assertEqual(imap.host, '1.2.3.4')
self.assertEqual(imap.port, 993)
self.assertEqual(imap.ssl, True)
+ self.assertEqual(imap.ssl_context, sentinel.context)
self.assertEqual(imap.stream, False)
- def test_SSL_kwargs(self):
- self.imaplib.IMAP4_SSL.return_value = sentinel.IMAP4_SSL
-
- imap = IMAPClient('1.2.3.4', ssl=True, keyfile='key.pem',
- certfile='cert.pem')
-
- self.assertEqual(imap._imap, sentinel.IMAP4_SSL)
- self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4', 993,
- keyfile='key.pem', certfile='cert.pem')
- self.assertEqual(imap.ssl, True)
- self.assertEqual(imap.stream, False)
-
- imap = IMAPClient('1.2.3.4', ssl=True, ssl_context=sentinel.context)
- self.imaplib.IMAP4_SSL.assert_called_with('1.2.3.4', 993,
- ssl_context=sentinel.context)
- self.assertEqual(imap.ssl, True)
- self.assertEqual(imap.stream, False)
-
-
def test_stream(self):
self.imaplib.IMAP4_stream.return_value = sentinel.IMAP4_stream
-# -*- coding: utf-8 -*-
+# Copyright (c) 2015, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
"""
-Solid TLS support for Python 2.6+ and 3.3+ built on backports.ssl/pyOpenSSL.
+Good TLS support for IMAPClient built on backports.ssl/pyOpenSSL.
+
+Works with Python 2.6+ and 3.3+.
"""
__all__ = ('create_default_context', 'wrap_socket')
-import sys
import imaplib
+import socket
+import sys
from backports import ssl
_ossl = ssl.ossl
+
if sys.platform == "win32":
try:
from ssl import enum_certificates, Purpose
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
- # do certificate hostname checks.
+ # require certificate that matches the host name.
+ context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
# SSLv2 considered harmful.
ssl_context = create_default_context()
try:
- return ssl_context.wrap_socket(sock, server_hostname=hostname)
+ newsock = ssl_context.wrap_socket(sock, server_hostname=hostname)
except ssl.CertificateError:
sock.shutdown()
sock.close()
raise imaplib.Error("server certificate not valid for %s" % hostname)
+
+ return _SSLSocketWithShutdown(newsock)
+
+
+class IMAP4_TLS(imaplib.IMAP4):
+ """IMAP4 client class for TLS/SSL connections.
+
+ Adapted from imaplib.IMAP4_SSL.
+ """
+
+ def __init__(self, host, port, ssl_context):
+ self.ssl_context = ssl_context
+ imaplib.IMAP4.__init__(self, host, port)
+
+ def open(self, host, port):
+ self.host = host
+ self.port = port
+ sock = socket.create_connection((host, port))
+ self.sock = wrap_socket(sock, self.ssl_context, host)
+ self.file = self.sock.makefile('rb')
+
+
+# TODO: get shutdown added in backports.ssl.SSLSocket
+class _SSLSocketWithShutdown(object):
+
+ def __init__(self, sslsock):
+ self.sslsock = sslsock
+
+ def shutdown(self, _):
+ return self.sslsock._conn.shutdown()
+
+ def __getattr__(self, name):
+ return getattr(self.sslsock, name)