From 046a7885eaa2c834c1b76d2172e581770815c711 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 19 Aug 2022 11:50:18 -0600 Subject: [PATCH] support custom port for gmail oauth2 local server (#341) --- README.rst | 1 + docs/index.rst | 1 + parsedmarc/cli.py | 7 ++++++- parsedmarc/mail/gmail.py | 9 +++++---- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index e437e8b..f2f6dd3 100644 --- a/README.rst +++ b/README.rst @@ -295,6 +295,7 @@ The full set of configuration options are: - ``token_file`` - str: Path to save the token file (Default: .token) - ``include_spam_trash`` - bool: Include messages in Spam and Trash when searching reports (Default: False) - ``scopes`` - str: Comma separated list of scopes to use when acquiring credentials (Default: https://www.googleapis.com/auth/gmail.modify) + - ``oauth2_port`` - int: The TCP port for the local server to listen on for the OAuth2 response (Default: 8080) .. warning:: diff --git a/docs/index.rst b/docs/index.rst index 49d1a52..b892bcf 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -294,6 +294,7 @@ The full set of configuration options are: - ``token_file`` - str: Path to save the token file (Default: .token) - ``include_spam_trash`` - bool: Include messages in Spam and Trash when searching reports (Default: False) - ``scopes`` - str: Comma separated list of scopes to use when acquiring credentials (Default: https://www.googleapis.com/auth/gmail.modify) + - ``oauth2_port`` - int: The TCP port for the local server to listen on for the OAuth2 response (Default: 8080) .. warning:: diff --git a/parsedmarc/cli.py b/parsedmarc/cli.py index 56683db..2012606 100644 --- a/parsedmarc/cli.py +++ b/parsedmarc/cli.py @@ -334,6 +334,7 @@ def _main(): gmail_api_token_file=None, gmail_api_include_spam_trash=False, gmail_api_scopes=[], + gmail_api_oauth2_port=8080, log_file=args.log_file, n_procs=1, chunk_size=1, @@ -733,6 +734,9 @@ def _main(): default_gmail_api_scope) opts.gmail_api_scopes = \ _str_to_list(opts.gmail_api_scopes) + if "oauth2_port" in gmail_api_config: + opts.gmail_api_oauth2_port = \ + gmail_api_config.get("oauth2_port", 8080) logger.setLevel(logging.WARNING) @@ -925,7 +929,8 @@ def _main(): token_file=opts.gmail_api_token_file, scopes=opts.gmail_api_scopes, include_spam_trash=opts.gmail_api_include_spam_trash, - reports_folder=opts.mailbox_reports_folder + reports_folder=opts.mailbox_reports_folder, + oauth2_port=opts.gmail_api_oauth2_port ) except Exception as error: diff --git a/parsedmarc/mail/gmail.py b/parsedmarc/mail/gmail.py index 1b85430..504d28a 100644 --- a/parsedmarc/mail/gmail.py +++ b/parsedmarc/mail/gmail.py @@ -16,7 +16,7 @@ from parsedmarc.mail.mailbox_connection import MailboxConnection logger = logging.getLogger("parsedmarc") -def _get_creds(token_file, credentials_file, scopes): +def _get_creds(token_file, credentials_file, scopes, oauth2_port): creds = None if Path(token_file).exists(): @@ -29,7 +29,7 @@ def _get_creds(token_file, credentials_file, scopes): else: flow = InstalledAppFlow.from_client_secrets_file( credentials_file, scopes) - creds = flow.run_local_server(open_browser=False) + creds = flow.run_local_server(open_browser=False, oauth2_port=oauth2_port) # Save the credentials for the next run with Path(token_file).open('w') as token: token.write(creds.to_json()) @@ -42,8 +42,9 @@ class GmailConnection(MailboxConnection): credentials_file: str, scopes: List[str], include_spam_trash: bool, - reports_folder: str): - creds = _get_creds(token_file, credentials_file, scopes) + reports_folder: str, + oauth2_port: int): + creds = _get_creds(token_file, credentials_file, scopes, oauth2_port) self.service = build('gmail', 'v1', credentials=creds) self.include_spam_trash = include_spam_trash self.reports_label_id = self._find_label_id_for_label(reports_folder)