Class: Amazon::FPS::SignatureUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon/fps/signatureutils.rb

Overview

Copyright

Copyright © 2009 Amazon.com, Inc. or its affiliates. All Rights Reserved.

RFC 2104-compliant HMAC signature for request parameters

Implements AWS Signature, as per following spec:

If Signature Version is 1, it performs the following:

Sorts all parameters (including SignatureVersion and excluding Signature, the value of which is being created), ignoring case.

Iterate over the sorted list and append the parameter name (in original case) and then its value. It will not URL-encode the parameter values before constructing this string. There are no separators.

If Signature Version is 2, string to sign is based on following:

1. The HTTP Request Method followed by an ASCII newline (%0A)
2. The HTTP Host header in the form of lowercase host, followed by an ASCII newline.
3. The URL encoded HTTP absolute path component of the URI
   (up to but not including the query string parameters);
   if this is empty use a forward '/'. This parameter is followed by an ASCII newline.
4. The concatenation of all query string components (names and values)
   as UTF-8 characters which are URL encoded as per RFC 3986
   (hex characters MUST be uppercase), sorted using lexicographic byte ordering.
   Parameter names are separated from their values by the '=' character
   (ASCII character 61), even if the value is empty.
   Pairs of parameter and values are separated by the '&' character (ASCII code 38).

Constant Summary collapse

SIGNATURE_KEYNAME =
"Signature"
SIGNATURE_METHOD_KEYNAME =
"SignatureMethod"
SIGNATURE_VERSION_KEYNAME =
"SignatureVersion"
HMAC_SHA256_ALGORITHM =
"HmacSHA256"
HMAC_SHA1_ALGORITHM =
"HmacSHA1"

Class Method Summary collapse

Class Method Details

.sign_parameters(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/amazon/fps/signatureutils.rb', line 56

def self.sign_parameters(args)
  signature_version = args[:parameters][SIGNATURE_VERSION_KEYNAME]
  string_to_sign = "";
  algorithm = 'sha1';
  if (signature_version == '1') then
    string_to_sign = calculate_string_to_sign_v1(args)
  elsif (signature_version == '2') then
    algorithm = get_algorithm(args[:parameters][SIGNATURE_METHOD_KEYNAME])
    string_to_sign = calculate_string_to_sign_v2(args)
  else
    raise "Invalid Signature Version specified"
  end
  return compute_signature(string_to_sign, args[:aws_secret_key], algorithm)
end

.urlencode(plaintext) ⇒ Object

Convert a string into URL encoded form.



72
73
74
# File 'lib/amazon/fps/signatureutils.rb', line 72

def self.urlencode(plaintext)
  CGI.escape(plaintext.to_s).gsub("+", "%20").gsub("%7E", "~")
end