From ddc60f6ee5ac395774ecea0731c52b7b83b79e63 Mon Sep 17 00:00:00 2001 From: Adam Kelly Date: Mon, 29 May 2017 17:41:32 +0100 Subject: [PATCH] Wrap gmail authentication in a multiprocessing Lock. 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. --- bugwarrior/services/gmail.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bugwarrior/services/gmail.py b/bugwarrior/services/gmail.py index 370b92ce0..bcc443ba1 100644 --- a/bugwarrior/services/gmail.py +++ b/bugwarrior/services/gmail.py @@ -2,6 +2,7 @@ import os import email import re +import multiprocessing import googleapiclient.discovery import oauth2client.client @@ -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) @@ -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()