spec/util_jwt_spec.lua
author Kim Alvefur <zash@zash.se>
Sun, 24 Mar 2024 20:39:42 +0100
changeset 13466 720aed1f5cf2
parent 12740 ad4ab01f9b11
permissions -rw-r--r--
util.startup: Check root after detecting platform and reading config (thanks SigmaTel71) Ensures that startup.detect_platform() runs so know whether to use the POSIX method of checking the current user or something else. Also after reading the config so we know whether the root override setting is set.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10664
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";
12704
899c057781cd spec: Move test crypto keys to a shared file for clarity and easy maintenance
Matthew Wild <mwild1@gmail.com>
parents: 12703
diff changeset
     2
local test_keys = require "spec.inputs.test_keys";
10664
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     4
local array = require "util.array";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     5
local iter = require "util.iterators";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     6
local set = require "util.set";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     7
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     8
-- Ignore long lines. We have some long tokens embedded here.
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
     9
--luacheck: ignore 631
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    10
10664
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
describe("util.jwt", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
	it("validates", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
		local key = "secret";
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		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
    15
		assert.string(token);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
		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
    17
		assert.truthy(ok)
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
		assert.same({ payload = "this" }, parsed);
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
    19
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
    20
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
    21
10664
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
	end);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	it("rejects invalid", function ()
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
		local key = "secret";
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
		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
    26
		assert.string(token);
10665
4eee1aaa9405 util.jwt: Remove unused return value from tests [luacheck]
Kim Alvefur <zash@zash.se>
parents: 10664
diff changeset
    27
		local ok = jwt.verify(key, token);
10664
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
		assert.falsy(ok)
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
	end);
12700
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10665
diff changeset
    30
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    31
	local function jwt_reference_token(token)
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    32
		return {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    33
			name = "jwt.io reference";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    34
			token;
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    35
			{     -- payload
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    36
				sub = "1234567890";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    37
				name = "John Doe";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    38
				admin = true;
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    39
				iat = 1516239022;
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    40
			};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    41
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    42
	end
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    43
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    44
	local untested_algorithms = set.new(array.collect(iter.keys(jwt._algorithms)));
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    45
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    46
	local test_cases = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    47
		{
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    48
			algorithm = "HS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    49
			keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    50
				{ "your-256-bit-secret", "your-256-bit-secret" };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    51
				{ "another-secret", "another-secret" };
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    52
			};
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    53
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    54
			jwt_reference_token [[eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJhZG1pbiI6dHJ1ZX0.F-cvL2RcfQhUtCavIM7q7zYE8drmj2LJk0JRkrS6He4]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    55
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    56
		{
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    57
			algorithm = "HS384";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    58
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    59
				{ "your-384-bit-secret", "your-384-bit-secret" };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    60
				{ "another-secret", "another-secret" };
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    61
			};
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    62
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    63
			jwt_reference_token [[eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.bQTnz6AuMJvmXXQsVPrxeQNvzDkimo7VNXxHeSBfClLufmCVZRUuyTwJF311JHuh]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    64
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    65
		{
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    66
			algorithm = "HS512";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    67
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    68
				{ "your-512-bit-secret", "your-512-bit-secret" };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    69
				{ "another-secret", "another-secret" };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    70
			};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    71
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
    72
			jwt_reference_token [[eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.VFb0qJ1LRg_4ujbZoRMXnVkUgiuKq5KxWqNdbKq_G9Vvz-S1zZa9LPxtHWKa64zDl2ofkT8F6jBt_K4riU-fPg]];
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    73
		};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    74
		{
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    75
			algorithm = "ES256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    76
			keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    77
				{ 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: 12704
diff changeset
    78
				{ 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: 12704
diff changeset
    79
			};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    80
			{
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    81
				name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    82
				[[eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.tyh-VfuzIxCyGYDlkBA7DfyjrqmSHu6pQ2hoZuFqUSLPNY2N0mpHb3nk5K17HWP_3cYHBw7AhHale5wky6-sVA]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    83
				{     -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    84
					sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    85
					name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    86
					admin = true;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    87
					iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    88
				};
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
    89
			};
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    90
		};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
    91
		{
12740
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    92
			algorithm = "ES512";
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    93
			keys = {
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    94
				{ test_keys.ecdsa_521_private_pem, test_keys.ecdsa_521_public_pem };
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    95
				{ test_keys.alt_ecdsa_521_private_pem, test_keys.alt_ecdsa_521_public_pem };
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    96
			};
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    97
			{
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    98
				name = "jwt.io reference";
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
    99
				[[eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.AbVUinMiT3J_03je8WTOIl-VdggzvoFgnOsdouAs-DLOtQzau9valrq-S6pETyi9Q18HH-EuwX49Q7m3KC0GuNBJAc9Tksulgsdq8GqwIqZqDKmG7hNmDzaQG1Dpdezn2qzv-otf3ZZe-qNOXUMRImGekfQFIuH_MjD2e8RZyww6lbZk]];
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   100
				{     -- payload
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   101
					sub = "1234567890";
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   102
					name = "John Doe";
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   103
					admin = true;
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   104
					iat = 1516239022;
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   105
				};
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   106
			};
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   107
		};
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   108
		{
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   109
			algorithm = "RS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   110
			keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   111
				{ 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: 12704
diff changeset
   112
				{ 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: 12704
diff changeset
   113
			};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   114
			{
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   115
				name = "jwt.io reference";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   116
				[[eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.NHVaYe26MbtOYhSKkoKYdFVomg4i8ZJd8_-RU8VNbftc4TSMb4bXP3l3YlNWACwyXPGffz5aXHc6lty1Y2t4SWRqGteragsVdZufDn5BlnJl9pdR_kdVFUsra2rWKEofkZeIC4yWytE58sMIihvo9H1ScmmVwBcQP6XETqYd0aSHp1gOa9RdUPDvoXQ5oqygTqVtxaDr6wUFKrKItgBMzWIdNZ6y7O9E0DhEPTbE9rfBo6KTFsHAZnMg4k68CDp2woYIaXbmYTWcvbzIuHO7_37GT79XdIwkm95QJ7hYC9RiwrV7mesbY4PAahERJawntho0my942XheVLmGwLMBkQ]];
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   117
				{     -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   118
					sub = "1234567890";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   119
					name = "John Doe";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   120
					admin = true;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   121
					iat = 1516239022;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   122
				};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   123
			};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   124
		};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   125
		{
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   126
			algorithm = "RS384";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   127
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   128
				{ test_keys.rsa_private_pem, test_keys.rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   129
				{ test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   130
			};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   131
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   132
			jwt_reference_token [[eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.o1hC1xYbJolSyh0-bOY230w22zEQSk5TiBfc-OCvtpI2JtYlW-23-8B48NpATozzMHn0j3rE0xVUldxShzy0xeJ7vYAccVXu2Gs9rnTVqouc-UZu_wJHkZiKBL67j8_61L6SXswzPAQu4kVDwAefGf5hyYBUM-80vYZwWPEpLI8K4yCBsF6I9N1yQaZAJmkMp_Iw371Menae4Mp4JusvBJS-s6LrmG2QbiZaFaxVJiW8KlUkWyUCns8-qFl5OMeYlgGFsyvvSHvXCzQrsEXqyCdS4tQJd73ayYA4SPtCb9clz76N1zE5WsV4Z0BYrxeb77oA7jJhh994RAPzCG0hmQ]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   133
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   134
		{
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   135
			algorithm = "RS512";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   136
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   137
				{ test_keys.rsa_private_pem, test_keys.rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   138
				{ test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   139
			};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   140
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   141
			jwt_reference_token [[eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.jYW04zLDHfR1v7xdrW3lCGZrMIsVe0vWCfVkN2DRns2c3MN-mcp_-RE6TN9umSBYoNV-mnb31wFf8iun3fB6aDS6m_OXAiURVEKrPFNGlR38JSHUtsFzqTOj-wFrJZN4RwvZnNGSMvK3wzzUriZqmiNLsG8lktlEn6KA4kYVaM61_NpmPHWAjGExWv7cjHYupcjMSmR8uMTwN5UuAwgW6FRstCJEfoxwb0WKiyoaSlDuIiHZJ0cyGhhEmmAPiCwtPAwGeaL1yZMcp0p82cpTQ5Qb-7CtRov3N4DcOHgWYk6LomPR5j5cCkePAz87duqyzSMpCB0mCOuE3CU2VMtGeQ]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   142
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   143
		{
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   144
			algorithm = "PS256";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   145
			keys = {
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   146
				{ 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: 12704
diff changeset
   147
				{ 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: 12704
diff changeset
   148
			};
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   149
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   150
			jwt_reference_token [[eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.iOeNU4dAFFeBwNj6qdhdvm-IvDQrTa6R22lQVJVuWJxorJfeQww5Nwsra0PjaOYhAMj9jNMO5YLmud8U7iQ5gJK2zYyepeSuXhfSi8yjFZfRiSkelqSkU19I-Ja8aQBDbqXf2SAWA8mHF8VS3F08rgEaLCyv98fLLH4vSvsJGf6ueZSLKDVXz24rZRXGWtYYk_OYYTVgR1cg0BLCsuCvqZvHleImJKiWmtS0-CymMO4MMjCy_FIl6I56NqLE9C87tUVpo1mT-kbg5cHDD8I7MjCW5Iii5dethB4Vid3mZ6emKjVYgXrtkOQ-JyGMh6fnQxEFN1ft33GX2eRHluK9eg]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   151
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   152
		{
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   153
			algorithm = "PS384";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   154
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   155
				{ test_keys.rsa_private_pem, test_keys.rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   156
				{ test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   157
			};
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   158
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   159
			jwt_reference_token [[eyJhbGciOiJQUzM4NCIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.Lfe_aCQme_gQpUk9-6l9qesu0QYZtfdzfy08w8uqqPH_gnw-IVyQwyGLBHPFBJHMbifdSMxPjJjkCD0laIclhnBhowILu6k66_5Y2z78GHg8YjKocAvB-wSUiBhuV6hXVxE5emSjhfVz2OwiCk2bfk2hziRpkdMvfcITkCx9dmxHU6qcEIsTTHuH020UcGayB1-IoimnjTdCsV1y4CMr_ECDjBrqMdnontkqKRIM1dtmgYFsJM6xm7ewi_ksG_qZHhaoBkxQ9wq9OVQRGiSZYowCp73d2BF3jYMhdmv2JiaUz5jRvv6lVU7Quq6ylVAlSPxeov9voYHO1mgZFCY1kQ]];
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   160
		};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   161
		{
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   162
			algorithm = "PS512";
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   163
			keys = {
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   164
				{ test_keys.rsa_private_pem, test_keys.rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   165
				{ test_keys.alt_rsa_private_pem, test_keys.alt_rsa_public_pem };
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   166
			};
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   167
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   168
			jwt_reference_token [[eyJhbGciOiJQUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.J5W09-rNx0pt5_HBiydR-vOluS6oD-RpYNa8PVWwMcBDQSXiw6-EPW8iSsalXPspGj3ouQjAnOP_4-zrlUUlvUIt2T79XyNeiKuooyIFvka3Y5NnGiOUBHWvWcWp4RcQFMBrZkHtJM23sB5D7Wxjx0-HFeNk-Y3UJgeJVhg5NaWXypLkC4y0ADrUBfGAxhvGdRdULZivfvzuVtv6AzW6NRuEE6DM9xpoWX_4here-yvLS2YPiBTZ8xbB3axdM99LhES-n52lVkiX5AWg2JJkEROZzLMpaacA_xlbUz_zbIaOaoqk8gB5oO7kI6sZej3QAdGigQy-hXiRnW_L98d4GQ]];
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   169
		};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   170
	};
12700
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10665
diff changeset
   171
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   172
	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: 12704
diff changeset
   173
		local verify = jwt.new_verifier(algorithm, verifying_key);
12700
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10665
diff changeset
   174
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   175
		assert.is_string(token);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   176
		local result = {verify(token)};
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   177
		if expect_payload then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   178
			assert.same({
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   179
				true; -- success
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   180
				expect_payload; -- payload
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   181
			}, result);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   182
		else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   183
			assert.same({
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   184
				false;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   185
				"signature-mismatch";
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   186
			}, result);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   187
		end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   188
	end
12700
27a72982e331 util.jwt: Add support/tests for ES256 via improved API and using util.crypto
Matthew Wild <mwild1@gmail.com>
parents: 10665
diff changeset
   189
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   190
	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: 12704
diff changeset
   191
		local sign = jwt.new_signer(algorithm, signing_key);
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   192
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   193
		local test_payload = {
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   194
			sub = "1234567890";
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   195
			name = "John Doe";
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   196
			admin = true;
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   197
			iat = 1516239022;
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   198
		};
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   199
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   200
		local token = sign(test_payload);
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   201
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   202
		if expect_token then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   203
			assert.equal(expect_token, token);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   204
		end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   205
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   206
		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: 12704
diff changeset
   207
	end
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   208
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   209
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   210
	for _, algorithm_tests in ipairs(test_cases) do
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   211
		local algorithm = algorithm_tests.algorithm;
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   212
		local keypairs = algorithm_tests.keys;
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   213
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   214
		untested_algorithms:remove(algorithm);
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   215
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   216
		describe(algorithm, function ()
12740
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   217
			describe("can do basic sign and verify", function ()
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   218
				for keypair_n, keypair in ipairs(keypairs) do
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   219
					local signing_key, verifying_key = keypair[1], keypair[2];
12740
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   220
					it(("(test key pair %d)"):format(keypair_n), function ()
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   221
						do_sign_verify_test(algorithm, signing_key, verifying_key, true);
ad4ab01f9b11 util.jwt: Add support for ES512 (+ tests)
Matthew Wild <mwild1@gmail.com>
parents: 12708
diff changeset
   222
					end);
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   223
				end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   224
			end);
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   225
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   226
			if #keypairs >= 2 then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   227
				it("rejects invalid tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   228
					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: 12704
diff changeset
   229
				end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   230
			else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   231
				pending("rejects invalid tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   232
					error("Needs at least 2 key pairs");
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   233
				end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   234
			end
12703
b3d0c1457584 util.jwt: Add support for RSA-based algorithms (RS256, PS256)
Matthew Wild <mwild1@gmail.com>
parents: 12700
diff changeset
   235
12705
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   236
			if #algorithm_tests > 0 then
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   237
				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: 12704
diff changeset
   238
					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: 12704
diff changeset
   239
						do_verify_test(
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   240
							algorithm,
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   241
							test_case.verifying_key or keypairs[1][2],
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   242
							test_case[1],
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   243
							test_case[2]
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   244
						);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   245
					end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   246
				end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   247
			else
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   248
				pending("can verify reference tokens", function ()
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   249
					error("No test tokens provided");
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   250
				end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   251
			end
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   252
		end);
8e402a2ae1b8 util.jwt: Overhaul of tests to use declarative approach
Matthew Wild <mwild1@gmail.com>
parents: 12704
diff changeset
   253
	end
12708
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   254
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   255
	for algorithm in untested_algorithms do
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   256
		pending(algorithm.." tests", function () end);
31a2bd84191d util.jwt: All the algorithms (+ all the tests!)
Matthew Wild <mwild1@gmail.com>
parents: 12705
diff changeset
   257
	end
10664
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   258
end);
c4ded3be7cc0 util.jwt: Basic JSON Web Token library supporting HS256 tokens
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   259