From: Menno Smits Date: Sun, 4 Nov 2012 23:58:45 +0000 (+0000) Subject: Added OAUTH2 support to livetest/interact configs X-Git-Tag: 0.9.1~4 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=3ca8c568393174c07bc3b799e99eeff7811e31e8;p=imapclient.git Added OAUTH2 support to livetest/interact configs --- diff --git a/imapclient/config.py b/imapclient/config.py index 6c8d12e..2e54e2c 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -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 diff --git a/livetest-sample.ini b/livetest-sample.ini index a7b1165..8494add 100644 --- a/livetest-sample.ini +++ b/livetest-sample.ini @@ -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 = username = @@ -21,3 +18,7 @@ password = # oauth_token = ... # oauth_token_secret = ... +# oauth2 = false +# oauth2_client_id = ... +# oauth2_client_secret = ... +# oauth2_refresh_token = ...