2022-08-10 18:48:32 +02:00
|
|
|
import arrow
|
|
|
|
from flask import redirect, url_for, request, flash
|
2022-08-12 13:17:21 +02:00
|
|
|
from flask_login import login_user
|
2022-08-10 18:48:32 +02:00
|
|
|
|
|
|
|
from app.auth.base import auth_bp
|
|
|
|
from app.models import ApiToCookieToken
|
|
|
|
from app.utils import sanitize_next_url
|
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/api_to_cookie", methods=["GET"])
|
|
|
|
def api_to_cookie():
|
|
|
|
code = request.args.get("token")
|
|
|
|
if not code:
|
|
|
|
flash("Missing token", "error")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
|
|
|
token = ApiToCookieToken.get_by(code=code)
|
|
|
|
if not token or token.created_at < arrow.now().shift(minutes=-5):
|
|
|
|
flash("Missing token", "error")
|
|
|
|
return redirect(url_for("auth.login"))
|
|
|
|
|
2022-08-12 13:17:21 +02:00
|
|
|
user = token.user
|
2022-08-10 18:48:32 +02:00
|
|
|
ApiToCookieToken.delete(token.id, commit=True)
|
2022-08-12 13:17:21 +02:00
|
|
|
login_user(user)
|
2022-08-10 18:48:32 +02:00
|
|
|
|
|
|
|
next_url = sanitize_next_url(request.args.get("next"))
|
|
|
|
if next_url:
|
|
|
|
return redirect(next_url)
|
|
|
|
else:
|
|
|
|
return redirect(url_for("dashboard.index"))
|