From b4cda492a76f549cd794abefa7aceb3d56b94bee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20K=C3=BCnzel?= <simonk@fsmpi.rwth-aachen.de>
Date: Sat, 10 May 2025 01:00:13 +0200
Subject: [PATCH] Better handling of OAuth servers response

---
 api/src/api/authentication.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/api/src/api/authentication.py b/api/src/api/authentication.py
index 3c80f7b..0d880cf 100644
--- a/api/src/api/authentication.py
+++ b/api/src/api/authentication.py
@@ -396,10 +396,21 @@ else:
             raise ValueError("Session auth scope set to something different than rwth or moodle")
         try:
             token_response = __make_oauth_request("token", code=api_session.running_oauth_code, grant_type="device")
-            if token_response.get("status") != "ok":
-                # TODO debug statement
-                print(f"Got non-'ok' status for OAuth: {token_response}")
-                return
+            match token_response.get("status"):
+                case "error: authorization pending.":
+                    return
+                case "error: device code invalid.":
+                    # We get this when the user declines the authorization
+                    api_session.running_oauth_code = None
+                    api_session.running_oauth_scope = None
+                    return
+                case "error: slow down.":
+                    ERROR_NOTIFIER.notify("Warning: Got 'error: slow down.' from RWTH OAuth server")
+                    return
+                case "ok":
+                    pass
+                case _:
+                    raise Exception(f"Got unknown status from RWTH OAuth server: {token_response.get("status")}. Full response: {token_response}")
             api_session.running_oauth_code = None
             api_session.running_oauth_scope = None
             
-- 
GitLab