Class: Boxr::WebhookValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/boxr/webhook_validator.rb

Constant Summary collapse

MAXIMUM_MESSAGE_AGE =

10 minutes (in seconds)

600

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/boxr/webhook_validator.rb', line 5

def payload
  @payload
end

#primary_signatureObject (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_keyObject (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_signatureObject (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_keyObject (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

#timestampObject (readonly)

Returns the value of attribute timestamp.



5
6
7
# File 'lib/boxr/webhook_validator.rb', line 5

def timestamp
  @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)
  message_as_bytes = (payload.bytes + timestamp.bytes).pack('U')
  digest = OpenSSL::HMAC.hexdigest('SHA256', key, message_as_bytes)
  Base64.encode64(digest)
end

#valid_message?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/boxr/webhook_validator.rb', line 25

def valid_message?
  verify_delivery_timestamp && verify_signature
end

#verify_delivery_timestampObject



29
30
31
# File 'lib/boxr/webhook_validator.rb', line 29

def verify_delivery_timestamp
  message_age < MAXIMUM_MESSAGE_AGE
end

#verify_signatureObject



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