diff options
author | Frederick Muriuki Muriithi | 2024-07-29 13:19:03 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-08-05 09:52:17 -0500 |
commit | 889ff3116c8e412bb8bf969bd9484269ee716cb0 (patch) | |
tree | dd16324b7bc6b135dc60bfb17ecdcefeeb713bb3 | |
parent | 92d0d0c387fd9d41c8371fcef7d3bd5beacdc2ac (diff) | |
download | gn-uploader-889ff3116c8e412bb8bf969bd9484269ee716cb0.tar.gz |
Enable logout.
-rw-r--r-- | uploader/__init__.py | 3 | ||||
-rw-r--r-- | uploader/oauth2/views.py | 37 | ||||
-rw-r--r-- | uploader/session.py | 3 | ||||
-rw-r--r-- | uploader/templates/base.html | 13 | ||||
-rw-r--r-- | uploader/templates/index.html | 3 | ||||
-rw-r--r-- | 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 @@ <ul class="nav navbar-nav"> <li><a href="/" style="font-weight: bold">GN Uploader</a></li> <li> - <a href="{{gnuri or 'https://genenetwork.org'}}">GeneNetwork</a> + <a href="{{gn2server_uri()}}">GeneNetwork</a> + </li> + </ul> + <ul class="nav navbar-nav" style="margin-left: 2em;"> + <li> + {%if user_logged_in()%} + <a href="{{url_for('oauth2.logout')}}" + title="Log out of the system">Log Out</a> + {%else%} + <a href="{{authserver_authorise_uri()}}" + title="Log in to the system">Log In</a> + {%endif%} </li> </ul> </div> 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%} <div class="row"> + {{flash_all_messages()}} + <h1 class="heading">data upload</h1> <div class="explainer"> diff --git a/uploader/templates/login.html b/uploader/templates/login.html index 68510aa..6ebf72e 100644 --- a/uploader/templates/login.html +++ b/uploader/templates/login.html @@ -1,9 +1,12 @@ {%extends "base.html"%} +{%from "flash_messages.html" import flash_all_messages%} {%block title%}Data Upload{%endblock%} {%block contents%} <div class="row"> + {{flash_all_messages()}} + <h1 class="heading">log in</h1> <div class="explainer"> |