@nivinjoseph/n-sec 5.0.5 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +111 -0
- package/.yarn/releases/yarn-4.0.2.cjs +893 -0
- package/.yarnrc.yml +3 -0
- package/dist/api-security/alg-type.d.ts +1 -0
- package/dist/api-security/alg-type.d.ts.map +1 -0
- package/dist/api-security/alg-type.js +2 -5
- package/dist/api-security/alg-type.js.map +1 -1
- package/dist/api-security/claim.d.ts +1 -0
- package/dist/api-security/claim.d.ts.map +1 -0
- package/dist/api-security/claim.js +5 -9
- package/dist/api-security/claim.js.map +1 -1
- package/dist/api-security/claims-identity.d.ts +2 -1
- package/dist/api-security/claims-identity.d.ts.map +1 -0
- package/dist/api-security/claims-identity.js +4 -8
- package/dist/api-security/claims-identity.js.map +1 -1
- package/dist/api-security/expired-token-exception.d.ts +1 -0
- package/dist/api-security/expired-token-exception.d.ts.map +1 -0
- package/dist/api-security/expired-token-exception.js +5 -9
- package/dist/api-security/expired-token-exception.js.map +1 -1
- package/dist/api-security/invalid-token-exception.d.ts +1 -0
- package/dist/api-security/invalid-token-exception.d.ts.map +1 -0
- package/dist/api-security/invalid-token-exception.js +7 -11
- package/dist/api-security/invalid-token-exception.js.map +1 -1
- package/dist/api-security/json-web-token.d.ts +3 -2
- package/dist/api-security/json-web-token.d.ts.map +1 -0
- package/dist/api-security/json-web-token.js +39 -43
- package/dist/api-security/json-web-token.js.map +1 -1
- package/dist/api-security/security-token.d.ts +1 -0
- package/dist/api-security/security-token.d.ts.map +1 -0
- package/dist/api-security/security-token.js +6 -10
- package/dist/api-security/security-token.js.map +1 -1
- package/dist/bin.d.ts +1 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +16 -20
- package/dist/bin.js.map +1 -1
- package/dist/crypto/asymmetric-encryption.d.ts +2 -0
- package/dist/crypto/asymmetric-encryption.d.ts.map +1 -0
- package/dist/crypto/asymmetric-encryption.js +1 -1
- package/dist/crypto/asymmetric-encryption.js.map +1 -1
- package/dist/crypto/crypto-exception.d.ts +1 -0
- package/dist/crypto/crypto-exception.d.ts.map +1 -0
- package/dist/crypto/crypto-exception.js +2 -6
- package/dist/crypto/crypto-exception.js.map +1 -1
- package/dist/crypto/digital-signature.d.ts +2 -0
- package/dist/crypto/digital-signature.d.ts.map +1 -0
- package/dist/crypto/digital-signature.js +1 -1
- package/dist/crypto/digital-signature.js.map +1 -1
- package/dist/crypto/hash.d.ts +1 -0
- package/dist/crypto/hash.d.ts.map +1 -0
- package/dist/crypto/hash.js +7 -11
- package/dist/crypto/hash.js.map +1 -1
- package/dist/crypto/hmac.d.ts +1 -0
- package/dist/crypto/hmac.d.ts.map +1 -0
- package/dist/crypto/hmac.js +6 -10
- package/dist/crypto/hmac.js.map +1 -1
- package/dist/crypto/symmetric-encryption.d.ts +1 -0
- package/dist/crypto/symmetric-encryption.d.ts.map +1 -0
- package/dist/crypto/symmetric-encryption.js +13 -17
- package/dist/crypto/symmetric-encryption.js.map +1 -1
- package/dist/index.d.ts +13 -12
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -28
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.json +2 -1
- package/eslint.config.js +591 -0
- package/package.json +24 -24
- package/src/api-security/claims-identity.ts +1 -1
- package/src/api-security/json-web-token.ts +21 -21
- package/src/bin.ts +2 -1
- package/src/crypto/hash.ts +9 -9
- package/src/crypto/hmac.ts +7 -7
- package/src/crypto/symmetric-encryption.ts +11 -11
- package/src/index.ts +17 -18
- package/test/asymmetric-encryption.test.ts +37 -37
- package/test/digital-signature.test.ts +22 -22
- package/test/hash.test.ts +109 -109
- package/test/hmac.test.ts +56 -55
- package/test/json-web-token.test.ts +95 -95
- package/test/other.test.ts +8 -7
- package/test/symmetric-encryption.test.ts +49 -48
- package/tsconfig.json +8 -5
- package/.eslintignore +0 -2
- package/.eslintrc +0 -335
package/test/hmac.test.ts
CHANGED
|
@@ -1,79 +1,80 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { describe, test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { Hmac, SymmetricEncryption } from "./../src/index.js";
|
|
3
4
|
// import { CryptoException } from "./../src/crypto-exception";
|
|
4
5
|
import "@nivinjoseph/n-ext";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
await describe("Hmac", async () =>
|
|
7
8
|
{
|
|
8
|
-
|
|
9
|
+
await describe("create", async () =>
|
|
9
10
|
{
|
|
10
|
-
test("should return string value that is not null, empty, whitespace or same as the key or input", async () =>
|
|
11
|
+
await test("should return string value that is not null, empty, whitespace or same as the key or input", async () =>
|
|
11
12
|
{
|
|
12
13
|
const key = await SymmetricEncryption.generateKey();
|
|
13
14
|
const value = "hello world";
|
|
14
15
|
const hmac = Hmac.create(key, value);
|
|
15
16
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
assert.ok(hmac !== null && !hmac.isEmptyOrWhiteSpace());
|
|
18
|
+
assert.notStrictEqual(hmac, key);
|
|
19
|
+
assert.notStrictEqual(hmac, value);
|
|
19
20
|
});
|
|
20
|
-
|
|
21
|
-
test("multiple invocations with the same key and value must return the same output", async () =>
|
|
21
|
+
|
|
22
|
+
await test("multiple invocations with the same key and value must return the same output", async () =>
|
|
22
23
|
{
|
|
23
24
|
const key = await SymmetricEncryption.generateKey();
|
|
24
25
|
const value = "hello world";
|
|
25
26
|
const hmac1 = Hmac.create(key, value);
|
|
26
27
|
const hmac2 = Hmac.create(key, value);
|
|
27
|
-
|
|
28
|
+
assert.strictEqual(hmac1, hmac2);
|
|
28
29
|
});
|
|
29
|
-
|
|
30
|
-
test("multiple invocations with different keys and different values must return different outputs", async () =>
|
|
30
|
+
|
|
31
|
+
await test("multiple invocations with different keys and different values must return different outputs", async () =>
|
|
31
32
|
{
|
|
32
33
|
const key1 = await SymmetricEncryption.generateKey();
|
|
33
34
|
const value1 = "hello world";
|
|
34
35
|
const hmac1 = Hmac.create(key1, value1);
|
|
35
|
-
|
|
36
|
+
|
|
36
37
|
const key2 = await SymmetricEncryption.generateKey();
|
|
37
38
|
const value2 = "goodbye world";
|
|
38
39
|
const hmac2 = Hmac.create(key2, value2);
|
|
39
|
-
|
|
40
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
40
41
|
});
|
|
41
|
-
|
|
42
|
-
test("multiple invocations with the same key and different values must return different outputs", async () =>
|
|
42
|
+
|
|
43
|
+
await test("multiple invocations with the same key and different values must return different outputs", async () =>
|
|
43
44
|
{
|
|
44
45
|
const key = await SymmetricEncryption.generateKey();
|
|
45
46
|
const value1 = "hello world";
|
|
46
47
|
const value2 = "goodbye world";
|
|
47
48
|
const hmac1 = Hmac.create(key, value1);
|
|
48
49
|
const hmac2 = Hmac.create(key, value2);
|
|
49
|
-
|
|
50
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
50
51
|
});
|
|
51
|
-
|
|
52
|
-
test("multiple invocations with different keys and the same value must return different outputs", async () =>
|
|
52
|
+
|
|
53
|
+
await test("multiple invocations with different keys and the same value must return different outputs", async () =>
|
|
53
54
|
{
|
|
54
55
|
const key1 = await SymmetricEncryption.generateKey();
|
|
55
56
|
const key2 = await SymmetricEncryption.generateKey();
|
|
56
57
|
const value = "hello world";
|
|
57
58
|
const hmac1 = Hmac.create(key1, value);
|
|
58
59
|
const hmac2 = Hmac.create(key2, value);
|
|
59
|
-
|
|
60
|
+
assert.notStrictEqual(hmac1, hmac2);
|
|
60
61
|
});
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// test("should create same Hmacs for a given value and key", async () =>
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// await test("should create same Hmacs for a given value and key", async () =>
|
|
65
66
|
// {
|
|
66
67
|
// let key = await SymmetricEncryption.generateKey();
|
|
67
68
|
// let hmac1 = await Hmac.create(key, "some-string");
|
|
68
69
|
// let hmac2 = await Hmac.create(key, "some-string");
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
//
|
|
70
|
+
// assert.ok(hmac1 !== null && !hmac1.isEmptyOrWhiteSpace());
|
|
71
|
+
// assert.ok(hmac2 !== null && !hmac2.isEmptyOrWhiteSpace());
|
|
72
|
+
// assert.notStrictEqual(hmac1, "some-string");
|
|
73
|
+
// assert.notStrictEqual(hmac2, "some-string");
|
|
74
|
+
// assert.strictEqual(hmac1, hmac2);
|
|
74
75
|
// });
|
|
75
|
-
|
|
76
|
-
// test("should throw CryptoException when key is null", async () =>
|
|
76
|
+
|
|
77
|
+
// await test("should throw CryptoException when key is null", async () =>
|
|
77
78
|
// {
|
|
78
79
|
// try
|
|
79
80
|
// {
|
|
@@ -81,14 +82,14 @@ suite("Hmac", () =>
|
|
|
81
82
|
// }
|
|
82
83
|
// catch (exception)
|
|
83
84
|
// {
|
|
84
|
-
//
|
|
85
|
-
//
|
|
85
|
+
// assert.ok(exception instanceof CryptoException);
|
|
86
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
86
87
|
// return;
|
|
87
88
|
// }
|
|
88
|
-
//
|
|
89
|
+
// assert.ok(false);
|
|
89
90
|
// });
|
|
90
|
-
|
|
91
|
-
// test("should throw CryptoException when value is null", async () =>
|
|
91
|
+
|
|
92
|
+
// await test("should throw CryptoException when value is null", async () =>
|
|
92
93
|
// {
|
|
93
94
|
// try
|
|
94
95
|
// {
|
|
@@ -97,14 +98,14 @@ suite("Hmac", () =>
|
|
|
97
98
|
// }
|
|
98
99
|
// catch (exception)
|
|
99
100
|
// {
|
|
100
|
-
//
|
|
101
|
-
//
|
|
101
|
+
// assert.ok(exception instanceof CryptoException);
|
|
102
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
102
103
|
// return;
|
|
103
104
|
// }
|
|
104
|
-
//
|
|
105
|
+
// assert.ok(false);
|
|
105
106
|
// });
|
|
106
|
-
|
|
107
|
-
// test("should throw CryptoException when key is undefined", async () =>
|
|
107
|
+
|
|
108
|
+
// await test("should throw CryptoException when key is undefined", async () =>
|
|
108
109
|
// {
|
|
109
110
|
// try
|
|
110
111
|
// {
|
|
@@ -112,14 +113,14 @@ suite("Hmac", () =>
|
|
|
112
113
|
// }
|
|
113
114
|
// catch (exception)
|
|
114
115
|
// {
|
|
115
|
-
//
|
|
116
|
-
//
|
|
116
|
+
// assert.ok(exception instanceof CryptoException);
|
|
117
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
117
118
|
// return;
|
|
118
119
|
// }
|
|
119
|
-
//
|
|
120
|
+
// assert.ok(false);
|
|
120
121
|
// });
|
|
121
122
|
|
|
122
|
-
// test("should throw CryptoException when value is undefined", async () =>
|
|
123
|
+
// await test("should throw CryptoException when value is undefined", async () =>
|
|
123
124
|
// {
|
|
124
125
|
// try
|
|
125
126
|
// {
|
|
@@ -128,14 +129,14 @@ suite("Hmac", () =>
|
|
|
128
129
|
// }
|
|
129
130
|
// catch (exception)
|
|
130
131
|
// {
|
|
131
|
-
//
|
|
132
|
-
//
|
|
132
|
+
// assert.ok(exception instanceof CryptoException);
|
|
133
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
133
134
|
// return;
|
|
134
135
|
// }
|
|
135
|
-
//
|
|
136
|
+
// assert.ok(false);
|
|
136
137
|
// });
|
|
137
|
-
|
|
138
|
-
// test("should throw CryptoException when invalid key", async () =>
|
|
138
|
+
|
|
139
|
+
// await test("should throw CryptoException when invalid key", async () =>
|
|
139
140
|
// {
|
|
140
141
|
// try
|
|
141
142
|
// {
|
|
@@ -143,12 +144,12 @@ suite("Hmac", () =>
|
|
|
143
144
|
// }
|
|
144
145
|
// catch (exception)
|
|
145
146
|
// {
|
|
146
|
-
//
|
|
147
|
-
//
|
|
147
|
+
// assert.ok(exception instanceof CryptoException);
|
|
148
|
+
// assert.strictEqual(exception.message, "Parameter count mismatch.");
|
|
148
149
|
// return;
|
|
149
150
|
// }
|
|
150
|
-
//
|
|
151
|
+
// assert.ok(false);
|
|
151
152
|
// });
|
|
152
153
|
});
|
|
153
|
-
|
|
154
|
+
|
|
154
155
|
});
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import { describe, test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { JsonWebToken } from "./../src/api-security/json-web-token.js";
|
|
4
|
+
import { Claim } from "../src/api-security/claim.js";
|
|
5
|
+
import { SymmetricEncryption } from "../src/index.js";
|
|
6
|
+
import { InvalidTokenException } from "../src/api-security/invalid-token-exception.js";
|
|
7
|
+
import { ExpiredTokenException } from "../src/api-security/expired-token-exception.js";
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
await describe("Json Web Token ", async () =>
|
|
11
11
|
{
|
|
12
|
-
|
|
12
|
+
await describe("Hmac", async () =>
|
|
13
13
|
{
|
|
14
|
-
|
|
15
|
-
test("should successfully create a token using hmac with one claim", async () =>
|
|
14
|
+
|
|
15
|
+
await test("should successfully create a token using hmac with one claim", async () =>
|
|
16
16
|
{
|
|
17
17
|
const claim = new Claim("this_claim", "ThisValue");
|
|
18
18
|
const key = await SymmetricEncryption.generateKey();
|
|
@@ -20,14 +20,14 @@ suite("Json Web Token ", () =>
|
|
|
20
20
|
const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim]).generateToken();
|
|
21
21
|
const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
|
|
22
22
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
assert.ok(jwt !== null || jwt !== undefined);
|
|
24
|
+
assert.strictEqual(jwt.issuer, "issuer1");
|
|
25
|
+
assert.strictEqual(jwt.algType, 1);
|
|
26
|
+
assert.strictEqual(jwt.expiry, time + 10000000);
|
|
27
|
+
assert.deepStrictEqual(jwt.claims, [claim]);
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
test("should successfully create a token using hmac with 2 claims", async () =>
|
|
30
|
+
await test("should successfully create a token using hmac with 2 claims", async () =>
|
|
31
31
|
{
|
|
32
32
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
33
33
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -36,14 +36,14 @@ suite("Json Web Token ", () =>
|
|
|
36
36
|
const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
|
|
37
37
|
const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
|
|
38
38
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
assert.ok(jwt !== null || jwt !== undefined);
|
|
40
|
+
assert.strictEqual(jwt.issuer, "issuer1");
|
|
41
|
+
assert.strictEqual(jwt.algType, 1);
|
|
42
|
+
assert.strictEqual(jwt.expiry, time + 10000000);
|
|
43
|
+
assert.deepStrictEqual(jwt.claims, [claim1, claim2]);
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
test("should successfully create a token using hmac with 2 claims", async () =>
|
|
46
|
+
await test("should successfully create a token using hmac with 2 claims", async () =>
|
|
47
47
|
{
|
|
48
48
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
49
49
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -52,14 +52,14 @@ suite("Json Web Token ", () =>
|
|
|
52
52
|
const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
|
|
53
53
|
const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
|
|
54
54
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
assert.ok(jwt !== null || jwt !== undefined);
|
|
56
|
+
assert.strictEqual(jwt.issuer, "issuer1");
|
|
57
|
+
assert.strictEqual(jwt.algType, 1);
|
|
58
|
+
assert.strictEqual(jwt.expiry, time + 10000000);
|
|
59
|
+
assert.deepStrictEqual(jwt.claims, [claim1, claim2]);
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
test("should throw an exception when getting JWT with a different issuer that what was user to generate token", async () =>
|
|
62
|
+
await test("should throw an exception when getting JWT with a different issuer that what was user to generate token", async () =>
|
|
63
63
|
{
|
|
64
64
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
65
65
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -72,14 +72,14 @@ suite("Json Web Token ", () =>
|
|
|
72
72
|
}
|
|
73
73
|
catch (exp)
|
|
74
74
|
{
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
assert.ok(exp instanceof InvalidTokenException);
|
|
76
|
+
assert.equal(exp.message, `Token '${token}' is invalid because iss was expected to be 'notTheIssuer' but instead was 'issuer1'.`);
|
|
77
77
|
return;
|
|
78
78
|
}
|
|
79
|
-
|
|
79
|
+
assert.ok(false);
|
|
80
80
|
});
|
|
81
|
-
|
|
82
|
-
test("should throw an exception when getting JWT when the token is expired", async () =>
|
|
81
|
+
|
|
82
|
+
await test("should throw an exception when getting JWT when the token is expired", async () =>
|
|
83
83
|
{
|
|
84
84
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
85
85
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -92,15 +92,15 @@ suite("Json Web Token ", () =>
|
|
|
92
92
|
}
|
|
93
93
|
catch (exp)
|
|
94
94
|
{
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
assert.ok(exp instanceof ExpiredTokenException);
|
|
96
|
+
assert.equal(exp.message, `Token '${token}' is expired.`);
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
|
-
|
|
99
|
+
assert.ok(false);
|
|
100
100
|
});
|
|
101
|
-
|
|
101
|
+
|
|
102
102
|
// TODO: right now we only support one alg type. When we support others, we should uncomment this test
|
|
103
|
-
// test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
103
|
+
// await test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
104
104
|
// {
|
|
105
105
|
// let claim1 = new Claim("this_claim", "ThisValue");
|
|
106
106
|
// let claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -114,14 +114,14 @@ suite("Json Web Token ", () =>
|
|
|
114
114
|
// catch (exp)
|
|
115
115
|
// {
|
|
116
116
|
// console.log(exp);
|
|
117
|
-
//
|
|
118
|
-
//
|
|
117
|
+
// assert.ok(exp instanceof InvalidTokenException);
|
|
118
|
+
// assert.equal(exp.message, `Token '${token}' is invalid because alg was expected to be '${2}' but instead was '${1}'.`);
|
|
119
119
|
// return;
|
|
120
120
|
// }
|
|
121
|
-
//
|
|
121
|
+
// assert.ok(false);
|
|
122
122
|
// });
|
|
123
|
-
|
|
124
|
-
test("should throw an exception when getting JWT key given is different than what was used for the token generation", async () =>
|
|
123
|
+
|
|
124
|
+
await test("should throw an exception when getting JWT key given is different than what was used for the token generation", async () =>
|
|
125
125
|
{
|
|
126
126
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
127
127
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -135,14 +135,14 @@ suite("Json Web Token ", () =>
|
|
|
135
135
|
}
|
|
136
136
|
catch (exp)
|
|
137
137
|
{
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
assert.ok(exp instanceof InvalidTokenException);
|
|
139
|
+
assert.equal(exp.message, `Token '${token}' is invalid because signature could not be verified.`);
|
|
140
140
|
return;
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
assert.ok(false);
|
|
143
143
|
});
|
|
144
|
-
|
|
145
|
-
test("should throw an exception when getting JWT when the token is tampered with", async () =>
|
|
144
|
+
|
|
145
|
+
await test("should throw an exception when getting JWT when the token is tampered with", async () =>
|
|
146
146
|
{
|
|
147
147
|
const claim1 = new Claim("this_claim", "ThisValue");
|
|
148
148
|
const claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -156,31 +156,31 @@ suite("Json Web Token ", () =>
|
|
|
156
156
|
}
|
|
157
157
|
catch (exp)
|
|
158
158
|
{
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
assert.ok(exp instanceof InvalidTokenException);
|
|
160
|
+
assert.equal(exp.message, `Token '${token}' is invalid because signature could not be verified.`);
|
|
161
161
|
return;
|
|
162
162
|
}
|
|
163
|
-
|
|
163
|
+
assert.ok(false);
|
|
164
164
|
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
//
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// await describe("digital Signature", () =>
|
|
168
168
|
// {
|
|
169
|
-
// test("should successfully create a token using keyPair using digital Signature with one claim and get jwt using keyPair", async () =>
|
|
169
|
+
// await test("should successfully create a token using keyPair using digital Signature with one claim and get jwt using keyPair", async () =>
|
|
170
170
|
// {
|
|
171
171
|
// let keyPair = await AsymmetricEncryption.generateKeyPair();
|
|
172
172
|
// let claim = new Claim("this_claim", "ThisValue");
|
|
173
173
|
// let time = Date.now();
|
|
174
174
|
// let token = await JsonWebToken.fromClaims("issuer1", 2, keyPair, time + 10000000, [claim]).generateToken();
|
|
175
175
|
// let jwt = await JsonWebToken.fromToken("issuer1", 2, keyPair, token);
|
|
176
|
-
//
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
//
|
|
176
|
+
// assert.ok(jwt !== null || jwt !== undefined);
|
|
177
|
+
// assert.strictEqual(jwt.issuer, "issuer1");
|
|
178
|
+
// assert.strictEqual(jwt.algType, 2);
|
|
179
|
+
// assert.strictEqual(jwt.expiry, time + 10000000);
|
|
180
|
+
// assert.deepStrictEqual(jwt.claims, [claim]);
|
|
181
181
|
// });
|
|
182
|
-
|
|
183
|
-
// test("should successfully create a token using keyPair using digital Signature with one claim and get jwt using public key", async () =>
|
|
182
|
+
|
|
183
|
+
// await test("should successfully create a token using keyPair using digital Signature with one claim and get jwt using public key", async () =>
|
|
184
184
|
// {
|
|
185
185
|
// let keyPair = await AsymmetricEncryption.generateKeyPair();
|
|
186
186
|
// let pubKey = await AsymmetricEncryption.getPublicKey(keyPair);
|
|
@@ -188,14 +188,14 @@ suite("Json Web Token ", () =>
|
|
|
188
188
|
// let time = Date.now();
|
|
189
189
|
// let token = await JsonWebToken.fromClaims("issuer1", 2, keyPair, time + 10000000, [claim]).generateToken();
|
|
190
190
|
// let jwt = await JsonWebToken.fromToken("issuer1", 2, pubKey, token);
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
//
|
|
195
|
-
//
|
|
191
|
+
// assert.ok(jwt !== null || jwt !== undefined);
|
|
192
|
+
// assert.strictEqual(jwt.issuer, "issuer1");
|
|
193
|
+
// assert.strictEqual(jwt.algType, 2);
|
|
194
|
+
// assert.strictEqual(jwt.expiry, time + 10000000);
|
|
195
|
+
// assert.deepStrictEqual(jwt.claims, [claim]);
|
|
196
196
|
// });
|
|
197
|
-
|
|
198
|
-
// test("should throw an exception when getting JWT with a different issuer that what was user to generate token", async () =>
|
|
197
|
+
|
|
198
|
+
// await test("should throw an exception when getting JWT with a different issuer that what was user to generate token", async () =>
|
|
199
199
|
// {
|
|
200
200
|
// let keyPair = await AsymmetricEncryption.generateKeyPair();
|
|
201
201
|
// let pubKey = await AsymmetricEncryption.getPublicKey(keyPair);
|
|
@@ -208,14 +208,14 @@ suite("Json Web Token ", () =>
|
|
|
208
208
|
// }
|
|
209
209
|
// catch (exp)
|
|
210
210
|
// {
|
|
211
|
-
//
|
|
212
|
-
//
|
|
211
|
+
// assert.ok(exp instanceof InvalidTokenException);
|
|
212
|
+
// assert.equal(exp.message, `Token '${token}' is invalid because iss was expected to be 'notTheIssuer' but instead was 'issuer1'.`);
|
|
213
213
|
// return;
|
|
214
214
|
// }
|
|
215
|
-
//
|
|
215
|
+
// assert.ok(false);
|
|
216
216
|
// });
|
|
217
|
-
|
|
218
|
-
// test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
217
|
+
|
|
218
|
+
// await test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
219
219
|
// {
|
|
220
220
|
// let claim1 = new Claim("this_claim", "ThisValue");
|
|
221
221
|
// let claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -229,14 +229,14 @@ suite("Json Web Token ", () =>
|
|
|
229
229
|
// }
|
|
230
230
|
// catch (exp)
|
|
231
231
|
// {
|
|
232
|
-
//
|
|
233
|
-
//
|
|
232
|
+
// assert.ok(exp instanceof InvalidTokenException);
|
|
233
|
+
// assert.equal(exp.message, `Token '${token}' is invalid because alg was expected to be '${1}' but instead was '${2}'.`);
|
|
234
234
|
// return;
|
|
235
235
|
// }
|
|
236
|
-
//
|
|
236
|
+
// assert.ok(false);
|
|
237
237
|
// });
|
|
238
|
-
|
|
239
|
-
// test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
238
|
+
|
|
239
|
+
// await test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
|
|
240
240
|
// {
|
|
241
241
|
// let claim1 = new Claim("this_claim", "ThisValue");
|
|
242
242
|
// let claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -250,14 +250,14 @@ suite("Json Web Token ", () =>
|
|
|
250
250
|
// }
|
|
251
251
|
// catch (exp)
|
|
252
252
|
// {
|
|
253
|
-
//
|
|
254
|
-
//
|
|
253
|
+
// assert.ok(exp instanceof ExpiredTokenException);
|
|
254
|
+
// assert.equal(exp.message, `Token '${token}' is expired.`);
|
|
255
255
|
// return;
|
|
256
256
|
// }
|
|
257
|
-
//
|
|
257
|
+
// assert.ok(false);
|
|
258
258
|
// });
|
|
259
|
-
|
|
260
|
-
// test("should throw an exception when getting JWT key given is different than what was used for the token generation", async () =>
|
|
259
|
+
|
|
260
|
+
// await test("should throw an exception when getting JWT key given is different than what was used for the token generation", async () =>
|
|
261
261
|
// {
|
|
262
262
|
// let claim1 = new Claim("this_claim", "ThisValue");
|
|
263
263
|
// let claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -271,15 +271,15 @@ suite("Json Web Token ", () =>
|
|
|
271
271
|
// }
|
|
272
272
|
// catch (exp)
|
|
273
273
|
// {
|
|
274
|
-
//
|
|
275
|
-
//
|
|
274
|
+
// assert.ok(exp instanceof InvalidTokenException);
|
|
275
|
+
// assert.equal(exp.message, `Token '${token}' is invalid because signature could not be verified.`);
|
|
276
276
|
// return;
|
|
277
277
|
// }
|
|
278
|
-
//
|
|
278
|
+
// assert.ok(false);
|
|
279
279
|
// });
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
// test("should throw an exception when getting JWT when the token is tampered with", async () =>
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
// await test("should throw an exception when getting JWT when the token is tampered with", async () =>
|
|
283
283
|
// {
|
|
284
284
|
// let claim1 = new Claim("this_claim", "ThisValue");
|
|
285
285
|
// let claim2 = new Claim("that_claim", "ThatValue");
|
|
@@ -293,12 +293,12 @@ suite("Json Web Token ", () =>
|
|
|
293
293
|
// }
|
|
294
294
|
// catch (exp)
|
|
295
295
|
// {
|
|
296
|
-
//
|
|
297
|
-
//
|
|
296
|
+
// assert.ok(exp instanceof InvalidTokenException);
|
|
297
|
+
// assert.equal(exp.message, `Token '${token}' is invalid because signature could not be verified.`);
|
|
298
298
|
// return;
|
|
299
299
|
// }
|
|
300
|
-
//
|
|
300
|
+
// assert.ok(false);
|
|
301
301
|
// });
|
|
302
302
|
// });
|
|
303
|
-
|
|
303
|
+
|
|
304
304
|
});
|
package/test/other.test.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { describe, test } from "node:test";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
await describe("Other", async () =>
|
|
4
5
|
{
|
|
5
|
-
test("encoding decoding", () =>
|
|
6
|
+
await test("encoding decoding", () =>
|
|
6
7
|
{
|
|
7
8
|
const text = "moonlight43iuj90/;msdnnksdkdkdk[[[][][";
|
|
8
9
|
const encodedText = Buffer.from(text, "utf8").toString("base64");
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
assert.notStrictEqual(encodedText, text);
|
|
11
|
+
|
|
11
12
|
const decodedText = Buffer.from(encodedText, "base64").toString("utf8");
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
assert.notStrictEqual(decodedText, encodedText);
|
|
14
|
+
assert.strictEqual(decodedText, text);
|
|
14
15
|
});
|
|
15
16
|
});
|