Class: Shmac::SignatureCalculator
- Inherits:
-
Object
- Object
- Shmac::SignatureCalculator
- Defined in:
- lib/shmac/signature_calculator.rb
Instance Attribute Summary collapse
-
#header_namespace ⇒ Object
readonly
Returns the value of attribute header_namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #canonicalized_platform_headers ⇒ Object
-
#initialize(secret:, request:, header_namespace: nil, options: {}) ⇒ SignatureCalculator
constructor
A new instance of SignatureCalculator.
- #signature ⇒ Object
- #string_to_sign ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(secret:, request:, header_namespace: nil, options: {}) ⇒ SignatureCalculator
Returns a new instance of SignatureCalculator.
8 9 10 11 12 13 |
# File 'lib/shmac/signature_calculator.rb', line 8 def initialize secret:, request:, header_namespace: nil, options: {} @secret = secret @request = request @header_namespace = header_namespace.downcase if header_namespace @options = end |
Instance Attribute Details
#header_namespace ⇒ Object (readonly)
Returns the value of attribute header_namespace.
6 7 8 |
# File 'lib/shmac/signature_calculator.rb', line 6 def header_namespace @header_namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/shmac/signature_calculator.rb', line 6 def @options end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
6 7 8 |
# File 'lib/shmac/signature_calculator.rb', line 6 def request @request end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
6 7 8 |
# File 'lib/shmac/signature_calculator.rb', line 6 def secret @secret end |
Instance Method Details
#canonicalized_platform_headers ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/shmac/signature_calculator.rb', line 42 def canonicalized_platform_headers return unless self.header_namespace normalize_key = -> (key) { key.to_s.downcase.strip } normalize_value = -> (value) { value.to_s.strip } canonicalized_header_row = -> (k,v) { "%s:%s" % [normalize_key.(k), normalize_value.(v)] } matches_namespace = ->(value) { value.start_with?(header_namespace) } self.request.headers .map(&canonicalized_header_row) .find_all(&matches_namespace) .sort .join("\n") end |
#signature ⇒ Object
19 20 21 22 23 |
# File 'lib/shmac/signature_calculator.rb', line 19 def signature digest = OpenSSL::Digest.new "sha1" hmac = OpenSSL::HMAC.digest digest, secret, string_to_sign Base64.strict_encode64 hmac end |
#string_to_sign ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/shmac/signature_calculator.rb', line 25 def string_to_sign parts = [ request.method, request.content_md5, request.content_type, request.date(self.header_namespace) ] platform_headers = canonicalized_platform_headers.to_s.strip parts << platform_headers unless platform_headers.empty? # Some clients do not know to which endpoint a message is sent parts << request.path unless [:skip_path] parts.join("\n") end |
#to_s ⇒ Object
15 16 17 |
# File 'lib/shmac/signature_calculator.rb', line 15 def to_s signature end |