compact_json_bytes (webhook_hmac.py)

shkeeper.io · auth

Creates compact token payload from arbitrary bytes for HMAC signature calculation.

from __future__ import annotations

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

WEBHOOK_SIGNATURE_HEADER = "X-Shkeeper-Signature"
WEBHOOK_TIMESTAMP_HEADER = "X-Shkeeper-Timestamp"


def compact_json_bytes(payload: dict[str, Any]) -> bytes:
    return json.dumps(
        payload,
        separators=(",", ":"),
        sort_keys=True,
        ensure_ascii=False,
    ).encode("utf-8")


def _hmac_v1(secret: str, body: bytes, ts: int) -> str:
    key = secret.encode("utf-8")
    msg = f"{ts}.".encode("ascii") + body
    return hmac.new(key, msg, hashlib.sha256).hexdigest()


def shkeeper_webhook_auth_headers(
    secret: str, body: bytes, timestamp: int | None = None
) -> dict[str, str]:
    ts = int(time.time()) if timestamp is None else int(timestamp)
    diges

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key