Module: BuckarooJson::AuthorizationHeader

Defined in:
lib/buckaroo_json/authorization_header.rb

Class Method Summary collapse

Class Method Details

.content_hash(content) ⇒ Object



42
43
44
45
# File 'lib/buckaroo_json/authorization_header.rb', line 42

def content_hash(content)
  return '' if content.nil? || content == ''
  Digest::MD5.base64digest(content)
end

.create(website_key:, api_key:, method:, url:, content: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/buckaroo_json/authorization_header.rb', line 9

def create(website_key:, api_key:, method:, url:, content: nil)
  timestamp = current_timestamp
  nonce = current_nonce
  base64_hash = hmac_sha256_hash(
    secret: api_key,
    message: hmac_sha256_message(
      website_key, method, url, timestamp, nonce, content
    )
  )
  "hmac #{website_key}:#{base64_hash}:#{nonce}:#{timestamp}"
end

.current_nonceObject



51
52
53
# File 'lib/buckaroo_json/authorization_header.rb', line 51

def current_nonce
  SecureRandom.hex
end

.current_timestampObject



47
48
49
# File 'lib/buckaroo_json/authorization_header.rb', line 47

def current_timestamp
  Time.now.getutc.to_i
end

.hmac_sha256_hash(secret:, message:) ⇒ Object



21
22
23
24
# File 'lib/buckaroo_json/authorization_header.rb', line 21

def hmac_sha256_hash(secret:, message:)
  hash = OpenSSL::HMAC.digest('sha256', secret, message)
  Base64.encode64(hash).strip
end

.hmac_sha256_message(website_key, method, url, timestamp, nonce, content) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/buckaroo_json/authorization_header.rb', line 26

def hmac_sha256_message(website_key, method, url, timestamp, nonce, content)
  website_key +
    method +
    normalized_url(url) +
    timestamp.to_s +
    nonce +
    content_hash(content)
end

.normalized_url(raw_url) ⇒ Object



35
36
37
38
39
40
# File 'lib/buckaroo_json/authorization_header.rb', line 35

def normalized_url(raw_url)
  url = URI(raw_url)
  url_parts = url.host + url.path
  url_parts += '?' + url.query if url.query
  CGI.escape(url_parts).downcase
end