]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added PLAIN authentication support
authorMenno Smits <menno@freshfoo.com>
Mon, 11 Jul 2016 18:18:08 +0000 (19:18 +0100)
committerMenno Smits <menno@freshfoo.com>
Mon, 11 Jul 2016 18:18:08 +0000 (19:18 +0100)
Closes #214.

THANKS
doc/src/releases.rst
imapclient/imapclient.py
imapclient/test/test_auth.py [new file with mode: 0644]

diff --git a/THANKS b/THANKS
index 5b0bd2985c233743885cb46b2201f5b6c16dbc2b..6c9956eaa7b37bf976e01b1401a8313833892e72 100644 (file)
--- a/THANKS
+++ b/THANKS
@@ -60,6 +60,8 @@ Thomas Steinacher
 Eben Freeman
      ID support
 
+Aviv Salem
+     PLAIN authentication support
 
 Bug Reports
 ------------
index ac18a6c52fc6e2098a4b95925145e16839e5a8cf..27401d8b8b193bf0b7b65261d08a1f708f883192 100644 (file)
@@ -1,5 +1,13 @@
 :tocdepth: 1
 
+===============
+ Version 1.1.0
+===============
+
+Added
+-----
+- PLAIN authentication support (via `plain_login` method)
+
 ===============
  Version 1.0.1
 ===============
index 43f0e6bc32fd51c787528b658f5f04bd8a46f8bd..c24f5c19d09012997bab26f6a4cb35d3c8eb6f62 100644 (file)
@@ -241,6 +241,14 @@ class IMAPClient(object):
         auth_string += '\1'
         return self._command_and_check('authenticate', mech, lambda x: auth_string)
 
+    def plain_login(self, identity, password, authorization_identity=None):
+        """Authenticate using the PLAIN method (requires server support).
+        """
+        if not authorization_identity:
+            authorization_identity = ""
+        auth_string = '%s\0%s\0%s' % (authorization_identity, identity, password)
+        return self._command_and_check('authenticate', 'PLAIN', lambda _: auth_string, unpack=True)
+
     def logout(self):
         """Logout, returning the server response.
         """
diff --git a/imapclient/test/test_auth.py b/imapclient/test/test_auth.py
new file mode 100644 (file)
index 0000000..5c7b0da
--- /dev/null
@@ -0,0 +1,34 @@
+# Copyright (c) 2016, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+
+from imapclient import IMAPClient
+from .imapclient_test import IMAPClientTest
+
+
+class TestPlainLogin(IMAPClientTest):
+
+    def assert_authenticate_call(self, expected_auth_string):
+        authenticate = self.client._imap.authenticate
+        self.assertEqual(authenticate.call_count, 1)
+        auth_type, auth_func = authenticate.call_args[0]
+        self.assertEqual(auth_type, "PLAIN")
+        self.assertEqual(auth_func(None), expected_auth_string)
+
+    def test_simple(self):
+        self.client._imap.authenticate.return_value = ('OK', [b'Success'])
+        result = self.client.plain_login("user", "secret")
+        self.assertEqual(result, b'Success')
+        self.assert_authenticate_call("\0user\0secret")
+
+    def test_fail(self):
+        self.client._imap.authenticate.return_value = ('NO', [b'Boom'])
+        self.assertRaises(IMAPClient.Error, self.client.plain_login, "user", "secret")
+
+    def test_with_authorization_identity(self):
+        self.client._imap.authenticate.return_value = ('OK', [b'Success'])
+        result = self.client.plain_login("user", "secret", "authid")
+        self.assertEqual(result, b'Success')
+        self.assert_authenticate_call("authid\0user\0secret")