MCPcopy
hub / github.com/OfflineIMAP/offlineimap / __xoauth2handler

Method __xoauth2handler

offlineimap/imapserver.py:223–278  ·  view source on GitHub ↗
(self, response)

Source from the content-addressed store, hash-verified

221 return retval
222
223 def __xoauth2handler(self, response):
224 now = datetime.datetime.now()
225 if self.oauth2_access_token_expires_at \
226 and self.oauth2_access_token_expires_at < now:
227 self.oauth2_access_token = None
228 self.ui.debug('imap', 'xoauth2handler: oauth2_access_token expired')
229
230 if self.oauth2_access_token is None:
231 if self.oauth2_request_url is None:
232 raise OfflineImapError("No remote oauth2_request_url for "
233 "repository '%s' specified."%
234 self, OfflineImapError.ERROR.REPO)
235
236 # Generate new access token.
237 params = {}
238 params['client_id'] = self.oauth2_client_id
239 params['client_secret'] = self.oauth2_client_secret
240 params['refresh_token'] = self.oauth2_refresh_token
241 params['grant_type'] = 'refresh_token'
242
243 self.ui.debug('imap', 'xoauth2handler: url "%s"'%
244 self.oauth2_request_url)
245 self.ui.debug('imap', 'xoauth2handler: params "%s"'% params)
246
247 original_socket = socket.socket
248 socket.socket = self.authproxied_socket
249 try:
250 response = urllib.urlopen(
251 self.oauth2_request_url, urllib.urlencode(params)).read()
252 except Exception as e:
253 try:
254 msg = "%s (configuration is: %s)"% (e, str(params))
255 except Exception as eparams:
256 msg = "%s [cannot display configuration: %s]"% (e, eparams)
257 six.reraise(type(e), type(e)(msg), exc_info()[2])
258 finally:
259 socket.socket = original_socket
260
261 resp = json.loads(response)
262 self.ui.debug('imap', 'xoauth2handler: response "%s"'% resp)
263 if u'error' in resp:
264 raise OfflineImapError("xoauth2handler got: %s"% resp,
265 OfflineImapError.ERROR.REPO)
266 self.oauth2_access_token = resp['access_token']
267 if u'expires_in' in resp:
268 self.oauth2_access_token_expires_at = now + datetime.timedelta(
269 seconds=resp['expires_in']/2
270 )
271
272 self.ui.debug('imap', 'xoauth2handler: access_token "%s expires %s"'% (
273 self.oauth2_access_token, self.oauth2_access_token_expires_at))
274 auth_string = 'user=%s\1auth=Bearer %s\1\1'% (
275 self.username, self.oauth2_access_token)
276 #auth_string = base64.b64encode(auth_string)
277 self.ui.debug('imap', 'xoauth2handler: returning "%s"'% auth_string)
278 return auth_string
279
280 # Perform the next step handling a GSSAPI connection.

Callers

nothing calls this directly

Calls 3

OfflineImapErrorClass · 0.90
debugMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected