Class: SendGrid::EventWebhook

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/helpers/eventwebhook/eventwebhook.rb

Overview

This class allows you to use the Event Webhook feature. Read the docs for more details: sendgrid.com/docs/for-developers/tracking-events/event

Defined Under Namespace

Classes: Error, NotSupportedError

Instance Method Summary collapse

Instance Method Details

#convert_public_key_to_ecdsa(public_key) ⇒ Object

  • Args :

    • public_key -> verification key under Mail Settings



12
13
14
15
# File 'lib/sendgrid/helpers/eventwebhook/eventwebhook.rb', line 12

def convert_public_key_to_ecdsa(public_key)
  verify_engine
  OpenSSL::PKey::EC.new(Base64.decode64(public_key))
end

#verify_engineObject

Raises:



32
33
34
35
# File 'lib/sendgrid/helpers/eventwebhook/eventwebhook.rb', line 32

def verify_engine
  # JRuby does not fully support ECDSA: https://github.com/jruby/jruby-openssl/issues/193
  raise NotSupportedError, "Event Webhook verification is not supported by JRuby" if RUBY_PLATFORM == "java"
end

#verify_signature(public_key, payload, signature, timestamp) ⇒ Object

  • Args :

    • public_key -> elliptic curve public key

    • payload -> event payload in the request body

    • signature -> signature value obtained from the ‘X-Twilio-Email-Event-Webhook-Signature’ header

    • timestamp -> timestamp value obtained from the ‘X-Twilio-Email-Event-Webhook-Timestamp’ header



22
23
24
25
26
27
28
29
30
# File 'lib/sendgrid/helpers/eventwebhook/eventwebhook.rb', line 22

def verify_signature(public_key, payload, signature, timestamp)
  verify_engine
  timestamped_playload = "#{timestamp}#{payload}"
  payload_digest = Digest::SHA256.digest(timestamped_playload)
  decoded_signature = Base64.decode64(signature)
  public_key.dsa_verify_asn1(payload_digest, decoded_signature)
rescue StandardError
  false
end