Changeset 207:835686a87307

Show
Ignore:
Timestamp:
03/23/11 00:43:05 (14 months ago)
Author:
Johannes Heckel <stone.pixel@…>
Branch:
oauth
Children:
209:80f530da50d7, 213:8e81638b42ec
Message:

Implemented oauth authentication using oauth2.

Also added an example to demonstrate the new oauth_login function's use.

Location:
imapclient
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • imapclient/imapclient.py

    r204 r207  
    99import warnings 
    1010#imaplib.Debug = 5 
     11 
     12try: 
     13    import oauth2 
     14except ImportError: 
     15    oauth2 = None 
    1116 
    1217import imap_utf7 
     
    113118        return data[0] 
    114119 
     120    def oauth_login(self, url, oauth_token, oauth_token_secret, 
     121                    consumer_key='anonymous', consumer_secret='anonymous'): 
     122        """Authenticate using oauth. 
     123 
     124        @param url: The OAuth request URL. 
     125        @param oauth_token: An OAuth key. 
     126        @param oauth_token_secret: An OAuth secret. 
     127        @param consumer_key: An OAuth consumer key (defaults to 'anonymous'). 
     128        @param consumer_secret: An OAuth consumer secret (defaults to 'anonymous'). 
     129        """ 
     130        if oauth2: 
     131            token = oauth2.Token(oauth_token, oauth_token_secret) 
     132            consumer = oauth2.Consumer(consumer_key, consumer_secret) 
     133            xoauth_callable = lambda x: oauth2.build_xoauth_string(url, consumer, token) 
     134             
     135            typ, data = self._imap.authenticate('XOAUTH', xoauth_callable) 
     136            self._checkok('authenticate', typ, data) 
     137            return data[0] 
     138        else: 
     139            raise self.Error('The optional oauth2 dependency is needed for oauth authentication') 
    115140 
    116141    def logout(self):