class Mailgun::Tracking::Auth

Used to ensure the authenticity of event requests.

Constants

SIGNATURE

Public Class Methods

call(payload, _env = nil) click to toggle source

@param payload [Hash] webhook payload.

@return [Boolean]

# File lib/mailgun/tracking/auth.rb, line 14
def self.call(payload, _env = nil)
  new(payload).valid?
end
new(payload) click to toggle source

Initialize a Mailgun::Tracking::Auth object.

@param payload [Hash] webhook payload.

@return [Mailgun::Tracking::Auth]

# File lib/mailgun/tracking/auth.rb, line 23
def initialize(payload)
  @payload = payload
end

Public Instance Methods

valid?() click to toggle source

Compare the resulting hexdigest to the signature.

@return [Boolean]

# 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

Private Instance Methods

data() click to toggle source

Concatenate timestamp and token values.

@return [String]

# File lib/mailgun/tracking/auth.rb, line 44
def data
  [
    @payload.dig(SIGNATURE, 'timestamp'),
    @payload.dig(SIGNATURE, 'token')
  ].join
end