Skip to content
Snippets Groups Projects
app.py 1.5 KiB
Newer Older
from flask import abort, Flask, redirect, request
Mark's avatar
Mark committed
from flask.views import View
Mark's avatar
Mark committed
from os import urandom, environ
Mark's avatar
Mark committed
from hydra_client import HydraAdmin
from db import User
import urllib
Mark's avatar
Mark committed

Mark's avatar
Mark committed
HYDRA_ADMIN_URL = environ['HYDRA_ADMIN_URL']
Mark's avatar
Mark committed

app = Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    hydra = HydraAdmin(HYDRA_ADMIN_URL)
Mark's avatar
Mark committed
    challenge = request.args.get("consent_challenge")
    if not challenge:
Mark's avatar
Mark committed
        abort(403)
    consent_request = hydra.consent_request(challenge)
Mark's avatar
Mark committed
    app_name = consent_request.client["client_name"]
    username = consent_request.subject
    try:
        user = User(username)
    except urllib.error.HTTPError as e:
        # TODO: replace with propper logging via logger
        print("Retrieving user object from GraphQL server failed")
        print(e)
        return redirect(consent_request.reject(
            "Permission denied",
            error_description="Login request was denied due to an internal server error"))
    access_granted = user.has_app_permission(app_name)
    if access_granted:
        session = user.get_oauth_session()
        return redirect(consent_request.accept(
            grant_scope=consent_request.requested_scope,
            grant_access_token_audience=consent_request.requested_access_token_audience,
            session=session,
            ))
Mark's avatar
Mark committed
    return redirect(consent_request.reject(
        "Permission denied",
        error_description="Login request was denied due to missing application permission"))

if __name__ == '__main__':
    app.run()