Class: Boxr::WebhookValidator
- Inherits:
-
Object
- Object
- Boxr::WebhookValidator
- Defined in:
- lib/boxr/webhook_validator.rb
Constant Summary collapse
- MAXIMUM_MESSAGE_AGE =
10 minutes (in seconds)
600
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#primary_signature ⇒ Object
readonly
Returns the value of attribute primary_signature.
-
#primary_signature_key ⇒ Object
readonly
Returns the value of attribute primary_signature_key.
-
#secondary_signature ⇒ Object
readonly
Returns the value of attribute secondary_signature.
-
#secondary_signature_key ⇒ Object
readonly
Returns the value of attribute secondary_signature_key.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #generate_signature(key) ⇒ Object
-
#initialize(headers, payload, primary_signature_key: nil, secondary_signature_key: nil) ⇒ WebhookValidator
constructor
A new instance of WebhookValidator.
- #valid_message? ⇒ Boolean
- #verify_delivery_timestamp ⇒ Object
- #verify_signature ⇒ Object
Constructor Details
#initialize(headers, payload, primary_signature_key: nil, secondary_signature_key: nil) ⇒ WebhookValidator
Returns a new instance of WebhookValidator.
16 17 18 19 20 21 22 23 |
# File 'lib/boxr/webhook_validator.rb', line 16 def initialize(headers, payload, primary_signature_key: nil, secondary_signature_key: nil) @payload = payload @timestamp = headers['BOX-DELIVERY-TIMESTAMP'].to_s @primary_signature_key = primary_signature_key.to_s @secondary_signature_key = secondary_signature_key.to_s @primary_signature = headers['BOX-SIGNATURE-PRIMARY'] @secondary_signature = headers['BOX-SIGNATURE-SECONDARY'] end |
Instance Attribute Details
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def payload @payload end |
#primary_signature ⇒ Object (readonly)
Returns the value of attribute primary_signature.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def primary_signature @primary_signature end |
#primary_signature_key ⇒ Object (readonly)
Returns the value of attribute primary_signature_key.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def primary_signature_key @primary_signature_key end |
#secondary_signature ⇒ Object (readonly)
Returns the value of attribute secondary_signature.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def secondary_signature @secondary_signature end |
#secondary_signature_key ⇒ Object (readonly)
Returns the value of attribute secondary_signature_key.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def secondary_signature_key @secondary_signature_key end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
5 6 7 |
# File 'lib/boxr/webhook_validator.rb', line 5 def @timestamp end |
Instance Method Details
#generate_signature(key) ⇒ Object
37 38 39 40 41 |
# File 'lib/boxr/webhook_validator.rb', line 37 def generate_signature(key) = (payload.bytes + .bytes).pack('U') digest = OpenSSL::HMAC.hexdigest('SHA256', key, ) Base64.encode64(digest) end |
#valid_message? ⇒ Boolean
25 26 27 |
# File 'lib/boxr/webhook_validator.rb', line 25 def && verify_signature end |
#verify_delivery_timestamp ⇒ Object
29 30 31 |
# File 'lib/boxr/webhook_validator.rb', line 29 def < MAXIMUM_MESSAGE_AGE end |
#verify_signature ⇒ Object
33 34 35 |
# File 'lib/boxr/webhook_validator.rb', line 33 def verify_signature generate_signature(primary_signature_key) == primary_signature || generate_signature(secondary_signature_key) == secondary_signature end |