@@ -17,10 +17,12 @@ function HmacAuthV4Handler:new(o)
1717 setmetatable (o , self )
1818 self .__index = self
1919 if ( o ~= nil ) then
20+ self .aws_endpoint = o .aws_endpoint
2021 self .aws_service = o .aws_service
2122 self .aws_region = o .aws_region
2223 self .aws_secret_key = o .aws_secret_key
2324 self .aws_access_key = o .aws_access_key
25+ self .token = o .token
2426 end
2527 -- set amazon formatted dates
2628 local utc = ngx .utctime ()
@@ -52,7 +54,8 @@ local function get_hashed_canonical_request(method, uri, querystring, headers, r
5254 -- add canonicalHeaders
5355 local canonicalHeaders = " "
5456 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 ]
5659 -- todo: trim and lowercase
5760 canonicalHeaders = canonicalHeaders .. h_n .. " :" .. h_v .. " \n "
5861 signedHeaders = signedHeaders .. h_n .. " ;"
@@ -63,13 +66,14 @@ local function get_hashed_canonical_request(method, uri, querystring, headers, r
6366 hash = hash .. canonicalHeaders .. " \n "
6467 .. signedHeaders .. " \n "
6568
66- hash = hash .. _hash (requestPayload or " " )
69+ requestPayloadHash = _hash (requestPayload or " " )
70+ hash = hash .. requestPayloadHash
6771
6872 ngx .log (ngx .DEBUG , " Canonical String to Sign is:\n " .. hash )
6973
7074 local final_hash = _hash (hash )
7175 ngx .log (ngx .DEBUG , " Canonical String HASHED is:\n " .. final_hash .. " \n " )
72- return final_hash
76+ return final_hash , signedHeaders , requestPayloadHash
7377end
7478
7579local function get_string_to_sign (algorithm , request_date , credential_scope , hashed_canonical_request )
@@ -141,36 +145,44 @@ function HmacAuthV4Handler:formatQueryString(uri_args)
141145 return uri
142146end
143147
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 )
145149 local uri_args = self :formatQueryString (uri_arg_table )
146150 local utc = ngx .utctime ()
147151 local date1 = self .aws_date_short
148152 local date2 = self .aws_date
153+ local host = self .aws_endpoint
154+ if host_override ~= nil then
155+ host = host_override
156+ end
149157
150158 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
153164
154165 -- 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 )
155170 local sign = _sign ( get_derived_signing_key ( self .aws_secret_key ,
156171 date1 ,
157172 self .aws_region ,
158173 self .aws_service ),
159174 get_string_to_sign (" AWS4-HMAC-SHA256" ,
160175 date2 ,
161176 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
167179end
168180
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 )
171183 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
174186end
175187
176188return HmacAuthV4Handler
0 commit comments