Files
moloapp/src/anthropic/admin/webhook.h
T

38 lines
1.4 KiB
C++

#pragma once
// Webhook signature verification for Managed Agents and organization
// webhooks. Scheme: Standard Webhooks (what the official SDK's `unwrap`
// delegates to): headers webhook-id, webhook-timestamp, webhook-signature;
// signature = base64(HMAC-SHA256(key, "<id>.<timestamp>.<payload>")),
// header value "v1,<sig>[ v1,<sig>...]"; key is "whsec_<base64>". Timestamp
// tolerance is capped (Caps::kWebhookToleranceSeconds).
#include <map>
#include <string>
#include "clients/body/json.h"
namespace molo::anthropic::admin {
class WebhookVerifier {
public:
explicit WebhookVerifier(const std::string& signing_key);
// Signature header value for a message (used by tests and by senders).
std::string sign(const std::string& id, long timestamp, const std::string& payload) const;
// True when some v1 signature matches and |now - timestamp| is within the
// tolerance. `headers` keys are matched case-insensitively. Never throws.
bool verify(const std::string& payload, const std::map<std::string, std::string>& headers,
long now, std::string* why = nullptr) const;
// Verifies, then parses the payload. Throws llm::ApiError on failure.
clients::body::Json unwrap(const std::string& payload,
const std::map<std::string, std::string>& headers, long now) const;
private:
std::string key_;
};
} // namespace molo::anthropic::admin