StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
WebhookVerifier.h
1/** WebhookVerifier.h
2 *
3 * Stripe webhook signature verification interfaces and default verifier.
4 *
5 * @author Hans de Ruiter
6 *
7 * @license See LICENSE.md for details.
8 */
9
10#pragma once
11
12#include <chrono>
13#include <cstdint>
14#include <string>
15#include <string_view>
16
17namespace stripekit {
18
19/**
20 * @addtogroup webhooks
21 * @{
22 */
23
24/**
25 * Everything needed to check a webhook signature.
26 */
28 /**
29 * The request body exactly as received.
30 *
31 * The signature covers these precise bytes, so any reformatting, parsing,
32 * or character-set conversion beforehand will cause the check to fail.
33 */
34 std::string_view payload;
35
36 /** Value of the "Stripe-Signature" header. */
37 std::string_view stripe_signature_header;
38
39 /** Signing secret for this endpoint, of the form "whsec_...". */
40 std::string_view endpoint_secret;
41
42 /** Current time, used to check the signature's timestamp. */
43 std::chrono::system_clock::time_point now;
44
45 /** How far the signature's timestamp may be from #now. */
46 std::chrono::seconds tolerance = std::chrono::minutes(5);
47};
48
49/**
50 * What was learned from a signature that checked out.
51 */
53 /** When Stripe signed the delivery, as a Unix timestamp. */
54 std::int64_t timestamp_unix = 0;
55
56 /** The exact string the signature was computed over. */
57 std::string signed_payload;
58};
59
60/**
61 * Checks that a webhook delivery really came from Stripe.
62 *
63 * Applications do not normally use this directly — stripekit::webhooks::construct_event()
64 * and stripekit::webhooks::process_event_once() do the verification for them.
65 * It is worth implementing only to substitute a different crypto backend, or to
66 * inject failures in tests.
67 */
69public:
70 /**
71 * Destroys the verifier instance.
72 */
73 virtual ~WebhookSignatureVerifier() = default;
74
75 /**
76 * Checks a signature and reports what it contained.
77 *
78 * @param input The payload, signature header, secret, and timing rules.
79 * @return The timestamp and signed string from the verified signature.
80 * @throws SignatureVerificationException The header is malformed, no
81 * signature matches, or the timestamp is outside the tolerance.
82 */
83 virtual VerificationMetadata verify(const WebhookVerificationInput& input) const = 0;
84};
85
86/**
87 * The signature verifier StripeKit uses by default.
88 *
89 * Implements Stripe's scheme: HMAC-SHA256 over the timestamp and payload,
90 * compared in constant time so the comparison itself leaks nothing.
91 *
92 * @sa https://docs.stripe.com/webhooks#verify-events
93 */
95public:
96 /**
97 * Checks a signature and reports what it contained.
98 *
99 * @param input The payload, signature header, secret, and timing rules.
100 * @return The timestamp and signed string from the verified signature.
101 * @throws SignatureVerificationException The header is malformed, no
102 * signature matches, or the timestamp is outside the tolerance.
103 * @throws ConfigurationException This build has no cryptography support.
104 */
106
107 /**
108 * Reports whether this build can verify signatures.
109 *
110 * Verification needs libsodium, which is a build option. Without it,
111 * webhooks cannot be trusted and must not be accepted.
112 *
113 * @return True when signature verification is available.
114 */
115 static bool is_available();
116};
117
118/** @} */
119
120} // namespace stripekit
The signature verifier StripeKit uses by default.
VerificationMetadata verify(const WebhookVerificationInput &input) const override
Checks a signature and reports what it contained.
static bool is_available()
Reports whether this build can verify signatures.
Checks that a webhook delivery really came from Stripe.
virtual VerificationMetadata verify(const WebhookVerificationInput &input) const =0
Checks a signature and reports what it contained.
virtual ~WebhookSignatureVerifier()=default
Destroys the verifier instance.
Async.h.
Definition Async.h:20
What was learned from a signature that checked out.
std::int64_t timestamp_unix
When Stripe signed the delivery, as a Unix timestamp.
std::string signed_payload
The exact string the signature was computed over.
Everything needed to check a webhook signature.
std::string_view endpoint_secret
Signing secret for this endpoint, of the form "whsec_...".
std::chrono::system_clock::time_point now
Current time, used to check the signature's timestamp.
std::string_view payload
The request body exactly as received.
std::chrono::seconds tolerance
How far the signature's timestamp may be from now.
std::string_view stripe_signature_header
Value of the "Stripe-Signature" header.