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.


6
7
8
9
10
# File 'lib/api_auth/headers.rb', line 6

def initialize(request)
  @original_request = request
  @request = initialize_request_driver(request)
  true
end

Instance Method Details

#authorization_headerObject

Returns the authorization header from the request's headers


76
77
78
# File 'lib/api_auth/headers.rb', line 76

def authorization_header
  @request.authorization_header
end

#calculate_md5Object


84
85
86
# File 'lib/api_auth/headers.rb', line 84

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

#canonical_string(override_method = nil, headers_to_sign = []) ⇒ Object

Raises:

  • (ArgumentError)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/api_auth/headers.rb', line 55

def canonical_string(override_method = nil, headers_to_sign = [])
  request_method = override_method || @request.http_method

  raise ArgumentError, 'unable to determine the http method from the request, please supply an override' if request_method.nil?

  headers = @request.fetch_headers

  canonical_array = [request_method.upcase,
                     @request.content_type,
                     @request.content_md5,
                     parse_uri(@request.original_uri || @request.request_uri),
                     @request.timestamp]

  if headers_to_sign.is_a?(Array) && headers_to_sign.any?
    headers_to_sign.each { |h| canonical_array << headers[h] if headers[h].present? }
  end

  canonical_array.join(',')
end

#md5_mismatch?Boolean

Returns:

  • (Boolean)

88
89
90
91
92
93
94
# File 'lib/api_auth/headers.rb', line 88

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

#set_dateObject


80
81
82
# File 'lib/api_auth/headers.rb', line 80

def set_date
  @request.set_date if @request.timestamp.nil?
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.


101
102
103
# File 'lib/api_auth/headers.rb', line 101

def sign_header(header)
  @request.set_auth_header header
end

#timestampObject

Returns the request timestamp


51
52
53
# File 'lib/api_auth/headers.rb', line 51

def timestamp
  @request.timestamp
end