MirrorAPI/actions/validations/staticFiles.py

31 lines
1 KiB
Python

from fastapi import Request, HTTPException, status
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from actions.jwt.verify_token import VerifyToken
import os
class ProtectedStaticFiles(StaticFiles):
async def get_response(self, path, scope):
# Extrai o token do cabeçalho Authorization
headers = dict(scope["headers"])
auth_header = headers.get(b"authorization", b"").decode()
if not auth_header.startswith("Bearer "):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token ausente"
)
token = auth_header.split("Bearer ")[1]
# Valida o token
verify = VerifyToken()
result = verify.execute(token)
if result["status"] != "valid":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Token inválido"
)
# Token válido → entrega o arquivo
return await super().get_response(path, scope)