philiprehberger-webhook_builder 0.2.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc97be824aada00ec5793cb30aedac642f075494d8e5d73410995d65270a60f0
4
- data.tar.gz: 79ff8ead44f6bdb0493eb6af38b145ad9d5a44ff6eec49d41aec49d2d24d66f7
3
+ metadata.gz: 5c3d746e099083b6d9da206ce5b4630ca0a4c5ee2c4f1f99373af3c992d89c38
4
+ data.tar.gz: 23ed5e8d7b8e40f27a71cf7f4cb42c91d501ef8a5028d39d0a77cd1d08a589be
5
5
  SHA512:
6
- metadata.gz: 020aaf34af4930b84e913fe0fcb23100cd9aa10a9e0b7cc8d50bd1dd2e4e5a7531e327bf28879bca64a5961adb1709839ff767e75d60051b48a815b11b37448e
7
- data.tar.gz: 657d920700388d52e2512d1b5d4de25e0c17a6266041f468857f41628f68fe024844d41892a72224f4632422ba7d2e7237b4703d8dacc19c0cc40236d80f2311
6
+ metadata.gz: 7e503cd54a9fb91a78879f0ccd541a24b91a447ca30ba89c7d832a4c757d07de2a35e7c0066c8c0e13c01b130f6765afdc96b4b8e234c4ba60d492aba2d3e536
7
+ data.tar.gz: 6f9942dc1678940173ab109c17b236f83bf14087ed1dae8880f17a2703e8e89a87c2455e752ea7c9b6aff14f6992bd408db65bd46b0499729ec8644d27e7d561
data/CHANGELOG.md CHANGED
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2026-04-22
11
+
12
+ ### Added
13
+ - `Client#signature_for(body:)` — compute the HMAC-SHA256 signature this client would send for a given body without performing a delivery.
14
+
15
+ ## [0.3.0] - 2026-04-18
16
+
17
+ ### Added
18
+ - `Client#verify_signature(body:, signature:)` — constant-time HMAC-SHA256 comparison for the receiving side; returns `false` (never raises) on length mismatch
19
+
10
20
  ## [0.2.2] - 2026-04-15
11
21
 
12
22
  ### Changed
data/README.md CHANGED
@@ -114,6 +114,44 @@ client.deliver(
114
114
  )
115
115
  ```
116
116
 
117
+ ### Verifying signatures
118
+
119
+ On the receiving side, use the same secret to verify the signature sent in the
120
+ `X-Webhook-Signature` header. The comparison is constant-time and will never
121
+ raise on malformed input.
122
+
123
+ ```ruby
124
+ require "philiprehberger/webhook_builder"
125
+
126
+ secret = "shared-signing-secret"
127
+ sender = Philiprehberger::WebhookBuilder.new(url: "https://example.com/webhooks", secret: secret)
128
+ receiver = Philiprehberger::WebhookBuilder.new(url: "https://example.com/webhooks", secret: secret)
129
+
130
+ body = '{"event":"order.created","payload":{"id":1}}'
131
+ signature = OpenSSL::HMAC.hexdigest("SHA256", secret, body)
132
+
133
+ receiver.verify_signature(body: body, signature: signature) # => true
134
+ receiver.verify_signature(body: body, signature: "tampered") # => false
135
+ ```
136
+
137
+ You can also compute the signature the client would send for a body without
138
+ performing a delivery — useful for preparing payloads offline or mirroring
139
+ `verify_signature`:
140
+
141
+ ```ruby
142
+ require "philiprehberger/webhook_builder"
143
+
144
+ client = Philiprehberger::WebhookBuilder.new(
145
+ url: "https://example.com/webhooks",
146
+ secret: "shared-signing-secret"
147
+ )
148
+
149
+ body = '{"event":"order.created","payload":{"id":1}}'
150
+ signature = client.signature_for(body: body)
151
+
152
+ client.verify_signature(body: body, signature: signature) # => true
153
+ ```
154
+
117
155
  ### Delivery Tracking
118
156
 
119
157
  ```ruby
@@ -143,6 +181,8 @@ delivery.error # => nil or error message
143
181
  | `.new(url:, secret:, timeout:, max_retries:, backoff:, concurrency:, default_headers:)` | Create a webhook client |
144
182
  | `#deliver(event:, payload:, headers:)` | Deliver a webhook event and return a Delivery |
145
183
  | `#deliver_batch(events)` | Deliver multiple events concurrently and return an array of Delivery results |
184
+ | `#verify_signature(body:, signature:)` | Constant-time HMAC-SHA256 verification of an incoming signature; returns `true`/`false` and never raises |
185
+ | `#signature_for(body:)` | Compute the HMAC-SHA256 signature for a body without sending |
146
186
 
147
187
  ### `Delivery`
148
188
 
@@ -137,6 +137,34 @@ module Philiprehberger
137
137
  results
138
138
  end
139
139
 
140
+ # Verify an HMAC-SHA256 signature against a raw request body.
141
+ #
142
+ # Uses a constant-time comparison to resist timing attacks. Returns
143
+ # +false+ (never raises) when the provided signature has a different
144
+ # length than the expected hex digest.
145
+ #
146
+ # @param body [String] the raw request body
147
+ # @param signature [String] the hex-encoded signature to verify
148
+ # @return [Boolean] true when the signature matches, false otherwise
149
+ def verify_signature(body:, signature:)
150
+ expected = sign(body)
151
+ return false unless signature.is_a?(String) && signature.bytesize == expected.bytesize
152
+
153
+ OpenSSL.fixed_length_secure_compare(expected, signature)
154
+ end
155
+
156
+ # Compute the HMAC-SHA256 hex signature this client would send for the given body.
157
+ #
158
+ # Mirrors +verify_signature+: the signature returned by +signature_for(body:)+
159
+ # will verify against the same body. Useful when preparing payloads offline or
160
+ # re-computing signatures without sending a request.
161
+ #
162
+ # @param body [String] the raw request body
163
+ # @return [String] the hex-encoded HMAC signature
164
+ def signature_for(body:)
165
+ sign(body)
166
+ end
167
+
140
168
  private
141
169
 
142
170
  # Sign the request body with HMAC-SHA256.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module WebhookBuilder
5
- VERSION = '0.2.2'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-webhook_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-15 00:00:00.000000000 Z
11
+ date: 2026-04-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A webhook delivery client that signs payloads with HMAC-SHA256, retries
14
14
  failed deliveries with configurable backoff strategies, supports batch delivery,