Restructure backend and frontend modules; add story, library, and editor schema migrations. Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews. Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
import secrets
|
|
from datetime import timezone
|
|
from sqlalchemy import select
|
|
from app.core.security import Actor, token_hash
|
|
from app.models import utcnow
|
|
from app.models import ApiToken
|
|
from app.core.errors import fail
|
|
from app.services.snapshots import iso
|
|
from app.schemas.tokens import TokenInput
|
|
|
|
SCOPES = {
|
|
"jobs:read",
|
|
"jobs:write",
|
|
"assets:read",
|
|
"assets:write",
|
|
"folders:read",
|
|
"folders:write",
|
|
"events:read",
|
|
"workflows:read",
|
|
"workflows:write",
|
|
"batches:read",
|
|
"batches:write",
|
|
"interactive:write",
|
|
}
|
|
|
|
|
|
def record(row):
|
|
return {
|
|
"id": row.id,
|
|
"name": row.name,
|
|
"scopes": row.scopes,
|
|
"expires_at": iso(row.expires_at),
|
|
"created_at": iso(row.created_at),
|
|
"revoked_at": iso(row.revoked_at),
|
|
}
|
|
|
|
|
|
def owner_session(actor):
|
|
if actor.auth_kind != "session":
|
|
fail(403, "session_required", "Session required")
|
|
|
|
|
|
async def list_tokens(runtime, actor: Actor = None):
|
|
owner_session(actor)
|
|
async with runtime.db.session() as session:
|
|
rows = (await session.scalars(select(ApiToken).where(ApiToken.owner_id == actor.id))).all()
|
|
return {"items": [record(r) for r in rows]}
|
|
|
|
|
|
async def create_token(body: TokenInput, runtime, actor: Actor = None):
|
|
owner_session(actor)
|
|
if set(body.scopes) - SCOPES:
|
|
fail(422, "invalid_scope", "Unknown token scope")
|
|
expires = body.expires_at
|
|
if expires:
|
|
if expires.tzinfo is None:
|
|
fail(422, "timezone_required", "Expiry requires timezone")
|
|
expires = expires.astimezone(timezone.utc).replace(tzinfo=None)
|
|
if expires <= utcnow():
|
|
fail(422, "invalid_expiry", "Expiry must be in the future")
|
|
if getattr(runtime.settings, "public_access", False) and actor.must_change_password:
|
|
fail(403, "password_change_required", "Change password first")
|
|
token = "cmvr_" + secrets.token_urlsafe(36)
|
|
async with runtime.db.session() as session:
|
|
row = ApiToken(
|
|
owner_id=actor.id,
|
|
name=body.name,
|
|
scopes=list(set(body.scopes)),
|
|
token_hash=token_hash(token),
|
|
expires_at=expires,
|
|
)
|
|
session.add(row)
|
|
await session.commit()
|
|
return {"token": token, "record": record(row)}
|
|
|
|
|
|
async def revoke(token_id: str, runtime, actor: Actor = None):
|
|
owner_session(actor)
|
|
async with runtime.db.session() as session:
|
|
row = await session.scalar(
|
|
select(ApiToken).where(ApiToken.id == token_id, ApiToken.owner_id == actor.id)
|
|
)
|
|
if not row:
|
|
fail(404, "token_not_found", "Token not found")
|
|
row.revoked_at = utcnow()
|
|
await session.commit()
|
|
return None
|