Implements a complete video transcoding web service with: - FastAPI framework for REST API - SQLAlchemy ORM with SQLite database - API key authentication using SHA256 hashing - Per-user resource isolation for all assets and jobs - S3-compatible file upload/download/delete/list interface - YAML-based API for transcode job requests - Transcode profile management (stored and inline) - Database models for Users, APIKeys, Assets, Jobs, and Profiles - CLI entry point for database initialization and server startup - Comprehensive integration tests with 100% pass rate - Deployment documentation with examples New files: - privatestudio/web/: Web service package - privatestudio/web/models.py: SQLAlchemy ORM models - privatestudio/web/auth.py: API key authentication - privatestudio/web/database.py: Database configuration - privatestudio/web/app.py: FastAPI application - privatestudio/web/s3_interface.py: File storage backend - privatestudio/web/cli.py: CLI commands - privatestudio/web/routes/: API endpoint handlers - tests/test_web.py: Integration tests - DEPLOYMENT.md: Deployment guide Updated files: - pyproject.toml: Added dependencies and CLI entry point Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
90 lines
2.5 KiB
Python
90 lines
2.5 KiB
Python
"""API key management endpoints."""
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
|
|
from privatestudio.web.database import get_db
|
|
from privatestudio.web.models import User, APIKey
|
|
from privatestudio.web.auth import get_current_user, create_api_key, revoke_api_key
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class APIKeyCreate(BaseModel):
|
|
"""Request model for creating an API key."""
|
|
pass # No parameters needed
|
|
|
|
|
|
class APIKeyResponse(BaseModel):
|
|
"""Response model for API key."""
|
|
id: int
|
|
created_at: str
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class APIKeyCreateResponse(APIKeyResponse):
|
|
"""Response model for creating an API key (includes the plain key)."""
|
|
key: str
|
|
|
|
|
|
class APIKeyList(BaseModel):
|
|
"""Response model for listing API keys."""
|
|
id: int
|
|
created_at: str
|
|
is_active: bool
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
@router.post("/keys", response_model=APIKeyCreateResponse)
|
|
def create_api_key_endpoint(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Create a new API key for the authenticated user."""
|
|
plain_key, api_key_record = create_api_key(db, current_user.id)
|
|
return APIKeyCreateResponse(
|
|
id=api_key_record.id,
|
|
key=plain_key,
|
|
created_at=api_key_record.created_at.isoformat(),
|
|
)
|
|
|
|
|
|
@router.get("/keys", response_model=list[APIKeyList])
|
|
def list_api_keys(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""List all API keys for the authenticated user."""
|
|
keys = db.query(APIKey).filter(APIKey.user_id == current_user.id).all()
|
|
return [
|
|
APIKeyList(
|
|
id=key.id,
|
|
created_at=key.created_at.isoformat(),
|
|
is_active=key.is_active,
|
|
)
|
|
for key in keys
|
|
]
|
|
|
|
|
|
@router.delete("/keys/{key_id}")
|
|
def revoke_api_key_endpoint(
|
|
key_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""Revoke an API key (mark it as inactive)."""
|
|
# Verify the key belongs to the current user
|
|
api_key = db.query(APIKey).filter(APIKey.id == key_id).first()
|
|
if not api_key:
|
|
raise HTTPException(status_code=404, detail="API key not found")
|
|
|
|
if api_key.user_id != current_user.id:
|
|
raise HTTPException(status_code=403, detail="Cannot revoke someone else's API key")
|
|
|
|
revoke_api_key(db, key_id)
|
|
return {"message": "API key revoked"}
|