-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac-proxy.conf
63 lines (51 loc) · 1.65 KB
/
hmac-proxy.conf
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
server {
listen $HMAC_LISTEN;
location / {
access_by_lua_block {
local hmac_file_whitelist = "$HMAC_FILE_WHITELIST"
if (hmac_file_whitelist ~= nil and hmac_file_whitelist ~= '') then
for whitelist_path in string.gmatch(hmac_file_whitelist, '([^,]+)') do
if string.match(ngx.var.request_filename, whitelist_path) then
return
end
end
end
local hmac_signing_key = "$HMAC_SIGNING_KEY"
local hmac = require "resty.hmac"
local hmac_256 = hmac:new(hmac_signing_key, hmac.ALGOS.SHA256)
local url = require "net.url"
local params = ngx.req.get_uri_args()
if next(params) == nil then
local referer = url.parse(ngx.var.http_referer)
params = referer.query
end
local code = params["hmac"]
if code == nil or code == '' then
return ngx.exit(401)
end
local hashedParams = {"code", "locale", "shop", "state", "timestamp"}
for k, _ in pairs(params) do
local foundParam = false
for index = 1, #hashedParams do
if k == hashedParams[index] then
foundParam = true
break
end
end
if not foundParam then
params[k] = nil
end
end
table.sort(params)
local hash_query = url.buildQuery(params)
local signature = hmac_256:final(ngx.unescape_uri(hash_query), true)
hmac_256:reset()
if code ~= signature then
return ngx.exit(401)
end
}
proxy_pass $HMAC_PROXY_BASE_URL$request_uri;
proxy_read_timeout $HMAC_PROXY_READ_TIMEOUT;
proxy_send_timeout $HMAC_PROXY_SEND_TIMEOUT;
}
}