Skip to content

Commit

Permalink
Added type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
x25 committed Jul 17, 2014
1 parent 36c3fb4 commit 510eabc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
6 changes: 3 additions & 3 deletions example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local function t2s(o)
end

--
local JWT = require "luajwt"
local jwt = require "luajwt"

local key = "example_key"

Expand All @@ -26,11 +26,11 @@ local claim = {
}

local alg = "HS256" -- default alg
local token, err = JWT.encode(claim, key, alg)
local token, err = jwt.encode(claim, key, alg)

print("Token:", token)

local validate = true -- validate exp and nbf (default: true)
local decoded, err = JWT.decode(token, key, validate)
local decoded, err = jwt.decode(token, key, validate)

print("Claim:", t2s(decoded) )
4 changes: 2 additions & 2 deletions luajwt-1.3-1.rockspec → luajwt-1.3-2.rockspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package = "luajwt"
version = "1.3-1"
version = "1.3-2"

source = {
url = "git://github.com/x25/luajwt",
Expand All @@ -8,7 +8,7 @@ source = {

description = {
summary = "JSON Web Tokens for Lua",
detailed = "Very fast and compatible with pyjwt, php-jwt and ruby-jwt",
detailed = "Very fast and compatible with pyjwt, php-jwt, ruby-jwt, node-jwt-simple and others",
homepage = "https://github.com/x25/luajwt",
license = "MIT <http://opensource.org/licenses/MIT>"
}
Expand Down
22 changes: 19 additions & 3 deletions luajwt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,23 @@ function M.decode(data, key, verify)

if verify then

if not header.alg or not alg_verify[header.alg] then
if not header.typ or header.typ ~= "JWT" then
return nil, "Invalid typ"
end

if not header.alg or type(header.alg) ~= "string" then
return nil, "Invalid alg"
end

if body.exp and type(body.exp) ~= "number" then
return nil, "exp must be number"
end

if body.nbf and type(body.nbf) ~= "number" then
return nil, "nbf must be number"
end

if not alg_verify[header.alg] then
return nil, "Algorithm not supported"
end

Expand All @@ -120,11 +136,11 @@ function M.decode(data, key, verify)
end

if body.exp and os.time() >= body.exp then
return nil, "Invalid exp"
return nil, "Not acceptable by exp"
end

if body.nbf and os.time() < body.nbf then
return nil, "Invalid nbf"
return nil, "Not acceptable by nbf"
end
end

Expand Down

0 comments on commit 510eabc

Please sign in to comment.