From 2babf1077277d6654203f642994866007aff3509 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 9 May 2023 13:19:50 +0300 Subject: Integrate OAuth2 auth with GN2 UI Update the templates to make use of the OAuth2 authentication rather than the older authentication that used Redis. --- wqflask/wqflask/__init__.py | 7 ++++- wqflask/wqflask/oauth2/checks.py | 3 +- wqflask/wqflask/oauth2/request_utils.py | 14 +++++++-- wqflask/wqflask/oauth2/toplevel.py | 6 ++-- wqflask/wqflask/oauth2/ui.py | 2 +- wqflask/wqflask/templates/base.html | 33 ++++++++++++++++++---- .../wqflask/templates/oauth2/create-resource.html | 2 +- wqflask/wqflask/templates/oauth2/create-role.html | 2 +- .../templates/oauth2/data-list-genotype.html | 2 +- .../wqflask/templates/oauth2/data-list-mrna.html | 2 +- .../templates/oauth2/data-list-phenotype.html | 2 +- wqflask/wqflask/templates/oauth2/data-list.html | 2 +- wqflask/wqflask/templates/oauth2/group.html | 2 +- .../templates/oauth2/group_join_or_create.html | 2 +- .../wqflask/templates/oauth2/join-requests.html | 2 +- wqflask/wqflask/templates/oauth2/list_roles.html | 2 +- wqflask/wqflask/templates/oauth2/profile_nav.html | 4 +-- .../wqflask/templates/oauth2/request_error.html | 2 +- wqflask/wqflask/templates/oauth2/resources.html | 2 +- wqflask/wqflask/templates/oauth2/role.html | 2 +- .../wqflask/templates/oauth2/view-group-role.html | 2 +- .../wqflask/templates/oauth2/view-resource.html | 2 +- wqflask/wqflask/templates/oauth2/view-user.html | 2 +- 23 files changed, 69 insertions(+), 32 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/__init__.py b/wqflask/wqflask/__init__.py index 66ed0e91..38a69d33 100644 --- a/wqflask/wqflask/__init__.py +++ b/wqflask/wqflask/__init__.py @@ -31,6 +31,8 @@ from wqflask.api.markdown import blogs_blueprint from wqflask.api.markdown import news_blueprint from wqflask.api.jobs import jobs as jobs_bp from wqflask.oauth2.routes import oauth2 +from wqflask.oauth2.checks import user_logged_in +from wqflask.oauth2.request_utils import user_details, authserver_authorise_uri from wqflask.jupyter_notebooks import jupyter_notebooks @@ -50,7 +52,10 @@ app.config["DB_PORT"] = DB_PORT app.jinja_env.globals.update( undefined=jinja2.StrictUndefined, - numify=formatting.numify) + numify=formatting.numify, + logged_in=user_logged_in, + authserver_authorise_uri=authserver_authorise_uri, + user_details=user_details) app.config["SESSION_REDIS"] = redis.from_url(app.config["REDIS_URL"]) diff --git a/wqflask/wqflask/oauth2/checks.py b/wqflask/wqflask/oauth2/checks.py index e6859c4a..c60ab1de 100644 --- a/wqflask/wqflask/oauth2/checks.py +++ b/wqflask/wqflask/oauth2/checks.py @@ -29,6 +29,7 @@ def require_oauth2(func): session.pop("user_details", None) flash("You need to be logged in.", "alert-warning") - return redirect(url_for("oauth2.user.login", next=request.endpoint)) + # return redirect(url_for("oauth2.user.login", next=request.endpoint)) + return redirect("/") return __token_valid__ diff --git a/wqflask/wqflask/oauth2/request_utils.py b/wqflask/wqflask/oauth2/request_utils.py index 0cd1697b..ac21e223 100644 --- a/wqflask/wqflask/oauth2/request_utils.py +++ b/wqflask/wqflask/oauth2/request_utils.py @@ -1,12 +1,22 @@ """General request utilities""" from typing import Optional +from urllib.parse import urljoin, urlparse import simplejson from flask import ( - flash, session, url_for, redirect, Response, render_template, + flash, request, session, url_for, redirect, Response, render_template, current_app as app) -from .client import oauth2_get +from .client import SCOPE, oauth2_get + +def authserver_authorise_uri(): + req_baseurl = urlparse(request.base_url) + host_uri = f"{req_baseurl.scheme}://{req_baseurl.netloc}/" + return urljoin( + app.config["GN_SERVER_URL"], + "oauth2/authorise?response_type=code" + f"&client_id={app.config['OAUTH2_CLIENT_ID']}" + f"&redirect_uri={urljoin(host_uri, 'oauth2/code')}") def raise_unimplemented(): raise Exception("NOT IMPLEMENTED") diff --git a/wqflask/wqflask/oauth2/toplevel.py b/wqflask/wqflask/oauth2/toplevel.py index df2ff0aa..109ed06c 100644 --- a/wqflask/wqflask/oauth2/toplevel.py +++ b/wqflask/wqflask/oauth2/toplevel.py @@ -4,15 +4,12 @@ from flask import ( flash, request, session, Blueprint, url_for, redirect, render_template, current_app as app) -from .client import no_token_post +from .client import SCOPE, no_token_post from .request_utils import process_error from .checks import require_oauth2, user_logged_in toplevel = Blueprint("toplevel", __name__) - - - @toplevel.route("/register-client", methods=["GET", "POST"]) @require_oauth2 def register_client(): @@ -36,6 +33,7 @@ def authorisation_code(): request_data = { "grant_type": "authorization_code", "code": code, + "scope": SCOPE, "redirect_uri": urljoin( request.base_url, url_for("oauth2.toplevel.authorisation_code")), diff --git a/wqflask/wqflask/oauth2/ui.py b/wqflask/wqflask/oauth2/ui.py index 4fdbe869..c5ea1468 100644 --- a/wqflask/wqflask/oauth2/ui.py +++ b/wqflask/wqflask/oauth2/ui.py @@ -6,7 +6,7 @@ from .request_utils import process_error def render_ui(templatepath: str, **kwargs): """Handle repetitive UI rendering stuff.""" - logged_in = ("oauth2_token" in session and bool(session["oauth2_token"])) + logged_in = lambda: ("oauth2_token" in session and bool(session["oauth2_token"])) roles = kwargs.get("roles", tuple()) # Get roles if already provided if logged_in and not bool(roles): # If not, try fetching them roles_results = oauth2_get("oauth2/user/roles").either( diff --git a/wqflask/wqflask/templates/base.html b/wqflask/wqflask/templates/base.html index 518510de..38819240 100644 --- a/wqflask/wqflask/templates/base.html +++ b/wqflask/wqflask/templates/base.html @@ -162,14 +162,37 @@ {% endif %} + + + + + + +