From 5c8f97a8ddf64e01f85ed53e5e2026456d6f75ab Mon Sep 17 00:00:00 2001
From: Jonathan Harris <github@marginal.org.uk>
Date: Thu, 3 Jan 2019 15:53:18 +0000
Subject: [PATCH] Fix for hang on failed authentication through Steam

Fixes #376 & #377
---
 EDMarketConnector.py | 3 ++-
 README.md            | 2 +-
 companion.py         | 7 +++++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/EDMarketConnector.py b/EDMarketConnector.py
index 0fc998ad..0834156d 100755
--- a/EDMarketConnector.py
+++ b/EDMarketConnector.py
@@ -465,6 +465,8 @@ class AppWindow:
                         if config.getint('output') & config.OUT_MKT_TD:
                             td.export(fixed)
 
+                self.holdofftime = querytime + companion.holdoff
+
         # Companion API problem
         except (companion.ServerError, companion.ServerLagging) as e:
             if retrying:
@@ -491,7 +493,6 @@ class AppWindow:
         if play_sound and play_bad:
             hotkeymgr.play_bad()
 
-        self.holdofftime = querytime + companion.holdoff
         self.cooldown()
 
     def retry_for_shipyard(self, tries):
diff --git a/README.md b/README.md
index 319f5f5a..a1bce460 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ Windows:
 
 Setup
 --------
-The first time that you run the app while playing the game you are redirected to Frontier's authentication website and prompted for your username and password. This is the same username and password combination that you use to log into the Elite: Dangerous launcher. If you run the game via Steam, press the Steam button on the website and enter your Steam credentials instead. This step is required so that the Frontier servers can send the app *your* data and the data for the station that *you* are docked at.
+The first time that you run the app while playing the game you are redirected to Frontier's authentication website and prompted for your username and password. This is the same username and password combination that you use to log into the Elite: Dangerous launcher. If you run the game via Steam and **don't** have a Frontier login, press the Steam button on the website and enter your Steam credentials instead. This step is required so that the Frontier servers can send the app *your* data and the data for the station that *you* are docked at.
 
 Refer to the [Privacy Policy](PRIVACY.md) for how this app handles your data.
 
diff --git a/companion.py b/companion.py
index eab3b2bb..ecd68eef 100644
--- a/companion.py
+++ b/companion.py
@@ -342,6 +342,13 @@ class Session:
             if __debug__: print_exc()
             raise ServerError()
 
+        if r.url.startswith(SERVER_AUTH):
+           # Redirected back to Auth server - force full re-authentication
+            self.dump(r)
+            self.invalidate()
+            self.login()
+            raise CredentialsError()
+
         try:
             r.raise_for_status()
             data = r.json()	# Will fail here if token expired since response is empty