Class: HTTPSignature::Faraday

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/http_signature/faraday.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/http_signature/faraday.rb', line 8

def key
  @key
end

.key_idObject

Returns the value of attribute key_id.



8
9
10
# File 'lib/http_signature/faraday.rb', line 8

def key_id
  @key_id
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/http_signature/faraday.rb', line 11

def call(env)
  raise 'key and key_id needs to be set' if self.class.key.nil? || self.class.key_id.nil?

  body =
    if env[:body] && env[:body].respond_to?(:read)
      string = env[:body].read
      env[:body].rewind
      string
    else
      env[:body].to_s
    end

  # Choose which headers to sign
  filtered_headers = %w[Host Date Digest]
  headers_to_sign = env[:request_headers].select { |k, _v| filtered_headers.include?(k.to_s) }

  signature = HTTPSignature.create(
    url: env[:url],
    method: env[:method],
    headers: headers_to_sign,
    key: self.class.key,
    key_id: self.class.key_id,
    algorithm: 'hmac-sha256',
    body: body
  )

  env[:request_headers]['Signature'] = signature

  @app.call(env)
end