diff options
-rw-r--r-- | uploader/__init__.py | 5 | ||||
-rw-r--r-- | uploader/default_settings.py | 2 | ||||
-rw-r--r-- | uploader/entry.py | 3 | ||||
-rw-r--r-- | uploader/oauth2/client.py | 20 | ||||
-rw-r--r-- | uploader/templates/login.html | 29 |
5 files changed, 56 insertions, 3 deletions
diff --git a/uploader/__init__.py b/uploader/__init__.py index a3b36e7..e4b5b32 100644 --- a/uploader/__init__.py +++ b/uploader/__init__.py @@ -6,6 +6,8 @@ from pathlib import Path from flask import Flask, request from flask_session import Session +from uploader.oauth2.client import authserver_authorise_uri + from .entry import entrybp from .upload import upload from .parse import parsebp @@ -41,6 +43,9 @@ def create_app(): # setup jinja2 symbols app.add_template_global(lambda : request.url, name="request_url") + app.add_template_global(authserver_authorise_uri) + app.add_template_global(lambda: app.config["GN2_SERVER_URL"], + name="gn2server_uri") Session(app) diff --git a/uploader/default_settings.py b/uploader/default_settings.py index 3add7ef..f7ca48c 100644 --- a/uploader/default_settings.py +++ b/uploader/default_settings.py @@ -13,6 +13,8 @@ JOBS_TTL_SECONDS = 1209600 # 14 days GNQC_REDIS_PREFIX="GNQC" SQL_URI = "" +GN2_SERVER_URL = "https://genenetwork.org/" + SESSION_TYPE = "redis" SESSION_PERMANENT = True SESSION_USE_SIGNER = True diff --git a/uploader/entry.py b/uploader/entry.py index 4a02f1e..941200a 100644 --- a/uploader/entry.py +++ b/uploader/entry.py @@ -17,6 +17,7 @@ from flask import ( from uploader.db import species from uploader.db_utils import with_db_connection +from uploader.oauth2.client import user_logged_in entrybp = Blueprint("entry", __name__) @@ -87,7 +88,7 @@ def zip_file_errors(filepath, upload_dir) -> Tuple[str, ...]: @entrybp.route("/", methods=["GET"]) def index(): """Load the landing page""" - return render_template("index.html") + return render_template("index.html" if user_logged_in() else "login.html") @entrybp.route("/upload", methods=["GET", "POST"]) def upload_file(): diff --git a/uploader/oauth2/client.py b/uploader/oauth2/client.py index 6e101ae..f20af4a 100644 --- a/uploader/oauth2/client.py +++ b/uploader/oauth2/client.py @@ -1,9 +1,9 @@ """OAuth2 client utilities.""" -from urllib.parse import urljoin from datetime import datetime, timedelta +from urllib.parse import urljoin, urlparse import requests -from flask import current_app as app +from flask import request, current_app as app from pymonad.either import Left, Right @@ -99,3 +99,19 @@ def oauth2_client(): return session.user_token().either( lambda _notok: __client__(None), __client__) + + +def user_logged_in(): + """Check whether the user has logged in.""" + suser = session.session_info()["user"] + # return suser["logged_in"] and suser["token"].is_right() + return False + +def authserver_authorise_uri(): + req_baseurl = urlparse(request.base_url, scheme=request.scheme) + host_uri = f"{req_baseurl.scheme}://{req_baseurl.netloc}/" + return urljoin( + authserver_uri(), + "auth/authorise?response_type=code" + f"&client_id={oauth2_clientid()}" + f"&redirect_uri={urljoin(host_uri, 'oauth2/code')}") diff --git a/uploader/templates/login.html b/uploader/templates/login.html new file mode 100644 index 0000000..68510aa --- /dev/null +++ b/uploader/templates/login.html @@ -0,0 +1,29 @@ +{%extends "base.html"%} + +{%block title%}Data Upload{%endblock%} + +{%block contents%} +<div class="row"> + <h1 class="heading">log in</h1> + + <div class="explainer"> + <p> + This system enables you to upload data onto GeneNetwork. In order to do + that correctly, we need to know who you are.</p> + <p> + If you already have an account with GeneNetwork, you can simply click the + login button below, after which you can upload your data.<br /> + </p> + <a href="{{authserver_authorise_uri()}}" class="btn btn-primary" + style="display:block;width:15em;align:center;margin:1em 3em;"> + log in</a> + <p> + If you do not have an account with GeneNetwork, go to + <a href="{{gn2server_uri()}}" + title="GeneNetwork Service." + target="_blank">GeneNetwork</a> + and register for an account, then come back here to login and upload.</a> + </div> +</div> + +{%endblock%} |