Class: Shmac::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/shmac/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, method:, headers:, body: nil, content_type: nil) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
14
# File 'lib/shmac/request.rb', line 8

def initialize path:, method:, headers:, body: nil, content_type: nil
  @path = path
  @method = method
  self.headers = headers
  @body = body
  @content_type = content_type
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/shmac/request.rb', line 6

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



6
7
8
# File 'lib/shmac/request.rb', line 6

def content_type
  @content_type
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/shmac/request.rb', line 6

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/shmac/request.rb', line 6

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/shmac/request.rb', line 6

def path
  @path
end

Instance Method Details

#authorizationObject



39
40
41
42
# File 'lib/shmac/request.rb', line 39

def authorization
  # Test for x-authorization for clients that have issues with standard headers
  headers.fetch("x-authorization") { headers["authorization"] }
end

#content_md5Object



23
24
25
26
# File 'lib/shmac/request.rb', line 23

def content_md5
  # Fallback to x-content-md5 for clients that have issues with standard headers
  headers.fetch("content-md5") { headers["x-content-md5"] }
end

#content_md5_matches_body?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/shmac/request.rb', line 35

def content_md5_matches_body?
  Security.secure_compare content_md5, Digest::MD5.base64digest(body)
end

#date(namespace = nil) ⇒ Object

Prefer the value of a namespaced date key if present



17
18
19
20
21
# File 'lib/shmac/request.rb', line 17

def date namespace = nil
  date_key = [namespace, "date"].compact.join("-").downcase

  headers.fetch(date_key) { headers["date"] }
end

#tampered_body?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/shmac/request.rb', line 28

def tampered_body?
  return false unless body
  return false if content_md5.to_s.strip.empty?

  !content_md5_matches_body?
end