Class: ApiAuth::Headers

Inherits:
Object
  • Object
show all
Includes:
RequestDrivers
Defined in:
lib/api_auth/headers.rb

Overview

Builds the canonical string given a request object.

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Headers

Returns a new instance of Headers.


8
9
10
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
# File 'lib/api_auth/headers.rb', line 8

def initialize(request)
  @original_request = request

  case request.class.to_s
  when /Net::HTTP/
    @request = NetHttpRequest.new(request)
  when /RestClient/
    @request = RestClientRequest.new(request)
  when /Curl::Easy/
    @request = CurbRequest.new(request)
  when /ActionController::Request/
    @request = ActionControllerRequest.new(request)
  when /ActionController::TestRequest/
    if defined?(ActionDispatch)
      @request = ActionDispatchRequest.new(request)
    else
      @request = ActionControllerRequest.new(request)
    end
  when /ActionDispatch::Request/
    @request = ActionDispatchRequest.new(request)
  when /Rack::Request/
    @request = RackRequest.new(request)
  when /ActionController::CgiRequest/
    @request = ActionControllerRequest.new(request)
  else
    raise UnknownHTTPRequest, "#{request.class.to_s} is not yet supported."
  end
  true
end

Instance Method Details

#authorization_headerObject

Returns the authorization header from the request's headers


53
54
55
# File 'lib/api_auth/headers.rb', line 53

def authorization_header
  @request.authorization_header
end

#calculate_md5Object


61
62
63
# File 'lib/api_auth/headers.rb', line 61

def calculate_md5
  @request.populate_content_md5 if @request.content_md5.empty?
end

#canonical_stringObject

Returns the canonical string computed from the request's headers


44
45
46
47
48
49
50
# File 'lib/api_auth/headers.rb', line 44

def canonical_string
  [ @request.content_type,
    @request.content_md5,
    @request.request_uri.gsub(/https?:\/\/[^(,|\?|\/)]*/,''), # remove host
    @request.timestamp
  ].join(",")
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

65
66
67
68
69
70
71
# File 'lib/api_auth/headers.rb', line 65

def md5_mismatch?
  if @request.content_md5.empty?
    false
  else
    @request.md5_mismatch?
  end
end

#set_dateObject


57
58
59
# File 'lib/api_auth/headers.rb', line 57

def set_date
  @request.set_date if @request.timestamp.empty?
end

#sign_header(header) ⇒ Object

Sets the request's authorization header with the passed in value. The header should be the ApiAuth HMAC signature.

This will return the original request object with the signed Authorization header already in place.


78
79
80
# File 'lib/api_auth/headers.rb', line 78

def sign_header(header)
  @request.set_auth_header header
end

#timestampObject

Returns the request timestamp


39
40
41
# File 'lib/api_auth/headers.rb', line 39

def timestamp
   @request.timestamp
end