Modified login app to work in dashboard context
This commit is contained in:
parent
e8063b1de7
commit
755cb03aaf
36 changed files with 22251 additions and 24 deletions
|
|
@ -2,7 +2,7 @@ from flask import Blueprint
|
|||
|
||||
api_v1 = Blueprint("api_v1", __name__, url_prefix="/api/v1")
|
||||
web = Blueprint("web", __name__, url_prefix="/web")
|
||||
|
||||
# cli = Blueprint('cli', __name__)
|
||||
|
||||
@api_v1.route("/")
|
||||
@api_v1.route("/health")
|
||||
|
|
|
|||
|
|
@ -30,6 +30,19 @@ from ory_kratos_client.api import v0alpha2_api as kratos_api
|
|||
|
||||
from areas import web
|
||||
from config import *
|
||||
from flask import current_app
|
||||
|
||||
from helpers import (
|
||||
BadRequest,
|
||||
KratosError,
|
||||
HydraError,
|
||||
bad_request_error,
|
||||
validation_error,
|
||||
kratos_error,
|
||||
global_error,
|
||||
hydra_error,
|
||||
KratosUser
|
||||
)
|
||||
|
||||
# APIs
|
||||
# Create HYDRA & KRATOS API interfaces
|
||||
|
|
@ -143,7 +156,7 @@ def auth():
|
|||
challenge = request.args.post("login_challenge")
|
||||
|
||||
if not challenge:
|
||||
app.logger.error("No challenge given. Error in request")
|
||||
current_app.logger.error("No challenge given. Error in request")
|
||||
abort(400, description="Challenge required when requesting authorization")
|
||||
|
||||
|
||||
|
|
@ -160,25 +173,25 @@ def auth():
|
|||
url = PUBLIC_URL + "/auth?login_challenge=" + challenge
|
||||
url = urllib.parse.quote_plus(url)
|
||||
|
||||
app.logger.info("Redirecting to login. Setting flow_state cookies")
|
||||
app.logger.info("auth_url: " + url)
|
||||
current_app.logger.info("Redirecting to login. Setting flow_state cookies")
|
||||
current_app.logger.info("auth_url: " + url)
|
||||
|
||||
response = redirect(app.config["PUBLIC_URL"] + "/login")
|
||||
response = redirect(PUBLIC_URL + "/login")
|
||||
response.set_cookie('flow_state', 'auth')
|
||||
response.set_cookie('auth_url', url)
|
||||
return response
|
||||
|
||||
|
||||
|
||||
app.logger.info("User is logged in. We can authorize the user")
|
||||
current_app.logger.info("User is logged in. We can authorize the user")
|
||||
|
||||
try:
|
||||
login_request = HYDRA.login_request(challenge)
|
||||
except hydra_client.exceptions.NotFound:
|
||||
app.logger.error(f"Not Found. Login request not found. challenge={challenge}")
|
||||
current_app.logger.error(f"Not Found. Login request not found. challenge={challenge}")
|
||||
abort(404, description="Login request not found. Please try again.")
|
||||
except hydra_client.exceptions.HTTPError:
|
||||
app.logger.error(f"Conflict. Login request has been used already. challenge={challenge}")
|
||||
current_app.logger.error(f"Conflict. Login request has been used already. challenge={challenge}")
|
||||
abort(503, description="Login request already used. Please try again.")
|
||||
|
||||
# Authorize the user
|
||||
|
|
@ -208,10 +221,10 @@ def consent():
|
|||
try:
|
||||
consent_request = HYDRA.consent_request(challenge)
|
||||
except hydra_client.exceptions.NotFound:
|
||||
app.logger.error(f"Not Found. Consent request {challenge} not found")
|
||||
current_app.logger.error(f"Not Found. Consent request {challenge} not found")
|
||||
abort(404, description="Consent request does not exist. Please try again")
|
||||
except hydra_client.exceptions.HTTPError:
|
||||
app.logger.error(f"Conflict. Consent request {challenge} already used")
|
||||
current_app.logger.error(f"Conflict. Consent request {challenge} already used")
|
||||
abort(503, description="Consent request already used. Please try again")
|
||||
|
||||
# Get information about this consent request:
|
||||
|
|
@ -223,11 +236,12 @@ def consent():
|
|||
# Get the related user object
|
||||
user = KratosUser(KRATOS_ADMIN, kratos_id)
|
||||
if not user:
|
||||
app.logger.error(f"User not found in database: {kratos_id}")
|
||||
current_app.logger.error(f"User not found in database: {kratos_id}")
|
||||
abort(401, description="User not found. Please try again.")
|
||||
|
||||
# Get role on this app
|
||||
app_obj = db.session.query(App).filter(App.slug == app_id).first()
|
||||
#app_obj = db.session.query(App).filter(App.slug == app_id).first()
|
||||
app_obj = False
|
||||
|
||||
# Default access level
|
||||
roles = []
|
||||
|
|
@ -239,7 +253,7 @@ def consent():
|
|||
)
|
||||
for role_obj in role_objects:
|
||||
roles.append(role_obj.role)
|
||||
app.logger.info(f"Using '{roles}' when applying consent for {kratos_id}")
|
||||
current_app.logger.info(f"Using '{roles}' when applying consent for {kratos_id}")
|
||||
|
||||
# Get claims for this user, provided the current app
|
||||
claims = user.get_claims(app_id, roles)
|
||||
|
|
@ -247,8 +261,8 @@ def consent():
|
|||
# pylint: disable=fixme
|
||||
# TODO: Need to implement checking claims here, once the backend for that is
|
||||
# developed
|
||||
app.logger.info(f"Providing consent to {app_id} for {kratos_id}")
|
||||
app.logger.info(f"{kratos_id} was granted access to {app_id}")
|
||||
current_app.logger.info(f"Providing consent to {app_id} for {kratos_id}")
|
||||
current_app.logger.info(f"{kratos_id} was granted access to {app_id}")
|
||||
|
||||
# False positive: pylint: disable=no-member
|
||||
return redirect(consent_request.accept(
|
||||
|
|
@ -285,7 +299,7 @@ def get_auth():
|
|||
cookie = request.cookies.get('ory_kratos_session')
|
||||
cookie = "ory_kratos_session=" + cookie
|
||||
except TypeError:
|
||||
app.logger.info("User not logged in or cookie corrupted")
|
||||
current_app.logger.info("User not logged in or cookie corrupted")
|
||||
return False
|
||||
|
||||
# Given a cookie, check if it is valid and get the profile
|
||||
|
|
@ -297,7 +311,7 @@ def get_auth():
|
|||
return api_response.identity
|
||||
|
||||
except ory_kratos_client.ApiException as error:
|
||||
app.logger.error(f"Exception when calling V0alpha2Api->to_session(): {error}\n")
|
||||
current_app.logger.error(f"Exception when calling V0alpha2Api->to_session(): {error}\n")
|
||||
|
||||
return False
|
||||
|
||||
|
|
|
|||
Reference in a new issue