about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-06-03 16:10:39 -0500
committerAlexander_Kabui2024-08-28 15:02:45 +0300
commitdd4fd626ad763a890e130ac48087c40525dac207 (patch)
tree3d2c266630a851bc35c05a0f6ac868b7d9af2b7d
parent111593d7e1685db33188eb03685210db6656c0c8 (diff)
downloadgenenetwork2-dd4fd626ad763a890e130ac48087c40525dac207.tar.gz
Handle generic OAuthError gracefully
-rw-r--r--gn2/wqflask/app_errors.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/gn2/wqflask/app_errors.py b/gn2/wqflask/app_errors.py
index 503f4e1c..5b85bd53 100644
--- a/gn2/wqflask/app_errors.py
+++ b/gn2/wqflask/app_errors.py
@@ -6,7 +6,8 @@ import traceback
 from uuid import uuid4
 
 from werkzeug.exceptions import InternalServerError
-from authlib.integrations.base_client.errors import InvalidTokenError
+from authlib.integrations.base_client.errors import (
+    OAuthError, InvalidTokenError)
 from flask import (
     flash, request, redirect, current_app, render_template, make_response)
 
@@ -46,13 +47,35 @@ def handle_authorisation_error(exc: AuthorisationError):
         "authorisation_error.html", error_type=type(exc).__name__, error=exc)
 
 def handle_invalid_token_error(exc: InvalidTokenError):
+    """Handle InvalidTokenError"""
     flash("An invalid session token was detected. "
           "You have been logged out of the system.",
           "alert-danger")
     session.clear_session_info()
     return redirect("/")
 
+def __build_message__(exc: OAuthError) -> str:
+    """Build up the message to flash for any OAuthError"""
+    match exc.args[0]:
+        case "ForbiddenAccess: Token does not belong to client.":
+            return "An invalid token was used. The session has been cleared."
+        case "ForbiddenAccess: Token is expired.":
+            return "The session has expired."
+        case "ForbiddenAccess: Token has previously been revoked.":
+            return "Revoked token was used. The session has been cleared."
+        case _:
+            return exc.args[0]
+
+def handle_oauth_error(exc: OAuthError):
+    """Handle generic OAuthError"""
+    flash((f"{type(exc).__name__}: {__build_message__(exc)} "
+           "Please log in again to continue."),
+          "alert-danger")
+    session.clear_session_info()
+    return redirect("/")
+
 __handlers__ = {
+    OAuthError: handle_oauth_error,
     AuthorisationError: handle_authorisation_error,
     ExternalRequestError: lambda exc: render_error(exc),
     InternalServerError: lambda exc: render_error(exc),