From cbdacc5965fc401f7401b977aef806230bf556ef Mon Sep 17 00:00:00 2001 From: Jason Gunthorpe Date: Sun, 14 Jun 2020 22:27:21 -0300 Subject: OAUTH: Only start one webbrowser tab for concurrent authentications Starting a browser tab per authentication attempt makes a mess, so long as there is something in auth_redirs then the active browser tab will eventually get redirected to the new auth being added. Signed-off-by: Jason Gunthorpe --- cloud_mdir_sync/gmail.py | 5 ----- cloud_mdir_sync/oauth.py | 14 ++++++++++++++ cloud_mdir_sync/office365.py | 5 ----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cloud_mdir_sync/gmail.py b/cloud_mdir_sync/gmail.py index 15ba7e3..121e3b6 100644 --- a/cloud_mdir_sync/gmail.py +++ b/cloud_mdir_sync/gmail.py @@ -6,7 +6,6 @@ import datetime import functools import logging import secrets -import webbrowser from typing import Dict, List, Optional, Set import aiohttp @@ -147,10 +146,6 @@ class GmailAPI(oauth.Account): scopes=self.scopes, login_hint=self.user) - print( - f"Goto {self.cfg.web_app.url} in a web browser to authenticate" - ) - webbrowser.open(url) q = await self.cfg.web_app.auth_redir(url, state, self.redirect_url) diff --git a/cloud_mdir_sync/oauth.py b/cloud_mdir_sync/oauth.py index 0ab30ae..716e7c0 100644 --- a/cloud_mdir_sync/oauth.py +++ b/cloud_mdir_sync/oauth.py @@ -4,6 +4,7 @@ import base64 import hashlib import os import secrets +import webbrowser from abc import abstractmethod from typing import TYPE_CHECKING, Dict, List, Optional @@ -59,6 +60,19 @@ class WebServer(object): redirects back to the localhost server. The final query paremeters will be returned by this function""" queue = asyncio.Queue() + + # If this is the first auth to start then automatically launch a + # browser, otherwise assume the already running browser will take care + # of things + if not self.auth_redirs: + print( + f"Goto {self.url} in a web browser to authenticate (opening browser)" + ) + webbrowser.open(url) + else: + print( + f"Goto {self.url} in a web browser to authenticate (reusing browser)" + ) self.auth_redirs[state] = (url, queue, redir_url) return await queue.get() diff --git a/cloud_mdir_sync/office365.py b/cloud_mdir_sync/office365.py index d76c891..030254b 100644 --- a/cloud_mdir_sync/office365.py +++ b/cloud_mdir_sync/office365.py @@ -7,7 +7,6 @@ import logging import os import pickle import secrets -import webbrowser from typing import Any, Dict, Optional, Union import aiohttp @@ -187,10 +186,6 @@ class GraphAPI(oauth.Account): scopes=self.graph_scopes + self.owa_scopes, login_hint=self.user) - print( - f"Goto {self.cfg.web_app.url} in a web browser to authenticate" - ) - webbrowser.open(url) q = await self.cfg.web_app.auth_redir(url, state, self.redirect_url) -- cgit v1.2.3