SkyLothar / lua-resty-jwt

JWT For The Great Openresty
Apache License 2.0
513 stars 179 forks source link

I encountered an error while checking the token of rs256 #99

Closed ChaselT closed 2 years ago

ChaselT commented 2 years ago

this is error info: 2022/04/12 12:40:47 [error] 7#7: *1 lua entry thread aborted: runtime error: /usr/local/openresty/site/lualib/resty/evp.lua:216: /usr/local/openresty/luajit/lib/libluajit-5.1.so.2: undefined symbol: EVP_MD_CTX_create stack traceback: coroutine 0: [C]: in function '__index' /usr/local/openresty/site/lualib/resty/evp.lua:216: in function 'verify' /usr/local/openresty/site/lualib/resty/jwt.lua:812: in function 'verify_jwt_obj' /opt/lua-resty-jwt/lib/nginx-jwt.lua:54: in function 'auth'

and this is my code

local cjson = require "cjson"
local jwt = require "resty.jwt"

local secret = "-----BEGIN PUBLIC KEY-----\
prBOA67SwS1PaDHvGSuQqGRBfzOTaZUtyJdacvhMdME_NJUzYXA0DtMcCk8\
4PHZ1E_Q6VQjG4zgim3vBAaGcKHDO2c6cmh3w83rcp2eqvCEzuzcvIJiiMM\
iOWwdDdIbibPpITv1ZQEQyWEV38MZYvxQpdBSkgrZFfO_Za_Cs4Ok\
-----END PUBLIC KEY-----"
local no_need_token_api_list = {'/api/register', '/api/login'}

local function ignore_url (val)
    for index, value in ipairs(no_need_token_api_list) do
        if (value == val) then
            return true
        end
    end

    return false
end
local M = {}
function M.auth()

    if ignore_url(ngx.var.request_uri) then
        return
    else
    end

    -- require Authorization request header
    local auth_header = ngx.var.http_Authorization

    if auth_header == nil then
        ngx.log(ngx.WARN, "No Authorization header")
        ngx.exit(ngx.HTTP_UNAUTHORIZED)
    end

    local _, _, token = string.find(auth_header, "Bearer%s+(.+)")

    if token == nil then
        ngx.log(ngx.ERR, "Missing token")
        ngx.exit(ngx.HTTP_UNAUTHORIZED)
    end

    local jwt_obj = jwt:load_jwt(token, nil)
    jwt_obj = jwt:verify_jwt_obj(secret, jwt_obj)

    if jwt_obj.verified == false then
        ngx.log(ngx.ERR, "Invalid token: ".. jwt_obj.reason)
        ngx.status = ngx.HTTP_UNAUTHORIZED
        ngx.say(cjson.encode(jwt_obj))