]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Implemented oauth authentication using oauth2.
authorJohannes Heckel <stone.pixel@gmail.com>
Wed, 23 Mar 2011 00:43:05 +0000 (00:43 +0000)
committerJohannes Heckel <stone.pixel@gmail.com>
Wed, 23 Mar 2011 00:43:05 +0000 (00:43 +0000)
Also added an example to demonstrate the new oauth_login function's use.

--HG--
branch : oauth

imapclient/examples/oauth_example.py [new file with mode: 0644]
imapclient/imapclient.py

diff --git a/imapclient/examples/oauth_example.py b/imapclient/examples/oauth_example.py
new file mode 100644 (file)
index 0000000..8ca45cd
--- /dev/null
@@ -0,0 +1,22 @@
+from imapclient import IMAPClient\r
+\r
+CONSUMER_KEY = ''\r
+CONSUMER_SECRET = ''\r
+\r
+OAUTH_TOKEN = ''\r
+OAUTH_TOKEN_SECRET = ''\r
+\r
+HOST = 'imap.host.com'\r
+URL = "https://somedomain.com/someuser/imap/"\r
+ssl = True\r
+\r
+server = IMAPClient(HOST, use_uid=True, ssl=ssl)\r
+\r
+resp = server.oauth_login(URL, OAUTH_TOKEN, OAUTH_TOKEN_SECRET,\r
+                         CONSUMER_KEY, CONSUMER_SECRET)\r
+print resp\r
+\r
+select_info = server.select_folder('INBOX')\r
+print select_info\r
+\r
+server.logout()
\ No newline at end of file
index b2dd6c6971d397654be66cee6e83949a5ab81aed..796a8e9a0de6f040c3b283f4a4b1d3606558c1ea 100644 (file)
@@ -9,6 +9,11 @@ from operator import itemgetter
 import warnings
 #imaplib.Debug = 5
 
+try:
+    import oauth2
+except ImportError:
+    oauth2 = None
+
 import imap_utf7
 from fixed_offset import FixedOffset
 
@@ -112,6 +117,26 @@ class IMAPClient(object):
         self._checkok('login', typ, data)
         return data[0]
 
+    def oauth_login(self, url, oauth_token, oauth_token_secret,
+                    consumer_key='anonymous', consumer_secret='anonymous'):
+        """Authenticate using oauth.
+
+        @param url: The OAuth request URL.
+        @param oauth_token: An OAuth key.
+        @param oauth_token_secret: An OAuth secret.
+        @param consumer_key: An OAuth consumer key (defaults to 'anonymous').
+        @param consumer_secret: An OAuth consumer secret (defaults to 'anonymous').
+        """
+        if oauth2:
+            token = oauth2.Token(oauth_token, oauth_token_secret)
+            consumer = oauth2.Consumer(consumer_key, consumer_secret)
+            xoauth_callable = lambda x: oauth2.build_xoauth_string(url, consumer, token)
+            
+            typ, data = self._imap.authenticate('XOAUTH', xoauth_callable)
+            self._checkok('authenticate', typ, data)
+            return data[0]
+        else:
+            raise self.Error('The optional oauth2 dependency is needed for oauth authentication')
 
     def logout(self):
         """Perform a logout