Add cookie authentication, owned conversations, durable Live reservations, tool completion/replay fencing, revocation, and startup reconciliation. Introduce forward lifecycle/owner-chain migrations and the temporary Gemini adapter while preserving the existing OpenAI Realtime adapter. Include the executable audio client, regression coverage, configuration, API/database documentation, and recorded acceptance evidence. Validation: focused review suite 82 passed; Python/TOML parsing, JavaScript syntax, and whitespace checks passed. Full PostgreSQL/provider-free results are recorded in the repair report; real-provider/browser acceptance is unrun. Follow-up work remains for configurable OpenAI/Gemini selection, transport admission, authority diagnostics, playback message bounds, and client replay identity after ambiguous creation responses.
148 lines
5.1 KiB
Python
148 lines
5.1 KiB
Python
"""Trusted maintenance tool for provisioning demo and operator accounts.
|
|
|
|
No public signup or user management UI is exposed; only trusted maintenance
|
|
commands or scripts provision accounts.
|
|
"""
|
|
import argparse
|
|
from getpass import getpass
|
|
import json
|
|
import sys
|
|
from uuid import UUID, uuid4
|
|
|
|
from argon2 import PasswordHasher
|
|
from sqlalchemy import text
|
|
|
|
from app.infrastructure.database import create_database
|
|
from app.modules.documents.settings import IngestionSettings
|
|
|
|
|
|
def provision_user(
|
|
connection,
|
|
*,
|
|
username: str,
|
|
password: str,
|
|
role: str = "user",
|
|
organization_id: UUID | str | None = None,
|
|
is_enabled: bool = True,
|
|
) -> dict:
|
|
username = username.strip().lower()
|
|
if not username or len(username) > 120:
|
|
raise ValueError("Username must contain 1-120 characters")
|
|
if role not in ("admin", "user"):
|
|
raise ValueError("Role must be 'admin' or 'user'")
|
|
if not password:
|
|
raise ValueError("Password cannot be empty")
|
|
|
|
# If organization_id not provided, look up existing organization
|
|
if organization_id is None:
|
|
org_row = connection.execute(
|
|
text("SELECT id FROM organizations ORDER BY created_at ASC LIMIT 1")
|
|
).mappings().first()
|
|
if not org_row:
|
|
raise ValueError("No organization exists. Run bootstrap first.")
|
|
org_id = org_row["id"]
|
|
else:
|
|
org_id = UUID(str(organization_id))
|
|
org_row = connection.execute(
|
|
text("SELECT id FROM organizations WHERE id = :org_id"),
|
|
{"org_id": org_id}
|
|
).mappings().first()
|
|
if not org_row:
|
|
raise ValueError(f"Organization {organization_id} not found.")
|
|
|
|
password_hash = PasswordHasher().hash(password)
|
|
|
|
# Check if user already exists
|
|
existing = connection.execute(
|
|
text("SELECT id, organization_id, role, is_enabled FROM users WHERE username = :username"),
|
|
{"username": username}
|
|
).mappings().first()
|
|
|
|
if existing:
|
|
if existing["organization_id"] != org_id:
|
|
raise ValueError("Username belongs to another organization")
|
|
user_id = existing["id"]
|
|
connection.execute(
|
|
text("""
|
|
UPDATE users
|
|
SET password_hash = :hash, role = :role, is_enabled = :enabled, updated_at = now()
|
|
WHERE id = :id AND organization_id = :org_id
|
|
"""),
|
|
{"id": user_id, "org_id": org_id, "hash": password_hash, "role": role, "enabled": is_enabled}
|
|
)
|
|
return {
|
|
"status": "updated",
|
|
"user_id": str(user_id),
|
|
"organization_id": str(existing["organization_id"]),
|
|
"username": username,
|
|
"role": role,
|
|
"is_enabled": is_enabled,
|
|
}
|
|
else:
|
|
user_id = uuid4()
|
|
connection.execute(
|
|
text("""
|
|
INSERT INTO users (id, organization_id, username, password_hash, role, is_enabled)
|
|
VALUES (:id, :org_id, :username, :hash, :role, :enabled)
|
|
"""),
|
|
{
|
|
"id": user_id,
|
|
"org_id": org_id,
|
|
"username": username,
|
|
"hash": password_hash,
|
|
"role": role,
|
|
"enabled": is_enabled,
|
|
}
|
|
)
|
|
return {
|
|
"status": "created",
|
|
"user_id": str(user_id),
|
|
"organization_id": str(org_id),
|
|
"username": username,
|
|
"role": role,
|
|
"is_enabled": is_enabled,
|
|
}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--username", required=True, help="Username (1-120 characters)")
|
|
parser.add_argument("--role", default="user", choices=["admin", "user"], help="User role")
|
|
parser.add_argument("--organization-id", help="Organization UUID (defaults to first org)")
|
|
parser.add_argument("--disabled", action="store_true", help="Provision user as disabled")
|
|
parser.add_argument("--password-stdin", action="store_true", help="Read password from stdin")
|
|
args = parser.parse_args()
|
|
|
|
if args.password_stdin:
|
|
password = sys.stdin.readline().rstrip("\r\n")
|
|
else:
|
|
if not sys.stdin.isatty():
|
|
parser.error("Use an interactive terminal or explicitly select --password-stdin")
|
|
password = getpass("Account password: ")
|
|
if password != getpass("Confirm password: "):
|
|
parser.error("Passwords do not match")
|
|
|
|
engine = None
|
|
try:
|
|
settings = IngestionSettings()
|
|
engine = create_database(settings.database_url.get_secret_value())
|
|
with engine.begin() as connection:
|
|
result = provision_user(
|
|
connection,
|
|
username=args.username,
|
|
password=password,
|
|
role=args.role,
|
|
organization_id=args.organization_id,
|
|
is_enabled=not args.disabled,
|
|
)
|
|
print(json.dumps(result))
|
|
except Exception as exc:
|
|
parser.exit(1, f"Account provisioning failed: {exc}\n")
|
|
finally:
|
|
if engine is not None:
|
|
engine.dispose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|