Skip to content

Commit 0674348

Browse files
committed
feat: discover alg by jwks
1 parent acd98d7 commit 0674348

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/resty/libjwt/init.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@ function _M.validate(user_params)
5858
if jwks_item == nil then
5959
goto continue
6060
end
61-
jwks_c.jwt_checker_setkey(checker, jwks_c.JWT_ALG_RS256, jwks_item);
61+
local alg = jwks_c.jwks_item_alg(jwks_item);
62+
63+
if alg == jwks_c.JWT_ALG_NONE then
64+
return nil, _M.response_error("No algorithm found on jwks", params.return_unauthorized_default)
65+
end
66+
67+
if alg == jwks_c.JWT_ALG_INVAL then
68+
return nil, _M.response_error("invalid algorithm found on jwks", params.return_unauthorized_default)
69+
end
70+
71+
jwks_c.jwt_checker_setkey(checker, alg, jwks_item);
6272
local result = jwks_c.jwt_checker_verify(checker, token);
6373
if result == TOKEN_VALID then
6474
return parsed_token, ""

lib/resty/libjwt/jwks_c.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ typedef enum {
2525
JWT_ALG_INVAL, /**< An invalid algorithm from the caller or the token */
2626
} jwt_alg_t;
2727

28-
29-
struct jwk_item {
30-
jwt_alg_t alg;
31-
};
32-
3328
typedef struct jwk_item jwk_item_t;
3429

3530
int jwt_checker_setkey(jwt_checker_t *checker, const jwt_alg_t alg, const jwk_item_t *key);
3631
typedef struct jwk_set jwk_set_t;
3732
jwk_set_t *jwks_create(const char *jwk_json_str);
3833
const jwk_item_t *jwks_item_get(const jwk_set_t *jwk_set, size_t index);
3934
jwk_item_t *jwks_find_bykid(jwk_set_t *jwk_set, const char *kid);
35+
jwt_alg_t jwks_item_alg(const jwk_item_t *item);
4036
]]
4137

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

0 commit comments

Comments
 (0)