import hashlib, hmac, json
from fastapi import Request, HTTPException
async def validar_auth_webhook(request: Request, secret: str) -> dict:
body = await request.body()
assinatura = request.headers.get('x-campeao-signature', '')
esperado = 'sha256=' + hmac.new(
secret.encode('utf-8'),
body,
hashlib.sha256
).hexdigest()
if not hmac.compare_digest(assinatura, esperado):
raise HTTPException(status_code=401, detail='Assinatura inválida')
return json.loads(body)
@app.post('/webhook/auth')
async def webhook_auth(request: Request):
evento = await validar_auth_webhook(request, os.environ['WEBHOOK_SECRET'])
if evento['authenticated']:
await liberar_operacao(evento['external_id'], evento['auth_id'])
return {'ok': True}