Class: TrainPlugins::Rest::AWSV4
Constant Summary
collapse
- VALID_CREDENTIALS =
%w[
access_keys
].freeze
%w[
content-type host x-amz-date x-amz-target
].freeze
Instance Attribute Summary
Attributes inherited from AuthHandler
#connection, #options
Instance Method Summary
collapse
Methods inherited from AuthHandler
#auth_headers, #auth_parameters, descendants, #initialize, #login, #logout, name, #renew_session, #renewal_needed?
Instance Method Details
#access_key ⇒ Object
70
71
72
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 70
def access_key
options[:access_key] || ENV['AWS_ACCESS_KEY_ID']
end
|
#check_options ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 17
def check_options
options[:credentials] ||= "access_keys"
unless VALID_CREDENTIALS.include? credentials
raise ArgumentError.new("Invalid type of credentials: #{credentials}")
end
if access_keys?
raise ArgumentError.new('Missing `access_key` credential') unless access_key
raise ArgumentError.new('Missing `secret_access_key` credential') unless secret_access_key
end
end
|
#process(payload: "", headers: {}, url: "", method: nil) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 34
def process(payload: "", headers: {}, url: "", method: nil)
.merge! ({
'Accept-Encoding' => 'identity',
'User-Agent' => "train-rest/#{TrainPlugins::Rest::VERSION}",
'Content-Type' => 'application/x-amz-json-1.0'
})
= .select do |name, _value|
SIGNED_HEADERS.include? name.downcase
end
@url = url
signature = signer(url).sign_request(
http_method: method.to_s.upcase,
url: url,
headers: ,
body: payload.to_json
)
{
headers: .merge(signature.)
}
end
|
#process_error(error) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 59
def process_error(error)
raise AuthenticationError.new("Authentication failed: #{error.response.to_s.chop}") if error.response.code == 401
raise BadRequest.new("Bad request: #{error.response.to_s.chop}") if error.response.code == 400
message = JSON.parse(error.response.to_s)
raise AuthenticationError.new(message["message"] || message["__type"])
rescue JSON::ParserError => e
raise AuthenticationError.new(error.response.to_s)
end
|
#region(url = default_url) ⇒ Object
74
75
76
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 74
def region(url = default_url)
url.delete_prefix('https://').split('.').at(1)
end
|
#signature_based? ⇒ Boolean
30
31
32
|
# File 'lib/train-rest/auth_handler/awsv4.rb', line 30
def signature_based?
true
end
|