Class: Bisques::AwsRequestAuthorization

Inherits:
Object
  • Object
show all
Defined in:
lib/bisques/aws_request_authorization.rb

Overview

Instances of this class are used to create HTTP authorization headers for AWS using Signature Version 4, as per docs.amazonwebservices.com/general/latest/gr/signature-version-4.html

Usage

This is just an example of extracting the relevant information.

url = "https://sqs.us-east-1.amazon.com/"
original_headers = {"Content-Type": "text/plain"}
query = {"Action" => "ListQueues"}
body = "some body text"
credentials = AwsCredentials.default

authorization = AwsRequestAuthorization.new
authorization.url = url
authorization.method = "POST"
authorization.query = query
authorization.body = body
authorization.region = "us-east-1"
authorization.service = "sqs"
authorization.headers = original_headers

new_headers = authorization.headers.merge(
  "Authorization" => authorization.authorization_header
)

url_with_query = url + "?" + query.map{|p| p.join("=")}.join("&")

Net::HTTP.post(
  url_with_query,
  body,
  new_headers
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

A string of the body being sent (for POST and PUT HTTP methods).



47
48
49
# File 'lib/bisques/aws_request_authorization.rb', line 47

def body
  @body
end

#credentialsObject

An AwsCredentials object.



53
54
55
# File 'lib/bisques/aws_request_authorization.rb', line 53

def credentials
  @credentials
end

#headersObject

The headers to read back.



55
56
57
# File 'lib/bisques/aws_request_authorization.rb', line 55

def headers
  @headers
end

#methodObject

The HTTP method.



43
44
45
# File 'lib/bisques/aws_request_authorization.rb', line 43

def method
  @method
end

#queryObject

A hash of key/pairs for the query string.



45
46
47
# File 'lib/bisques/aws_request_authorization.rb', line 45

def query
  @query
end

#regionObject

The AWS region.



49
50
51
# File 'lib/bisques/aws_request_authorization.rb', line 49

def region
  @region
end

#serviceObject

The AWS service.



51
52
53
# File 'lib/bisques/aws_request_authorization.rb', line 51

def service
  @service
end

#urlObject

The full URL to the API call.



41
42
43
# File 'lib/bisques/aws_request_authorization.rb', line 41

def url
  @url
end

Instance Method Details

#authorization_headerObject

The generated authorization header.



58
59
60
61
62
63
64
65
# File 'lib/bisques/aws_request_authorization.rb', line 58

def authorization_header
  [
    "AWS4-HMAC-SHA256",
    "Credential=#{credentials.aws_key}/#{request_datestamp}/#{region}/#{service}/aws4_request,",
    "SignedHeaders=#{signed_headers.join(";")},",
    "Signature=#{signature.to_s}"
  ].join(" ")
end