From 889ff3116c8e412bb8bf969bd9484269ee716cb0 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 29 Jul 2024 13:19:03 -0500 Subject: Enable logout. --- uploader/__init__.py | 3 ++- uploader/oauth2/views.py | 37 ++++++++++++++++++++++++++++++++++++- uploader/session.py | 3 +++ uploader/templates/base.html | 13 ++++++++++++- uploader/templates/index.html | 3 +++ uploader/templates/login.html | 3 +++ 6 files changed, 59 insertions(+), 3 deletions(-) diff --git a/uploader/__init__.py b/uploader/__init__.py index 787f220..266495c 100644 --- a/uploader/__init__.py +++ b/uploader/__init__.py @@ -6,7 +6,7 @@ from pathlib import Path from flask import Flask, request from flask_session import Session -from uploader.oauth2.client import authserver_authorise_uri +from uploader.oauth2.client import user_logged_in, authserver_authorise_uri from .entry import entrybp from .upload import upload @@ -47,6 +47,7 @@ def create_app(): app.add_template_global(authserver_authorise_uri) app.add_template_global(lambda: app.config["GN2_SERVER_URL"], name="gn2server_uri") + app.add_template_global(user_logged_in) Session(app) diff --git a/uploader/oauth2/views.py b/uploader/oauth2/views.py index 26c009b..d196e22 100644 --- a/uploader/oauth2/views.py +++ b/uploader/oauth2/views.py @@ -17,7 +17,13 @@ from uploader import session from uploader import monadic_requests as mrequests from . import jwks -from .client import SCOPE, oauth2_get, oauth2_clientid, authserver_uri +from .client import ( + SCOPE, + oauth2_get, + user_logged_in, + authserver_uri, + oauth2_clientid, + oauth2_clientsecret) oauth2 = Blueprint("oauth2", __name__) @@ -100,3 +106,32 @@ def public_jwks(): in jwks.list_jwks(jwks.jwks_directory( app, "UPLOADER_SECRETS"))) }) + + +@oauth2.route("/logout", methods=["GET"]) +def logout(): + """Log out of any active sessions.""" + def __unset_session__(session_info): + _user = session_info["user"] + _user_str = f"{_user['name']} ({_user['email']})" + session.clear_session_info() + flash("Successfully logged out.", "alert-success") + return redirect("/") + + def __handle_failure__(_failure): + app.logger.debug("There was a failure logging out of the system", + exc_info=True, stack_info=True) + __unset_session__(session.session_info()) + return redirect("/") + + if user_logged_in(): + return session.user_token().then( + lambda _tok: mrequests.post( + urljoin(authserver_uri(), "auth/revoke"), + json={ + "token": _tok, + "client_id": oauth2_clientid(), + "client_secret": oauth2_clientsecret() + })).either( + __handle_failure__, + lambda res: __unset_session__(session.session_info())) diff --git a/uploader/session.py b/uploader/session.py index 4706860..8b72bce 100644 --- a/uploader/session.py +++ b/uploader/session.py @@ -82,6 +82,9 @@ def set_user_details(userdets: UserDetails) -> SessionInfo: """Set the user details information""" return save_session_info({**session_info(), "user": userdets})#type: ignore[misc] +def user_details() -> UserDetails: + """Retrieve user details.""" + return session_info()["user"] def user_token() -> Either: """Retrieve the user token.""" diff --git a/uploader/templates/base.html b/uploader/templates/base.html index eb5e6b7..ee60fea 100644 --- a/uploader/templates/base.html +++ b/uploader/templates/base.html @@ -33,7 +33,18 @@
+ diff --git a/uploader/templates/index.html b/uploader/templates/index.html index 89d2ae9..94060b7 100644 --- a/uploader/templates/index.html +++ b/uploader/templates/index.html @@ -1,9 +1,12 @@ {%extends "base.html"%} +{%from "flash_messages.html" import flash_all_messages%} {%block title%}Data Upload{%endblock%} {%block contents%}