gabriel / musehub public
dependencies.py python
48 lines 1.5 KB
Raw
sha256:9590cee1e0ccd6c76528f005b95d634d80f5019f0dcb7c371e149adc31d1fb65 refactor: enforce gRPC framing on all MWP wire traffic Sonnet 4.6 minor ⚠ breaking 156 days ago
1 """FastAPI authentication dependencies — MSign per-request signing.
2
3 All authenticated endpoints use ``Depends(require_valid_token)`` or
4 ``Depends(optional_token)`` — the same names as before, now backed by
5 MSign Ed25519 per-request signing.
6 """
7 from __future__ import annotations
8
9 import logging
10 import uuid
11
12 from fastapi import Header, HTTPException, status
13
14 from musehub.auth.request_signing import (
15 MSignContext,
16 optional_signed_request,
17 require_scope,
18 require_signed_request,
19 )
20
21 logger = logging.getLogger(__name__)
22
23 # Re-export under the names the rest of the codebase expects
24 TokenClaims = MSignContext
25 require_valid_token = require_signed_request
26 optional_token = optional_signed_request
27
28
29 async def require_device_id(
30 x_device_id: str | None = Header(None, alias="X-Device-ID"),
31 ) -> str:
32 """FastAPI dependency for asset endpoints: require a valid X-Device-ID header (UUID)."""
33 if not x_device_id or not x_device_id.strip():
34 logger.warning("Asset request without X-Device-ID")
35 raise HTTPException(
36 status_code=status.HTTP_400_BAD_REQUEST,
37 detail="X-Device-ID header required",
38 )
39 value = x_device_id.strip()
40 try:
41 uuid.UUID(value)
42 except ValueError:
43 logger.warning("Invalid X-Device-ID format: %s", value[:32])
44 raise HTTPException(
45 status_code=status.HTTP_400_BAD_REQUEST,
46 detail="Invalid X-Device-ID format",
47 )
48 return value
File History 1 commit
sha256:9590cee1e0ccd6c76528f005b95d634d80f5019f0dcb7c371e149adc31d1fb65 refactor: enforce gRPC framing on all MWP wire traffic Sonnet 4.6 minor ⚠ 156 days ago