From 1c7eeb6f5c62bfbc4ebac340a0e53ee62232981d Mon Sep 17 00:00:00 2001 From: cakemanny Date: Fri, 1 Apr 2022 09:41:41 +0200 Subject: [PATCH] Handle running offlineimap on ssh remote --- get_token.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/get_token.py b/get_token.py index 3aa27ca..93bde9b 100644 --- a/get_token.py +++ b/get_token.py @@ -1,6 +1,7 @@ from msal import ConfidentialClientApplication, SerializableTokenCache import config import http.server +import os import sys import threading import urllib.parse @@ -15,11 +16,13 @@ app = ConfidentialClientApplication(config.ClientId, client_credential=config.Cl url = app.get_authorization_request_url(config.Scopes, redirect_uri=redirect_uri) +# webbrowser.open may fail silently +print("Navigate to the following url in a web browser, if doesn't open automatically:") +print(url) try: webbrowser.open(url) except Exception: - print('Navigate to the following url in a web browser:') - print(url) + pass class Handler(http.server.BaseHTTPRequestHandler): @@ -45,7 +48,11 @@ code = '' server_address = ('', 8745) httpd = http.server.HTTPServer(server_address, Handler) -httpd.serve_forever() + +# If we are running over ssh then the browser on the local machine +# would never be able access localhost:8745 +if not os.getenv('SSH_CONNECTION'): + httpd.serve_forever() if code == '': print('After login, you will be redirected to a blank (or error) page with a url containing an access code. Paste the url below.')