Skip to content

Commit

Permalink
remove code for test from jwt-auth.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
dspo committed Sep 25, 2024
1 parent 9f0c3f3 commit 80a7c7f
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 102 deletions.
86 changes: 0 additions & 86 deletions apisix/plugins/jwt-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ local new_tab = require ("table.new")
local ngx_encode_base64 = ngx.encode_base64
local ngx_decode_base64 = ngx.decode_base64
local ngx = ngx
local ngx_time = ngx.time
local sub_str = string.sub
local table_insert = table.insert
local table_concat = table.concat
local ngx_re_gmatch = ngx.re.gmatch
local plugin_name = "jwt-auth"
local pcall = pcall


local schema = {
Expand Down Expand Up @@ -281,88 +279,4 @@ function _M.rewrite(conf, ctx)
end


local function get_real_payload(key, exp, payload)
local real_payload = {
key = key,
exp = ngx_time() + exp
}
if payload then
local extra_payload = core.json.decode(payload)
core.table.merge(extra_payload, real_payload)
return extra_payload
end
return real_payload
end

local function sign_jwt_with_HS(key, auth_conf, payload)
local auth_secret, err = get_secret(auth_conf)
if not auth_secret then
core.log.error("failed to sign jwt, err: ", err)
return nil, "failed to sign jwt: failed to get auth_secret"
end
local ok, jwt_token = pcall(jwt.sign, _M,
auth_secret,
{
header = {
typ = "JWT",
alg = auth_conf.algorithm
},
payload = get_real_payload(key, auth_conf.exp, payload)
}
)
if not ok then
core.log.error("failed to sign jwt, err: ", jwt_token.reason)
return nil, "failed to sign jwt"
end
return jwt_token
end

local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
local ok, jwt_token = pcall(jwt.sign, _M,
auth_conf.private_key,
{
header = {
typ = "JWT",
alg = auth_conf.algorithm,
x5c = {
auth_conf.public_key,
}
},
payload = get_real_payload(key, auth_conf.exp, payload)
}
)
if not ok then
core.log.warn("failed to sign jwt, err: ", jwt_token.reason)
return nil, "failed to sign jwt"
end
return jwt_token
end

local function get_sign_handler(algorithm)
if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
return sign_jwt_with_HS
elseif algorithm == "RS256" or algorithm == "ES256" then
return sign_jwt_with_RS256_ES256
end
end

local function gen_token(auth_conf, payload)
if not auth_conf.exp then
auth_conf.exp = 86400
end
if not auth_conf.lifetime_grace_period then
auth_conf.lifetime_grace_period = 0
end
if not auth_conf.algorithm then
auth_conf.algorithm = "HS256"
end
local sign_handler = get_sign_handler(auth_conf.algorithm)
local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
return jwt_token, err
end

-- only for test
_M.gen_token = gen_token


return _M
6 changes: 3 additions & 3 deletions t/fips/jwt-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -193,7 +193,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -226,7 +226,7 @@ JWT token invalid: invalid jwt string
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down
122 changes: 122 additions & 0 deletions t/lib/apisix/plugins/jwt-auth.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--

local core = require("apisix.core")
local jwt = require("resty.jwt")

local ngx_time = ngx.time
local ngx_decode_base64 = ngx.decode_base64
local pcall = pcall


local _M = {}


local function get_secret(conf)
local secret = conf.secret

if conf.base64_secret then
return ngx_decode_base64(secret)
end

return secret
end

local function get_real_payload(key, exp, payload)
local real_payload = {
key = key,
exp = ngx_time() + exp
}
if payload then
local extra_payload = core.json.decode(payload)
core.table.merge(extra_payload, real_payload)
return extra_payload
end
return real_payload
end

local function sign_jwt_with_HS(key, auth_conf, payload)
local auth_secret, err = get_secret(auth_conf)
if not auth_secret then
core.log.error("failed to sign jwt, err: ", err)
return nil, "failed to sign jwt: failed to get auth_secret"
end
local ok, jwt_token = pcall(jwt.sign, _M,
auth_secret,
{
header = {
typ = "JWT",
alg = auth_conf.algorithm
},
payload = get_real_payload(key, auth_conf.exp, payload)
}
)
if not ok then
core.log.error("failed to sign jwt, err: ", jwt_token.reason)
return nil, "failed to sign jwt"
end
return jwt_token
end

local function sign_jwt_with_RS256_ES256(key, auth_conf, payload)
local ok, jwt_token = pcall(jwt.sign, _M,
auth_conf.private_key,
{
header = {
typ = "JWT",
alg = auth_conf.algorithm,
x5c = {
auth_conf.public_key,
}
},
payload = get_real_payload(key, auth_conf.exp, payload)
}
)
if not ok then
core.log.error("failed to sign jwt, err: ", jwt_token.reason)
return nil, "failed to sign jwt"
end
return jwt_token
end

local function get_sign_handler(algorithm)
if not algorithm or algorithm == "HS256" or algorithm == "HS512" then
return sign_jwt_with_HS
elseif algorithm == "RS256" or algorithm == "ES256" then
return sign_jwt_with_RS256_ES256
end
end

local function gen_token(auth_conf, payload)
if not auth_conf.exp then
auth_conf.exp = 86400
end
if not auth_conf.lifetime_grace_period then
auth_conf.lifetime_grace_period = 0
end
if not auth_conf.algorithm then
auth_conf.algorithm = "HS256"
end
local sign_handler = get_sign_handler(auth_conf.algorithm)
local jwt_token, err = sign_handler(auth_conf.key, auth_conf, payload)
return jwt_token, err
end


_M.gen_token = gen_token

return _M
18 changes: 9 additions & 9 deletions t/plugin/jwt-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key",
Expand Down Expand Up @@ -538,7 +538,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -637,7 +637,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -672,7 +672,7 @@ hello world
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -769,7 +769,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -802,7 +802,7 @@ hello world
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-rs256",
Expand Down Expand Up @@ -976,7 +976,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-HS512",
Expand Down Expand Up @@ -1008,7 +1008,7 @@ hello world
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-HS512",
Expand Down Expand Up @@ -1232,7 +1232,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key-es256",
Expand Down
4 changes: 2 additions & 2 deletions t/plugin/jwt-auth2.t
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ hello world
ngx.say(body)
end
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
exp = 1,
algorithm = "HS256",
Expand Down Expand Up @@ -412,7 +412,7 @@ qr/ailed to verify jwt: 'exp' claim expired at/
end
-- get JWT token
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
exp = 1,
algorithm = "HS256",
Expand Down
4 changes: 2 additions & 2 deletions t/plugin/multi-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ passed
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key",
secret = "my-secret-key"
Expand Down Expand Up @@ -608,7 +608,7 @@ hello world
end
ngx.sleep(0.1)

local gen_token = require("apisix.plugins.jwt-auth").gen_token
local gen_token = require("lib.apisix.plugins.jwt-auth").gen_token
local auth_conf = {
key = "user-key",
secret = "my-secret-key"
Expand Down

0 comments on commit 80a7c7f

Please sign in to comment.