Class: Shmac::SignatureCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/shmac/signature_calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = options
end

Instance Attribute Details

#header_namespaceObject (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

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/shmac/signature_calculator.rb', line 6

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/shmac/signature_calculator.rb', line 6

def request
  @request
end

#secretObject (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_headersObject



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

#signatureObject



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_signObject



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 options[:skip_path]

  parts.join("\n")
end

#to_sObject



15
16
17
# File 'lib/shmac/signature_calculator.rb', line 15

def to_s
  signature
end