Skip to content

Commit

Permalink
refactor(plugins/jwt): use encode_base64url in resty.core (#11569)
Browse files Browse the repository at this point in the history
lua-resty-core provides two functions base64.encode_base64url() and base64.decode_base64url(),
Which have the same effect as our Lua functions.
  • Loading branch information
chronolaw authored Sep 19, 2023
1 parent dde5c0c commit 7a32972
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions kong/plugins/jwt/jwt_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-- @see https://github.com/x25/luajwt

local json = require "cjson"
local b64 = require "ngx.base64"
local openssl_digest = require "resty.openssl.digest"
local openssl_hmac = require "resty.openssl.hmac"
local openssl_pkey = require "resty.openssl.pkey"
Expand All @@ -26,8 +27,8 @@ local assert = assert
local tostring = tostring
local setmetatable = setmetatable
local getmetatable = getmetatable
local encode_base64 = ngx.encode_base64
local decode_base64 = ngx.decode_base64
local encode_base64url = b64.encode_base64url
local decode_base64url = b64.decode_base64url


--- Supported algorithms for signing tokens.
Expand Down Expand Up @@ -126,8 +127,7 @@ local alg_verify = {
-- @param input String to base64 encode
-- @return Base64 encoded string
local function base64_encode(input)
local result = encode_base64(input, true)
result = result:gsub("+", "-"):gsub("/", "_")
local result = encode_base64url(input)
return result
end

Expand All @@ -143,8 +143,7 @@ local function base64_decode(input)
input = input .. rep("=", padlen)
end

input = input:gsub("-", "+"):gsub("_", "/")
return decode_base64(input)
return decode_base64url(input)
end


Expand All @@ -155,22 +154,23 @@ end
-- @param len Number of parts to retrieve
-- @return A table of strings
local function tokenize(str, div, len)
local result, pos = {}, 0
local result, idx, pos = {}, 1, 0

local iter = function()
return find(str, div, pos, true)
end

for st, sp in iter do
result[#result + 1] = sub(str, pos, st-1)
result[idx] = sub(str, pos, st - 1)
idx = idx + 1
pos = sp + 1
len = len - 1
if len <= 1 then
break
end
end

result[#result + 1] = sub(str, pos)
result[idx] = sub(str, pos)
return result
end

Expand Down

1 comment on commit 7a32972

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:7a32972f7bd203c9e5bd93d878150fe9d5e67298
Artifacts available https://github.com/Kong/kong/actions/runs/6230926681

Please sign in to comment.