2022-11-02 10:25:43 +01:00
|
|
|
from multiprocessing import current_process
|
|
|
|
from flask import jsonify, request
|
|
|
|
from flask_jwt_extended import create_access_token
|
|
|
|
from flask_cors import cross_origin
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from areas import api_v1
|
|
|
|
from config import *
|
2022-11-08 16:36:16 +01:00
|
|
|
from helpers import LITOauth, BadRequest
|
2022-11-02 10:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_v1.route("/login", methods=["POST"])
|
|
|
|
@cross_origin()
|
|
|
|
def login():
|
2022-11-08 16:36:16 +01:00
|
|
|
authorization_url = LITOauth.authorize()
|
2022-11-02 10:25:43 +01:00
|
|
|
return jsonify({"authorizationUrl": authorization_url})
|
|
|
|
|
|
|
|
|
|
|
|
@api_v1.route("/hydra/callback")
|
|
|
|
@cross_origin()
|
|
|
|
def hydra_callback():
|
|
|
|
state = request.args.get("state")
|
|
|
|
code = request.args.get("code")
|
|
|
|
if state == None:
|
|
|
|
raise BadRequest("Missing state query param")
|
|
|
|
|
|
|
|
if code == None:
|
|
|
|
raise BadRequest("Missing code query param")
|
|
|
|
|
2022-11-08 16:36:16 +01:00
|
|
|
token = LITOauth.get_token(state, code)
|
|
|
|
user_info = LITOauth.get_user_info()
|
2022-11-02 10:25:43 +01:00
|
|
|
access_token = create_access_token(
|
2022-11-08 16:36:16 +01:00
|
|
|
identity=token, expires_delta=timedelta(days=365))
|
|
|
|
isAdmin = "admin" in user_info["groups"]
|
|
|
|
app_roles = [
|
|
|
|
{
|
|
|
|
"name": "dashboard",
|
|
|
|
"role_id": 1 if isAdmin else 2
|
|
|
|
},
|
|
|
|
]
|
2022-11-02 10:25:43 +01:00
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
{
|
|
|
|
"accessToken": access_token,
|
|
|
|
"userInfo": {
|
|
|
|
"id": user_info["email"],
|
|
|
|
"email": user_info["email"],
|
|
|
|
"name": user_info["name"],
|
|
|
|
"preferredUsername": user_info["preferred_username"],
|
2022-11-08 16:36:16 +01:00
|
|
|
"app_roles": app_roles
|
|
|
|
}
|
2022-11-02 10:25:43 +01:00
|
|
|
}
|
|
|
|
)
|