Skip to content

Commit f8cfbc0

Browse files
committed
feat: enhance JWT validation and error handling with improved response mechanisms
1 parent 35ddb71 commit f8cfbc0

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

lib/resty/libjwt/init.lua

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ local utils = require("resty.libjwt.utils")
33
local cached = require("resty.libjwt.cached")
44
local decode = require("resty.libjwt.decode")
55
local cjson = require("cjson.safe")
6+
local ffi = require("ffi")
67
local _M = {}
78
local ngx = ngx
89

910
local open = io.open
10-
local function _read_file(path)
11+
function _M.read_file(path)
1112
local file = open(path, "rb")
1213
if not file then return nil end
1314
local content = file:read "*a"
@@ -16,30 +17,35 @@ local function _read_file(path)
1617
end
1718

1819
local TOKEN_VALID = 0
19-
20-
21-
local function _validate(params)
20+
function _M.validate(user_params)
21+
local params, err = utils.get_params(user_params)
22+
if params == nil then
23+
return false, _M.response_error(err, false)
24+
end
25+
if err ~= "" then
26+
return false, _M.response_error(err, params.return_unauthorized_default)
27+
end
2228
local headers = ngx.req.get_headers()
23-
local token, err
2429

30+
local token
2531
token, err = utils.get_token(headers, params.header_token)
2632
if err ~= "" then
27-
return nil, err
33+
return false, _M.response_error(err, params.return_unauthorized_default)
2834
end
2935
local parsed_token
3036
parsed_token, err = decode.jwt(token)
3137
if err ~= nil then
32-
return nil, err
38+
return nil, _M.response_error(err, params.return_unauthorized_default)
3339
end
3440
if parsed_token == nil or parsed_token.header.kid == nil then
35-
return nil, "kid not found"
41+
return nil, _M.response_error("kid not found", params.return_unauthorized_default)
3642
end
3743

3844
local files_cached = cached:getInstance()
3945
for _, jwks_file in ipairs(params.jwks_files) do
4046
local file
4147
if files_cached:get(jwks_file) == nil then
42-
file = _read_file(jwks_file)
48+
file = _M.read_file(jwks_file)
4349
if file == nil then
4450
goto continue
4551
end
@@ -48,19 +54,21 @@ local function _validate(params)
4854
file = files_cached:get(jwks_file)
4955
end
5056
local jwks_set = jwks_c.jwks_create(file);
57+
ffi.gc(jwks_set, jwks_c.jwks_free);
5158
local checker = jwks_c.jwt_checker_new();
59+
ffi.gc(checker, jwks_c.jwt_checker_free);
5260
local jwks_item = jwks_c.jwks_find_bykid(jwks_set, parsed_token.header.kid);
5361
if jwks_item == nil then
5462
goto continue
5563
end
5664
local alg = jwks_c.jwks_item_alg(jwks_item);
5765

5866
if alg == jwks_c.JWT_ALG_NONE then
59-
return nil, "No algorithm found on jwks"
67+
return nil, _M.response_error("No algorithm found on jwks", params.return_unauthorized_default)
6068
end
6169

6270
if alg == jwks_c.JWT_ALG_INVAL then
63-
return nil, "invalid algorithm found on jwks"
71+
return nil, _M.response_error("invalid algorithm found on jwks", params.return_unauthorized_default)
6472
end
6573

6674
jwks_c.jwt_checker_setkey(checker, alg, jwks_item);
@@ -70,11 +78,10 @@ local function _validate(params)
7078
end
7179
::continue::
7280
end
73-
74-
return nil, "invalid token"
81+
return nil, _M.response_error("invalid token", params.return_unauthorized_default)
7582
end
7683

77-
local function _response_error(error_message, return_unauthorized_default)
84+
function _M.response_error(error_message, return_unauthorized_default)
7885
if return_unauthorized_default == true then
7986
ngx.header.content_type = "application/json; charset=utf-8"
8087
local response = {
@@ -87,19 +94,4 @@ local function _response_error(error_message, return_unauthorized_default)
8794
return error_message
8895
end
8996

90-
91-
function _M.validate(user_params)
92-
local params, err = utils.get_params(user_params)
93-
if params == nil then
94-
return nil, _response_error(err, true)
95-
end
96-
97-
local parsed_token
98-
parsed_token, err = _validate(params)
99-
if err ~= "" then
100-
return nil, _response_error(err, params.return_unauthorized_default)
101-
end
102-
return parsed_token, ""
103-
end
104-
10597
return _M

lib/resty/libjwt/jwks_c.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ jwk_set_t *jwks_create(const char *jwk_json_str);
3333
const jwk_item_t *jwks_item_get(const jwk_set_t *jwk_set, size_t index);
3434
jwk_item_t *jwks_find_bykid(jwk_set_t *jwk_set, const char *kid);
3535
jwt_alg_t jwks_item_alg(const jwk_item_t *item);
36+
37+
38+
void jwks_free(jwk_set_t *jwk_set);
39+
void jwt_checker_free(jwt_checker_t *checker);
3640
]]
3741

3842
local libjwt = ffi.load("libjwt");

0 commit comments

Comments
 (0)