Skip to content

Commit

Permalink
Wrap gmail authentication in a multiprocessing Lock.
Browse files Browse the repository at this point in the history
Attempting to authenticate multiple accounts (or multiple targets
against the same account) at once is confusing - so let's do it
one at a time.
  • Loading branch information
cthulahoops authored and ryneeverett committed Jun 18, 2017
1 parent f914c13 commit ddc60f6
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions bugwarrior/services/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import email
import re
import multiprocessing

import googleapiclient.discovery
import oauth2client.client
Expand Down Expand Up @@ -85,6 +86,7 @@ class GmailService(IssueService):

ISSUE_CLASS = GmailIssue
CONFIG_PREFIX = 'gmail'
AUTHENTICATION_LOCK = multiprocessing.Lock()

def __init__(self, *args, **kw):
super(GmailService, self).__init__(*args, **kw)
Expand Down Expand Up @@ -118,16 +120,18 @@ def get_credentials(self):
Returns:
Credentials, the obtained credential.
"""
store = oauth2client.file.Storage(self.credentials_path)
credentials = store.get()
if not credentials or credentials.invalid:
log.info("No valid login. Starting OAUTH flow.")
flow = oauth2client.client.flow_from_clientsecrets(self.client_secret_path, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
flags = oauth2client.tools.argparser.parse_args()
credentials = oauth2client.tools.run_flow(flow, store, flags)
log.info('Storing credentials to %r', self.credentials_path)
return credentials
with self.AUTHENTICATION_LOCK:
log.info('Starting authentication for %s', self.target)
store = oauth2client.file.Storage(self.credentials_path)
credentials = store.get()
if not credentials or credentials.invalid:
log.info("No valid login. Starting OAUTH flow.")
flow = oauth2client.client.flow_from_clientsecrets(self.client_secret_path, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
flags = oauth2client.tools.argparser.parse_args()
credentials = oauth2client.tools.run_flow(flow, store, flags)
log.info('Storing credentials to %r', self.credentials_path)
return credentials

def get_labels(self):
result = self.gmail_api.users().labels().list(userId=self.login_name).execute()
Expand Down

0 comments on commit ddc60f6

Please sign in to comment.