File size: 978 Bytes
14b1bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""HMAC-signed webhook delivery."""

from __future__ import annotations

import hashlib
import hmac
import json
import os
import time
from typing import Any

import requests

_SECRET = os.getenv("MYTHOS_WEBHOOK_SECRET", "")


def sign(payload: bytes) -> str:
    if not _SECRET:
        return "unsigned"
    mac = hmac.new(_SECRET.encode(), payload, hashlib.sha256).hexdigest()
    return f"sha256={mac}"


def deliver(url: str, event: str, payload: dict[str, Any]) -> dict[str, Any]:
    body = json.dumps({"event": event, "ts": time.time(), "payload": payload},
                      default=str).encode()
    headers = {
        "Content-Type": "application/json",
        "X-Mythos-Event": event,
        "X-Mythos-Signature": sign(body),
    }
    try:
        r = requests.post(url, data=body, headers=headers, timeout=15)
        return {"status_code": r.status_code, "body": r.text[:500]}
    except Exception as exc:  # noqa: BLE001
        return {"error": str(exc)}