Skip to content

Commit a19ee21

Browse files
committed
fix web encoding, fix validation of signatures when no signature present
1 parent 72c94a0 commit a19ee21

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

jwt-0.2-0.rockspec renamed to jwt-0.2-1.rockspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "jwt"
2-
version = "0.2-0"
2+
version = "0.2-1"
33
source = {
44
url = "https://github.com/Olivine-Labs/lua-jwt/archive/v0.2.tar.gz",
55
dir = "lua-jwt-0.2"

spec/jwt_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ describe("JWT spec", function()
5858
assert.are.same(claims, decodedClaims)
5959
end)
6060

61+
it("it cannot decode a token without a signature but with a key specified", function()
62+
local claims = {
63+
test = "test",
64+
}
65+
local keyPair = crypto.pkey.generate("rsa", 512)
66+
local token, err = jwt.encode(claims, {alg = "RS256"})
67+
local decodedClaims = jwt.decode(token, {keys = {public = keyPair}})
68+
assert.are.same(decodedClaims, nil)
69+
end)
70+
6171
it("it cannot encode/decode a signed plain text token with alg=RS256 and an incorrect key", function()
6272
local claims = {
6373
test = "test",
@@ -68,6 +78,7 @@ describe("JWT spec", function()
6878
local decodedClaims = jwt.decode(token, {keys = {public = badPair}})
6979
assert.has_error(function() assert.are.same(claims, decodedClaims) end)
7080
end)
81+
7182
it("can verify a signature", function()
7283
local token = "eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJhOGZjZTFkZi1iNGFlLTRjNDEtYmFjNi1iNWJiZjI4MGMyOWMiLCJzdWIiOiI3YmZjNThlYy03N2RkLTQ4NzQtYmViZC1iYTg0MTAzMDEyNzkiLCJzY29wZSI6WyJvYXV0aC5hcHByb3ZhbHMiLCJvcGVuaWQiXSwiY2xpZW50X2lkIjoibG9naW4iLCJjaWQiOiJsb2dpbiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiI3YmZjNThlYy03N2RkLTQ4NzQtYmViZC1iYTg0MTAzMDEyNzkiLCJ1c2VyX25hbWUiOiJhZG1pbkBmb3Jpby5jb20iLCJlbWFpbCI6ImFkbWluQGZvcmlvLmNvbSIsImlhdCI6MTM5ODkwNjcyNywiZXhwIjoxMzk4OTQ5OTI3LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0Ojk3NjMvdWFhL29hdXRoL3Rva2VuIiwiYXVkIjpbIm9hdXRoIiwib3BlbmlkIl19.xOa5ZpXksgoaA_XJ3yHMjlLcbSoM6XJy-e60zfyP7bRmu0EKEGZdZrl2iJVh6OTIn8z6UuvcY282C1A5LtRgpir4wqhIrphd-Mi9gfxra0pJvtydd4XqVpuNdW7GDaC43VXpvUtetmfn-YAo2jkD9G22mUuT2sFdt5NqFL7Rk4tVRILes73OWxfQpuoReWvRBik-sJXxC9ADmTuzR36OvomIrso42R8aufU2ku_zPve8IhYLvn3vHmYCt0zNZkX-jSV8YtGodr9V-dKs9na41YvGp2UxkBcV7LKoGSRELSSNJ8JLF-bjO3zYSSbT42-yeHeKfoWAeP6R7S_0c_AYRA"
7384
local key = [[-----BEGIN PUBLIC KEY-----

src/jwt.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function data.encode(claims, options)
4545
local header = header(options)
4646
local token, err = jwt:encode(header, claims, options)
4747
if not token then return nil, err end
48-
return token
48+
return token:gsub('+','-'):gsub('/','_')
4949
end
5050

5151
function data.decode(str, options)

src/jwt/jws.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ function data:encode(header, claims, options)
2525
if not options then error("options are required") end
2626
local claims = json.encode(claims)
2727
local envelope = basexx.to_base64(json.encode(header)).."."..basexx.to_base64(claims)
28-
local signature, err = self.sign[header.alg](envelope, options.keys.private)
29-
if not signature then return nil, err end
30-
return envelope .. "." .. basexx.to_base64(signature)
28+
local signature
29+
if options.keys then
30+
local err
31+
signature, err = self.sign[header.alg](envelope, options.keys.private)
32+
if not signature then return nil, err end
33+
end
34+
return envelope .. "." .. (signature and basexx.to_base64(signature) or "")
3135
end
3236

3337
function data:decode(header, str, options)
@@ -43,6 +47,9 @@ function data:decode(header, str, options)
4347
signature = basexx.from_base64(str:sub(dotSecond+1))
4448
end
4549
else
50+
if options and options.keys and options.keys.public then
51+
return nil, "Invalid token"
52+
end
4653
bodyStr = str
4754
end
4855

0 commit comments

Comments
 (0)