From 10ddca2c2fd6b14bbd94fe57aed66a6de03e1777 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 2 Jan 2019 17:58:15 -0800 Subject: start on webface oauth2/oidc web auth --- python/fatcat_web/__init__.py | 12 +++++++++++- python/fatcat_web/auth.py | 27 +++++++++++++++++++++++++++ python/fatcat_web/routes.py | 13 +++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 python/fatcat_web/auth.py (limited to 'python/fatcat_web') diff --git a/python/fatcat_web/__init__.py b/python/fatcat_web/__init__.py index 3c790e7a..f8b72fd0 100644 --- a/python/fatcat_web/__init__.py +++ b/python/fatcat_web/__init__.py @@ -2,6 +2,9 @@ from flask import Flask from flask_uuid import FlaskUUID from flask_debugtoolbar import DebugToolbarExtension +from flask_login import LoginManager +from authlib.flask.client import OAuth +from loginpass import create_flask_blueprint, Gitlab from raven.contrib.flask import Sentry from web_config import Config import fatcat_client @@ -12,6 +15,10 @@ app.config.from_object(Config) toolbar = DebugToolbarExtension(app) FlaskUUID(app) +login_manager = LoginManager() +login_manager.init_app(app) +oauth = OAuth(app) + # Grabs sentry config from SENTRY_DSN environment variable sentry = Sentry(app) @@ -19,4 +26,7 @@ conf = fatcat_client.Configuration() conf.host = "http://localhost:9411/v0" api = fatcat_client.DefaultApi(fatcat_client.ApiClient(conf)) -from fatcat_web import routes +from fatcat_web import routes, auth + +gitlab_bp = create_flask_blueprint(Gitlab, oauth, auth.handle_oauth) +app.register_blueprint(gitlab_bp, url_prefix='/auth/gitlab') diff --git a/python/fatcat_web/auth.py b/python/fatcat_web/auth.py new file mode 100644 index 00000000..f6672e87 --- /dev/null +++ b/python/fatcat_web/auth.py @@ -0,0 +1,27 @@ + +from flask import Flask, render_template, send_from_directory, request, \ + url_for, abort, g, redirect, jsonify, session +from fatcat_web import login_manager + + +# This will need to login/signup via fatcatd API, then set token in session +def handle_oauth(remote, token, user_info): + print(remote) + if token: + print(remote.name, token) + if user_info: + # TODO: fetch api login/signup using user_info + print(user_info) + # TODO: write token and username to session + # TODO: call login_user(load_user(editor_id)) + return redirect("/") + raise some_error + + +@login_manager.user_loader +def load_user(editor_id): + # NOTE: this should look for extra info in session, and update the user + # object with that. If session isn't loaded/valid, should return None + user = UserMixin() + user.id = editor_id + return user diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py index 998697bc..51533a2f 100644 --- a/python/fatcat_web/routes.py +++ b/python/fatcat_web/routes.py @@ -367,6 +367,19 @@ def search(): return render_template('release_search.html', query=query, fulltext_only=fulltext_only) +### Auth #################################################################### + +@app.route('/login') +def login(): + # show the user a list of login options + return render_template('release_search.html', query=query, fulltext_only=fulltext_only) + +@app.route('/login') +def logout(): + # TODO: clear extra session info + logout_user() + return render_template('logout.html') + ### Static Routes ########################################################### @app.errorhandler(404) -- cgit v1.2.3