Class: Clearbit::Webhook
- Inherits:
-
Mash
- Object
- Hash
- Mash
- Clearbit::Webhook
show all
- Defined in:
- lib/clearbit/webhook.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Mash
#custom_reader, #custom_writer, #deep_merge, #deep_update, #delete, #dup, #fetch, #id, #initializing_reader, #key?, #method_missing, new, #regular_dup, #replace, #respond_to?, #shallow_merge, #shallow_update, #type, #underbang_reader
Constructor Details
#initialize(env, key = nil) ⇒ Webhook
Returns a new instance of Webhook.
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/clearbit/webhook.rb', line 31
def initialize(env, key = nil)
request = Rack::Request.new(env)
request.body.rewind
signature = request.env['HTTP_X_REQUEST_SIGNATURE']
body = request.body.read
self.class.valid!(signature, body, key)
merge!(JSON.parse(body))
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Clearbit::Mash
Class Method Details
.clearbit_key ⇒ Object
7
8
9
|
# File 'lib/clearbit/webhook.rb', line 7
def self.clearbit_key
Clearbit.key!
end
|
.generate_signature(key, body) ⇒ Object
25
26
27
28
29
|
# File 'lib/clearbit/webhook.rb', line 25
def self.generate_signature(key, body)
signed_body = body
signed_body = JSON.dump(signed_body) unless signed_body.is_a?(String)
'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), key, signed_body)
end
|
.valid!(signature, body, key = nil) ⇒ Object
21
22
23
|
# File 'lib/clearbit/webhook.rb', line 21
def self.valid!(signature, body, key = nil)
valid?(signature, body, key) ? true : raise(Errors::InvalidWebhookSignature.new)
end
|
.valid?(request_signature, body, key = nil) ⇒ Boolean
11
12
13
14
15
16
17
18
19
|
# File 'lib/clearbit/webhook.rb', line 11
def self.valid?(request_signature, body, key = nil)
return false unless request_signature && body
key = (key || clearbit_key).gsub(/\A(pk|sk)_/, '')
signature = generate_signature(key, body)
Rack::Utils.secure_compare(request_signature, signature)
end
|