Class: Distelli::RequestSigner

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/distelli/clientframework.rb

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initializeRequestSigner

Returns a new instance of RequestSigner.



37
38
# File 'lib/distelli/clientframework.rb', line 37

def initialize()
end

Instance Method Details

#get_signature(secret_key, request_info) ⇒ Object



100
101
102
103
104
# File 'lib/distelli/clientframework.rb', line 100

def get_signature(secret_key, request_info)
  string_to_sign = get_string_to_sign(request_info)
  logger.debug("StringToSign:\n"+string_to_sign)
  return hmacsha(secret_key, string_to_sign)
end

#get_string_to_sign(request_info) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/distelli/clientframework.rb', line 40

def get_string_to_sign(request_info)
  string_to_sign = Array.new
  string_to_sign.push(request_info.http_method)
  if request_info.contains_header(ServiceConstants::CONTENT_MD5_HEADER)
    string_to_sign.push(request_info.headers[ServiceConstants::CONTENT_MD5_HEADER])
  else
    string_to_sign.push("")
  end

  if request_info.contains_header(ServiceConstants::CONTENT_TYPE_HEADER)
    string_to_sign.push(request_info.headers[ServiceConstants::CONTENT_TYPE_HEADER])
  else
    string_to_sign.push("")
  end

  if request_info.contains_header(ServiceConstants::DATE_HEADER)
    string_to_sign.push(request_info.headers[ServiceConstants::DATE_HEADER])
  else
    string_to_sign.push("")
  end

  sorted_headers = Array.new
  request_info.headers.each do |header|
    if header[0].to_s().downcase().start_with?("x-dstli")
      sorted_headers.push(header[0])
    end
  end

  sorted_headers.sort!()
  sorted_headers.each do |header|
    string_to_sign.push(header.downcase()+":"+request_info.headers[header.to_s])
  end

  # Canonicalize the resource
  if request_info.resource_uri == nil
    string_to_sign.push("")
  else
    string_to_sign.push(request_info.resource_uri)
  end

  sorted_params = Array.new
  if request_info.query_params != nil
    request_info.query_params.each_pair do |param, values|
      if values == nil or values.length() == 0
        next
      end
      values.sort!()
      query_param_list = Array.new
      values.each do |value|
        query_param_list.push(value.to_s)
      end
      sorted_params.push(param+"="+query_param_list.join(','))
    end
  end

  sorted_params.sort!()
  string_to_sign.push(sorted_params.join('&'))
  return string_to_sign.join("\n")
end

#hmacsha(secret_key, string_to_sign) ⇒ Object



106
107
108
109
110
# File 'lib/distelli/clientframework.rb', line 106

def hmacsha(secret_key, string_to_sign)
  digest = OpenSSL::Digest::Digest.new("sha256")
  hmac = OpenSSL::HMAC.digest(digest, secret_key, string_to_sign)
  return Base64.encode64(hmac)
end