Skip to content

Commit a6fee52

Browse files
committed
resolved #11927
1 parent 6a84576 commit a6fee52

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

apisix/plugins/hmac-auth.lua

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ local ngx_re = require("ngx.re")
2121
local ipairs = ipairs
2222
local hmac_sha1 = ngx.hmac_sha1
2323
local core = require("apisix.core")
24-
local hmac = require("resty.hmac")
24+
local hmac = require("resty.openssl.hmac")
2525
local consumer = require("apisix.consumer")
2626
local ngx_decode_base64 = ngx.decode_base64
2727
local ngx_encode_base64 = ngx.encode_base64
2828
local plugin_name = "hmac-auth"
29-
local ALLOWED_ALGORITHMS = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}
29+
local ALLOWED_ALGORITHMS = {"hmac-sha1", "hmac-sha256", "hmac-sha512", "hmac-sm3"}
3030
local resty_sha256 = require("resty.sha256")
3131
local schema_def = require("apisix.schema_def")
3232
local auth_utils = require("apisix.utils.auth")
@@ -92,18 +92,21 @@ local hmac_funcs = {
9292
return hmac_sha1(secret_key, message)
9393
end,
9494
["hmac-sha256"] = function(secret_key, message)
95-
return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message)
95+
return hmac.new(secret_key, "sha256"):final(message)
9696
end,
9797
["hmac-sha512"] = function(secret_key, message)
98-
return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message)
98+
return hmac.new(secret_key, "sha512"):final(message)
99+
end,
100+
["hmac-sm3"] = function(secret_key, message)
101+
return hmac.new(secret_key, "sm3"):final(message)
99102
end,
100103
}
101104

102105

103106
local function array_to_map(arr)
104107
local map = core.table.new(0, #arr)
105108
for _, v in ipairs(arr) do
106-
map[v] = true
109+
map[v] = true
107110
end
108111

109112
return map
@@ -152,19 +155,19 @@ local function generate_signature(ctx, secret_key, params)
152155
for _, h in ipairs(params.headers) do
153156
local canonical_header = core.request.header(ctx, h)
154157
if not canonical_header then
155-
if h == "@request-target" then
156-
local request_target = request_method .. " " .. uri
157-
core.table.insert(signing_string_items, request_target)
158+
if h == "@request-target" then
159+
local request_target = request_method .. " " .. uri
160+
core.table.insert(signing_string_items, request_target)
161+
core.log.info("canonical_header name:", core.json.delay_encode(h))
162+
core.log.info("canonical_header value: ",
163+
core.json.delay_encode(request_target))
164+
end
165+
else
166+
core.table.insert(signing_string_items,
167+
h .. ": " .. canonical_header)
158168
core.log.info("canonical_header name:", core.json.delay_encode(h))
159169
core.log.info("canonical_header value: ",
160-
core.json.delay_encode(request_target))
161-
end
162-
else
163-
core.table.insert(signing_string_items,
164-
h .. ": " .. canonical_header)
165-
core.log.info("canonical_header name:", core.json.delay_encode(h))
166-
core.log.info("canonical_header value: ",
167-
core.json.delay_encode(canonical_header))
170+
core.json.delay_encode(canonical_header))
168171
end
169172
end
170173
end
@@ -208,10 +211,10 @@ local function validate(ctx, conf, params)
208211
end
209212

210213
for _, algo in ipairs(conf.allowed_algorithms) do
211-
if algo == params.algorithm then
212-
found_algorithm = true
213-
break
214-
end
214+
if algo == params.algorithm then
215+
found_algorithm = true
216+
break
217+
end
215218
end
216219

217220
if not found_algorithm then

0 commit comments

Comments
 (0)