Class: FaradayMiddleware::CloudfrontSigner

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/request/cloudfront_signer.rb

Constant Summary collapse

ALGORITHM =
'sha1'
CODE =
'm'
AUTHORIZATION =
'Authorization'
DATE =
'Date'
AWS =
'AWS'
SPACE =
' '
COLON =
':'

Instance Method Summary collapse

Constructor Details

#initialize(app, *args) ⇒ CloudfrontSigner

Returns a new instance of CloudfrontSigner.



15
16
17
18
19
# File 'lib/faraday_middleware/request/cloudfront_signer.rb', line 15

def initialize(app, *args)
  @app = app
  @aws_access_key_id = args.shift
  @aws_secret_access_key = args.shift
end

Instance Method Details

#build_signature(date_string) ⇒ String

Method that builds the signature to the request.

Parameters:

  • date_string (String)

    the string representation of date time.

Returns:



24
25
26
27
# File 'lib/faraday_middleware/request/cloudfront_signer.rb', line 24

def build_signature(date_string)
  digest = OpenSSL::Digest::Digest.new(ALGORITHM)
  [OpenSSL::HMAC.digest(digest, @aws_secret_access_key, date_string)].pack(CODE).strip
end

#call(env) ⇒ Object



29
30
31
32
33
# File 'lib/faraday_middleware/request/cloudfront_signer.rb', line 29

def call(env)
  date = Time.now.httpdate
  env[:request_headers].merge!(AUTHORIZATION => "#{AWS}#{SPACE}#{@aws_access_key_id}#{COLON}#{build_signature(date)}", DATE => date)
  @app.call env
end