api_key.py 1.3 KB

123456789101112131415161718192021222324252627282930
  1. from datetime import datetime
  2. from sqlalchemy import JSON, Boolean, DateTime, String, func
  3. from sqlalchemy.orm import Mapped, mapped_column
  4. from backend.app.core.database import Base
  5. class APIKey(Base):
  6. """API key for external webhook access."""
  7. __tablename__ = "api_keys"
  8. id: Mapped[int] = mapped_column(primary_key=True)
  9. name: Mapped[str] = mapped_column(String(100)) # User-friendly name
  10. key_hash: Mapped[str] = mapped_column(String(64)) # SHA256 hash of the key
  11. key_prefix: Mapped[str] = mapped_column(String(8)) # First 8 chars for identification
  12. # Permissions
  13. can_queue: Mapped[bool] = mapped_column(Boolean, default=True) # Add to queue
  14. can_control_printer: Mapped[bool] = mapped_column(Boolean, default=False) # Start/stop/cancel
  15. can_read_status: Mapped[bool] = mapped_column(Boolean, default=True) # Query status
  16. # Optional scope limits
  17. printer_ids: Mapped[list | None] = mapped_column(JSON, nullable=True) # null = all printers
  18. enabled: Mapped[bool] = mapped_column(Boolean, default=True)
  19. last_used: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  20. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  21. expires_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) # Optional expiry