annotate spec/util_jwt_spec.lua @ 12701:8e402a2ae1b8

util.jwt: Overhaul of tests to use declarative approach Now we can consistently apply the same tests to every algorithm, instead of duplicating code.
author Matthew Wild <mwild1@gmail.com>
date Sat, 02 Jul 2022 14:22:20 +0100
parents 899c057781cd
children 31a2bd84191d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10660
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local jwt = require "util.jwt";
12700
899c057781cd spec: Move test crypto keys to a shared file for clarity and easy maintenance
Matthew Wild <mwild1@gmail.com>
parents: 12699
diff changeset
2 local test_keys = require "spec.inputs.test_keys";
10660
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 describe("util.jwt", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 it("validates", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local key = "secret";
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local token = jwt.sign(key, { payload = "this" });
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 assert.string(token);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local ok, parsed = jwt.verify(key, token);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 assert.truthy(ok)
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 assert.same({ payload = "this" }, parsed);
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
12
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
13
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
14
10660
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 it("rejects invalid", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local key = "secret";
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local token = jwt.sign("wrong", { payload = "this" });
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 assert.string(token);
10661
4eee1aaa9405 util.jwt: Remove unused return value from tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10660
diff changeset
20 local ok = jwt.verify(key, token);
10660
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 assert.falsy(ok)
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end);
12696
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10661
diff changeset
23
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
24 local test_cases = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
25 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
26 algorithm = "HS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
27 keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
28 { "your-256-bit-secret", "your-256-bit-secret" };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
29 { "another-secret", "another-secret" };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
30 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
31 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
32 name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
33 [[eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
34 { -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
35 sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
36 name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
37 iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
38 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
39 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
40 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
41 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
42 algorithm = "ES256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
43 keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
44 { test_keys.ecdsa_private_pem, test_keys.ecdsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
45 { test_keys.alt_ecdsa_private_pem, test_keys.alt_ecdsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
46 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
47 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
48 name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
49 [[eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.tyh-VfuzIxCyGYDlkBA7DfyjrqmSHu6pQ2hoZuFqUSLPNY2N0mpHb3nk5K17HWP_3cYHBw7AhHale5wky6-sVA]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
50 { -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
51 sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
52 name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
53 admin = true;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
54 iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
55 };
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
56 };
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
57 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
58 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
59 algorithm = "RS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
60 keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
61 { test_keys.rsa_private_pem, test_keys.rsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
62 { test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
63 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
64 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
65 name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
66 [[eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
67 { -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
68 sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
69 name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
70 admin = true;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
71 iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
72 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
73 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
74 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
75 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
76 algorithm = "PS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
77 keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
78 { test_keys.rsa_private_pem, test_keys.rsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
79 { test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
80 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
81 {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
82 name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
83 [[eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.iOeNU4dAFFeBwNj6qdhdvm-IvDQrTa6R22lQVJVuWJxorJfeQww5Nwsra0PjaOYhAMj9jNMO5YLmud8U7iQ5gJK2zYyepeSuXhfSi8yjFZfRiSkelqSkU19I-Ja8aQBDbqXf2SAWA8mHF8VS3F08rgEaLCyv98fLLH4vSvsJGf6ueZSLKDVXz24rZRXGWtYYk_OYYTVgR1cg0BLCsuCvqZvHleImJKiWmtS0-CymMO4MMjCy_FIl6I56NqLE9C87tUVpo1mT-kbg5cHDD8I7MjCW5Iii5dethB4Vid3mZ6emKjVYgXrtkOQ-JyGMh6fnQxEFN1ft33GX2eRHluK9eg]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
84 { -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
85 sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
86 name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
87 admin = true;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
88 iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
89 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
90 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
91 };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
92 };
12696
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10661
diff changeset
93
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
94 local function do_verify_test(algorithm, verifying_key, token, expect_payload)
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
95 local verify = jwt.new_verifier(algorithm, verifying_key);
12696
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10661
diff changeset
96
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
97 assert.is_string(token);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
98 local result = {verify(token)};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
99 if expect_payload then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
100 assert.same({
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
101 true; -- success
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
102 expect_payload; -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
103 }, result);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
104 else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
105 assert.same({
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
106 false;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
107 "signature-mismatch";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
108 }, result);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
109 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
110 end
12696
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10661
diff changeset
111
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
112 local function do_sign_verify_test(algorithm, signing_key, verifying_key, expect_success, expect_token)
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
113 local sign = jwt.new_signer(algorithm, signing_key);
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
114
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
115 local test_payload = {
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
116 sub = "1234567890";
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
117 name = "John Doe";
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
118 admin = true;
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
119 iat = 1516239022;
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
120 };
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
121
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
122 local token = sign(test_payload);
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
123
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
124 if expect_token then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
125 assert.equal(expect_token, token);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
126 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
127
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
128 do_verify_test(algorithm, verifying_key, token, expect_success and test_payload or false);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
129 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
130
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
131
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
132
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
133 for _, algorithm_tests in ipairs(test_cases) do
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
134 local algorithm = algorithm_tests.algorithm;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
135 local keypairs = algorithm_tests.keys;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
136 describe(algorithm, function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
137 it("can do basic sign and verify", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
138 for _, keypair in ipairs(keypairs) do
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
139 local signing_key, verifying_key = keypair[1], keypair[2];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
140 do_sign_verify_test(algorithm, keypair[1], keypair[2], true);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
141 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
142 end);
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
143
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
144 if #keypairs >= 2 then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
145 it("rejects invalid tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
146 do_sign_verify_test(algorithm, keypairs[1][1], keypairs[2][2], false);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
147 end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
148 else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
149 pending("rejects invalid tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
150 error("Needs at least 2 key pairs");
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
151 end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
152 end
12699
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12696
diff changeset
153
12701
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
154 if #algorithm_tests > 0 then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
155 for test_n, test_case in ipairs(algorithm_tests) do
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
156 it("can verify "..(test_case.name or (("test case %d"):format(test_n))), function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
157 do_verify_test(
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
158 algorithm,
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
159 test_case.verifying_key or keypairs[1][2],
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
160 test_case[1],
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
161 test_case[2]
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
162 );
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
163 end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
164 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
165 else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
166 pending("can verify reference tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
167 error("No test tokens provided");
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
168 end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
169 end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
170 end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
171 end
10660
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 end);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173