Diff

spec/util_jwt_spec.lua @ 10660:c4ded3be7cc0

util.jwt: Basic JSON Web Token library supporting HS256 tokens
author Kim Alvefur <zash@zash.se>
date Mon, 24 Feb 2020 01:24:25 +0100
child 10661:4eee1aaa9405
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/util_jwt_spec.lua	Mon Feb 24 01:24:25 2020 +0100
@@ -0,0 +1,20 @@
+local jwt = require "util.jwt";
+
+describe("util.jwt", function ()
+	it("validates", function ()
+		local key = "secret";
+		local token = jwt.sign(key, { payload = "this" });
+		assert.string(token);
+		local ok, parsed = jwt.verify(key, token);
+		assert.truthy(ok)
+		assert.same({ payload = "this" }, parsed);
+	end);
+	it("rejects invalid", function ()
+		local key = "secret";
+		local token = jwt.sign("wrong", { payload = "this" });
+		assert.string(token);
+		local ok, err = jwt.verify(key, token);
+		assert.falsy(ok)
+	end);
+end);
+