-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathAwsV4Signature.lua
197 lines (162 loc) · 6.45 KB
/
AwsV4Signature.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
--
-- Created by IntelliJ IDEA.
-- User: ddascal
-- Date: 15/05/14
-- Time: 15:09
--
-- Implements the new Version 4 HMAC authorization.
--
local resty_sha256 = require "resty.sha256"
local str = require "resty.string"
local resty_hmac = require "api-gateway.resty.hmac"
local HmacAuthV4Handler = {}
function HmacAuthV4Handler:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
if ( o ~= nil) then
self.aws_endpoint = o.aws_endpoint
self.aws_service = o.aws_service
self.aws_region = o.aws_region
self.aws_secret_key = o.aws_secret_key
self.aws_access_key = o.aws_access_key
self.token = o.token
end
-- set amazon formatted dates
local utc = ngx.utctime()
self.aws_date_short = string.gsub(string.sub(utc, 1, 10),"-","")
self.aws_date = self.aws_date_short .. 'T' .. string.gsub(string.sub(utc, 12),":","") .. 'Z'
return o
end
local function _sign_sha256_FFI(key, msg, raw)
local hmac_sha256 = resty_hmac:new()
local digest = hmac_sha256:digest("sha256",key, msg, raw)
return digest
end
local function _sha256_hex(msg)
local sha256 = resty_sha256:new()
sha256:update(msg)
return str.to_hex(sha256:final())
end
local _sign = _sign_sha256_FFI
local _hash = _sha256_hex
local function get_hashed_canonical_request(method, uri, querystring, headers, requestPayload)
local hash = method .. '\n' ..
uri .. '\n' ..
(querystring or "") .. '\n'
-- add canonicalHeaders
local canonicalHeaders = ""
local signedHeaders = ""
for _, p in ipairs(headers) do
local h_n, h_v = p[1], p[2]
-- todo: trim and lowercase
canonicalHeaders = canonicalHeaders .. h_n .. ":" .. h_v .. "\n"
signedHeaders = signedHeaders .. h_n .. ";"
end
--remove the last ";" from the signedHeaders
signedHeaders = string.sub(signedHeaders, 1, -2)
hash = hash .. canonicalHeaders .. "\n"
.. signedHeaders .. "\n"
requestPayloadHash = _hash(requestPayload or "")
hash = hash .. requestPayloadHash
ngx.log(ngx.DEBUG, "Canonical String to Sign is:\n" .. hash)
local final_hash = _hash(hash)
ngx.log(ngx.DEBUG, "Canonical String HASHED is:\n" .. final_hash .. "\n")
return final_hash, signedHeaders, requestPayloadHash
end
local function get_string_to_sign(algorithm, request_date, credential_scope, hashed_canonical_request)
local s = algorithm .. "\n" .. request_date .. "\n" .. credential_scope .. "\n" .. hashed_canonical_request
ngx.log(ngx.DEBUG, "String-to-Sign is:\n" .. s)
return s
end
local function get_derived_signing_key(aws_secret_key, date, region, service )
local kDate = _sign("AWS4" .. aws_secret_key, date, true )
local kRegion = _sign(kDate, region, true)
local kService = _sign(kRegion, service, true)
local kSigning = _sign(kService, "aws4_request", true)
return kSigning
end
local function urlEncode(inputString)
if (inputString) then
inputString = string.gsub (inputString, "\n", "\r\n")
inputString = string.gsub (inputString, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
inputString = ngx.re.gsub (inputString, " ", "+", "ijo")
-- AWS workarounds
-- replace '+' ( %2B ) with ( %20 )
inputString = ngx.re.gsub(inputString, "%2B", "%20", "ijo")
end
return inputString
--[[local s = ngx.escape_uri(inputString)
-- replace '+' ( %2B ) with ( %20 )
s = ngx.re.gsub(s, "%2B", "%20", "ijo")
-- replace "," with %2C
s = ngx.re.gsub(s, ",", "%2C", "ijo")
return s]]
end
local function getTableIterator(uri_args, urlParameterKeys)
-- use the keys to get the values out of the uri_args table in alphabetical order
local i = 0
local keyValueIterator = function ()
i = i + 1
if urlParameterKeys[i] == nil then
return nil
end
return urlParameterKeys[i], uri_args[urlParameterKeys[i]]
end
return keyValueIterator
end
function HmacAuthV4Handler:formatQueryString(uri_args)
local uri = ""
local urlParameterKeys = {}
-- insert all the url parameter keys into array
for n in pairs(uri_args) do
table.insert(urlParameterKeys, n)
end
-- sort the keys
table.sort(urlParameterKeys)
local iterator = getTableIterator(uri_args, urlParameterKeys)
for param_key, param_value in iterator do
uri = uri .. urlEncode(param_key) .. "=" .. urlEncode(param_value) .. "&"
end
--remove the last "&" from the signedHeaders
uri = string.sub(uri, 1, -2)
return uri
end
function HmacAuthV4Handler:getSignature(http_method, request_uri, uri_arg_table, request_payload, host_override)
local uri_args = self:formatQueryString(uri_arg_table)
local utc = ngx.utctime()
local date1 = self.aws_date_short
local date2 = self.aws_date
local host = self.aws_endpoint
if host_override ~= nil then
host = host_override
end
local headers = {}
table.insert(headers, {"host", host})
table.insert(headers, {"x-amz-date", date2})
if self.token ~= nil then
table.insert(headers, {"x-amz-security-token", self.token})
end
-- ensure parameters in query string are in order
local hashed_canonical_request, signed_headers, request_payload_hash = get_hashed_canonical_request(
http_method, request_uri,
uri_args,
headers, request_payload)
local sign = _sign( get_derived_signing_key( self.aws_secret_key,
date1,
self.aws_region,
self.aws_service),
get_string_to_sign("AWS4-HMAC-SHA256",
date2,
date1 .. "/" .. self.aws_region .. "/" .. self.aws_service .. "/aws4_request",
hashed_canonical_request) )
return sign, signed_headers, request_payload_hash
end
function HmacAuthV4Handler:getAuthorizationHeader(http_method, request_uri, uri_arg_table, request_payload, host_override)
local auth_signature, signed_headers, request_payload_hash = self:getSignature(http_method, request_uri, uri_arg_table, request_payload, host_override)
local authHeader = "AWS4-HMAC-SHA256 Credential=" .. self.aws_access_key.."/" .. self.aws_date_short .. "/" .. self.aws_region
.."/" .. self.aws_service.."/aws4_request,SignedHeaders="..signed_headers..",Signature="..auth_signature
return authHeader, request_payload_hash
end
return HmacAuthV4Handler