Skip to content

Commit 1b6fbbe

Browse files
committed
refactor: improve validation and error handling in JWT module
1 parent 9c4931a commit 1b6fbbe

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

lib/resty/libjwt/init.lua

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local _M = {}
88
local ngx = ngx
99

1010
local open = io.open
11-
function _M.read_file(path)
11+
local function _read_file(path)
1212
local file = open(path, "rb")
1313
if not file then return nil end
1414
local content = file:read "*a"
@@ -17,35 +17,30 @@ function _M.read_file(path)
1717
end
1818

1919
local TOKEN_VALID = 0
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
20+
21+
22+
local function _validate(params)
2823
local headers = ngx.req.get_headers()
24+
local token, err
2925

30-
local token
3126
token, err = utils.get_token(headers, params.header_token)
3227
if err ~= "" then
33-
return false, _M.response_error(err, params.return_unauthorized_default)
28+
return nil, err
3429
end
3530
local parsed_token
3631
parsed_token, err = decode.jwt(token)
3732
if err ~= nil then
38-
return nil, _M.response_error(err, params.return_unauthorized_default)
33+
return nil, err
3934
end
4035
if parsed_token == nil or parsed_token.header.kid == nil then
41-
return nil, _M.response_error("kid not found", params.return_unauthorized_default)
36+
return nil, "kid not found"
4237
end
4338

4439
local files_cached = cached:getInstance()
4540
for _, jwks_file in ipairs(params.jwks_files) do
4641
local file
4742
if files_cached:get(jwks_file) == nil then
48-
file = _M.read_file(jwks_file)
43+
file = _read_file(jwks_file)
4944
if file == nil then
5045
goto continue
5146
end
@@ -64,11 +59,11 @@ function _M.validate(user_params)
6459
local alg = jwks_c.jwks_item_alg(jwks_item);
6560

6661
if alg == jwks_c.JWT_ALG_NONE then
67-
return nil, _M.response_error("No algorithm found on jwks", params.return_unauthorized_default)
62+
return nil, "No algorithm found on jwks"
6863
end
6964

7065
if alg == jwks_c.JWT_ALG_INVAL then
71-
return nil, _M.response_error("invalid algorithm found on jwks", params.return_unauthorized_default)
66+
return nil, "invalid algorithm found on jwks"
7267
end
7368

7469
jwks_c.jwt_checker_setkey(checker, alg, jwks_item);
@@ -78,10 +73,11 @@ function _M.validate(user_params)
7873
end
7974
::continue::
8075
end
81-
return nil, _M.response_error("invalid token", params.return_unauthorized_default)
76+
77+
return nil, "invalid token"
8278
end
8379

84-
function _M.response_error(error_message, return_unauthorized_default)
80+
local function _response_error(error_message, return_unauthorized_default)
8581
if return_unauthorized_default == true then
8682
ngx.header.content_type = "application/json; charset=utf-8"
8783
local response = {
@@ -94,4 +90,19 @@ function _M.response_error(error_message, return_unauthorized_default)
9490
return error_message
9591
end
9692

93+
94+
function _M.validate(user_params)
95+
local params, err = utils.get_params(user_params)
96+
if params == nil then
97+
return nil, _response_error(err, true)
98+
end
99+
100+
local parsed_token
101+
parsed_token, err = _validate(params)
102+
if err ~= "" then
103+
return nil, _response_error(err, params.return_unauthorized_default)
104+
end
105+
return parsed_token, ""
106+
end
107+
97108
return _M

0 commit comments

Comments
 (0)