about summary refs log tree commit diff
diff options
context:
space:
mode:
authorClaude Sonnet 5 <noreply@anthropic.com>2026-10-04 09:11:50 +0000
committerFrederick Muriuki Muriithi <fredmanglis@gmail.com>2026-10-05 11:36:26 -0500
commit76d7e68370019c28d55dfbebc0fce85e96839c8c (patch)
tree56174c889866178e0fc7bad56ddae8f8a8dc8533
parent2ebc99cf340547a7276209cf39a54caad62a1bb6 (diff)
downloadgn-auth-76d7e68370019c28d55dfbebc0fce85e96839c8c.tar.gz
fix(resources/views): don't crash on an invalid (not just missing) token
resources_authorisation()'s except _HTTPException branch only set
`resp` when err_msg["error"] == "missing_authorization", leaving it
unset for any other token-related _HTTPException -- in particular
"invalid_token", raised for a malformed or otherwise invalid bearer
token (as opposed to no Authorization header at all). `return resp`
then raised UnboundLocalError, a 500, instead of the same anonymous
public-only fallback this endpoint already gives a request with no
token.

Reproduced: POST /auth/resource/authorisation with
"Authorization: Bearer not-a-real-token" raised UnboundLocalError
before this change, and now returns the same {"public-read": ...}
shape a request with no Authorization header gets.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
-rw-r--r--gn_auth/auth/authorisation/resources/views.py22
1 files changed, 15 insertions, 7 deletions
diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py
index 2737916..3cc2404 100644
--- a/gn_auth/auth/authorisation/resources/views.py
+++ b/gn_auth/auth/authorisation/resources/views.py
@@ -534,13 +534,21 @@ def resources_authorisation():
                 } for resid in resource_ids
             })
     except _HTTPException as _httpe:
-        err_msg = json.loads(_httpe.body)
-        if err_msg["error"] == "missing_authorization":
-            resp = jsonify({
-                str(resid): {
-                    "public-read": resid in pubres
-                } for resid in resource_ids
-            })
+        # Any token-related failure here -- missing header, malformed
+        # token, expired token, etc. -- degrades to the same
+        # public-only view, not just the "no header at all"
+        # (missing_authorization) case. Without this, an invalid
+        # bearer token (error "invalid_token", not
+        # "missing_authorization") left `resp` unset below, raising
+        # UnboundLocalError instead of the graceful anonymous fallback
+        # this endpoint is documented to provide.
+        logger.debug("Token-related exception, falling back to public-only "
+                     "view.", exc_info=True)
+        resp = jsonify({
+            str(resid): {
+                "public-read": resid in pubres
+            } for resid in resource_ids
+        })
     except AssertionError as _aerr:
         resp = jsonify({
             "status": "bad-request",