From: Johannes Heckel Date: Wed, 23 Mar 2011 00:43:05 +0000 (+0000) Subject: Implemented oauth authentication using oauth2. X-Git-Tag: 0.8~34^2~21^2^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=2be998b966d0010f4de786f8adae318637a11725;p=imapclient.git Implemented oauth authentication using oauth2. Also added an example to demonstrate the new oauth_login function's use. --HG-- branch : oauth --- diff --git a/imapclient/examples/oauth_example.py b/imapclient/examples/oauth_example.py new file mode 100644 index 0000000..8ca45cd --- /dev/null +++ b/imapclient/examples/oauth_example.py @@ -0,0 +1,22 @@ +from imapclient import IMAPClient + +CONSUMER_KEY = '' +CONSUMER_SECRET = '' + +OAUTH_TOKEN = '' +OAUTH_TOKEN_SECRET = '' + +HOST = 'imap.host.com' +URL = "https://somedomain.com/someuser/imap/" +ssl = True + +server = IMAPClient(HOST, use_uid=True, ssl=ssl) + +resp = server.oauth_login(URL, OAUTH_TOKEN, OAUTH_TOKEN_SECRET, + CONSUMER_KEY, CONSUMER_SECRET) +print resp + +select_info = server.select_folder('INBOX') +print select_info + +server.logout() \ No newline at end of file diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index b2dd6c6..796a8e9 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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