@nivinjoseph/n-sec 4.0.6 → 5.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.
Files changed (54) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +335 -0
  3. package/dist/api-security/alg-type.js +2 -0
  4. package/dist/api-security/alg-type.js.map +1 -1
  5. package/dist/api-security/claim.d.ts +2 -3
  6. package/dist/api-security/claim.js +3 -2
  7. package/dist/api-security/claim.js.map +1 -1
  8. package/dist/api-security/claims-identity.js +2 -1
  9. package/dist/api-security/claims-identity.js.map +1 -1
  10. package/dist/api-security/expired-token-exception.d.ts +0 -1
  11. package/dist/api-security/expired-token-exception.js +2 -2
  12. package/dist/api-security/expired-token-exception.js.map +1 -1
  13. package/dist/api-security/invalid-token-exception.d.ts +0 -1
  14. package/dist/api-security/invalid-token-exception.js +3 -3
  15. package/dist/api-security/invalid-token-exception.js.map +1 -1
  16. package/dist/api-security/json-web-token.d.ts +4 -5
  17. package/dist/api-security/json-web-token.js +88 -81
  18. package/dist/api-security/json-web-token.js.map +1 -1
  19. package/dist/api-security/security-token.js +2 -2
  20. package/dist/api-security/security-token.js.map +1 -1
  21. package/dist/crypto/asymmetric-encryption.js +49 -0
  22. package/dist/crypto/asymmetric-encryption.js.map +1 -1
  23. package/dist/crypto/crypto-exception.js +1 -0
  24. package/dist/crypto/crypto-exception.js.map +1 -1
  25. package/dist/crypto/digital-signature.js +51 -0
  26. package/dist/crypto/digital-signature.js.map +1 -1
  27. package/dist/crypto/hash.d.ts +2 -3
  28. package/dist/crypto/hash.js +5 -5
  29. package/dist/crypto/hash.js.map +1 -1
  30. package/dist/crypto/hmac.d.ts +1 -2
  31. package/dist/crypto/hmac.js +4 -4
  32. package/dist/crypto/hmac.js.map +1 -1
  33. package/dist/crypto/symmetric-encryption.d.ts +1 -2
  34. package/dist/crypto/symmetric-encryption.js +15 -20
  35. package/dist/crypto/symmetric-encryption.js.map +1 -1
  36. package/dist/index.js +2 -0
  37. package/dist/index.js.map +1 -1
  38. package/package.json +12 -11
  39. package/src/api-security/claim.ts +5 -5
  40. package/src/api-security/claims-identity.ts +1 -1
  41. package/src/api-security/expired-token-exception.ts +1 -2
  42. package/src/api-security/invalid-token-exception.ts +2 -3
  43. package/src/api-security/json-web-token.ts +80 -79
  44. package/src/crypto/hash.ts +7 -9
  45. package/src/crypto/hmac.ts +4 -5
  46. package/src/crypto/symmetric-encryption.ts +17 -27
  47. package/test/hash.test.ts +42 -41
  48. package/test/hmac.test.ts +24 -23
  49. package/test/json-web-token.test.ts +66 -61
  50. package/test/other.test.ts +3 -3
  51. package/test/symmetric-encryption.test.ts +12 -10
  52. package/tsconfig.json +9 -11
  53. package/dist/crypto/uuid.js.map +0 -1
  54. package/tslint.json +0 -64
package/test/hmac.test.ts CHANGED
@@ -9,9 +9,10 @@ suite("Hmac", () =>
9
9
  {
10
10
  test("should return string value that is not null, empty, whitespace or same as the key or input", async () =>
11
11
  {
12
- let key = await SymmetricEncryption.generateKey();
13
- let value = "hello world";
14
- let hmac = await Hmac.create(key, value);
12
+ const key = await SymmetricEncryption.generateKey();
13
+ const value = "hello world";
14
+ const hmac = Hmac.create(key, value);
15
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
16
  Assert.ok(hmac !== null && !hmac.isEmptyOrWhiteSpace());
16
17
  Assert.notStrictEqual(hmac, key);
17
18
  Assert.notStrictEqual(hmac, value);
@@ -19,42 +20,42 @@ suite("Hmac", () =>
19
20
 
20
21
  test("multiple invocations with the same key and value must return the same output", async () =>
21
22
  {
22
- let key = await SymmetricEncryption.generateKey();
23
- let value = "hello world";
24
- let hmac1 = await Hmac.create(key, value);
25
- let hmac2 = await Hmac.create(key, value);
23
+ const key = await SymmetricEncryption.generateKey();
24
+ const value = "hello world";
25
+ const hmac1 = Hmac.create(key, value);
26
+ const hmac2 = Hmac.create(key, value);
26
27
  Assert.strictEqual(hmac1, hmac2);
27
28
  });
28
29
 
29
30
  test("multiple invocations with different keys and different values must return different outputs", async () =>
30
31
  {
31
- let key1 = await SymmetricEncryption.generateKey();
32
- let value1 = "hello world";
33
- let hmac1 = await Hmac.create(key1, value1);
32
+ const key1 = await SymmetricEncryption.generateKey();
33
+ const value1 = "hello world";
34
+ const hmac1 = Hmac.create(key1, value1);
34
35
 
35
- let key2 = await SymmetricEncryption.generateKey();
36
- let value2 = "goodbye world";
37
- let hmac2 = await Hmac.create(key2, value2);
36
+ const key2 = await SymmetricEncryption.generateKey();
37
+ const value2 = "goodbye world";
38
+ const hmac2 = Hmac.create(key2, value2);
38
39
  Assert.notStrictEqual(hmac1, hmac2);
39
40
  });
40
41
 
41
42
  test("multiple invocations with the same key and different values must return different outputs", async () =>
42
43
  {
43
- let key = await SymmetricEncryption.generateKey();
44
- let value1 = "hello world";
45
- let value2 = "goodbye world";
46
- let hmac1 = await Hmac.create(key, value1);
47
- let hmac2 = await Hmac.create(key, value2);
44
+ const key = await SymmetricEncryption.generateKey();
45
+ const value1 = "hello world";
46
+ const value2 = "goodbye world";
47
+ const hmac1 = Hmac.create(key, value1);
48
+ const hmac2 = Hmac.create(key, value2);
48
49
  Assert.notStrictEqual(hmac1, hmac2);
49
50
  });
50
51
 
51
52
  test("multiple invocations with different keys and the same value must return different outputs", async () =>
52
53
  {
53
- let key1 = await SymmetricEncryption.generateKey();
54
- let key2 = await SymmetricEncryption.generateKey();
55
- let value = "hello world";
56
- let hmac1 = await Hmac.create(key1, value);
57
- let hmac2 = await Hmac.create(key2, value);
54
+ const key1 = await SymmetricEncryption.generateKey();
55
+ const key2 = await SymmetricEncryption.generateKey();
56
+ const value = "hello world";
57
+ const hmac1 = Hmac.create(key1, value);
58
+ const hmac2 = Hmac.create(key2, value);
58
59
  Assert.notStrictEqual(hmac1, hmac2);
59
60
  });
60
61
 
@@ -14,11 +14,12 @@ suite("Json Web Token ", () =>
14
14
 
15
15
  test("should successfully create a token using hmac with one claim", async () =>
16
16
  {
17
- let claim = new Claim("this_claim", "ThisValue");
18
- let key = await SymmetricEncryption.generateKey();
19
- let time = Date.now();
20
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim]).generateToken();
21
- let jwt = await JsonWebToken.fromToken("issuer1", 1, key, token);
17
+ const claim = new Claim("this_claim", "ThisValue");
18
+ const key = await SymmetricEncryption.generateKey();
19
+ const time = Date.now();
20
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim]).generateToken();
21
+ const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
22
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
22
23
  Assert.ok(jwt !== null || jwt !== undefined);
23
24
  Assert.strictEqual(jwt.issuer, "issuer1");
24
25
  Assert.strictEqual(jwt.algType, 1);
@@ -28,12 +29,13 @@ suite("Json Web Token ", () =>
28
29
 
29
30
  test("should successfully create a token using hmac with 2 claims", async () =>
30
31
  {
31
- let claim1 = new Claim("this_claim", "ThisValue");
32
- let claim2 = new Claim("that_claim", "ThatValue");
33
- let key = await SymmetricEncryption.generateKey();
34
- let time = Date.now();
35
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
36
- let jwt = await JsonWebToken.fromToken("issuer1", 1, key, token);
32
+ const claim1 = new Claim("this_claim", "ThisValue");
33
+ const claim2 = new Claim("that_claim", "ThatValue");
34
+ const key = await SymmetricEncryption.generateKey();
35
+ const time = Date.now();
36
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
37
+ const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
38
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
37
39
  Assert.ok(jwt !== null || jwt !== undefined);
38
40
  Assert.strictEqual(jwt.issuer, "issuer1");
39
41
  Assert.strictEqual(jwt.algType, 1);
@@ -43,12 +45,13 @@ suite("Json Web Token ", () =>
43
45
 
44
46
  test("should successfully create a token using hmac with 2 claims", async () =>
45
47
  {
46
- let claim1 = new Claim("this_claim", "ThisValue");
47
- let claim2 = new Claim("that_claim", "ThatValue");
48
- let key = await SymmetricEncryption.generateKey();
49
- let time = Date.now();
50
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
51
- let jwt = await JsonWebToken.fromToken("issuer1", 1, key, token);
48
+ const claim1 = new Claim("this_claim", "ThisValue");
49
+ const claim2 = new Claim("that_claim", "ThatValue");
50
+ const key = await SymmetricEncryption.generateKey();
51
+ const time = Date.now();
52
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
53
+ const jwt = JsonWebToken.fromToken("issuer1", 1, key, token);
54
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
52
55
  Assert.ok(jwt !== null || jwt !== undefined);
53
56
  Assert.strictEqual(jwt.issuer, "issuer1");
54
57
  Assert.strictEqual(jwt.algType, 1);
@@ -58,14 +61,14 @@ suite("Json Web Token ", () =>
58
61
 
59
62
  test("should throw an exception when getting JWT with a different issuer that what was user to generate token", async () =>
60
63
  {
61
- let claim1 = new Claim("this_claim", "ThisValue");
62
- let claim2 = new Claim("that_claim", "ThatValue");
63
- let key = await SymmetricEncryption.generateKey();
64
- let time = Date.now();
65
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
64
+ const claim1 = new Claim("this_claim", "ThisValue");
65
+ const claim2 = new Claim("that_claim", "ThatValue");
66
+ const key = await SymmetricEncryption.generateKey();
67
+ const time = Date.now();
68
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 10000000, [claim1, claim2]).generateToken();
66
69
  try
67
70
  {
68
- await JsonWebToken.fromToken("notTheIssuer", 1, key, token);
71
+ JsonWebToken.fromToken("notTheIssuer", 1, key, token);
69
72
  }
70
73
  catch (exp)
71
74
  {
@@ -78,14 +81,14 @@ suite("Json Web Token ", () =>
78
81
 
79
82
  test("should throw an exception when getting JWT when the token is expired", async () =>
80
83
  {
81
- let claim1 = new Claim("this_claim", "ThisValue");
82
- let claim2 = new Claim("that_claim", "ThatValue");
83
- let key = await SymmetricEncryption.generateKey();
84
- let time = Date.now();
85
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time, [claim1, claim2]).generateToken();
84
+ const claim1 = new Claim("this_claim", "ThisValue");
85
+ const claim2 = new Claim("that_claim", "ThatValue");
86
+ const key = await SymmetricEncryption.generateKey();
87
+ const time = Date.now();
88
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time, [claim1, claim2]).generateToken();
86
89
  try
87
90
  {
88
- await JsonWebToken.fromToken("issuer1", 1, key, token);
91
+ JsonWebToken.fromToken("issuer1", 1, key, token);
89
92
  }
90
93
  catch (exp)
91
94
  {
@@ -96,37 +99,39 @@ suite("Json Web Token ", () =>
96
99
  Assert.ok(false);
97
100
  });
98
101
 
99
- test("should throw an exception when getting JWT algorithm given is different than what was used for the token generation", async () =>
100
- {
101
- let claim1 = new Claim("this_claim", "ThisValue");
102
- let claim2 = new Claim("that_claim", "ThatValue");
103
- let key = await SymmetricEncryption.generateKey();
104
- let time = Date.now();
105
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
106
- try
107
- {
108
- await JsonWebToken.fromToken("issuer1", 2, key, token);
109
- }
110
- catch (exp)
111
- {
112
- Assert.ok(exp instanceof InvalidTokenException);
113
- Assert.equal(exp.message, `Token '${token}' is invalid because alg was expected to be '${2}' but instead was '${1}'.`);
114
- return;
115
- }
116
- Assert.ok(false);
117
- });
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 () =>
104
+ // {
105
+ // let claim1 = new Claim("this_claim", "ThisValue");
106
+ // let claim2 = new Claim("that_claim", "ThatValue");
107
+ // let key = await SymmetricEncryption.generateKey();
108
+ // let time = Date.now();
109
+ // let token = JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
110
+ // try
111
+ // {
112
+ // JsonWebToken.fromToken("issuer1", 2, key, token);
113
+ // }
114
+ // catch (exp)
115
+ // {
116
+ // console.log(exp);
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
+ // return;
120
+ // }
121
+ // Assert.ok(false);
122
+ // });
118
123
 
119
124
  test("should throw an exception when getting JWT key given is different than what was used for the token generation", async () =>
120
125
  {
121
- let claim1 = new Claim("this_claim", "ThisValue");
122
- let claim2 = new Claim("that_claim", "ThatValue");
123
- let key = await SymmetricEncryption.generateKey();
124
- let key2 = await SymmetricEncryption.generateKey();
125
- let time = Date.now();
126
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
126
+ const claim1 = new Claim("this_claim", "ThisValue");
127
+ const claim2 = new Claim("that_claim", "ThatValue");
128
+ const key = await SymmetricEncryption.generateKey();
129
+ const key2 = await SymmetricEncryption.generateKey();
130
+ const time = Date.now();
131
+ const token = JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
127
132
  try
128
133
  {
129
- await JsonWebToken.fromToken("issuer1", 1, key2, token);
134
+ JsonWebToken.fromToken("issuer1", 1, key2, token);
130
135
  }
131
136
  catch (exp)
132
137
  {
@@ -139,15 +144,15 @@ suite("Json Web Token ", () =>
139
144
 
140
145
  test("should throw an exception when getting JWT when the token is tampered with", async () =>
141
146
  {
142
- let claim1 = new Claim("this_claim", "ThisValue");
143
- let claim2 = new Claim("that_claim", "ThatValue");
144
- let key = await SymmetricEncryption.generateKey();
145
- let time = Date.now();
146
- let token = await JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
147
+ const claim1 = new Claim("this_claim", "ThisValue");
148
+ const claim2 = new Claim("that_claim", "ThatValue");
149
+ const key = await SymmetricEncryption.generateKey();
150
+ const time = Date.now();
151
+ let token = JsonWebToken.fromClaims("issuer1", 1, key, time + 1000000, [claim1, claim2]).generateToken();
147
152
  token = token + "someStuff";
148
153
  try
149
154
  {
150
- await JsonWebToken.fromToken("issuer1", 1, key, token);
155
+ JsonWebToken.fromToken("issuer1", 1, key, token);
151
156
  }
152
157
  catch (exp)
153
158
  {
@@ -4,11 +4,11 @@ suite("Other", () =>
4
4
  {
5
5
  test("encoding decoding", () =>
6
6
  {
7
- let text = "moonlight43iuj90/;msdnnksdkdkdk[[[][][";
8
- let encodedText = Buffer.from(text, "utf8").toString("base64");
7
+ const text = "moonlight43iuj90/;msdnnksdkdkdk[[[][][";
8
+ const encodedText = Buffer.from(text, "utf8").toString("base64");
9
9
  Assert.notStrictEqual(encodedText, text);
10
10
 
11
- let decodedText = Buffer.from(encodedText, "base64").toString("utf8");
11
+ const decodedText = Buffer.from(encodedText, "base64").toString("utf8");
12
12
  Assert.notStrictEqual(decodedText, encodedText);
13
13
  Assert.strictEqual(decodedText, text);
14
14
  });
@@ -10,16 +10,17 @@ suite("SymmetricEncryption", () =>
10
10
  {
11
11
  test("must return string value that is not null, empty or whitespace", async () =>
12
12
  {
13
- let key = await SymmetricEncryption.generateKey();
13
+ const key = await SymmetricEncryption.generateKey();
14
14
  console.log("key", key);
15
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
16
  Assert.ok(key !== null && !key.isEmptyOrWhiteSpace());
16
17
  });
17
18
 
18
19
 
19
20
  test("consecutive calls must yield unique values", async () =>
20
21
  {
21
- let key1 = await SymmetricEncryption.generateKey();
22
- let key2 = await SymmetricEncryption.generateKey();
22
+ const key1 = await SymmetricEncryption.generateKey();
23
+ const key2 = await SymmetricEncryption.generateKey();
23
24
  Assert.notStrictEqual(key1, key2);
24
25
  });
25
26
  });
@@ -28,9 +29,10 @@ suite("SymmetricEncryption", () =>
28
29
  {
29
30
  test("must return cipher text value when called with key and plain text value", async () =>
30
31
  {
31
- let key = "E25B269440F88601C453CD171D76EDDC11D8CF33230742DF8CAD5873D28F78B2";
32
- let value = "password";
33
- let encrypted = await SymmetricEncryption.encrypt(key, value);
32
+ const key = "E25B269440F88601C453CD171D76EDDC11D8CF33230742DF8CAD5873D28F78B2";
33
+ const value = "password";
34
+ const encrypted = await SymmetricEncryption.encrypt(key, value);
35
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
34
36
  Assert.ok(encrypted !== null);
35
37
  Assert.notStrictEqual(encrypted, value);
36
38
  });
@@ -171,10 +173,10 @@ suite("SymmetricEncryption", () =>
171
173
  {
172
174
  test("must return plain text value when called with key and cipher text value", async () =>
173
175
  {
174
- let key = await SymmetricEncryption.generateKey();
175
- let value = "password";
176
- let encrypted = await SymmetricEncryption.encrypt(key, value);
177
- let decrypted = await SymmetricEncryption.decrypt(key, encrypted);
176
+ const key = await SymmetricEncryption.generateKey();
177
+ const value = "password";
178
+ const encrypted = await SymmetricEncryption.encrypt(key, value);
179
+ const decrypted = SymmetricEncryption.decrypt(key, encrypted);
178
180
  Assert.strictEqual(decrypted, value);
179
181
  });
180
182
 
package/tsconfig.json CHANGED
@@ -3,9 +3,9 @@
3
3
  "module": "commonjs",
4
4
  "target": "es2015",
5
5
  "strict": true,
6
- "strictNullChecks": false,
7
- "strictFunctionTypes": false,
8
- "noImplicitThis": false,
6
+ "strictNullChecks": true,
7
+ "strictFunctionTypes": true,
8
+ "noImplicitThis": true,
9
9
  "noImplicitReturns": true,
10
10
  "noUnusedLocals": true,
11
11
  "noUnusedParameters": true,
@@ -14,15 +14,13 @@
14
14
  "sourceMap": true,
15
15
  "experimentalDecorators": true,
16
16
  "emitDecoratorMetadata": true,
17
- "removeComments": true,
17
+ "removeComments": false,
18
18
  "forceConsistentCasingInFileNames": true,
19
19
  "incremental": false,
20
- "plugins": [
21
- {
22
- "name": "typescript-tslint-plugin",
23
- "configFile": "./tslint.json",
24
- "alwaysShowRuleFailuresAsWarnings": false
25
- }
26
- ]
20
+ "skipLibCheck": true,
21
+ "importHelpers": true,
22
+ "noEmitHelpers": true,
23
+ "noImplicitOverride": true,
24
+ "pretty": true
27
25
  }
28
26
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../src/crypto/uuid.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAI7B,MAAa,IAAI;IAEb,gBAAwB,CAAC;IAGlB,MAAM,CAAC,MAAM;QAEhB,OAAO,IAAI,CAAC,EAAE,EAAE,CAAC;IACrB,CAAC;CACJ;AATD,oBASC","sourcesContent":["import * as uuid from \"uuid\";\n\n\n// public\nexport class Uuid\n{\n private constructor() { }\n \n \n public static create(): string\n {\n return uuid.v4();\n }\n}"]}
package/tslint.json DELETED
@@ -1,64 +0,0 @@
1
- {
2
- "defaultSeverity": "error",
3
- "rules": {
4
- "class-name": true,
5
- "comment-format": [
6
- true,
7
- "check-space"
8
- ],
9
- "indent": [
10
- true,
11
- "spaces"
12
- ],
13
- "no-duplicate-variable": true,
14
- "no-eval": true,
15
- "no-internal-module": true,
16
- "no-trailing-whitespace": false,
17
- "no-var-keyword": true,
18
- "no-default-export": true,
19
- "one-line": [
20
- true,
21
- "check-whitespace"
22
- ],
23
- "quotemark": [
24
- true,
25
- "double"
26
- ],
27
- "semicolon": [
28
- true,
29
- "always"
30
- ],
31
- "triple-equals": [
32
- true,
33
- "allow-null-check"
34
- ],
35
- "typedef-whitespace": [
36
- true,
37
- {
38
- "call-signature": "nospace",
39
- "index-signature": "nospace",
40
- "parameter": "nospace",
41
- "property-declaration": "nospace",
42
- "variable-declaration": "nospace"
43
- }
44
- ],
45
- "variable-name": [
46
- true,
47
- "ban-keywords"
48
- ],
49
- "whitespace": [
50
- true,
51
- "check-branch",
52
- "check-decl",
53
- "check-operator",
54
- "check-separator",
55
- "check-type"
56
- ],
57
- "interface-name": [
58
- true,
59
- "never-prefix"
60
- ],
61
- "await-promise": true,
62
- "no-floating-promises": true
63
- }
64
- }