about summary refs log tree commit diff
path: root/wqflask
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask')
-rw-r--r--wqflask/wqflask/__init__.py6
-rw-r--r--wqflask/wqflask/app_errors.py24
2 files changed, 25 insertions, 5 deletions
diff --git a/wqflask/wqflask/__init__.py b/wqflask/wqflask/__init__.py
index f4599a0b..0313e24e 100644
--- a/wqflask/wqflask/__init__.py
+++ b/wqflask/wqflask/__init__.py
@@ -81,9 +81,9 @@ app.register_blueprint(group_management, url_prefix="/group-management")
 app.register_blueprint(jobs_bp, url_prefix="/jobs")
 app.register_blueprint(oauth2, url_prefix="/oauth2")
 
-from wqflask.decorators import AuthorisationError
-from wqflask.app_errors import handle_authorisation_error
-app.register_error_handler(AuthorisationError, handle_authorisation_error)
+from wqflask.app_errors import register_error_handlers
+register_error_handlers(app)
+
 try:
     check_mandatory_configs(app)
 except StartupError as serr:
diff --git a/wqflask/wqflask/app_errors.py b/wqflask/wqflask/app_errors.py
index 18b29841..8847ad2e 100644
--- a/wqflask/wqflask/app_errors.py
+++ b/wqflask/wqflask/app_errors.py
@@ -1,7 +1,8 @@
 """Handle errors at the application's top-level"""
+from flask import flash, redirect, current_app, render_template
+from authlib.integrations.base_client.errors import InvalidTokenError
 
-from flask import current_app, render_template
-
+from wqflask.oauth2 import session
 from wqflask.decorators import AuthorisationError
 
 def handle_authorisation_error(exc: AuthorisationError):
@@ -9,3 +10,22 @@ def handle_authorisation_error(exc: AuthorisationError):
     current_app.logger.error(exc)
     return render_template(
         "authorisation_error.html", error_type=type(exc).__name__, error=exc)
+
+def handle_invalid_token_error(exc: InvalidTokenError):
+    flash("An invalid session token was detected. "
+          "You have been logged out of the system.",
+          "alert-danger")
+    session.clear_session_info()
+    return redirect("/")
+
+__handlers__ = {
+    AuthorisationError: handle_authorisation_error,
+    InvalidTokenError: handle_invalid_token_error
+}
+
+def register_error_handlers(app):
+    """Register all error handlers."""
+    for klass, handler in __handlers__.items():
+        app.register_error_handler(klass, handler)
+
+    return app