XOAUTH2: don't force oauth2_request_url to be defined

Not all users want XOAUTH2.

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht 2016-04-25 18:38:31 +02:00
parent be940f3784
commit 2fb2f840dc
2 changed files with 21 additions and 17 deletions

View File

@ -220,36 +220,45 @@ class IMAPServer:
def __xoauth2handler(self, response): def __xoauth2handler(self, response):
if self.oauth2_refresh_token is None and self.oauth2_access_token is None: if self.oauth2_refresh_token is None \
and self.oauth2_access_token is None:
return None return None
if self.oauth2_access_token is None: if self.oauth2_access_token is None:
# need to move these to config if self.oauth2_request_url is None:
# generate new access token raise OfflineImapError("No remote oauth2_request_url for "
"repository '%s' specified."%
self, OfflineImapError.ERROR.REPO)
# Generate new access token.
params = {} params = {}
params['client_id'] = self.oauth2_client_id params['client_id'] = self.oauth2_client_id
params['client_secret'] = self.oauth2_client_secret params['client_secret'] = self.oauth2_client_secret
params['refresh_token'] = self.oauth2_refresh_token params['refresh_token'] = self.oauth2_refresh_token
params['grant_type'] = 'refresh_token' params['grant_type'] = 'refresh_token'
self.ui.debug('imap', 'xoauth2handler: url "%s"' % self.oauth2_request_url) self.ui.debug('imap', 'xoauth2handler: url "%s"'%
self.ui.debug('imap', 'xoauth2handler: params "%s"' % params) self.oauth2_request_url)
self.ui.debug('imap', 'xoauth2handler: params "%s"'% params)
original_socket = socket.socket original_socket = socket.socket
socket.socket = self.proxied_socket socket.socket = self.proxied_socket
try: try:
response = urllib.urlopen(self.oauth2_request_url, urllib.urlencode(params)).read() response = urllib.urlopen(
self.oauth2_request_url, urllib.urlencode(params)).read()
finally: finally:
socket.socket = original_socket socket.socket = original_socket
resp = json.loads(response) resp = json.loads(response)
self.ui.debug('imap', 'xoauth2handler: response "%s"' % resp) self.ui.debug('imap', 'xoauth2handler: response "%s"'% resp)
self.oauth2_access_token = resp['access_token'] self.oauth2_access_token = resp['access_token']
self.ui.debug('imap', 'xoauth2handler: access_token "%s"' % self.oauth2_access_token) self.ui.debug('imap', 'xoauth2handler: access_token "%s"'%
auth_string = 'user=%s\1auth=Bearer %s\1\1' % (self.username, self.oauth2_access_token) self.oauth2_access_token)
auth_string = 'user=%s\1auth=Bearer %s\1\1'% (
self.username, self.oauth2_access_token)
#auth_string = base64.b64encode(auth_string) #auth_string = base64.b64encode(auth_string)
self.ui.debug('imap', 'xoauth2handler: returning "%s"' % auth_string) self.ui.debug('imap', 'xoauth2handler: returning "%s"'% auth_string)
return auth_string return auth_string
def __gssauth(self, response): def __gssauth(self, response):

View File

@ -268,14 +268,9 @@ class IMAPRepository(BaseRepository):
if self._oauth2_request_url: # Use cached value if possible. if self._oauth2_request_url: # Use cached value if possible.
return self._oauth2_request_url return self._oauth2_request_url
oauth2_request_url = self.getconf('oauth2_request_url', None) self.oauth2_request_url = self.getconf('oauth2_request_url', None)
if oauth2_request_url != None:
self._oauth2_request_url = oauth2_request_url
return self._oauth2_request_url return self._oauth2_request_url
raise OfflineImapError("No remote oauth2_request_url for repository "
"'%s' specified."% self, OfflineImapError.ERROR.REPO)
def getoauth2_refresh_token(self): def getoauth2_refresh_token(self):
return self.getconf('oauth2_refresh_token', None) return self.getconf('oauth2_refresh_token', None)