feat(): Criado url protegida por token, acesso via endpoint
This commit is contained in:
parent
5ce1909eee
commit
a5a27c5fdc
2 changed files with 30 additions and 1 deletions
|
|
@ -0,0 +1,22 @@
|
||||||
|
from fastapi import APIRouter, Query, HTTPException
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
from jose import jwt, JWTError
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/view")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{file_id}/{filename}")
|
||||||
|
def visualizar_arquivo(file_id: str, filename: str, token: str = Query(...)):
|
||||||
|
"""Valida o token e retorna o arquivo se autorizado."""
|
||||||
|
try:
|
||||||
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||||
|
if payload["sub"] != file_id or payload["filename"] != filename:
|
||||||
|
raise HTTPException(status_code=403, detail="Token inválido.")
|
||||||
|
except JWTError:
|
||||||
|
raise HTTPException(status_code=401, detail="Token expirado ou inválido.")
|
||||||
|
|
||||||
|
file_path = f"files/{filename}"
|
||||||
|
try:
|
||||||
|
return FileResponse(file_path, media_type="application/pdf")
|
||||||
|
except FileNotFoundError:
|
||||||
|
raise HTTPException(status_code=404, detail="Arquivo não encontrado.")
|
||||||
|
|
@ -8,6 +8,7 @@ from packages.v1.administrativo.endpoints import ato_documento_endpoint
|
||||||
from packages.v1.administrativo.endpoints import ato_parte_endpoint
|
from packages.v1.administrativo.endpoints import ato_parte_endpoint
|
||||||
from packages.v1.administrativo.endpoints import auth_endpoint
|
from packages.v1.administrativo.endpoints import auth_endpoint
|
||||||
from packages.v1.administrativo.endpoints import atos_endpoint
|
from packages.v1.administrativo.endpoints import atos_endpoint
|
||||||
|
from packages.v1.administrativo.endpoints import atos_view_document_endpoint
|
||||||
|
|
||||||
# Cria uma instância do APIRouter que vai agregar todas as rotas da API
|
# Cria uma instância do APIRouter que vai agregar todas as rotas da API
|
||||||
api_router = APIRouter()
|
api_router = APIRouter()
|
||||||
|
|
@ -45,8 +46,14 @@ api_router.include_router(
|
||||||
tags=["Autentica o usuário e retorna o token de acesso"],
|
tags=["Autentica o usuário e retorna o token de acesso"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Inclui as rotas de ato_principal
|
# Inclui as rotas de ato_principal
|
||||||
api_router.include_router(
|
api_router.include_router(
|
||||||
atos_endpoint.router, prefix="/atos", tags=["Retorna o ato e suas dependências"]
|
atos_endpoint.router, prefix="/atos", tags=["Retorna o ato e suas dependências"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Inclui as rotas de visualização de documentos de atos
|
||||||
|
api_router.include_router(
|
||||||
|
atos_view_document_endpoint.router,
|
||||||
|
prefix="/view",
|
||||||
|
tags=["Retorna o ato e suas dependências"],
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue