@nivinjoseph/n-sec 5.0.1 → 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.
- package/.eslintignore +2 -0
- package/.eslintrc +335 -0
- package/dist/api-security/alg-type.js +2 -0
- package/dist/api-security/alg-type.js.map +1 -1
- package/dist/api-security/claim.d.ts +2 -2
- package/dist/api-security/claim.js +3 -1
- package/dist/api-security/claim.js.map +1 -1
- package/dist/api-security/claims-identity.js +2 -1
- package/dist/api-security/claims-identity.js.map +1 -1
- package/dist/api-security/expired-token-exception.js +2 -1
- package/dist/api-security/expired-token-exception.js.map +1 -1
- package/dist/api-security/invalid-token-exception.js +3 -2
- package/dist/api-security/invalid-token-exception.js.map +1 -1
- package/dist/api-security/json-web-token.d.ts +3 -3
- package/dist/api-security/json-web-token.js +57 -37
- package/dist/api-security/json-web-token.js.map +1 -1
- package/dist/api-security/security-token.js +2 -2
- package/dist/api-security/security-token.js.map +1 -1
- package/dist/crypto/asymmetric-encryption.js +49 -0
- package/dist/crypto/asymmetric-encryption.js.map +1 -1
- package/dist/crypto/crypto-exception.js +1 -0
- package/dist/crypto/crypto-exception.js.map +1 -1
- package/dist/crypto/digital-signature.js +51 -0
- package/dist/crypto/digital-signature.js.map +1 -1
- package/dist/crypto/hash.js +4 -3
- package/dist/crypto/hash.js.map +1 -1
- package/dist/crypto/hmac.js +3 -2
- package/dist/crypto/hmac.js.map +1 -1
- package/dist/crypto/symmetric-encryption.js +5 -4
- package/dist/crypto/symmetric-encryption.js.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +12 -11
- package/src/api-security/claim.ts +5 -4
- package/src/api-security/claims-identity.ts +1 -1
- package/src/api-security/expired-token-exception.ts +1 -1
- package/src/api-security/invalid-token-exception.ts +2 -2
- package/src/api-security/json-web-token.ts +68 -66
- package/src/crypto/hash.ts +1 -2
- package/src/crypto/symmetric-encryption.ts +0 -2
- package/test/hash.test.ts +34 -32
- package/test/hmac.test.ts +24 -23
- package/test/json-web-token.test.ts +40 -37
- package/test/other.test.ts +3 -3
- package/test/symmetric-encryption.test.ts +12 -10
- package/tsconfig.json +8 -11
- package/dist/crypto/uuid.js.map +0 -1
- package/tslint.json +0 -64
|
@@ -4,10 +4,10 @@ exports.SecurityToken = void 0;
|
|
|
4
4
|
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
5
5
|
class SecurityToken {
|
|
6
6
|
constructor(scheme, token) {
|
|
7
|
-
n_defensive_1.given(scheme, "scheme").ensureHasValue().ensureIsString()
|
|
7
|
+
(0, n_defensive_1.given)(scheme, "scheme").ensureHasValue().ensureIsString()
|
|
8
8
|
.ensure(t => !t.contains(" "), "cannot contain space");
|
|
9
9
|
this._scheme = scheme;
|
|
10
|
-
n_defensive_1.given(token, "token").ensureHasValue().ensureIsString()
|
|
10
|
+
(0, n_defensive_1.given)(token, "token").ensureHasValue().ensureIsString()
|
|
11
11
|
.ensure(t => !t.contains(" "), "cannot contain space");
|
|
12
12
|
this._token = token;
|
|
13
13
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"security-token.js","sourceRoot":"","sources":["../../src/api-security/security-token.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AAEjD,MAAa,aAAa;IAUtB,YAAmB,MAAc,EAAE,KAAa;QAE5C,mBAAK,
|
|
1
|
+
{"version":3,"file":"security-token.js","sourceRoot":"","sources":["../../src/api-security/security-token.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AAEjD,MAAa,aAAa;IAUtB,YAAmB,MAAc,EAAE,KAAa;QAE5C,IAAA,mBAAK,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE;aACpD,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,sBAAsB,CAAC,CAAC;QAC3D,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QAEtB,IAAA,mBAAK,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE;aAClD,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,sBAAsB,CAAC,CAAC;QAC3D,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAbD,IAAW,MAAM,KAAa,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IACpD,IAAW,KAAK,KAAa,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAe3C,QAAQ;QAEX,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;IAC5C,CAAC;CACJ;AA1BD,sCA0BC"}
|
|
@@ -1,2 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// const ursa = require("ursa");
|
|
3
|
+
// import { given } from "@nivinjoseph/n-defensive";
|
|
4
|
+
// import "@nivinjoseph/n-ext";
|
|
5
|
+
// // public
|
|
6
|
+
// export class AsymmetricEncryption
|
|
7
|
+
// {
|
|
8
|
+
// private constructor() { }
|
|
9
|
+
// public static generateKeyPair(): Promise<string>
|
|
10
|
+
// {
|
|
11
|
+
// const key = ursa.generatePrivateKey();
|
|
12
|
+
// return Promise.resolve(key.toPrivatePem().toString("hex").toUpperCase());
|
|
13
|
+
// }
|
|
14
|
+
// public static getPublicKey(keyPair: string): Promise<string>
|
|
15
|
+
// {
|
|
16
|
+
// given(keyPair, "keyPair").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
17
|
+
// keyPair = keyPair.trim();
|
|
18
|
+
// const key = ursa.createPrivateKey(Buffer.from(keyPair, "hex"));
|
|
19
|
+
// return Promise.resolve(key.toPublicPem().toString("hex").toUpperCase());
|
|
20
|
+
// }
|
|
21
|
+
// public static encrypt(keyPairOrPublicKey: string, value: string): Promise<string>
|
|
22
|
+
// {
|
|
23
|
+
// given(keyPairOrPublicKey, "keyPairOrPublicKey").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
24
|
+
// given(value, "value").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
25
|
+
// keyPairOrPublicKey = keyPairOrPublicKey.trim();
|
|
26
|
+
// value = value.trim();
|
|
27
|
+
// const buf = Buffer.from(keyPairOrPublicKey, "hex");
|
|
28
|
+
// let key;
|
|
29
|
+
// try
|
|
30
|
+
// {
|
|
31
|
+
// key = ursa.createPublicKey(buf);
|
|
32
|
+
// }
|
|
33
|
+
// catch (error)
|
|
34
|
+
// {
|
|
35
|
+
// key = ursa.createPrivateKey(buf);
|
|
36
|
+
// }
|
|
37
|
+
// const encrypted = key.encrypt(Buffer.from(value, "utf8"), "utf8", "hex", ursa.RSA_PKCS1_PADDING);
|
|
38
|
+
// return Promise.resolve(encrypted.toUpperCase());
|
|
39
|
+
// }
|
|
40
|
+
// public static decrypt(keyPair: string, value: string): Promise<string>
|
|
41
|
+
// {
|
|
42
|
+
// given(keyPair, "keyPair").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
43
|
+
// given(value, "value").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
44
|
+
// keyPair = keyPair.trim();
|
|
45
|
+
// value = value.trim();
|
|
46
|
+
// const key = ursa.createPrivateKey(Buffer.from(keyPair, "hex"));
|
|
47
|
+
// const decrypted = key.decrypt(Buffer.from(value, "hex"), "hex", "utf8", ursa.RSA_PKCS1_PADDING);
|
|
48
|
+
// return Promise.resolve(decrypted);
|
|
49
|
+
// }
|
|
50
|
+
// }
|
|
2
51
|
//# sourceMappingURL=asymmetric-encryption.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asymmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/asymmetric-encryption.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"asymmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/asymmetric-encryption.ts"],"names":[],"mappings":";AAAA,gCAAgC;AAChC,oDAAoD;AACpD,+BAA+B;AAG/B,YAAY;AACZ,oCAAoC;AACpC,IAAI;AACJ,gCAAgC;AAGhC,uDAAuD;AACvD,QAAQ;AACR,iDAAiD;AACjD,oFAAoF;AACpF,QAAQ;AAER,mEAAmE;AACnE,QAAQ;AACR,6GAA6G;AAE7G,oCAAoC;AAEpC,0EAA0E;AAC1E,mFAAmF;AACnF,QAAQ;AAER,wFAAwF;AACxF,QAAQ;AACR,mIAAmI;AACnI,yGAAyG;AAEzG,0DAA0D;AAC1D,gCAAgC;AAEhC,kEAAkE;AAClE,mBAAmB;AACnB,eAAe;AACf,YAAY;AACZ,+CAA+C;AAC/C,YAAY;AACZ,wBAAwB;AACxB,YAAY;AACZ,gDAAgD;AAChD,YAAY;AACZ,4GAA4G;AAC5G,2DAA2D;AAC3D,QAAQ;AAER,6EAA6E;AAC7E,QAAQ;AACR,6GAA6G;AAC7G,yGAAyG;AAEzG,oCAAoC;AACpC,gCAAgC;AAEhC,0EAA0E;AAC1E,4GAA4G;AAC5G,6CAA6C;AAC7C,QAAQ;AACR,IAAI"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CryptoException = void 0;
|
|
4
4
|
const n_exception_1 = require("@nivinjoseph/n-exception");
|
|
5
|
+
// public
|
|
5
6
|
class CryptoException extends n_exception_1.Exception {
|
|
6
7
|
}
|
|
7
8
|
exports.CryptoException = CryptoException;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-exception.js","sourceRoot":"","sources":["../../src/crypto/crypto-exception.ts"],"names":[],"mappings":";;;AAAA,0DAAqD;
|
|
1
|
+
{"version":3,"file":"crypto-exception.js","sourceRoot":"","sources":["../../src/crypto/crypto-exception.ts"],"names":[],"mappings":";;;AAAA,0DAAqD;AAGrD,SAAS;AACT,MAAa,eAAgB,SAAQ,uBAAS;CAC3C;AADH,0CACG"}
|
|
@@ -1,2 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// const ursa = require("ursa");
|
|
3
|
+
// import { given } from "@nivinjoseph/n-defensive";
|
|
4
|
+
// import "@nivinjoseph/n-ext";
|
|
5
|
+
// import * as Crypto from "crypto";
|
|
6
|
+
// // public
|
|
7
|
+
// export class DigitalSignature
|
|
8
|
+
// {
|
|
9
|
+
// private constructor() { }
|
|
10
|
+
// public static sign(keyPair: string, value: string): Promise<string>
|
|
11
|
+
// {
|
|
12
|
+
// given(keyPair, "keyPair").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
13
|
+
// given(value, "value").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
14
|
+
// keyPair = keyPair.trim();
|
|
15
|
+
// value = value.trim();
|
|
16
|
+
// const hash = Crypto.createHash("sha512");
|
|
17
|
+
// hash.update(value, "utf8");
|
|
18
|
+
// const key = ursa.createPrivateKey(Buffer.from(keyPair, "hex"));
|
|
19
|
+
// const signature = key.sign("sha512", hash.digest(), null, "hex");
|
|
20
|
+
// return Promise.resolve(signature.toUpperCase());
|
|
21
|
+
// }
|
|
22
|
+
// public static async verify(keyPairOrPublicKey: string, value: string, signature: string): Promise<boolean>
|
|
23
|
+
// {
|
|
24
|
+
// given(keyPairOrPublicKey, "keyPairOrPublicKey").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
25
|
+
// given(value, "value").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
26
|
+
// given(signature, "signature").ensureHasValue().ensureIsString().ensure(t => !t.isEmptyOrWhiteSpace());
|
|
27
|
+
// keyPairOrPublicKey = keyPairOrPublicKey.trim();
|
|
28
|
+
// value = value.trim();
|
|
29
|
+
// signature = signature.trim();
|
|
30
|
+
// const hash = Crypto.createHash("sha512");
|
|
31
|
+
// hash.update(value, "utf8");
|
|
32
|
+
// try
|
|
33
|
+
// {
|
|
34
|
+
// const buf = Buffer.from(keyPairOrPublicKey, "hex");
|
|
35
|
+
// let key;
|
|
36
|
+
// try
|
|
37
|
+
// {
|
|
38
|
+
// key = ursa.createPublicKey(buf);
|
|
39
|
+
// }
|
|
40
|
+
// catch (error)
|
|
41
|
+
// {
|
|
42
|
+
// key = ursa.createPrivateKey(buf);
|
|
43
|
+
// }
|
|
44
|
+
// const result = key.verify("sha512", hash.digest(), Buffer.from(signature, "hex"));
|
|
45
|
+
// return Promise.resolve(result);
|
|
46
|
+
// }
|
|
47
|
+
// catch (error)
|
|
48
|
+
// {
|
|
49
|
+
// return Promise.resolve(false);
|
|
50
|
+
// }
|
|
51
|
+
// }
|
|
52
|
+
// }
|
|
2
53
|
//# sourceMappingURL=digital-signature.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"digital-signature.js","sourceRoot":"","sources":["../../src/crypto/digital-signature.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"digital-signature.js","sourceRoot":"","sources":["../../src/crypto/digital-signature.ts"],"names":[],"mappings":";AAAA,gCAAgC;AAChC,oDAAoD;AACpD,+BAA+B;AAC/B,oCAAoC;AAGpC,YAAY;AACZ,gCAAgC;AAChC,IAAI;AACJ,gCAAgC;AAGhC,0EAA0E;AAC1E,QAAQ;AACR,6GAA6G;AAC7G,yGAAyG;AAEzG,oCAAoC;AACpC,gCAAgC;AAEhC,oDAAoD;AACpD,sCAAsC;AAEtC,0EAA0E;AAC1E,4EAA4E;AAC5E,2DAA2D;AAC3D,QAAQ;AAER,iHAAiH;AACjH,QAAQ;AACR,mIAAmI;AACnI,yGAAyG;AACzG,iHAAiH;AAEjH,0DAA0D;AAC1D,gCAAgC;AAChC,wCAAwC;AAExC,oDAAoD;AACpD,sCAAsC;AAEtC,eAAe;AACf,YAAY;AACZ,kEAAkE;AAClE,uBAAuB;AACvB,mBAAmB;AACnB,gBAAgB;AAChB,mDAAmD;AACnD,gBAAgB;AAChB,4BAA4B;AAC5B,gBAAgB;AAChB,oDAAoD;AACpD,gBAAgB;AAEhB,iGAAiG;AACjG,8CAA8C;AAC9C,YAAY;AACZ,wBAAwB;AACxB,YAAY;AACZ,6CAA6C;AAC7C,YAAY;AACZ,QAAQ;AACR,IAAI"}
|
package/dist/crypto/hash.js
CHANGED
|
@@ -3,18 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Hash = void 0;
|
|
4
4
|
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
5
5
|
const Crypto = require("crypto");
|
|
6
|
+
// public
|
|
6
7
|
class Hash {
|
|
7
8
|
constructor() { }
|
|
8
9
|
static create(value) {
|
|
9
|
-
n_defensive_1.given(value, "value").ensureHasValue().ensureIsString();
|
|
10
|
+
(0, n_defensive_1.given)(value, "value").ensureHasValue().ensureIsString();
|
|
10
11
|
value = value.trim();
|
|
11
12
|
const hash = Crypto.createHash("sha512");
|
|
12
13
|
hash.update(value, "utf8");
|
|
13
14
|
return hash.digest("hex").toUpperCase();
|
|
14
15
|
}
|
|
15
16
|
static createUsingSalt(value, salt) {
|
|
16
|
-
n_defensive_1.given(value, "value").ensureHasValue().ensureIsString();
|
|
17
|
-
n_defensive_1.given(salt, "salt").ensureHasValue().ensureIsString();
|
|
17
|
+
(0, n_defensive_1.given)(value, "value").ensureHasValue().ensureIsString();
|
|
18
|
+
(0, n_defensive_1.given)(salt, "salt").ensureHasValue().ensureIsString();
|
|
18
19
|
value = value.trim();
|
|
19
20
|
salt = salt.trim();
|
|
20
21
|
const reverse = (val) => {
|
package/dist/crypto/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/crypto/hash.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AACjD,iCAAiC;
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/crypto/hash.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AACjD,iCAAiC;AAGjC,SAAS;AACT,MAAa,IAAI;IAEb,gBAAwB,CAAC;IAGlB,MAAM,CAAC,MAAM,CAAC,KAAa;QAE9B,IAAA,mBAAK,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACxD,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;QAErB,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACzC,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC5C,CAAC;IAEM,MAAM,CAAC,eAAe,CAAC,KAAa,EAAE,IAAY;QAErD,IAAA,mBAAK,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACxD,IAAA,mBAAK,EAAC,IAAI,EAAE,MAAM,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QAEtD,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;QACrB,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QAEnB,MAAM,OAAO,GAAG,CAAC,GAAW,EAAU,EAAE;YAEpC,IAAI,GAAG,GAAG,EAAE,CAAC;YACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE;gBAC/B,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;YACvB,OAAO,GAAG,CAAC;QACf,CAAC,CAAC;QAEF,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;QACpC,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QAElC,MAAM,WAAW,GAAG,uBAAuB,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC;QAE3F,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACpC,CAAC;CACJ;AAtCD,oBAsCC"}
|
package/dist/crypto/hmac.js
CHANGED
|
@@ -3,11 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Hmac = void 0;
|
|
4
4
|
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
5
5
|
const Crypto = require("crypto");
|
|
6
|
+
// public
|
|
6
7
|
class Hmac {
|
|
7
8
|
constructor() { }
|
|
8
9
|
static create(key, value) {
|
|
9
|
-
n_defensive_1.given(key, "key").ensureHasValue().ensureIsString();
|
|
10
|
-
n_defensive_1.given(value, "value").ensureHasValue().ensureIsString();
|
|
10
|
+
(0, n_defensive_1.given)(key, "key").ensureHasValue().ensureIsString();
|
|
11
|
+
(0, n_defensive_1.given)(value, "value").ensureHasValue().ensureIsString();
|
|
11
12
|
key = key.trim();
|
|
12
13
|
value = value.trim();
|
|
13
14
|
const hmac = Crypto.createHmac("sha256", Buffer.from(key, "hex"));
|
package/dist/crypto/hmac.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AACjD,iCAAiC;
|
|
1
|
+
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":";;;AAAA,0DAAiD;AACjD,iCAAiC;AAGjC,SAAS;AACT,MAAa,IAAI;IAEb,gBAAwB,CAAC;IAGlB,MAAM,CAAC,MAAM,CAAC,GAAW,EAAE,KAAa;QAE3C,IAAA,mBAAK,EAAC,GAAG,EAAE,KAAK,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACpD,IAAA,mBAAK,EAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QAExD,GAAG,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;QACjB,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;QAErB,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;QAElE,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC5C,CAAC;CACJ;AAlBD,oBAkBC"}
|
|
@@ -4,6 +4,7 @@ exports.SymmetricEncryption = void 0;
|
|
|
4
4
|
const Crypto = require("crypto");
|
|
5
5
|
const crypto_exception_1 = require("./crypto-exception");
|
|
6
6
|
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
7
|
+
// public
|
|
7
8
|
class SymmetricEncryption {
|
|
8
9
|
constructor() { }
|
|
9
10
|
static generateKey() {
|
|
@@ -19,8 +20,8 @@ class SymmetricEncryption {
|
|
|
19
20
|
}
|
|
20
21
|
static encrypt(key, value) {
|
|
21
22
|
return new Promise((resolve, reject) => {
|
|
22
|
-
n_defensive_1.given(key, "key").ensureHasValue().ensureIsString();
|
|
23
|
-
n_defensive_1.given(value, "value").ensureHasValue().ensureIsString();
|
|
23
|
+
(0, n_defensive_1.given)(key, "key").ensureHasValue().ensureIsString();
|
|
24
|
+
(0, n_defensive_1.given)(value, "value").ensureHasValue().ensureIsString();
|
|
24
25
|
key = key.trim();
|
|
25
26
|
value = value.trim();
|
|
26
27
|
Crypto.randomBytes(16, (err, buf) => {
|
|
@@ -43,8 +44,8 @@ class SymmetricEncryption {
|
|
|
43
44
|
});
|
|
44
45
|
}
|
|
45
46
|
static decrypt(key, value) {
|
|
46
|
-
n_defensive_1.given(key, "key").ensureHasValue().ensureIsString();
|
|
47
|
-
n_defensive_1.given(value, "value").ensureHasValue().ensureIsString();
|
|
47
|
+
(0, n_defensive_1.given)(key, "key").ensureHasValue().ensureIsString();
|
|
48
|
+
(0, n_defensive_1.given)(value, "value").ensureHasValue().ensureIsString();
|
|
48
49
|
key = key.trim();
|
|
49
50
|
value = value.trim();
|
|
50
51
|
const splitted = value.split(".");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"symmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/symmetric-encryption.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,yDAAqD;AACrD,0DAAiD;
|
|
1
|
+
{"version":3,"file":"symmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/symmetric-encryption.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,8 @@ const hmac_1 = require("./crypto/hmac");
|
|
|
7
7
|
Object.defineProperty(exports, "Hmac", { enumerable: true, get: function () { return hmac_1.Hmac; } });
|
|
8
8
|
const hash_1 = require("./crypto/hash");
|
|
9
9
|
Object.defineProperty(exports, "Hash", { enumerable: true, get: function () { return hash_1.Hash; } });
|
|
10
|
+
// import { DigitalSignature } from "./crypto/digital-signature";
|
|
11
|
+
// import { AsymmetricEncryption } from "./crypto/asymmetric-encryption";
|
|
10
12
|
const crypto_exception_1 = require("./crypto/crypto-exception");
|
|
11
13
|
Object.defineProperty(exports, "CryptoException", { enumerable: true, get: function () { return crypto_exception_1.CryptoException; } });
|
|
12
14
|
const alg_type_1 = require("./api-security/alg-type");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,wEAAoE;AAkBhE,oGAlBK,0CAAmB,OAkBL;AAjBvB,wCAAqC;AAiBZ,qFAjBhB,WAAI,OAiBgB;AAhB7B,wCAAqC;AAgBN,qFAhBtB,WAAI,OAgBsB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,wEAAoE;AAkBhE,oGAlBK,0CAAmB,OAkBL;AAjBvB,wCAAqC;AAiBZ,qFAjBhB,WAAI,OAiBgB;AAhB7B,wCAAqC;AAgBN,qFAhBtB,WAAI,OAgBsB;AAfnC,iEAAiE;AACjE,yEAAyE;AACzE,gEAA4D;AAexD,gGAfK,kCAAe,OAeL;AAbnB,sDAAkD;AAe9C,wFAfK,kBAAO,OAeL;AAdX,gDAA6C;AAchC,sFAdJ,aAAK,OAcI;AAblB,oEAAgE;AAa5C,+FAbX,gCAAc,OAaW;AAZlC,oFAA+E;AAY3C,sGAZ3B,+CAAqB,OAY2B;AAXzD,oFAA+E;AAWpB,sGAXlD,+CAAqB,OAWkD;AAVhF,kEAA6D;AAUqB,6FAVzE,6BAAY,OAUyE;AAT9F,kEAA8D;AASkC,8FATvF,8BAAa,OASuF"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nivinjoseph/n-sec",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "Security library",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"ts-compile": "tsc -p .",
|
|
9
|
-
"ts-lint": "
|
|
9
|
+
"ts-lint": "eslint . --ext .ts",
|
|
10
10
|
"ts-build": "npm run ts-compile && npm run ts-lint",
|
|
11
11
|
"ts-build-dist": "npm run ts-build && tsc -p ./dist",
|
|
12
12
|
"clean-src": "find ./src -name '*.js' -delete -o -name '*.map' -delete",
|
|
@@ -32,17 +32,18 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/nivinjoseph/n-sec#readme",
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@types/mocha": "^
|
|
35
|
+
"@types/mocha": "^9.1.1",
|
|
36
36
|
"@types/node": "^14.14.43",
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
|
38
|
+
"@typescript-eslint/parser": "^5.25.0",
|
|
39
|
+
"eslint": "^8.15.0",
|
|
40
|
+
"mocha": "^10.0.0",
|
|
41
|
+
"ts-node": "^10.7.0",
|
|
42
|
+
"typescript": "^4.6.4"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
44
|
-
"@nivinjoseph/n-defensive": "^1.0.
|
|
45
|
-
"@nivinjoseph/n-exception": "^1.0.
|
|
46
|
-
"@nivinjoseph/n-ext": "^1.1.
|
|
45
|
+
"@nivinjoseph/n-defensive": "^1.0.39",
|
|
46
|
+
"@nivinjoseph/n-exception": "^1.0.26",
|
|
47
|
+
"@nivinjoseph/n-ext": "^1.1.35"
|
|
47
48
|
}
|
|
48
49
|
}
|
|
@@ -5,16 +5,16 @@ import { given } from "@nivinjoseph/n-defensive";
|
|
|
5
5
|
export class Claim
|
|
6
6
|
{
|
|
7
7
|
private readonly _type: string;
|
|
8
|
-
private readonly _value:
|
|
8
|
+
private readonly _value: unknown;
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
public get type(): string { return this._type; }
|
|
12
|
-
public get value():
|
|
12
|
+
public get value(): unknown { return this._value; }
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
public constructor(type: string, value:
|
|
15
|
+
public constructor(type: string, value: unknown)
|
|
16
16
|
{
|
|
17
|
-
given(type, "type").ensureHasValue();
|
|
17
|
+
given(type, "type").ensureHasValue().ensureIsString();
|
|
18
18
|
|
|
19
19
|
this._type = type.trim();
|
|
20
20
|
this._value = value;
|
|
@@ -23,6 +23,7 @@ export class Claim
|
|
|
23
23
|
|
|
24
24
|
public equals(claim: Claim): boolean
|
|
25
25
|
{
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
26
27
|
if (claim == null)
|
|
27
28
|
return false;
|
|
28
29
|
|
|
@@ -13,7 +13,7 @@ export class ExpiredTokenException extends Exception
|
|
|
13
13
|
|
|
14
14
|
public constructor(token: string)
|
|
15
15
|
{
|
|
16
|
-
given(token, "token").ensureHasValue();
|
|
16
|
+
given(token, "token").ensureHasValue().ensureIsString();
|
|
17
17
|
token = token.trim();
|
|
18
18
|
super(`Token '${token}' is expired.`);
|
|
19
19
|
this._token = token;
|
|
@@ -15,8 +15,8 @@ export class InvalidTokenException extends Exception
|
|
|
15
15
|
|
|
16
16
|
public constructor(token: string, reason: string)
|
|
17
17
|
{
|
|
18
|
-
given(token, "token").ensureHasValue();
|
|
19
|
-
given(reason, "reason").ensureHasValue();
|
|
18
|
+
given(token, "token").ensureHasValue().ensureIsString();
|
|
19
|
+
given(reason, "reason").ensureHasValue().ensureIsString();
|
|
20
20
|
|
|
21
21
|
token = token.trim();
|
|
22
22
|
super(`Token '${token}' is invalid because ${reason}.`);
|
|
@@ -31,14 +31,13 @@ export class JsonWebToken
|
|
|
31
31
|
private constructor(issuer: string, algType: AlgType, key: string, isFullKey: boolean, expiry: number,
|
|
32
32
|
claims: Array<Claim>)
|
|
33
33
|
{
|
|
34
|
-
given(issuer, "issuer").ensureHasValue();
|
|
34
|
+
given(issuer, "issuer").ensureHasValue().ensureIsString();
|
|
35
35
|
given(algType, "algType").ensureHasValue().ensureIsEnum(AlgType);
|
|
36
|
-
given(key, "key").ensureHasValue();
|
|
37
|
-
given(isFullKey, "isFullKey").ensureHasValue();
|
|
38
|
-
given(expiry, "expiry").ensureHasValue();
|
|
39
|
-
given(claims, "claims")
|
|
40
|
-
.
|
|
41
|
-
.ensure(t => t.length > 0);
|
|
36
|
+
given(key, "key").ensureHasValue().ensureIsString();
|
|
37
|
+
given(isFullKey, "isFullKey").ensureHasValue().ensureIsBoolean();
|
|
38
|
+
given(expiry, "expiry").ensureHasValue().ensureIsNumber();
|
|
39
|
+
given(claims, "claims").ensureHasValue().ensureIsArray()
|
|
40
|
+
.ensure(t => t.isNotEmpty, "cannot be empty");
|
|
42
41
|
|
|
43
42
|
this._issuer = issuer.trim();
|
|
44
43
|
this._algType = algType;
|
|
@@ -48,84 +47,61 @@ export class JsonWebToken
|
|
|
48
47
|
this._claims = [...claims];
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
public generateToken(): string
|
|
52
|
-
{
|
|
53
|
-
if (!this._isfullKey)
|
|
54
|
-
throw new InvalidOperationException("generating token using an instance created from token");
|
|
55
|
-
|
|
56
|
-
const header: Header = {
|
|
57
|
-
iss: this._issuer,
|
|
58
|
-
alg: this._algType,
|
|
59
|
-
exp: this._expiry
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const body: any = {};
|
|
63
|
-
this._claims.forEach(t => body[t.type] = t.value);
|
|
64
|
-
|
|
65
|
-
const headerAndBody = this.toHex(header) + "." + this.toHex(body);
|
|
66
|
-
|
|
67
|
-
// let signature = this._algType === AlgType.hmac
|
|
68
|
-
// ? await Hmac.create(this._key, headerAndBody)
|
|
69
|
-
// : await DigitalSignature.sign(this._key, headerAndBody);
|
|
70
|
-
|
|
71
|
-
const signature = Hmac.create(this._key, headerAndBody);
|
|
72
|
-
|
|
73
|
-
const token = headerAndBody + "." + signature;
|
|
74
|
-
return token;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
50
|
public static fromClaims(issuer: string, algType: AlgType, key: string, expiry: number,
|
|
79
51
|
claims: Array<Claim>): JsonWebToken
|
|
80
52
|
{
|
|
81
53
|
return new JsonWebToken(issuer, algType, key, true, expiry, claims);
|
|
82
54
|
}
|
|
83
|
-
|
|
55
|
+
|
|
84
56
|
public static fromToken(issuer: string, algType: AlgType, key: string, token: string): JsonWebToken
|
|
85
57
|
{
|
|
86
58
|
given(issuer, "issuer").ensureHasValue();
|
|
87
59
|
given(algType, "algType").ensureHasValue().ensureIsEnum(AlgType);
|
|
88
60
|
given(key, "key").ensureHasValue();
|
|
89
61
|
given(token, "token").ensureHasValue();
|
|
90
|
-
|
|
62
|
+
|
|
91
63
|
issuer = issuer.trim();
|
|
92
64
|
key = key.trim();
|
|
93
65
|
token = token.trim();
|
|
94
|
-
|
|
66
|
+
|
|
95
67
|
const tokenSplitted = token.split(".");
|
|
96
68
|
if (tokenSplitted.length !== 3)
|
|
97
69
|
throw new InvalidTokenException(token, "format is incorrect");
|
|
98
|
-
|
|
70
|
+
|
|
99
71
|
const headerString = tokenSplitted[0];
|
|
100
72
|
const bodyString = tokenSplitted[1];
|
|
101
73
|
const signature = tokenSplitted[2];
|
|
102
|
-
|
|
103
|
-
const header: Header = JsonWebToken.
|
|
104
|
-
const body: any = JsonWebToken.
|
|
105
|
-
|
|
74
|
+
|
|
75
|
+
const header: Header = JsonWebToken._toObject(headerString) as Header;
|
|
76
|
+
const body: any = JsonWebToken._toObject(bodyString);
|
|
77
|
+
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
106
79
|
if (header.iss === undefined || header.iss === null)
|
|
107
80
|
throw new InvalidTokenException(token, "iss was not present");
|
|
108
|
-
|
|
81
|
+
|
|
109
82
|
if (header.iss !== issuer)
|
|
110
83
|
throw new InvalidTokenException(token,
|
|
111
|
-
`iss was expected to be '${issuer}' but instead was '${header.iss}'`);
|
|
112
|
-
|
|
84
|
+
`iss was expected to be '${issuer}' but instead was '${header.iss}'`);
|
|
85
|
+
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
113
87
|
if (header.alg === undefined || header.alg === null)
|
|
114
88
|
throw new InvalidTokenException(token, "alg was not present");
|
|
115
|
-
|
|
89
|
+
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
116
91
|
if (header.alg !== algType)
|
|
117
92
|
throw new InvalidTokenException(token,
|
|
118
|
-
`alg was expected to be '${algType}' but instead was '${header.alg}'`);
|
|
119
|
-
|
|
93
|
+
`alg was expected to be '${algType}' but instead was '${header.alg}'`);
|
|
94
|
+
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
120
96
|
if (header.exp === undefined || header.exp === null)
|
|
121
97
|
throw new InvalidTokenException(token, "exp was not present");
|
|
122
|
-
|
|
123
|
-
if (typeof
|
|
98
|
+
|
|
99
|
+
if (typeof header.exp !== "number")
|
|
124
100
|
throw new InvalidTokenException(token, `exp value '${header.exp}' is invalid`);
|
|
125
|
-
|
|
101
|
+
|
|
126
102
|
if (header.exp <= Date.now())
|
|
127
103
|
throw new ExpiredTokenException(token);
|
|
128
|
-
|
|
104
|
+
|
|
129
105
|
// if (algType === AlgType.hmac)
|
|
130
106
|
// {
|
|
131
107
|
// let computedSignature = await Hmac.create(key, headerString + "." + bodyString);
|
|
@@ -138,30 +114,56 @@ export class JsonWebToken
|
|
|
138
114
|
// if (!verification)
|
|
139
115
|
// throw new InvalidTokenException(token, "signature could not be verified");
|
|
140
116
|
// }
|
|
141
|
-
|
|
117
|
+
|
|
142
118
|
const computedSignature = Hmac.create(key, headerString + "." + bodyString);
|
|
143
119
|
if (computedSignature !== signature)
|
|
144
|
-
throw new InvalidTokenException(token, "signature could not be verified");
|
|
145
|
-
|
|
120
|
+
throw new InvalidTokenException(token, "signature could not be verified");
|
|
121
|
+
|
|
146
122
|
const claims = new Array<Claim>();
|
|
147
|
-
for (
|
|
148
|
-
claims.push(new Claim(item, body[item]));
|
|
149
|
-
|
|
123
|
+
for (const item in body)
|
|
124
|
+
claims.push(new Claim(item, body[item]));
|
|
125
|
+
|
|
150
126
|
return new JsonWebToken(issuer, algType, key, false, header.exp, claims);
|
|
151
127
|
}
|
|
128
|
+
|
|
129
|
+
private static _toObject(hex: string): object
|
|
130
|
+
{
|
|
131
|
+
const json = Buffer.from(hex.toLowerCase(), "hex").toString("utf8");
|
|
132
|
+
const obj = JSON.parse(json) as object;
|
|
133
|
+
return obj;
|
|
134
|
+
}
|
|
152
135
|
|
|
153
|
-
|
|
136
|
+
public generateToken(): string
|
|
154
137
|
{
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
138
|
+
if (!this._isfullKey)
|
|
139
|
+
throw new InvalidOperationException("generating token using an instance created from token");
|
|
140
|
+
|
|
141
|
+
const header: Header = {
|
|
142
|
+
iss: this._issuer,
|
|
143
|
+
alg: this._algType,
|
|
144
|
+
exp: this._expiry
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const body: any = {};
|
|
148
|
+
this._claims.forEach(t => body[t.type] = t.value);
|
|
149
|
+
|
|
150
|
+
const headerAndBody = this._toHex(header) + "." + this._toHex(body);
|
|
151
|
+
|
|
152
|
+
// let signature = this._algType === AlgType.hmac
|
|
153
|
+
// ? await Hmac.create(this._key, headerAndBody)
|
|
154
|
+
// : await DigitalSignature.sign(this._key, headerAndBody);
|
|
155
|
+
|
|
156
|
+
const signature = Hmac.create(this._key, headerAndBody);
|
|
157
|
+
|
|
158
|
+
const token = headerAndBody + "." + signature;
|
|
159
|
+
return token;
|
|
158
160
|
}
|
|
159
161
|
|
|
160
|
-
private
|
|
162
|
+
private _toHex(obj: object): string
|
|
161
163
|
{
|
|
162
|
-
const json =
|
|
163
|
-
const
|
|
164
|
-
return
|
|
164
|
+
const json = JSON.stringify(obj);
|
|
165
|
+
const hex = Buffer.from(json, "utf8").toString("hex");
|
|
166
|
+
return hex.toUpperCase();
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
|
package/src/crypto/hash.ts
CHANGED
|
@@ -11,7 +11,6 @@ export class Hash
|
|
|
11
11
|
public static create(value: string): string
|
|
12
12
|
{
|
|
13
13
|
given(value, "value").ensureHasValue().ensureIsString();
|
|
14
|
-
|
|
15
14
|
value = value.trim();
|
|
16
15
|
|
|
17
16
|
const hash = Crypto.createHash("sha512");
|
|
@@ -27,7 +26,7 @@ export class Hash
|
|
|
27
26
|
value = value.trim();
|
|
28
27
|
salt = salt.trim();
|
|
29
28
|
|
|
30
|
-
const reverse = (val: string) =>
|
|
29
|
+
const reverse = (val: string): string =>
|
|
31
30
|
{
|
|
32
31
|
let rev = "";
|
|
33
32
|
for (let i = 0; i < val.length; i++)
|