availabili.tf/backend-flask/middleware.py

28 lines
789 B
Python
Raw Normal View History

2024-11-02 12:33:27 -07:00
from functools import wraps
from flask import abort, make_response, request
2024-11-10 17:21:25 -08:00
from app_db import db
from models.auth_session import AuthSession
2024-11-02 12:33:27 -07:00
def requires_authentication(f):
@wraps(f)
def decorator(*args, **kwargs):
auth = request.cookies.get("auth")
if not auth:
abort(401)
2024-11-10 17:21:25 -08:00
statement = db.select(AuthSession).filter_by(key=auth)
auth_session: AuthSession | None = \
2024-11-02 12:33:27 -07:00
db.session.execute(statement).scalar_one_or_none()
if not auth_session:
abort(make_response({
"error": "Invalid auth token"
}, 401))
player = auth_session.player
kwargs["player"] = player
kwargs["auth_session"] = auth_session
return f(*args, **kwargs)
return decorator