Handle running offlineimap on ssh remote

This commit is contained in:
cakemanny 2022-04-01 09:41:41 +02:00 committed by Gerrit Oomens
parent 76814733d7
commit 1c7eeb6f5c

View file

@ -1,6 +1,7 @@
from msal import ConfidentialClientApplication, SerializableTokenCache from msal import ConfidentialClientApplication, SerializableTokenCache
import config import config
import http.server import http.server
import os
import sys import sys
import threading import threading
import urllib.parse 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) 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: try:
webbrowser.open(url) webbrowser.open(url)
except Exception: except Exception:
print('Navigate to the following url in a web browser:') pass
print(url)
class Handler(http.server.BaseHTTPRequestHandler): class Handler(http.server.BaseHTTPRequestHandler):
@ -45,7 +48,11 @@ code = ''
server_address = ('', 8745) server_address = ('', 8745)
httpd = http.server.HTTPServer(server_address, Handler) 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 == '': 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.') print('After login, you will be redirected to a blank (or error) page with a url containing an access code. Paste the url below.')