@@ -17,10 +17,12 @@ function HmacAuthV4Handler:new(o)
17
17
setmetatable (o , self )
18
18
self .__index = self
19
19
if ( o ~= nil ) then
20
+ self .aws_endpoint = o .aws_endpoint
20
21
self .aws_service = o .aws_service
21
22
self .aws_region = o .aws_region
22
23
self .aws_secret_key = o .aws_secret_key
23
24
self .aws_access_key = o .aws_access_key
25
+ self .token = o .token
24
26
end
25
27
-- set amazon formatted dates
26
28
local utc = ngx .utctime ()
@@ -52,7 +54,8 @@ local function get_hashed_canonical_request(method, uri, querystring, headers, r
52
54
-- add canonicalHeaders
53
55
local canonicalHeaders = " "
54
56
local signedHeaders = " "
55
- for h_n ,h_v in pairs (headers ) do
57
+ for _ , p in ipairs (headers ) do
58
+ local h_n , h_v = p [1 ], p [2 ]
56
59
-- todo: trim and lowercase
57
60
canonicalHeaders = canonicalHeaders .. h_n .. " :" .. h_v .. " \n "
58
61
signedHeaders = signedHeaders .. h_n .. " ;"
@@ -63,13 +66,14 @@ local function get_hashed_canonical_request(method, uri, querystring, headers, r
63
66
hash = hash .. canonicalHeaders .. " \n "
64
67
.. signedHeaders .. " \n "
65
68
66
- hash = hash .. _hash (requestPayload or " " )
69
+ requestPayloadHash = _hash (requestPayload or " " )
70
+ hash = hash .. requestPayloadHash
67
71
68
72
ngx .log (ngx .DEBUG , " Canonical String to Sign is:\n " .. hash )
69
73
70
74
local final_hash = _hash (hash )
71
75
ngx .log (ngx .DEBUG , " Canonical String HASHED is:\n " .. final_hash .. " \n " )
72
- return final_hash
76
+ return final_hash , signedHeaders , requestPayloadHash
73
77
end
74
78
75
79
local function get_string_to_sign (algorithm , request_date , credential_scope , hashed_canonical_request )
@@ -141,36 +145,44 @@ function HmacAuthV4Handler:formatQueryString(uri_args)
141
145
return uri
142
146
end
143
147
144
- function HmacAuthV4Handler :getSignature (http_method , request_uri , uri_arg_table , request_payload )
148
+ function HmacAuthV4Handler :getSignature (http_method , request_uri , uri_arg_table , request_payload , host_override )
145
149
local uri_args = self :formatQueryString (uri_arg_table )
146
150
local utc = ngx .utctime ()
147
151
local date1 = self .aws_date_short
148
152
local date2 = self .aws_date
153
+ local host = self .aws_endpoint
154
+ if host_override ~= nil then
155
+ host = host_override
156
+ end
149
157
150
158
local headers = {}
151
- headers .host = self .aws_service .. " ." .. self .aws_region .. " .amazonaws.com"
152
- headers [" x-amz-date" ] = date2
159
+ table.insert (headers , {" host" , host })
160
+ table.insert (headers , {" x-amz-date" , date2 })
161
+ if self .token ~= nil then
162
+ table.insert (headers , {" x-amz-security-token" , self .token })
163
+ end
153
164
154
165
-- ensure parameters in query string are in order
166
+ local hashed_canonical_request , signed_headers , request_payload_hash = get_hashed_canonical_request (
167
+ http_method , request_uri ,
168
+ uri_args ,
169
+ headers , request_payload )
155
170
local sign = _sign ( get_derived_signing_key ( self .aws_secret_key ,
156
171
date1 ,
157
172
self .aws_region ,
158
173
self .aws_service ),
159
174
get_string_to_sign (" AWS4-HMAC-SHA256" ,
160
175
date2 ,
161
176
date1 .. " /" .. self .aws_region .. " /" .. self .aws_service .. " /aws4_request" ,
162
- get_hashed_canonical_request (
163
- http_method , request_uri ,
164
- uri_args ,
165
- headers , request_payload ) ) )
166
- return sign
177
+ hashed_canonical_request ) )
178
+ return sign , signed_headers , request_payload_hash
167
179
end
168
180
169
- function HmacAuthV4Handler :getAuthorizationHeader (http_method , request_uri , uri_arg_table , request_payload )
170
- local auth_signature = self :getSignature (http_method , request_uri , uri_arg_table , request_payload )
181
+ function HmacAuthV4Handler :getAuthorizationHeader (http_method , request_uri , uri_arg_table , request_payload , host_override )
182
+ local auth_signature , signed_headers , request_payload_hash = self :getSignature (http_method , request_uri , uri_arg_table , request_payload , host_override )
171
183
local authHeader = " AWS4-HMAC-SHA256 Credential=" .. self .aws_access_key .. " /" .. self .aws_date_short .. " /" .. self .aws_region
172
- .. " /" .. self .aws_service .. " /aws4_request,SignedHeaders=host;x-amz-date ,Signature=" .. auth_signature
173
- return authHeader
184
+ .. " /" .. self .aws_service .. " /aws4_request,SignedHeaders=" .. signed_headers .. " ,Signature=" .. auth_signature
185
+ return authHeader , request_payload_hash
174
186
end
175
187
176
188
return HmacAuthV4Handler
0 commit comments