standardwebhooks 1.1.0 → 1.3.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 +4 -4
- data/README.md +21 -0
- data/lib/standardwebhooks/errors.rb +3 -0
- data/lib/standardwebhooks/webhooks.rb +35 -15
- metadata +15 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8bb740db7282bf24af2820d6e40f0670851b48be3247ec02f07d4134fe3efd81
|
|
4
|
+
data.tar.gz: deeb2a023eb1b42e001f16e480b62d208c398d0eb9b2bf07aa86d802ddacbafa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: da5d0530e6984ab6f5b3214d322125194b94d904b243998870af685bd6fd8d2baefcb6018cb8998dd7d3d0f06f25f369f8c97476ee59514e2c6641062f2e7b07
|
|
7
|
+
data.tar.gz: 3db16e0362fffca2b3d407e1dd5e2cc9e0ab2671f0da120760378dd61dcae9e5d0094f7e948a69d28f2393207bbc94c40fc2f052b965d04beef1e103b6777d6c
|
data/README.md
CHANGED
|
@@ -37,6 +37,27 @@ puts payload['type']
|
|
|
37
37
|
Pass the raw body exactly as received. Verifying a parsed-and-reserialized JSON body
|
|
38
38
|
can fail because the signature is computed over the original bytes.
|
|
39
39
|
|
|
40
|
+
## Responding to endpoint verification
|
|
41
|
+
|
|
42
|
+
To support the [endpoint verification](../../spec/extensions/endpoint-verification.md) extension,
|
|
43
|
+
use `#endpoint_verification_response` when the request includes a `webhook-challenge` header:
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
challenge = request_headers['webhook-challenge']
|
|
47
|
+
if challenge
|
|
48
|
+
response_headers['webhook-challenge-response'] = webhook.endpoint_verification_response(challenge)
|
|
49
|
+
# Not a webhook delivery: respond (e.g. with 204 No Content) without processing the body
|
|
50
|
+
return
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Producers can verify the response with `#verify_endpoint_verification_response`, which raises
|
|
55
|
+
`StandardWebhooks::WebhookVerificationError` if it doesn't match:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
webhook.verify_endpoint_verification_response(challenge, response_headers['webhook-challenge-response'])
|
|
59
|
+
```
|
|
60
|
+
|
|
40
61
|
## Rails example
|
|
41
62
|
|
|
42
63
|
```ruby
|
|
@@ -7,9 +7,8 @@ require "uri"
|
|
|
7
7
|
|
|
8
8
|
module StandardWebhooks
|
|
9
9
|
class Webhook
|
|
10
|
-
|
|
11
10
|
def self.new_using_raw_bytes(secret)
|
|
12
|
-
|
|
11
|
+
new(secret.pack("C*").force_encoding("UTF-8"))
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
def initialize(secret)
|
|
@@ -18,6 +17,10 @@ module StandardWebhooks
|
|
|
18
17
|
end
|
|
19
18
|
|
|
20
19
|
@secret = Base64.decode64(secret)
|
|
20
|
+
|
|
21
|
+
if @secret.empty?
|
|
22
|
+
raise EmptyWebhookSecretError, "Webhook secret must not be blank"
|
|
23
|
+
end
|
|
21
24
|
end
|
|
22
25
|
|
|
23
26
|
def verify(payload, headers)
|
|
@@ -31,28 +34,23 @@ module StandardWebhooks
|
|
|
31
34
|
|
|
32
35
|
verify_timestamp(msg_timestamp)
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
signature = sign(msg_id, msg_timestamp, payload)
|
|
35
38
|
|
|
36
39
|
passed_signatures = msg_signature.split(" ")
|
|
40
|
+
matched = passed_signatures.any? do |versioned_signature|
|
|
41
|
+
::StandardWebhooks.secure_compare(versioned_signature, signature)
|
|
42
|
+
end
|
|
37
43
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if version != "v1"
|
|
42
|
-
next
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
if ::StandardWebhooks::secure_compare(signature, expected_signature)
|
|
46
|
-
return JSON.parse(payload)
|
|
47
|
-
end
|
|
44
|
+
unless matched
|
|
45
|
+
raise WebhookVerificationError, "No matching signature found"
|
|
48
46
|
end
|
|
49
47
|
|
|
50
|
-
|
|
48
|
+
JSON.parse(payload)
|
|
51
49
|
end
|
|
52
50
|
|
|
53
51
|
def sign(msg_id, timestamp, payload)
|
|
54
52
|
begin
|
|
55
|
-
|
|
53
|
+
timestamp = Integer(timestamp)
|
|
56
54
|
rescue
|
|
57
55
|
raise WebhookSigningError, "Invalid timestamp"
|
|
58
56
|
end
|
|
@@ -63,6 +61,28 @@ module StandardWebhooks
|
|
|
63
61
|
"v1,#{signature}"
|
|
64
62
|
end
|
|
65
63
|
|
|
64
|
+
# Calculate the response to an endpoint verification challenge. Takes the value of the
|
|
65
|
+
# `webhook-challenge` request header and returns the value for the
|
|
66
|
+
# `webhook-challenge-response` response header.
|
|
67
|
+
def endpoint_verification_response(challenge)
|
|
68
|
+
verification_key = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), @secret, "wev-v1")
|
|
69
|
+
digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), verification_key, challenge)
|
|
70
|
+
signature = Base64.encode64(digest).strip
|
|
71
|
+
|
|
72
|
+
"v1,#{signature}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Verify the response to an endpoint verification challenge. Takes the challenge sent in
|
|
76
|
+
# the `webhook-challenge` request header and the value of the `webhook-challenge-response`
|
|
77
|
+
# response header. Raises WebhookVerificationError if no matching signature is found.
|
|
78
|
+
def verify_endpoint_verification_response(challenge, header)
|
|
79
|
+
signature = endpoint_verification_response(challenge)
|
|
80
|
+
|
|
81
|
+
unless ::StandardWebhooks.secure_compare(header, signature)
|
|
82
|
+
raise WebhookVerificationError, "No matching signature found"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
66
86
|
SECRET_PREFIX = "whsec_"
|
|
67
87
|
TOLERANCE = 5 * 60
|
|
68
88
|
private_constant :SECRET_PREFIX, :TOLERANCE
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: standardwebhooks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Standard Webhooks
|
|
@@ -9,6 +9,20 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: base64
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.3.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.3.0
|
|
12
26
|
- !ruby/object:Gem::Dependency
|
|
13
27
|
name: bundler
|
|
14
28
|
requirement: !ruby/object:Gem::Requirement
|