From dbd772d26cb979f24d93484be3a83dc09f1e9adc Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 21 Nov 2023 17:42:05 +0800 Subject: [PATCH 1/2] refactor(plugins/jwt): use string.buffer to replace table.concat --- kong/plugins/jwt/jwt_parser.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/kong/plugins/jwt/jwt_parser.lua b/kong/plugins/jwt/jwt_parser.lua index e22b6b11f621..8080a0f28ff4 100644 --- a/kong/plugins/jwt/jwt_parser.lua +++ b/kong/plugins/jwt/jwt_parser.lua @@ -7,6 +7,7 @@ local json = require "cjson" local b64 = require "ngx.base64" +local buffer = require "string.buffer" local openssl_digest = require "resty.openssl.digest" local openssl_hmac = require "resty.openssl.hmac" local openssl_pkey = require "resty.openssl.pkey" @@ -20,7 +21,6 @@ local time = ngx.time local pairs = pairs local error = error local pcall = pcall -local concat = table.concat local insert = table.insert local unpack = unpack local assert = assert @@ -242,12 +242,17 @@ local function encode_token(data, key, alg, header) base64_encode(json.encode(data)) } - local signing_input = concat(segments, ".") - local signature = alg_sign[alg](signing_input, key) + local buf = buffer.new() - segments[#segments+1] = base64_encode(signature) + buf:put(base64_encode(json.encode(header))):put(".") + :put(base64_encode(json.encode(data))) - return concat(segments, ".") + local signature = alg_sign[alg](buf:tostring(), key) + + buf:put(".") + :put(base64_encode(signature)) + + return buf:get() end From f87258f2ce61e2b894bb02d0db774e20920f1021 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 21 Nov 2023 17:44:52 +0800 Subject: [PATCH 2/2] clean --- kong/plugins/jwt/jwt_parser.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/kong/plugins/jwt/jwt_parser.lua b/kong/plugins/jwt/jwt_parser.lua index 8080a0f28ff4..5bad71635915 100644 --- a/kong/plugins/jwt/jwt_parser.lua +++ b/kong/plugins/jwt/jwt_parser.lua @@ -237,11 +237,6 @@ local function encode_token(data, key, alg, header) end local header = header or { typ = "JWT", alg = alg } - local segments = { - base64_encode(json.encode(header)), - base64_encode(json.encode(data)) - } - local buf = buffer.new() buf:put(base64_encode(json.encode(header))):put(".")