@nivinjoseph/n-sec 5.0.4 → 6.0.1
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/.eslintrc +13 -4
- package/.vscode/settings.json +103 -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 +15 -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 +9 -12
- 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 +14 -12
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -27
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.json +2 -1
- package/package.json +23 -24
- package/src/api-security/claims-identity.ts +1 -1
- package/src/api-security/json-web-token.ts +21 -21
- package/src/bin.ts +1 -1
- package/src/crypto/hash.ts +11 -9
- package/src/crypto/hmac.ts +7 -7
- package/src/crypto/symmetric-encryption.ts +11 -11
- package/src/index.ts +19 -18
- package/test/asymmetric-encryption.test.ts +37 -37
- package/test/digital-signature.test.ts +22 -22
- package/test/hash.test.ts +120 -105
- 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/dist/crypto/hash.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/crypto/hash.ts"],"names":[],"mappings":"AAKA,qBAAa,IAAI;IAEb,OAAO;WAGO,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;WAU7B,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM;CAyBrE"}
|
package/dist/crypto/hash.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Hash = void 0;
|
|
4
|
-
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
5
|
-
const Crypto = require("crypto");
|
|
1
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
6
3
|
// public
|
|
7
|
-
class Hash {
|
|
4
|
+
export class Hash {
|
|
8
5
|
constructor() { }
|
|
9
6
|
static create(value) {
|
|
10
|
-
|
|
7
|
+
given(value, "value").ensureHasValue().ensureIsString();
|
|
11
8
|
value = value.trim();
|
|
12
|
-
const hash =
|
|
9
|
+
const hash = createHash("sha512");
|
|
13
10
|
hash.update(value, "utf8");
|
|
14
11
|
return hash.digest("hex").toUpperCase();
|
|
15
12
|
}
|
|
16
13
|
static createUsingSalt(value, salt) {
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
given(value, "value").ensureHasValue().ensureIsString();
|
|
15
|
+
given(salt, "salt").ensureHasValue().ensureIsString();
|
|
19
16
|
value = value.trim();
|
|
20
17
|
salt = salt.trim();
|
|
21
18
|
const reverse = (val) => {
|
|
@@ -26,9 +23,9 @@ class Hash {
|
|
|
26
23
|
};
|
|
27
24
|
const valueReverse = reverse(value);
|
|
28
25
|
const saltReverse = reverse(salt);
|
|
29
|
-
const saltedValue = "{1}{0}{2}{1}{3}{1}{2}".format(value, salt, valueReverse, saltReverse);
|
|
26
|
+
// const saltedValue = "{1}{0}{2}{1}{3}{1}{2}".format(value, salt, valueReverse, saltReverse);
|
|
27
|
+
const saltedValue = `${salt}${value}${valueReverse}${salt}${saltReverse}${salt}${valueReverse}`;
|
|
30
28
|
return Hash.create(saltedValue);
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
exports.Hash = Hash;
|
|
34
31
|
//# sourceMappingURL=hash.js.map
|
package/dist/crypto/hash.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/crypto/hash.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/crypto/hash.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,SAAS;AACT,MAAM,OAAO,IAAI;IAEb,gBAAwB,CAAC;IAGlB,MAAM,CAAC,MAAM,CAAC,KAAa;QAE9B,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACxD,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;QAErB,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;QAClC,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,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACxD,KAAK,CAAC,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,8FAA8F;QAE9F,MAAM,WAAW,GAAG,GAAG,IAAI,GAAG,KAAK,GAAG,YAAY,GAAG,IAAI,GAAG,WAAW,GAAG,IAAI,GAAG,YAAY,EAAE,CAAC;QAEhG,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACpC,CAAC;CACJ"}
|
package/dist/crypto/hmac.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":"AAKA,qBAAa,IAAI;IAEb,OAAO;WAGO,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAa3D"}
|
package/dist/crypto/hmac.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Hmac = void 0;
|
|
4
|
-
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
5
|
-
const Crypto = require("crypto");
|
|
1
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
+
import { createHmac } from "node:crypto";
|
|
6
3
|
// public
|
|
7
|
-
class Hmac {
|
|
4
|
+
export class Hmac {
|
|
8
5
|
constructor() { }
|
|
9
6
|
static create(key, value) {
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
given(key, "key").ensureHasValue().ensureIsString();
|
|
8
|
+
given(value, "value").ensureHasValue().ensureIsString();
|
|
12
9
|
key = key.trim();
|
|
13
10
|
value = value.trim();
|
|
14
|
-
const hmac =
|
|
11
|
+
const hmac = createHmac("sha256", Buffer.from(key, "hex"));
|
|
15
12
|
hmac.update(value, "utf8");
|
|
16
13
|
return hmac.digest("hex").toUpperCase();
|
|
17
14
|
}
|
|
18
15
|
}
|
|
19
|
-
exports.Hmac = Hmac;
|
|
20
16
|
//# sourceMappingURL=hmac.js.map
|
package/dist/crypto/hmac.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/crypto/hmac.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,SAAS;AACT,MAAM,OAAO,IAAI;IAEb,gBAAwB,CAAC;IAGlB,MAAM,CAAC,MAAM,CAAC,GAAW,EAAE,KAAa;QAE3C,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,cAAc,EAAE,CAAC,cAAc,EAAE,CAAC;QACpD,KAAK,CAAC,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,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;QAE3D,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAC5C,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"symmetric-encryption.d.ts","sourceRoot":"","sources":["../../src/crypto/symmetric-encryption.ts"],"names":[],"mappings":"AAMA,qBAAa,mBAAmB;IAE5B,OAAO;WAGO,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC;WAiB9B,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;WAmCpD,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM;CAkB5D"}
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const Crypto = require("crypto");
|
|
5
|
-
const crypto_exception_1 = require("./crypto-exception");
|
|
6
|
-
const n_defensive_1 = require("@nivinjoseph/n-defensive");
|
|
1
|
+
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
3
|
+
import { CryptoException } from "./crypto-exception.js";
|
|
7
4
|
// public
|
|
8
|
-
class SymmetricEncryption {
|
|
5
|
+
export class SymmetricEncryption {
|
|
9
6
|
constructor() { }
|
|
10
7
|
static generateKey() {
|
|
11
8
|
return new Promise((resolve, reject) => {
|
|
12
|
-
|
|
9
|
+
randomBytes(32, (err, buf) => {
|
|
13
10
|
if (err) {
|
|
14
11
|
reject(err);
|
|
15
12
|
return;
|
|
@@ -20,18 +17,18 @@ class SymmetricEncryption {
|
|
|
20
17
|
}
|
|
21
18
|
static encrypt(key, value) {
|
|
22
19
|
return new Promise((resolve, reject) => {
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
given(key, "key").ensureHasValue().ensureIsString();
|
|
21
|
+
given(value, "value").ensureHasValue().ensureIsString();
|
|
25
22
|
key = key.trim();
|
|
26
23
|
value = value.trim();
|
|
27
|
-
|
|
24
|
+
randomBytes(16, (err, buf) => {
|
|
28
25
|
if (err) {
|
|
29
26
|
reject(err);
|
|
30
27
|
return;
|
|
31
28
|
}
|
|
32
29
|
try {
|
|
33
30
|
const iv = buf;
|
|
34
|
-
const cipher =
|
|
31
|
+
const cipher = createCipheriv("AES-256-CBC", Buffer.from(key, "hex"), iv);
|
|
35
32
|
let encrypted = cipher.update(value, "utf8", "hex");
|
|
36
33
|
encrypted += cipher.final("hex");
|
|
37
34
|
const cipherText = `${encrypted}.${iv.toString("hex")}`;
|
|
@@ -44,19 +41,18 @@ class SymmetricEncryption {
|
|
|
44
41
|
});
|
|
45
42
|
}
|
|
46
43
|
static decrypt(key, value) {
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
given(key, "key").ensureHasValue().ensureIsString();
|
|
45
|
+
given(value, "value").ensureHasValue().ensureIsString();
|
|
49
46
|
key = key.trim();
|
|
50
47
|
value = value.trim();
|
|
51
48
|
const splitted = value.split(".");
|
|
52
49
|
if (splitted.length !== 2)
|
|
53
|
-
throw new
|
|
50
|
+
throw new CryptoException("Invalid value.");
|
|
54
51
|
const iv = Buffer.from(splitted[1], "hex");
|
|
55
|
-
const deCipher =
|
|
52
|
+
const deCipher = createDecipheriv("AES-256-CBC", Buffer.from(key, "hex"), iv);
|
|
56
53
|
let decrypted = deCipher.update(splitted[0], "hex", "utf8");
|
|
57
54
|
decrypted += deCipher.final("utf8");
|
|
58
55
|
return decrypted;
|
|
59
56
|
}
|
|
60
57
|
}
|
|
61
|
-
exports.SymmetricEncryption = SymmetricEncryption;
|
|
62
58
|
//# sourceMappingURL=symmetric-encryption.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"symmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/symmetric-encryption.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"symmetric-encryption.js","sourceRoot":"","sources":["../../src/crypto/symmetric-encryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK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AC,CAAC;QAC9E,IAAI,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QAC5D,SAAS,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACpC,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
1
|
+
import "@nivinjoseph/n-ext";
|
|
2
|
+
import { Hash } from "./crypto/hash.js";
|
|
3
|
+
import { Hmac } from "./crypto/hmac.js";
|
|
4
|
+
import { SymmetricEncryption } from "./crypto/symmetric-encryption.js";
|
|
5
|
+
import { CryptoException } from "./crypto/crypto-exception.js";
|
|
6
|
+
import { AlgType } from "./api-security/alg-type.js";
|
|
7
|
+
import { Claim } from "./api-security/claim.js";
|
|
8
|
+
import { ClaimsIdentity } from "./api-security/claims-identity.js";
|
|
9
|
+
import { ExpiredTokenException } from "./api-security/expired-token-exception.js";
|
|
10
|
+
import { InvalidTokenException } from "./api-security/invalid-token-exception.js";
|
|
11
|
+
import { JsonWebToken } from "./api-security/json-web-token.js";
|
|
12
|
+
import { SecurityToken } from "./api-security/security-token.js";
|
|
13
|
+
export { AlgType, Claim, ClaimsIdentity, CryptoException, ExpiredTokenException, Hash, Hmac, InvalidTokenException, JsonWebToken, SecurityToken, SymmetricEncryption };
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,CAAC;AAE5B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAGvE,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAGjE,OACA,EAEI,OAAO,EAAE,KAAK,EAAE,cAAc,EAE9B,eAAe,EAAE,qBAAqB,EAAE,IAAI,EAAE,IAAI,EAAE,qBAAqB,EAAE,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAC9H,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const claims_identity_1 = require("./api-security/claims-identity");
|
|
19
|
-
Object.defineProperty(exports, "ClaimsIdentity", { enumerable: true, get: function () { return claims_identity_1.ClaimsIdentity; } });
|
|
20
|
-
const expired_token_exception_1 = require("./api-security/expired-token-exception");
|
|
21
|
-
Object.defineProperty(exports, "ExpiredTokenException", { enumerable: true, get: function () { return expired_token_exception_1.ExpiredTokenException; } });
|
|
22
|
-
const invalid_token_exception_1 = require("./api-security/invalid-token-exception");
|
|
23
|
-
Object.defineProperty(exports, "InvalidTokenException", { enumerable: true, get: function () { return invalid_token_exception_1.InvalidTokenException; } });
|
|
24
|
-
const json_web_token_1 = require("./api-security/json-web-token");
|
|
25
|
-
Object.defineProperty(exports, "JsonWebToken", { enumerable: true, get: function () { return json_web_token_1.JsonWebToken; } });
|
|
26
|
-
const security_token_1 = require("./api-security/security-token");
|
|
27
|
-
Object.defineProperty(exports, "SecurityToken", { enumerable: true, get: function () { return security_token_1.SecurityToken; } });
|
|
1
|
+
import "@nivinjoseph/n-ext";
|
|
2
|
+
import { Hash } from "./crypto/hash.js";
|
|
3
|
+
import { Hmac } from "./crypto/hmac.js";
|
|
4
|
+
import { SymmetricEncryption } from "./crypto/symmetric-encryption.js";
|
|
5
|
+
// import { DigitalSignature } from "./crypto/digital-signature.js";
|
|
6
|
+
// import { AsymmetricEncryption } from "./crypto/asymmetric-encryption.js";
|
|
7
|
+
import { CryptoException } from "./crypto/crypto-exception.js";
|
|
8
|
+
import { AlgType } from "./api-security/alg-type.js";
|
|
9
|
+
import { Claim } from "./api-security/claim.js";
|
|
10
|
+
import { ClaimsIdentity } from "./api-security/claims-identity.js";
|
|
11
|
+
import { ExpiredTokenException } from "./api-security/expired-token-exception.js";
|
|
12
|
+
import { InvalidTokenException } from "./api-security/invalid-token-exception.js";
|
|
13
|
+
import { JsonWebToken } from "./api-security/json-web-token.js";
|
|
14
|
+
import { SecurityToken } from "./api-security/security-token.js";
|
|
15
|
+
export { AlgType, Claim, ClaimsIdentity,
|
|
16
|
+
// DigitalSignature, AsymmetricEncryption,
|
|
17
|
+
CryptoException, ExpiredTokenException, Hash, Hmac, InvalidTokenException, JsonWebToken, SecurityToken, SymmetricEncryption };
|
|
28
18
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,CAAC;AAE5B,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,oEAAoE;AACpE,4EAA4E;AAC5E,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAGjE,OACA,EAEI,OAAO,EAAE,KAAK,EAAE,cAAc;AAC9B,0CAA0C;AAC1C,eAAe,EAAE,qBAAqB,EAAE,IAAI,EAAE,IAAI,EAAE,qBAAqB,EAAE,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAC9H,CAAC"}
|
package/dist/tsconfig.json
CHANGED
package/package.json
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nivinjoseph/n-sec",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Security library",
|
|
5
|
-
"
|
|
5
|
+
"packageManager": "yarn@4.0.2",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": "./dist/index.js",
|
|
6
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": "./dist/bin.js",
|
|
7
10
|
"scripts": {
|
|
8
11
|
"ts-compile": "tsc -p .",
|
|
9
12
|
"ts-lint": "eslint . --ext .ts",
|
|
10
|
-
"ts-build": "
|
|
11
|
-
"ts-build-dist": "
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"pretest": "npm run ts-build",
|
|
15
|
-
"test": "mocha --timeout 5000 --reporter spec --ui tdd --require ts-node/register './test/**/*.test.js' || true",
|
|
16
|
-
"pretest-ci": "npm run pretest",
|
|
17
|
-
"test-ci": "mocha --timeout 5000 --reporter spec --ui tdd --require ts-node/register './test/**/*.test.js'",
|
|
18
|
-
"publish-package": "npm run ts-build-dist && git add . && git commit -m 'preparing to publish new version' && npm version patch && git push && npm publish --access=public"
|
|
13
|
+
"ts-build": "yarn ts-compile && yarn ts-lint",
|
|
14
|
+
"ts-build-dist": "yarn ts-build && tsc -p ./dist",
|
|
15
|
+
"test": "yarn ts-build && node --test --enable-source-maps ./test",
|
|
16
|
+
"publish-package": "yarn ts-build-dist && git add . && git commit -m 'preparing to publish new version' && npm version patch && git push && npm publish --access=public"
|
|
19
17
|
},
|
|
20
|
-
"bin": "./dist/bin.js",
|
|
21
18
|
"repository": {
|
|
22
19
|
"type": "git",
|
|
23
20
|
"url": "git+https://github.com/nivinjoseph/n-sec.git"
|
|
@@ -33,19 +30,21 @@
|
|
|
33
30
|
},
|
|
34
31
|
"homepage": "https://github.com/nivinjoseph/n-sec#readme",
|
|
35
32
|
"devDependencies": {
|
|
36
|
-
"@types/
|
|
37
|
-
"@
|
|
38
|
-
"@typescript-eslint/
|
|
39
|
-
"
|
|
40
|
-
"eslint": "^
|
|
41
|
-
"
|
|
42
|
-
"ts-node": "^10.7.0",
|
|
43
|
-
"typescript": "^4.6.4"
|
|
33
|
+
"@types/node": "^20.10",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^6.15.0",
|
|
35
|
+
"@typescript-eslint/parser": "^6.15.0",
|
|
36
|
+
"eslint": "^8.56.0",
|
|
37
|
+
"eslint-plugin-require-extensions": "^0.1.3",
|
|
38
|
+
"typescript": "^5.3.3"
|
|
44
39
|
},
|
|
45
40
|
"dependencies": {
|
|
46
|
-
"@nivinjoseph/n-defensive": "^
|
|
47
|
-
"@nivinjoseph/n-exception": "^
|
|
48
|
-
"@nivinjoseph/n-ext": "^
|
|
49
|
-
"@nivinjoseph/n-util": "^
|
|
41
|
+
"@nivinjoseph/n-defensive": "^2.0.1",
|
|
42
|
+
"@nivinjoseph/n-exception": "^2.0.1",
|
|
43
|
+
"@nivinjoseph/n-ext": "^2.0.1",
|
|
44
|
+
"@nivinjoseph/n-util": "^2.0.1"
|
|
45
|
+
},
|
|
46
|
+
"engineStrict": true,
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=20.10"
|
|
50
49
|
}
|
|
51
50
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Claim } from "./claim";
|
|
2
|
-
import { InvalidOperationException } from "@nivinjoseph/n-exception";
|
|
3
1
|
import { given } from "@nivinjoseph/n-defensive";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import {
|
|
2
|
+
import { InvalidOperationException } from "@nivinjoseph/n-exception";
|
|
3
|
+
import { Hmac } from "./../crypto/hmac.js";
|
|
4
|
+
import { AlgType } from "./alg-type.js";
|
|
5
|
+
import { Claim } from "./claim.js";
|
|
6
|
+
import { InvalidTokenException } from "./invalid-token-exception.js";
|
|
7
|
+
// import { DigitalSignature } from "./../crypto/digital-signature.js";
|
|
8
|
+
import { ExpiredTokenException } from "./expired-token-exception.js";
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
// public
|
|
@@ -18,7 +18,7 @@ export class JsonWebToken
|
|
|
18
18
|
private readonly _expiry: number;
|
|
19
19
|
private readonly _claims: Array<Claim>;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
public get issuer(): string { return this._issuer; }
|
|
23
23
|
public get algType(): AlgType { return this._algType; }
|
|
24
24
|
public get key(): string { return this._key; }
|
|
@@ -26,8 +26,8 @@ export class JsonWebToken
|
|
|
26
26
|
public get expiry(): number { return this._expiry; }
|
|
27
27
|
public get isExpired(): boolean { return this._expiry <= Date.now(); }
|
|
28
28
|
public get claims(): ReadonlyArray<Claim> { return this._claims; }
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
|
|
30
|
+
|
|
31
31
|
private constructor(issuer: string, algType: AlgType, key: string, isFullKey: boolean, expiry: number,
|
|
32
32
|
claims: Array<Claim>)
|
|
33
33
|
{
|
|
@@ -38,7 +38,7 @@ export class JsonWebToken
|
|
|
38
38
|
given(expiry, "expiry").ensureHasValue().ensureIsNumber();
|
|
39
39
|
given(claims, "claims").ensureHasValue().ensureIsArray()
|
|
40
40
|
.ensure(t => t.isNotEmpty, "cannot be empty");
|
|
41
|
-
|
|
41
|
+
|
|
42
42
|
this._issuer = issuer.trim();
|
|
43
43
|
this._algType = algType;
|
|
44
44
|
this._key = key.trim();
|
|
@@ -46,7 +46,7 @@ export class JsonWebToken
|
|
|
46
46
|
this._expiry = expiry;
|
|
47
47
|
this._claims = [...claims];
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
public static fromClaims(issuer: string, algType: AlgType, key: string, expiry: number,
|
|
51
51
|
claims: Array<Claim>): JsonWebToken
|
|
52
52
|
{
|
|
@@ -132,33 +132,33 @@ export class JsonWebToken
|
|
|
132
132
|
const obj = JSON.parse(json) as object;
|
|
133
133
|
return obj;
|
|
134
134
|
}
|
|
135
|
-
|
|
135
|
+
|
|
136
136
|
public generateToken(): string
|
|
137
137
|
{
|
|
138
138
|
if (!this._isfullKey)
|
|
139
|
-
throw new InvalidOperationException("generating token using an instance created from token");
|
|
140
|
-
|
|
139
|
+
throw new InvalidOperationException("generating token using an instance created from token");
|
|
140
|
+
|
|
141
141
|
const header: Header = {
|
|
142
142
|
iss: this._issuer,
|
|
143
143
|
alg: this._algType,
|
|
144
144
|
exp: this._expiry
|
|
145
145
|
};
|
|
146
|
-
|
|
146
|
+
|
|
147
147
|
const body: any = {};
|
|
148
148
|
this._claims.forEach(t => body[t.type] = t.value);
|
|
149
|
-
|
|
149
|
+
|
|
150
150
|
const headerAndBody = this._toHex(header) + "." + this._toHex(body);
|
|
151
|
-
|
|
151
|
+
|
|
152
152
|
// let signature = this._algType === AlgType.hmac
|
|
153
153
|
// ? await Hmac.create(this._key, headerAndBody)
|
|
154
154
|
// : await DigitalSignature.sign(this._key, headerAndBody);
|
|
155
|
-
|
|
155
|
+
|
|
156
156
|
const signature = Hmac.create(this._key, headerAndBody);
|
|
157
|
-
|
|
157
|
+
|
|
158
158
|
const token = headerAndBody + "." + signature;
|
|
159
159
|
return token;
|
|
160
160
|
}
|
|
161
|
-
|
|
161
|
+
|
|
162
162
|
private _toHex(obj: object): string
|
|
163
163
|
{
|
|
164
164
|
const json = JSON.stringify(obj);
|
package/src/bin.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// import { given } from "@nivinjoseph/n-defensive";
|
|
4
4
|
import { TypeHelper } from "@nivinjoseph/n-util";
|
|
5
|
-
import { SymmetricEncryption } from "./crypto/symmetric-encryption";
|
|
5
|
+
import { SymmetricEncryption } from "./crypto/symmetric-encryption.js";
|
|
6
6
|
|
|
7
7
|
enum SupportedCommands
|
|
8
8
|
{
|
package/src/crypto/hash.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
-
import
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
// public
|
|
6
6
|
export class Hash
|
|
7
7
|
{
|
|
8
8
|
private constructor() { }
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
|
|
11
11
|
public static create(value: string): string
|
|
12
12
|
{
|
|
13
13
|
given(value, "value").ensureHasValue().ensureIsString();
|
|
14
14
|
value = value.trim();
|
|
15
|
-
|
|
16
|
-
const hash =
|
|
15
|
+
|
|
16
|
+
const hash = createHash("sha512");
|
|
17
17
|
hash.update(value, "utf8");
|
|
18
18
|
return hash.digest("hex").toUpperCase();
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
|
|
21
21
|
public static createUsingSalt(value: string, salt: string): string
|
|
22
22
|
{
|
|
23
23
|
given(value, "value").ensureHasValue().ensureIsString();
|
|
@@ -25,7 +25,7 @@ export class Hash
|
|
|
25
25
|
|
|
26
26
|
value = value.trim();
|
|
27
27
|
salt = salt.trim();
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
const reverse = (val: string): string =>
|
|
30
30
|
{
|
|
31
31
|
let rev = "";
|
|
@@ -33,12 +33,14 @@ export class Hash
|
|
|
33
33
|
rev = val[i] + rev;
|
|
34
34
|
return rev;
|
|
35
35
|
};
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
const valueReverse = reverse(value);
|
|
38
38
|
const saltReverse = reverse(salt);
|
|
39
39
|
|
|
40
|
-
const saltedValue = "{1}{0}{2}{1}{3}{1}{2}".format(value, salt, valueReverse, saltReverse);
|
|
41
|
-
|
|
40
|
+
// const saltedValue = "{1}{0}{2}{1}{3}{1}{2}".format(value, salt, valueReverse, saltReverse);
|
|
41
|
+
|
|
42
|
+
const saltedValue = `${salt}${value}${valueReverse}${salt}${saltReverse}${salt}${valueReverse}`;
|
|
43
|
+
|
|
42
44
|
return Hash.create(saltedValue);
|
|
43
45
|
}
|
|
44
46
|
}
|
package/src/crypto/hmac.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import { given } from "@nivinjoseph/n-defensive";
|
|
2
|
-
import
|
|
2
|
+
import { createHmac } from "node:crypto";
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
// public
|
|
6
6
|
export class Hmac
|
|
7
7
|
{
|
|
8
8
|
private constructor() { }
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
|
|
10
|
+
|
|
11
11
|
public static create(key: string, value: string): string
|
|
12
12
|
{
|
|
13
13
|
given(key, "key").ensureHasValue().ensureIsString();
|
|
14
14
|
given(value, "value").ensureHasValue().ensureIsString();
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
key = key.trim();
|
|
17
17
|
value = value.trim();
|
|
18
|
-
|
|
19
|
-
const hmac =
|
|
20
|
-
|
|
18
|
+
|
|
19
|
+
const hmac = createHmac("sha256", Buffer.from(key, "hex"));
|
|
20
|
+
|
|
21
21
|
hmac.update(value, "utf8");
|
|
22
22
|
return hmac.digest("hex").toUpperCase();
|
|
23
23
|
}
|