Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added token date checks & signature is now forced to lowercase hex before validation. #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/jwt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local meta = {}
local data = {}

local json = require 'cjson'
local json_safe = require 'cjson.safe'
json.encode_empty_table('array')
local basexx = require 'basexx'
local jws = require 'jwt.jws'
Expand Down Expand Up @@ -55,7 +56,8 @@ function data.decode(str, options)
if not str then return nil, "Parameter 1 cannot be nil" end
local dotFirst = str:find("%.")
if not dotFirst then return nil, "Invalid token" end
local header = json.decode((basexx.from_url64(str:sub(1,dotFirst-1))))
local header, err = json_safe.decode((basexx.from_url64(str:sub(1,dotFirst-1))))
if (header == nil) then return nil, err end

return getJwt(header):decode(header, str, options)
end
Expand Down
20 changes: 17 additions & 3 deletions src/jwt/jws.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,26 @@ function data:decode(header, str, options)

local message = basexx.from_url64(bodyStr)
if signature then
if not self.verify[header.alg](rawHeader.."."..bodyStr, signature, options.keys.public) then
return nil, "Invalid token"
if not self.verify[header.alg](rawHeader.."."..bodyStr, string.lower(basexx.to_hex(signature)), options.keys.public) then
return nil, "Token Invalid"
end
end

local decoded_message = json.decode(message)

if decoded_message.nbf ~= nil then
if math.floor(ngx.now()) < decoded_message.nbf then
return nil, "Token not yet valid"
end
end

if decoded_message.exp ~= nil then
if math.floor(ngx.now()) > decoded_message.exp then
return nil, "Token expired"
end
end

return json.decode(message)
return decoded_message
end

return data