Build reusable bot framework
Some checks failed
CI / test (push) Has been cancelled
CI / compose-smoke (push) Has been cancelled

This commit is contained in:
Chelsea Lee
2026-07-19 21:53:24 -05:00
parent 7ecc1107b2
commit fbdf33e894
66 changed files with 8428 additions and 0 deletions

50
api/security.py Normal file
View File

@@ -0,0 +1,50 @@
"""Authentication decorators shared by API route modules."""
from functools import wraps
import flask
from core import auth
def requireUser(requireLogin=False):
def decorator(route):
@wraps(route)
def wrapped(*args, **kwargs):
principal = auth.authenticateBearerToken(
flask.request.headers.get("Authorization"),
allowService=False,
)
if not auth.isUserPrincipal(principal, requireLogin=requireLogin):
return flask.jsonify({"error": "unauthorized"}), 401
flask.g.principal = principal
flask.g.user_uuid = principal["user_uuid"]
return route(*args, **kwargs)
return wrapped
return decorator
def requireService(scope):
def decorator(route):
@wraps(route)
def wrapped(*args, **kwargs):
principal = auth.authenticateBearerToken(
flask.request.headers.get("Authorization"),
requiredScopes=[scope],
allowUser=False,
)
if not auth.hasServiceScope(principal, scope):
return flask.jsonify({"error": "unauthorized"}), 401
flask.g.principal = principal
return route(*args, **kwargs)
return wrapped
return decorator
def jsonObject():
data = flask.request.get_json(silent=True)
return data if isinstance(data, dict) else None