Class: Mailgun::Tracking::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/mailgun/tracking/auth.rb

Overview

Used to ensure the authenticity of event requests.

Constant Summary collapse

SIGNATURE =
'signature'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Mailgun::Tracking::Auth

Initialize a Mailgun::Tracking::Auth object.

Parameters:

  • payload (Hash)

    webhook payload.



23
24
25
# File 'lib/mailgun/tracking/auth.rb', line 23

def initialize(payload)
  @payload = payload
end

Class Method Details

.call(payload, _env = nil) ⇒ Boolean

Parameters:

  • payload (Hash)

    webhook payload.

Returns:

  • (Boolean)


14
15
16
# File 'lib/mailgun/tracking/auth.rb', line 14

def self.call(payload, _env = nil)
  new(payload).valid?
end

Instance Method Details

#valid?Boolean

Compare the resulting hexdigest to the signature.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/mailgun/tracking/auth.rb', line 30

def valid?
  @payload.dig(SIGNATURE, SIGNATURE) == \
    ::OpenSSL::HMAC.hexdigest(
      ::OpenSSL::Digest::SHA256.new,
      ::Mailgun::Tracking.api_key,
      data
    )
end