]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added OAUTH2 support to livetest/interact configs
authorMenno Smits <menno@freshfoo.com>
Sun, 4 Nov 2012 23:58:45 +0000 (23:58 +0000)
committerMenno Smits <menno@freshfoo.com>
Sun, 4 Nov 2012 23:58:45 +0000 (23:58 +0000)
imapclient/config.py
livetest-sample.ini

index 6c8d12e02615f87a313ce752dd40bf02bd07e9b7..2e54e2c97111f6543d2f7ee929932cc46e69556d 100644 (file)
@@ -1,5 +1,11 @@
 from ConfigParser import SafeConfigParser, NoOptionError
 import imapclient
+import urllib
+
+try:
+    import json
+except ImportError:
+    json = None
 
 
 def parse_config_file(path):
@@ -16,6 +22,10 @@ def parse_config_file(path):
         oauth_token=None,
         oauth_token_secret=None,
         oauth_url=None,
+        oauth2='false',
+        oauth2_client_id=None,
+        oauth2_client_secret=None,
+        oauth2_refresh_token=None,
         ))
     fh = file(path)
     parser.readfp(fh)
@@ -33,14 +43,43 @@ def parse_config_file(path):
         port=port,
         ssl=parser.getboolean(section, 'ssl'),
         stream=parser.getboolean(section, 'stream'),
+
         username=parser.get(section, 'username'),
         password=parser.get(section, 'password'),
+
         oauth=parser.getboolean(section, 'oauth'),
         oauth_url=parser.get(section, 'oauth_url'),
         oauth_token=parser.get(section, 'oauth_token'),
         oauth_token_secret=parser.get(section, 'oauth_token_secret'),
+
+        oauth2=parser.getboolean(section, 'oauth2'),
+        oauth2_client_id=parser.get(section, 'oauth2_client_id'),
+        oauth2_client_secret=parser.get(section, 'oauth2_client_secret'),
+        oauth2_refresh_token=parser.get(section, 'oauth2_refresh_token'),
     )
 
+def refresh_oauth2_token(client_id, client_secret, refresh_token):
+    if not json:
+        raise RuntimeError("livetest OAUTH2 functionality relies on 'json' module")
+    post = dict(client_id=client_id,
+                client_secret=client_secret,
+                refresh_token=refresh_token,
+                grant_type='refresh_token')
+    response = urllib.urlopen('https://accounts.google.com/o/oauth2/token',
+                              urllib.urlencode(post)).read()
+    return json.loads(response)['access_token']
+
+# Tokens are expensive to refresh so use the same one for the duration of the process.
+_oauth2_cache = {}
+def get_oauth2_token(client_id, client_secret, refresh_token):
+    cache_key = (client_id, client_secret, refresh_token)
+    token = _oauth2_cache.get(cache_key)
+    if token:
+        return token
+    token = refresh_oauth2_token(client_id, client_secret, refresh_token)
+    _oauth2_cache[cache_key] = token
+    return token
+
 def create_client_from_config(conf):
     client = imapclient.IMAPClient(conf.host, port=conf.port,
                                    ssl=conf.ssl, stream=conf.stream)
@@ -48,6 +87,12 @@ def create_client_from_config(conf):
         client.oauth_login(conf.oauth_url,
                            conf.oauth_token,
                            conf.oauth_token_secret)
+    elif conf.oauth2:
+        access_token = get_oauth2_token(conf.oauth2_client_id,
+                                        conf.oauth2_client_secret,
+                                        conf.oauth2_refresh_token)
+        client.oauth2_login(conf.username, access_token)
+
     elif not conf.stream:
         client.login(conf.username, conf.password)
     return client
index a7b1165169b95854eb42a85cb754c77e4f27d3d5..8494add81a933ead802448592d6ac77a16dc2e9c 100644 (file)
@@ -1,12 +1,9 @@
 # Sample configuration file for livetest and interact. 
 
 # An INI file like this is used to specify the IMAP account details to
-# run tests. These files are also supported by IMAPClient's
+# run "live" tests. These files are also supported by IMAPClient's
 # interactive shell.
 
-# WARNING: the tests run by livetest.py are destructive! Only use an
-# IMAP account you don't care about.
-
 [main]
 host = <host>
 username = <user>
@@ -21,3 +18,7 @@ password = <password>
 # oauth_token = ...
 # oauth_token_secret = ...
 
+# oauth2 = false
+# oauth2_client_id = ...
+# oauth2_client_secret = ...
+# oauth2_refresh_token = ...