Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The hmac-auth plugin support HMAC-SM3 algorithm. Resolved #11927 #11930

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions apisix/plugins/hmac-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ local ngx_re = require("ngx.re")
local ipairs = ipairs
local hmac_sha1 = ngx.hmac_sha1
local core = require("apisix.core")
local hmac = require("resty.hmac")
local hmac = require("resty.openssl.hmac")
local consumer = require("apisix.consumer")
local ngx_decode_base64 = ngx.decode_base64
local ngx_encode_base64 = ngx.encode_base64
local plugin_name = "hmac-auth"
local ALLOWED_ALGORITHMS = {"hmac-sha1", "hmac-sha256", "hmac-sha512"}
local ALLOWED_ALGORITHMS = {"hmac-sha1", "hmac-sha256", "hmac-sha512", "hmac-sm3"}
local resty_sha256 = require("resty.sha256")
local schema_def = require("apisix.schema_def")
local auth_utils = require("apisix.utils.auth")
Expand Down Expand Up @@ -92,18 +92,21 @@ local hmac_funcs = {
return hmac_sha1(secret_key, message)
end,
["hmac-sha256"] = function(secret_key, message)
return hmac:new(secret_key, hmac.ALGOS.SHA256):final(message)
return hmac.new(secret_key, "sha256"):final(message)
end,
["hmac-sha512"] = function(secret_key, message)
return hmac:new(secret_key, hmac.ALGOS.SHA512):final(message)
return hmac.new(secret_key, "sha512"):final(message)
end,
["hmac-sm3"] = function(secret_key, message)
return hmac.new(secret_key, "sm3"):final(message)
end,
}


local function array_to_map(arr)
local map = core.table.new(0, #arr)
for _, v in ipairs(arr) do
map[v] = true
map[v] = true
end

return map
Expand Down Expand Up @@ -152,19 +155,19 @@ local function generate_signature(ctx, secret_key, params)
for _, h in ipairs(params.headers) do
local canonical_header = core.request.header(ctx, h)
if not canonical_header then
if h == "@request-target" then
local request_target = request_method .. " " .. uri
core.table.insert(signing_string_items, request_target)
if h == "@request-target" then
local request_target = request_method .. " " .. uri
core.table.insert(signing_string_items, request_target)
core.log.info("canonical_header name:", core.json.delay_encode(h))
core.log.info("canonical_header value: ",
core.json.delay_encode(request_target))
end
else
core.table.insert(signing_string_items,
h .. ": " .. canonical_header)
core.log.info("canonical_header name:", core.json.delay_encode(h))
core.log.info("canonical_header value: ",
core.json.delay_encode(request_target))
end
else
core.table.insert(signing_string_items,
h .. ": " .. canonical_header)
core.log.info("canonical_header name:", core.json.delay_encode(h))
core.log.info("canonical_header value: ",
core.json.delay_encode(canonical_header))
core.json.delay_encode(canonical_header))
end
end
end
Expand Down Expand Up @@ -208,10 +211,10 @@ local function validate(ctx, conf, params)
end

for _, algo in ipairs(conf.allowed_algorithms) do
if algo == params.algorithm then
found_algorithm = true
break
end
if algo == params.algorithm then
found_algorithm = true
break
end
end

if not found_algorithm then
Expand Down
Loading