Module: Shared::HmacSignature

Included in:
PushyDaemon::Consumer
Defined in:
lib/shared/hmac_signature.rb

Instance Method Summary collapse

Instance Method Details

#headers_md5(headers, payload) ⇒ Object



7
8
9
# File 'lib/shared/hmac_signature.rb', line 7

def headers_md5 headers, payload
  headers['Content-MD5'] = Digest::MD5.hexdigest(payload.to_s)
end

#headers_sign(headers, config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shared/hmac_signature.rb', line 11

def headers_sign headers, config
  # Extract and check
  return unless config.is_a? Hash
  hmac_method   = config[:method]
  hmac_user     = config[:user]
  hmac_secret   = config[:secret]
  log_debug "headers_sign config", config

  # Check params
  unless hmac_method && hmac_user && hmac_secret
    log_error "headers_sign: missing method/user/secret"
    return
  end

  # Check headers, and translate names to strings
  unless config[:headers].is_a? Array
    log_error "headers_sign: [headers] should be an array of headers to be signed"
    return
  end
  hmac_headers = config[:headers].map(&:to_s)

  # Update date
  headers['Date'] = Time.now.utc.strftime('%a, %d %b %Y %H:%M:%S GMT')

  # Let's apply the requested method
  case hmac_method
  when 'hmac-kong'
    hmac_sign_kong headers, hmac_user, hmac_secret, hmac_headers
  else
    log_error "headers_sign: only [hmac-kong] method is supported"
    return
  end
end