Class: Canistor::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/canistor/authorization.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization) ⇒ Authorization

Returns a new instance of Authorization.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/canistor/authorization.rb', line 11

def initialize(authorization)
  @protocol, params = authorization.split(' ', 2)
  params.split(', ').inject({}) do |unpacked, part|
    name, values = part.split('=')
    case name
    when 'Credential'
      self.credential = values.split('/')
    when 'Signature'
      self.signature = values
    end
    unpacked
  end
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



7
8
9
# File 'lib/canistor/authorization.rb', line 7

def access_key_id
  @access_key_id
end

#dateObject (readonly)

Returns the value of attribute date.



9
10
11
# File 'lib/canistor/authorization.rb', line 9

def date
  @date
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/canistor/authorization.rb', line 5

def protocol
  @protocol
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/canistor/authorization.rb', line 6

def region
  @region
end

#signatureObject

Returns the value of attribute signature.



8
9
10
# File 'lib/canistor/authorization.rb', line 8

def signature
  @signature
end

Instance Method Details

#valid_signature?(request, credentials) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/canistor/authorization.rb', line 25

def valid_signature?(request, credentials)
  return false if signature.to_s.strip == ''
  signer = Aws::Sigv4::Signer.new(
    service: 's3',
    region: region,
    credentials_provider: credentials,
    uri_escape_path: false,
    unsigned_headers: ['content-length', 'x-amzn-trace-id']
  )
  signed_request = signer.sign_request(
    http_method: request.http_method,
    url: request.endpoint.to_s,
    headers: request.headers.to_hash,
    body: request.body
  )
  signature == signer.send(
    :signature,
    credentials.secret_access_key,
    date,
    signed_request.string_to_sign
  )
end