Class: Distelli::RequestSigner
- Inherits:
-
Object
- Object
- Distelli::RequestSigner
- Defined in:
- lib/distelli/clientframework.rb
Instance Method Summary collapse
- #get_signature(secret_key, request_info) ⇒ Object
- #get_string_to_sign(request_info) ⇒ Object
- #hmacsha(secret_key, string_to_sign) ⇒ Object
-
#initialize ⇒ RequestSigner
constructor
A new instance of RequestSigner.
Constructor Details
#initialize ⇒ RequestSigner
Returns a new instance of RequestSigner.
27 28 29 |
# File 'lib/distelli/clientframework.rb', line 27 def initialize() @logger = Logging::Logger['RequestSigner'] end |
Instance Method Details
#get_signature(secret_key, request_info) ⇒ Object
91 92 93 94 95 |
# File 'lib/distelli/clientframework.rb', line 91 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
31 32 33 34 35 36 37 38 39 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 |
# File 'lib/distelli/clientframework.rb', line 31 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 == nil string_to_sign.push("") else string_to_sign.push(request_info.resource) 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
97 98 99 100 101 |
# File 'lib/distelli/clientframework.rb', line 97 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 |