Module: Gigya::Utils

Defined in:
lib/gigya/utils.rb

Class Method Summary collapse

Class Method Details

.build_signature_params(client_id, client_secret, base_url, query) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gigya/utils.rb', line 3

def self.build_signature_params(client_id, client_secret, base_url, query)
  timestamp      = Time.now.gmtime.to_i
  nonce          = CGI.escape("#{rand(0..10)}#{timestamp}")

  query          = CGI.escape(query).gsub('+', '%20')
  escaped_url    = CGI.escape(base_url)

  decoded_secret = Base64.decode64(client_secret)

  params = { apiKey: CGI.escape(client_id), nonce: nonce, query: query, timestamp: timestamp }

  query_string = CGI.escape(build_query_string(params))

  base_string = "POST&#{escaped_url}&#{query_string}"

  hmac_sha1 = HMAC::SHA1.digest(decoded_secret, base_string)
  signature = Base64.encode64(hmac_sha1).chomp.gsub(/\n/,'')

  params.merge!(sig: signature)
end