Class: Ey::Hmac::Adapter::Rack

Inherits:
Ey::Hmac::Adapter show all
Defined in:
lib/ey-hmac/adapter/rack.rb

Constant Summary

Constants inherited from Ey::Hmac::Adapter

AUTHORIZATION_REGEXP

Instance Attribute Summary

Attributes inherited from Ey::Hmac::Adapter

#authorization_header, #options, #request, #service

Instance Method Summary collapse

Methods inherited from Ey::Hmac::Adapter

#authenticated!, #authenticated?, #authorization, #canonicalize, #secure_compare, #signature

Constructor Details

#initialize(request, options) ⇒ Rack

Returns a new instance of Rack.



4
5
6
7
# File 'lib/ey-hmac/adapter/rack.rb', line 4

def initialize(request, options)
  super
  @request = ::Rack::Request.new(request) if request.is_a?(Hash)
end

Instance Method Details

#authorization_signatureObject



51
52
53
# File 'lib/ey-hmac/adapter/rack.rb', line 51

def authorization_signature
  request.env["HTTP_#{authorization_header.to_s.upcase}"]
end

#bodyObject



25
26
27
28
29
30
31
32
33
# File 'lib/ey-hmac/adapter/rack.rb', line 25

def body
  if request.env["rack.input"]
    request.env["rack.input"].rewind
    body = request.env["rack.input"].read
    request.env["rack.input"].rewind
    body == "" ? nil : body
  else nil
  end
end

#content_digestObject



17
18
19
20
21
22
23
# File 'lib/ey-hmac/adapter/rack.rb', line 17

def content_digest
  if existing = request.env['HTTP_CONTENT_DIGEST']
    existing
  elsif digest = body && Digest::MD5.hexdigest(body)
    request.env['HTTP_CONTENT_DIGEST'] = digest
  end
end

#content_typeObject



13
14
15
# File 'lib/ey-hmac/adapter/rack.rb', line 13

def content_type
  request.content_type
end

#dateObject



35
36
37
# File 'lib/ey-hmac/adapter/rack.rb', line 35

def date
  request.env['HTTP_DATE'] ||= Time.now.httpdate
end

#methodObject



9
10
11
# File 'lib/ey-hmac/adapter/rack.rb', line 9

def method
  request.request_method.to_s.upcase
end

#pathObject



39
40
41
# File 'lib/ey-hmac/adapter/rack.rb', line 39

def path
  request.path
end

#sign!(key_id, key_secret) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ey-hmac/adapter/rack.rb', line 43

def sign!(key_id, key_secret)
  if options[:version]
    request.env['HTTP_X_SIGNATURE_VERSION'] = options[:version]
  end

  request.env["HTTP_#{authorization_header.to_s.upcase}"] = authorization(key_id, key_secret)
end