fixes generated image download
This commit is contained in:
@@ -37,6 +37,31 @@ def create_access_token(user_id: int, is_admin: bool) -> str:
|
||||
return jwt.encode(payload, settings.JWT_SECRET_KEY, algorithm="HS256")
|
||||
|
||||
|
||||
def create_download_token(artifact_id: int) -> str:
|
||||
"""Create a short-lived JWT for downloading a specific artifact."""
|
||||
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.DOWNLOAD_TOKEN_MINUTES)
|
||||
payload = {
|
||||
"sub": "download",
|
||||
"artifact_id": artifact_id,
|
||||
"exp": expire,
|
||||
}
|
||||
return jwt.encode(payload, settings.JWT_SECRET_KEY, algorithm="HS256")
|
||||
|
||||
|
||||
def verify_download_token(token: str) -> int | None:
|
||||
"""Verify a download token and return the artifact_id, or None on failure."""
|
||||
try:
|
||||
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms=["HS256"])
|
||||
if payload.get("sub") != "download":
|
||||
return None
|
||||
artifact_id = payload.get("artifact_id")
|
||||
if not isinstance(artifact_id, int):
|
||||
return None
|
||||
return artifact_id
|
||||
except JWTError:
|
||||
return None
|
||||
|
||||
|
||||
def decode_token(token: str) -> dict:
|
||||
"""Decode and verify a JWT token. Raises HTTPException 401 on failure."""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user