standardwebhooks 1.0.1 → 1.1.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 +113 -16
- data/lib/standardwebhooks/util.rb +4 -26
- data/lib/standardwebhooks/webhooks.rb +5 -4
- metadata +10 -16
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -35
- data/Rakefile +0 -2
- data/standardwebhooks.gemspec +0 -38
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 527be86f5f1283493f1bcfa756fa7b2a4be47b1cbbee65ca14e75c5003c91e35
|
|
4
|
+
data.tar.gz: 42e508d57aff131e82198316ed8ead6e9f783ab9145ee9fe6110cf29e6fdc116
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b987125637df2690de12b2ec5fe72efc74ace94c4c5f00e932c333fc5626acf35773eccdeb5c652f250cf522798d7b064900da6d12638b7a279303fec467034b
|
|
7
|
+
data.tar.gz: 99ffc3f44c41321e71fb039be51851cc24858de63fc91bd3b52b4d2d447f170f25a802f43d42aeb02d3afd8bc1ea627c9731cbf1cb08d53b97dfd60bfa8f33a2
|
data/README.md
CHANGED
|
@@ -1,35 +1,132 @@
|
|
|
1
|
-
Ruby library for Standard Webhooks
|
|
1
|
+
Ruby library for Standard Webhooks
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
# Installation
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Add `standardwebhooks` to your `Gemfile` and run `bundle install`.
|
|
6
|
+
|
|
7
|
+
Or install it without bundler:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
gem install standardwebhooks
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
# Verifying webhooks
|
|
14
|
+
|
|
15
|
+
`StandardWebhooks::Webhook#verify` expects:
|
|
16
|
+
|
|
17
|
+
- The exact raw request body as a string.
|
|
18
|
+
- A hash containing `webhook-id`, `webhook-timestamp`, and `webhook-signature`.
|
|
19
|
+
|
|
20
|
+
It returns the parsed JSON payload as a Ruby hash and raises
|
|
21
|
+
`StandardWebhooks::WebhookVerificationError` when verification fails.
|
|
6
22
|
|
|
7
23
|
```ruby
|
|
8
|
-
require
|
|
24
|
+
require 'standardwebhooks'
|
|
25
|
+
|
|
26
|
+
webhook = StandardWebhooks::Webhook.new(ENV.fetch('WEBHOOK_SECRET'))
|
|
9
27
|
|
|
10
|
-
|
|
11
|
-
|
|
28
|
+
payload = webhook.verify(raw_body, {
|
|
29
|
+
'webhook-id' => request_headers.fetch('webhook-id'),
|
|
30
|
+
'webhook-timestamp' => request_headers.fetch('webhook-timestamp'),
|
|
31
|
+
'webhook-signature' => request_headers.fetch('webhook-signature'),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
puts payload['type']
|
|
12
35
|
```
|
|
13
36
|
|
|
14
|
-
|
|
37
|
+
Pass the raw body exactly as received. Verifying a parsed-and-reserialized JSON body
|
|
38
|
+
can fail because the signature is computed over the original bytes.
|
|
15
39
|
|
|
16
|
-
##
|
|
40
|
+
## Rails example
|
|
17
41
|
|
|
18
|
-
```
|
|
19
|
-
|
|
42
|
+
```ruby
|
|
43
|
+
class WebhooksController < ApplicationController
|
|
44
|
+
def create
|
|
45
|
+
webhook = StandardWebhooks::Webhook.new(ENV.fetch('WEBHOOK_SECRET'))
|
|
46
|
+
|
|
47
|
+
payload = webhook.verify(request.raw_post, {
|
|
48
|
+
'webhook-id' => request.headers.fetch('webhook-id'),
|
|
49
|
+
'webhook-timestamp' => request.headers.fetch('webhook-timestamp'),
|
|
50
|
+
'webhook-signature' => request.headers.fetch('webhook-signature'),
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
process_event(payload)
|
|
54
|
+
|
|
55
|
+
head :ok
|
|
56
|
+
rescue StandardWebhooks::WebhookVerificationError => error
|
|
57
|
+
Rails.logger.warn("Invalid webhook: #{error.message}")
|
|
58
|
+
|
|
59
|
+
head :bad_request
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def process_event(payload)
|
|
65
|
+
case payload['type']
|
|
66
|
+
when 'customer.created'
|
|
67
|
+
CustomerCreateJob.perform_later(payload.fetch('data'))
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Rack or Sinatra example
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
post '/webhooks' do
|
|
77
|
+
request.body.rewind
|
|
78
|
+
raw_body = request.body.read
|
|
79
|
+
|
|
80
|
+
webhook = StandardWebhooks::Webhook.new(ENV.fetch('WEBHOOK_SECRET'))
|
|
81
|
+
|
|
82
|
+
payload = webhook.verify(raw_body, {
|
|
83
|
+
'webhook-id' => request.env.fetch('HTTP_WEBHOOK_ID'),
|
|
84
|
+
'webhook-timestamp' => request.env.fetch('HTTP_WEBHOOK_TIMESTAMP'),
|
|
85
|
+
'webhook-signature' => request.env.fetch('HTTP_WEBHOOK_SIGNATURE'),
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
handle_event(payload)
|
|
89
|
+
|
|
90
|
+
status 204
|
|
91
|
+
rescue StandardWebhooks::WebhookVerificationError
|
|
92
|
+
halt 400
|
|
93
|
+
end
|
|
20
94
|
```
|
|
21
95
|
|
|
22
|
-
|
|
96
|
+
# Signing payloads
|
|
23
97
|
|
|
24
|
-
|
|
98
|
+
If you are producing Standard Webhooks or need deterministic signatures in tests,
|
|
99
|
+
use `#sign`:
|
|
25
100
|
|
|
26
|
-
```
|
|
27
|
-
|
|
101
|
+
```ruby
|
|
102
|
+
require 'standardwebhooks'
|
|
103
|
+
|
|
104
|
+
webhook = StandardWebhooks::Webhook.new(ENV.fetch('WEBHOOK_SECRET'))
|
|
105
|
+
|
|
106
|
+
msg_id = 'msg_2KWPBgLlAfxdpx2AI54pPJ85f4W'
|
|
107
|
+
timestamp = Time.now.to_i
|
|
108
|
+
payload = '{"type":"example.event","timestamp":"2022-11-03T20:26:10.344522Z","data":{"foo":"bar"}}'
|
|
109
|
+
|
|
110
|
+
signature = webhook.sign(msg_id, timestamp, payload)
|
|
111
|
+
|
|
112
|
+
headers = {
|
|
113
|
+
"webhook-id" => msg_id,
|
|
114
|
+
"webhook-timestamp" => timestamp.to_s,
|
|
115
|
+
"webhook-signature" => signature,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
# …
|
|
28
119
|
```
|
|
29
120
|
|
|
30
|
-
|
|
121
|
+
# Development
|
|
122
|
+
|
|
123
|
+
## Building
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
bundler exec rake build
|
|
127
|
+
```
|
|
31
128
|
|
|
32
|
-
|
|
129
|
+
## Running tests
|
|
33
130
|
|
|
34
131
|
```sh
|
|
35
132
|
bundle exec rspec spec
|
|
@@ -1,38 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Constant time string comparison, for fixed length strings.
|
|
4
|
-
# Code borrowed from ActiveSupport
|
|
5
|
-
# https://github.com/rails/rails/blob/75ac626c4e21129d8296d4206a1960563cc3d4aa/activesupport/lib/active_support/security_utils.rb#L33
|
|
6
|
-
#
|
|
7
|
-
# The values compared should be of fixed length, such as strings
|
|
8
|
-
# that have already been processed by HMAC. Raises in case of length mismatch.
|
|
9
3
|
module StandardWebhooks
|
|
10
|
-
|
|
11
|
-
def fixed_length_secure_compare(a, b)
|
|
12
|
-
OpenSSL.fixed_length_secure_compare(a, b)
|
|
13
|
-
end
|
|
14
|
-
else
|
|
15
|
-
def fixed_length_secure_compare(a, b)
|
|
16
|
-
raise ArgumentError, "string length mismatch." unless a.bytesize == b.bytesize
|
|
17
|
-
|
|
18
|
-
l = a.unpack "C#{a.bytesize}"
|
|
19
|
-
|
|
20
|
-
res = 0
|
|
21
|
-
b.each_byte { |byte| res |= byte ^ l.shift }
|
|
22
|
-
res == 0
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
module_function :fixed_length_secure_compare
|
|
27
|
-
|
|
28
|
-
# Secure string comparison for strings of variable length.
|
|
4
|
+
# Secure string comparison for strings of fixed length
|
|
29
5
|
#
|
|
30
6
|
# While a timing attack would not be able to discern the content of
|
|
31
7
|
# a secret compared via secure_compare, it is possible to determine
|
|
32
8
|
# the secret length. This should be considered when using secure_compare
|
|
33
9
|
# to compare weak, short secrets to user input.
|
|
34
10
|
def secure_compare(a, b)
|
|
35
|
-
a.
|
|
11
|
+
return false unless a.bytesize == b.bytesize
|
|
12
|
+
|
|
13
|
+
OpenSSL.fixed_length_secure_compare(a, b)
|
|
36
14
|
end
|
|
37
15
|
|
|
38
16
|
module_function :secure_compare
|
|
@@ -43,7 +43,7 @@ module StandardWebhooks
|
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
if ::StandardWebhooks::secure_compare(signature, expected_signature)
|
|
46
|
-
return JSON.parse(payload
|
|
46
|
+
return JSON.parse(payload)
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
@@ -60,13 +60,14 @@ module StandardWebhooks
|
|
|
60
60
|
to_sign = "#{msg_id}.#{timestamp}.#{payload}"
|
|
61
61
|
signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), @secret, to_sign)).strip
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
"v1,#{signature}"
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
-
private
|
|
67
|
-
|
|
68
66
|
SECRET_PREFIX = "whsec_"
|
|
69
67
|
TOLERANCE = 5 * 60
|
|
68
|
+
private_constant :SECRET_PREFIX, :TOLERANCE
|
|
69
|
+
|
|
70
|
+
private
|
|
70
71
|
|
|
71
72
|
def verify_timestamp(timestamp_header)
|
|
72
73
|
begin
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: standardwebhooks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Standard Webhooks
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
@@ -52,28 +51,23 @@ dependencies:
|
|
|
52
51
|
- - "~>"
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
53
|
version: '3.2'
|
|
55
|
-
description:
|
|
56
|
-
email:
|
|
57
54
|
executables: []
|
|
58
55
|
extensions: []
|
|
59
56
|
extra_rdoc_files: []
|
|
60
57
|
files:
|
|
61
|
-
- Gemfile
|
|
62
|
-
- Gemfile.lock
|
|
63
58
|
- README.md
|
|
64
|
-
- Rakefile
|
|
65
59
|
- lib/standardwebhooks.rb
|
|
66
60
|
- lib/standardwebhooks/errors.rb
|
|
67
61
|
- lib/standardwebhooks/util.rb
|
|
68
62
|
- lib/standardwebhooks/webhooks.rb
|
|
69
|
-
- standardwebhooks.gemspec
|
|
70
|
-
homepage:
|
|
71
63
|
licenses:
|
|
72
64
|
- MIT
|
|
73
65
|
metadata:
|
|
74
66
|
allowed_push_host: https://rubygems.org
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
homepage_uri: https://www.standardwebhooks.com/
|
|
68
|
+
bug_tracker_uri: https://github.com/standard-webhooks/standard-webhooks/issues
|
|
69
|
+
source_code_uri: https://github.com/standard-webhooks/standard-webhooks/tree/main/libraries/ruby
|
|
70
|
+
documentation_uri: https://github.com/standard-webhooks/standard-webhooks/blob/main/libraries/ruby/README.md
|
|
77
71
|
rdoc_options: []
|
|
78
72
|
require_paths:
|
|
79
73
|
- lib
|
|
@@ -81,15 +75,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
81
75
|
requirements:
|
|
82
76
|
- - ">="
|
|
83
77
|
- !ruby/object:Gem::Version
|
|
84
|
-
version: '
|
|
78
|
+
version: '3.3'
|
|
85
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
80
|
requirements:
|
|
87
81
|
- - ">="
|
|
88
82
|
- !ruby/object:Gem::Version
|
|
89
83
|
version: '0'
|
|
90
84
|
requirements: []
|
|
91
|
-
rubygems_version: 3.
|
|
92
|
-
signing_key:
|
|
85
|
+
rubygems_version: 3.6.9
|
|
93
86
|
specification_version: 4
|
|
94
|
-
summary:
|
|
87
|
+
summary: Library for creating and verifying webhook signatures for the Standard Webhook
|
|
88
|
+
spec.
|
|
95
89
|
test_files: []
|
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
PATH
|
|
2
|
-
remote: .
|
|
3
|
-
specs:
|
|
4
|
-
standardwebhooks (1.0.1)
|
|
5
|
-
|
|
6
|
-
GEM
|
|
7
|
-
remote: https://rubygems.org/
|
|
8
|
-
specs:
|
|
9
|
-
diff-lcs (1.5.1)
|
|
10
|
-
rake (13.2.1)
|
|
11
|
-
rspec (3.13.0)
|
|
12
|
-
rspec-core (~> 3.13.0)
|
|
13
|
-
rspec-expectations (~> 3.13.0)
|
|
14
|
-
rspec-mocks (~> 3.13.0)
|
|
15
|
-
rspec-core (3.13.0)
|
|
16
|
-
rspec-support (~> 3.13.0)
|
|
17
|
-
rspec-expectations (3.13.0)
|
|
18
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
-
rspec-support (~> 3.13.0)
|
|
20
|
-
rspec-mocks (3.13.0)
|
|
21
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
-
rspec-support (~> 3.13.0)
|
|
23
|
-
rspec-support (3.13.0)
|
|
24
|
-
|
|
25
|
-
PLATFORMS
|
|
26
|
-
x86_64-linux
|
|
27
|
-
|
|
28
|
-
DEPENDENCIES
|
|
29
|
-
bundler (>= 2.2.10)
|
|
30
|
-
rake (~> 13.0)
|
|
31
|
-
rspec (~> 3.2)
|
|
32
|
-
standardwebhooks!
|
|
33
|
-
|
|
34
|
-
BUNDLED WITH
|
|
35
|
-
2.4.22
|
data/Rakefile
DELETED
data/standardwebhooks.gemspec
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "standardwebhooks"
|
|
7
|
-
spec.version = "1.0.1"
|
|
8
|
-
spec.authors = ["Standard Webhooks"]
|
|
9
|
-
spec.license = "MIT"
|
|
10
|
-
|
|
11
|
-
spec.summary = "Ruby library for creating and verifying webhook signatures."
|
|
12
|
-
|
|
13
|
-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
14
|
-
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
15
|
-
if spec.respond_to?(:metadata)
|
|
16
|
-
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
17
|
-
spec.metadata["source_code_uri"] = "https://github.com/standard-webhooks/standard-webhooks"
|
|
18
|
-
else
|
|
19
|
-
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
20
|
-
"public gem pushes."
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
|
24
|
-
ignored = Regexp.union(
|
|
25
|
-
/\Aspec/,
|
|
26
|
-
/\Apkg/,
|
|
27
|
-
/\Atemplates/,
|
|
28
|
-
/\A.gitignore/,
|
|
29
|
-
/.gem\z/
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
spec.files = Dir['**/*'].reject {|f| !File.file?(f) || ignored.match(f) }
|
|
33
|
-
spec.require_paths = ["lib"]
|
|
34
|
-
|
|
35
|
-
spec.add_development_dependency "bundler", ">= 2.2.10"
|
|
36
|
-
spec.add_development_dependency "rake", "~> 13.0"
|
|
37
|
-
spec.add_development_dependency "rspec", "~> 3.2"
|
|
38
|
-
end
|