Class: Cryptograpi::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptograpi_ruby/signature.rb

Overview

HTTP authentication for our platform

Class Method Summary collapse

Class Method Details

.get_host(host) ⇒ Object



58
59
60
61
62
63
# File 'lib/cryptograpi_ruby/signature.rb', line 58

def self.get_host(host)
  uri = URI(host)
  ret = uri.hostname.to_s
  ret += ":#{uri.port}" if /:[0-9]/.match?(host)
  ret
end

.headers(endpoint, host, http_method, papi, query, sapi) ⇒ Object



8
9
10
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
44
45
46
47
48
49
50
51
52
# File 'lib/cryptograpi_ruby/signature.rb', line 8

def self.headers(endpoint, host, http_method, papi, query, sapi)

  # Request Target (http_method path?query)
  req_target = "#{http_method} #{endpoint}"

  # Unix time for signature creation
  created_at = Time.now.to_i

  # We hash the body of the HTTP message. Even if it's empty
  ha_sha512 = OpenSSL::Digest.new('SHA512')
  ha_sha512 << JSON.dump(query)
  digest = "SHA-512=#{Base64.strict_encode64(ha_sha512.digest)}"

  # Initialize headers
  header_signature = {}
  header_signature['user-agent'] = "cryptograpi_ruby/#{Cryptograpi::VERSION}"
  header_signature['content-type'] = 'application/json'
  header_signature['(request-target'] = req_target
  header_signature['date'] = sign_date
  header_signature['host'] = get_host(host)
  header_signature['(created)'] = created_at
  header_signature['digest'] = digest
  headers = %w[content-type date host (created) (request-target) digest]

  # Calculate HMAC including the headers
  hmac = OpenSSL::HMAC.new(sapi, OpenSSL::Digest.new('SHA512'))
  headers.each do |header|
    hmac << "#{header}: #{header_signature[header]}\n" if header_signature.key?(header)
  end

  header_signature.delete('(created)')
  header_signature.delete('(request-target)')
  header_signature.delete('(host)')

  # Build the final signature
  header_signature['signature'] = "keyId=\"#{papi}\""
  header_signature['signature'] += ', algorithm="hmac-sha512"'
  header_signature['signature'] += ", created=#{created_at}"
  header_signature['signature'] += ", headers=#{headers.join(' ')}\""
  header_signature['signature'] += ', signature='
  header_signature['signature'] += Base64.strict_encode64(hmac.digest)
  header_signature['signature'] += '"'

  header_signature
end

.sign_dateObject



54
55
56
# File 'lib/cryptograpi_ruby/signature.rb', line 54

def self.sign_date
  "#{DateTime.now.in_time_zone('GMT').strftime('%a, %d %b %Y')} #{DateTime.now.in_time_zone('GMT').strftime('%H:%M:%S')} GMT"
end