api_keys.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import logging
  2. from fastapi import APIRouter, Depends, HTTPException
  3. from sqlalchemy import select
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from backend.app.core.auth import RequirePermissionIfAuthEnabled, generate_api_key
  6. from backend.app.core.database import get_db
  7. from backend.app.core.permissions import Permission
  8. from backend.app.models.api_key import APIKey
  9. from backend.app.models.user import User
  10. from backend.app.schemas.api_key import (
  11. APIKeyCreate,
  12. APIKeyCreateResponse,
  13. APIKeyResponse,
  14. APIKeyUpdate,
  15. )
  16. logger = logging.getLogger(__name__)
  17. router = APIRouter(prefix="/api-keys", tags=["api-keys"])
  18. @router.get("/", response_model=list[APIKeyResponse])
  19. async def list_api_keys(
  20. db: AsyncSession = Depends(get_db),
  21. _: User | None = RequirePermissionIfAuthEnabled(Permission.API_KEYS_READ),
  22. ):
  23. """List all API keys (without full key values)."""
  24. result = await db.execute(select(APIKey).order_by(APIKey.created_at.desc()))
  25. return list(result.scalars().all())
  26. @router.post("/", response_model=APIKeyCreateResponse)
  27. async def create_api_key(
  28. data: APIKeyCreate,
  29. db: AsyncSession = Depends(get_db),
  30. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.API_KEYS_CREATE),
  31. ):
  32. """Create a new API key.
  33. IMPORTANT: The full API key is only returned in this response.
  34. Store it securely - it cannot be retrieved again.
  35. """
  36. # Reject can_access_cloud on auth-disabled deployments — there's no per-user
  37. # cloud_token to read against, so the flag would just silently do nothing.
  38. # Surfacing the rejection at create time prevents the user from thinking
  39. # they've configured cloud access when they actually haven't.
  40. if data.can_access_cloud and current_user is None:
  41. raise HTTPException(
  42. status_code=400,
  43. detail="can_access_cloud requires authentication to be enabled (per-user cloud tokens)",
  44. )
  45. # Generate the key
  46. full_key, key_hash, key_prefix = generate_api_key()
  47. api_key = APIKey(
  48. name=data.name,
  49. key_hash=key_hash,
  50. key_prefix=key_prefix,
  51. user_id=current_user.id if current_user else None,
  52. can_queue=data.can_queue,
  53. can_control_printer=data.can_control_printer,
  54. can_read_status=data.can_read_status,
  55. can_manage_library=data.can_manage_library,
  56. can_manage_inventory=data.can_manage_inventory,
  57. can_access_cloud=data.can_access_cloud,
  58. can_update_energy_cost=data.can_update_energy_cost,
  59. printer_ids=data.printer_ids,
  60. expires_at=data.expires_at,
  61. )
  62. db.add(api_key)
  63. await db.flush()
  64. await db.refresh(api_key)
  65. # Return with full key (only time it's shown)
  66. return APIKeyCreateResponse(
  67. id=api_key.id,
  68. name=api_key.name,
  69. key_prefix=api_key.key_prefix,
  70. key=full_key, # Only returned on creation
  71. user_id=api_key.user_id,
  72. can_queue=api_key.can_queue,
  73. can_control_printer=api_key.can_control_printer,
  74. can_read_status=api_key.can_read_status,
  75. can_manage_library=api_key.can_manage_library,
  76. can_manage_inventory=api_key.can_manage_inventory,
  77. can_access_cloud=api_key.can_access_cloud,
  78. can_update_energy_cost=api_key.can_update_energy_cost,
  79. printer_ids=api_key.printer_ids,
  80. enabled=api_key.enabled,
  81. last_used=api_key.last_used,
  82. created_at=api_key.created_at,
  83. expires_at=api_key.expires_at,
  84. )
  85. @router.get("/{key_id}", response_model=APIKeyResponse)
  86. async def get_api_key(
  87. key_id: int,
  88. db: AsyncSession = Depends(get_db),
  89. _: User | None = RequirePermissionIfAuthEnabled(Permission.API_KEYS_READ),
  90. ):
  91. """Get an API key by ID."""
  92. result = await db.execute(select(APIKey).where(APIKey.id == key_id))
  93. api_key = result.scalar_one_or_none()
  94. if not api_key:
  95. raise HTTPException(status_code=404, detail="API key not found")
  96. return api_key
  97. @router.patch("/{key_id}", response_model=APIKeyResponse)
  98. async def update_api_key(
  99. key_id: int,
  100. data: APIKeyUpdate,
  101. db: AsyncSession = Depends(get_db),
  102. _: User | None = RequirePermissionIfAuthEnabled(Permission.API_KEYS_UPDATE),
  103. ):
  104. """Update an API key."""
  105. result = await db.execute(select(APIKey).where(APIKey.id == key_id))
  106. api_key = result.scalar_one_or_none()
  107. if not api_key:
  108. raise HTTPException(status_code=404, detail="API key not found")
  109. # Update fields if provided
  110. if data.name is not None:
  111. api_key.name = data.name
  112. if data.can_queue is not None:
  113. api_key.can_queue = data.can_queue
  114. if data.can_control_printer is not None:
  115. api_key.can_control_printer = data.can_control_printer
  116. if data.can_read_status is not None:
  117. api_key.can_read_status = data.can_read_status
  118. if data.can_manage_library is not None:
  119. api_key.can_manage_library = data.can_manage_library
  120. if data.can_manage_inventory is not None:
  121. api_key.can_manage_inventory = data.can_manage_inventory
  122. if data.can_access_cloud is not None:
  123. # Same constraint as create — flipping cloud access on a legacy key
  124. # without an owner would be silently broken; reject at the route layer.
  125. if data.can_access_cloud and api_key.user_id is None:
  126. raise HTTPException(
  127. status_code=400,
  128. detail="can_access_cloud requires the API key to have an owner; recreate the key after upgrading",
  129. )
  130. api_key.can_access_cloud = data.can_access_cloud
  131. if data.can_update_energy_cost is not None:
  132. api_key.can_update_energy_cost = data.can_update_energy_cost
  133. if data.printer_ids is not None:
  134. api_key.printer_ids = data.printer_ids
  135. if data.enabled is not None:
  136. api_key.enabled = data.enabled
  137. if data.expires_at is not None:
  138. api_key.expires_at = data.expires_at
  139. await db.flush()
  140. await db.refresh(api_key)
  141. return api_key
  142. @router.delete("/{key_id}")
  143. async def delete_api_key(
  144. key_id: int,
  145. db: AsyncSession = Depends(get_db),
  146. _: User | None = RequirePermissionIfAuthEnabled(Permission.API_KEYS_DELETE),
  147. ):
  148. """Delete (revoke) an API key."""
  149. result = await db.execute(select(APIKey).where(APIKey.id == key_id))
  150. api_key = result.scalar_one_or_none()
  151. if not api_key:
  152. raise HTTPException(status_code=404, detail="API key not found")
  153. await db.delete(api_key)
  154. return {"message": "API key deleted"}