Class: ActiveElasticJob::MessageVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/active_elastic_job/message_verifier.rb

Overview

:nodoc:

Defined Under Namespace

Classes: InvalidDigest

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ MessageVerifier

Returns a new instance of MessageVerifier.



12
13
14
# File 'lib/active_elastic_job/message_verifier.rb', line 12

def initialize(secret)
  @secret = secret
end

Instance Method Details

#generate_digest(message) ⇒ Object



29
30
31
32
# File 'lib/active_elastic_job/message_verifier.rb', line 29

def generate_digest(message)
  require 'openssl' unless defined?(OpenSSL)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, @secret, message)
end

#verify(message, digest) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/active_elastic_job/message_verifier.rb', line 16

def verify(message, digest)
  if message.nil? || message.blank? || digest.nil? || digest.blank?
    return false
  end

  return ActiveSupport::SecurityUtils.secure_compare(
    digest, generate_digest(message))
end

#verify!(message, digest) ⇒ Object

Raises:



25
26
27
# File 'lib/active_elastic_job/message_verifier.rb', line 25

def verify!(message, digest)
  raise InvalidDigest unless verify(message, digest)
end