Class: Vufer::Signature

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

Constant Summary collapse

HEX_DIGEST =
'd41d8cd98f00b204e9800998ecf8427e'.freeze

Class Method Summary collapse

Class Method Details

.generate(path, body, verb, time) ⇒ String

Generates the signature based on path, body, http verb and time.

Parameters:

  • Path (String)

    the actual path you’re sending the request, eg: ‘/targets’.

  • Body (Hash)

    the content when request is in POST or PUT formats.

  • Verb (String)

    the HTTP verb used to send the request, eg: POST, PUT, GET, etc…

  • Time (Time)

    time in GMT the request is made.

Returns:

  • (String)

    contains an encrypted token with all the above informartion.

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vufer/signature.rb', line 23

def generate(path, body, verb, time)
  raise KeyEnvironmentError if Vufer.access_key.empty? || Vufer.secret_key.empty?

  hex_digest = HEX_DIGEST
  content_type = ''

  if verb == 'POST' || verb == 'PUT'
    content_type = 'application/json'
    hex_digest = Digest::MD5.hexdigest(body.to_json)
  end

  to_digest = "#{verb}\n#{hex_digest}\n#{content_type}\n#{time}\n#{path}"

  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::SHA1.new,
      Vufer.secret_key,
      to_digest
    )
  )
end