class HMACAuth::Signature

Attributes

keep_values_type[R]
reject_keys[R]
secret[R]
valid_for[R]

Public Class Methods

new(params, options = {}) click to toggle source
# File lib/hmac_auth/signature.rb, line 15
def initialize(params, options = {})
  @secret = options.delete(:secret) || HMACAuth.secret
  @valid_for = options.delete(:valid_for) || HMACAuth.valid_for
  @reject_keys = options.delete(:reject_keys) || HMACAuth.reject_keys
  @keep_values_type = options.delete(:keep_values_type) ||
    HMACAuth.keep_values_type
  @_params = params

  raise Error.new 'You *must* tell me a secret!' unless @secret
end
sign(params, options = {}) click to toggle source
# File lib/hmac_auth/signature.rb, line 10
def sign(params, options = {})
  self.new(params, options).sign
end
verify(params, options = {}) click to toggle source
# File lib/hmac_auth/signature.rb, line 6
def verify(params, options = {})
  self.new(params, options).verify
end

Public Instance Methods

sign() click to toggle source

@return [Hash] Signed parameters

# File lib/hmac_auth/signature.rb, line 31
def sign
  timestamp || params['timestamp'] = Time.now.to_i.to_s
  params.merge('signature' => calculated_signature)
end
verify() click to toggle source
# File lib/hmac_auth/signature.rb, line 26
def verify
  valid_timestamp && signature == calculated_signature
end

Private Instance Methods

calculated_signature() click to toggle source
# File lib/hmac_auth/signature.rb, line 38
def calculated_signature
  OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest.new('sha256'),
    secret,
    JSON.generate(deep_sort(params_without_signature)))
end
deep_sort(hash) click to toggle source
# File lib/hmac_auth/signature.rb, line 45
def deep_sort(hash)
  Hash[hash.sort.map { |k, v| [k, v.is_a?(Hash) ? deep_sort(v) : v] }]
end
deep_stringify(hash) click to toggle source
# File lib/hmac_auth/signature.rb, line 49
def deep_stringify(hash)
  Hash[hash.map do |k, v|
    [k.to_s, v.is_a?(Hash) ? deep_stringify(v) : v.to_s]
  end]
end
deep_stringify_skip_values(hash) click to toggle source
# File lib/hmac_auth/signature.rb, line 55
def deep_stringify_skip_values(hash)
  Hash[hash.map do |k, v|
    [k.to_s, v.is_a?(Hash) ? deep_stringify_skip_values(v) : v]
  end]
end
params() click to toggle source
# File lib/hmac_auth/signature.rb, line 79
def params
  reject_keys!
  @params ||= if keep_values_type
     deep_stringify_skip_values(reject_keys!)
  else
    deep_stringify(reject_keys!)
  end
end
params_without_signature() click to toggle source
# File lib/hmac_auth/signature.rb, line 75
def params_without_signature
  params.reject { |k, v| k == 'signature' }
end
reject_keys!() click to toggle source
# File lib/hmac_auth/signature.rb, line 88
def reject_keys!
  @_params.reject do |k, v|
    reject_keys.include? k
  end
end
signature() click to toggle source
# File lib/hmac_auth/signature.rb, line 71
def signature
  params['signature']
end
timestamp() click to toggle source
# File lib/hmac_auth/signature.rb, line 65
def timestamp
  params['timestamp'].present? &&
    params['timestamp'].to_s =~ /\A\d+\Z/ &&
    params['timestamp'].to_i
end
valid_timestamp() click to toggle source
# File lib/hmac_auth/signature.rb, line 61
def valid_timestamp
  timestamp && timestamp >= valid_for.ago.to_i
end