jsonauthtoken 3.0.0 → 3.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/dist/index.js +3 -3
- package/dist/{crypto/node.crypto.d.ts → runtime/node.runtime.d.ts} +11 -9
- package/dist/{crypto/node.crypto.js → runtime/node.runtime.js} +52 -7
- package/dist/{crypto/crypto.d.ts → runtime/runtime.d.ts} +1 -1
- package/dist/{crypto/crypto.js → runtime/runtime.js} +12 -12
- package/package.json +1 -1
- /package/dist/{crypto/web.crypto.d.ts → runtime/web.runtime.d.ts} +0 -0
- /package/dist/{crypto/web.crypto.js → runtime/web.runtime.js} +0 -0
package/dist/index.js
CHANGED
|
@@ -6,11 +6,11 @@ const runtime_config_1 = require("./config/runtime.config");
|
|
|
6
6
|
const algo_config_1 = require("./config/algo.config");
|
|
7
7
|
const functions_lib_1 = require("./lib/functions.lib");
|
|
8
8
|
const timeformat_1 = require("./lib/timeformat");
|
|
9
|
-
const
|
|
9
|
+
const runtime_1 = require("./runtime/runtime");
|
|
10
10
|
class JATClass {
|
|
11
11
|
runtime;
|
|
12
12
|
dev = false;
|
|
13
|
-
crypto = new
|
|
13
|
+
crypto = new runtime_1.RuntimeCrypto();
|
|
14
14
|
constructor(config) {
|
|
15
15
|
try {
|
|
16
16
|
if (config && config.dev == true)
|
|
@@ -67,7 +67,7 @@ class JATClass {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
class PrivatePublicKeyGeneration {
|
|
70
|
-
crypto = new
|
|
70
|
+
crypto = new runtime_1.RuntimeCrypto();
|
|
71
71
|
async generateKeyPair(runtime, dev) {
|
|
72
72
|
let finalRuntime = (0, runtime_config_1.detectRuntime)();
|
|
73
73
|
const development = dev === true ? true : false;
|
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
export declare class NodeCrypto {
|
|
2
|
+
private crypto;
|
|
3
|
+
private __init;
|
|
2
4
|
private _encrypt;
|
|
3
5
|
private _decrypt;
|
|
4
6
|
private _rsaPublicKeyGeneration;
|
|
5
7
|
private _rsaPrivatePublicKeyGeneration;
|
|
6
|
-
encrypt(algo: 'aes-256-gcm', key: string, payload: any, exp: number): string
|
|
8
|
+
encrypt(algo: 'aes-256-gcm', key: string, payload: any, exp: number): Promise<string>;
|
|
7
9
|
decrypt<T>(algo: 'aes-256-gcm', key: string, encryptedData: {
|
|
8
10
|
iv: string;
|
|
9
11
|
encrypted: string;
|
|
10
12
|
tag: string;
|
|
11
|
-
}): {
|
|
13
|
+
}): Promise<{
|
|
12
14
|
payload: T;
|
|
13
15
|
exp: number;
|
|
14
|
-
}
|
|
15
|
-
encryptRSA(payload: any, publicKey: string, exp: number): string
|
|
16
|
+
}>;
|
|
17
|
+
encryptRSA(payload: any, publicKey: string, exp: number): Promise<string>;
|
|
16
18
|
decryptRSA<T>(privateKey: string, encryptedKey: string, encryptedData: {
|
|
17
19
|
iv: string;
|
|
18
20
|
encrypted: string;
|
|
19
21
|
tag: string;
|
|
20
|
-
}): {
|
|
22
|
+
}): Promise<{
|
|
21
23
|
payload: T;
|
|
22
24
|
exp: number;
|
|
23
|
-
}
|
|
24
|
-
rsaPrivatePublicKeyGeneration(): {
|
|
25
|
+
}>;
|
|
26
|
+
rsaPrivatePublicKeyGeneration(): Promise<{
|
|
25
27
|
privateKey: string;
|
|
26
28
|
publicKey: string;
|
|
27
|
-
}
|
|
28
|
-
rsaPublicKeyGeneration(privateKeyPem: string): string | Buffer<ArrayBufferLike
|
|
29
|
+
}>;
|
|
30
|
+
rsaPublicKeyGeneration(privateKeyPem: string): Promise<string | Buffer<ArrayBufferLike>>;
|
|
29
31
|
}
|
|
@@ -1,4 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
37
|
};
|
|
@@ -7,6 +40,12 @@ exports.NodeCrypto = void 0;
|
|
|
7
40
|
const crypto_1 = __importDefault(require("crypto"));
|
|
8
41
|
const functions_lib_1 = require("../lib/functions.lib");
|
|
9
42
|
class NodeCrypto {
|
|
43
|
+
crypto;
|
|
44
|
+
async __init() {
|
|
45
|
+
if (!this.crypto) {
|
|
46
|
+
return await Promise.resolve().then(() => __importStar(require('crypto')));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
10
49
|
_encrypt(algorithm, key, payload) {
|
|
11
50
|
const iv = crypto_1.default.randomBytes(12);
|
|
12
51
|
const cipher = crypto_1.default.createCipheriv(algorithm, key, iv);
|
|
@@ -51,8 +90,9 @@ class NodeCrypto {
|
|
|
51
90
|
});
|
|
52
91
|
return { privateKey, publicKey };
|
|
53
92
|
}
|
|
54
|
-
encrypt(algo, key, payload, exp) {
|
|
55
|
-
|
|
93
|
+
async encrypt(algo, key, payload, exp) {
|
|
94
|
+
await this.__init();
|
|
95
|
+
const keyHash = this.crypto.createHash('sha256').update(key).digest();
|
|
56
96
|
const newPayload = { payload: payload, exp: exp };
|
|
57
97
|
const { iv, encrypted, tag } = this._encrypt(algo, keyHash, newPayload);
|
|
58
98
|
return (0, functions_lib_1.tokenFormatCreate)({
|
|
@@ -64,11 +104,13 @@ class NodeCrypto {
|
|
|
64
104
|
tag
|
|
65
105
|
}, encrypted);
|
|
66
106
|
}
|
|
67
|
-
decrypt(algo, key, encryptedData) {
|
|
107
|
+
async decrypt(algo, key, encryptedData) {
|
|
108
|
+
await this.__init();
|
|
68
109
|
const keyHash = crypto_1.default.createHash('sha256').update(key).digest();
|
|
69
110
|
return this._decrypt(algo, keyHash, encryptedData);
|
|
70
111
|
}
|
|
71
|
-
encryptRSA(payload, publicKey, exp) {
|
|
112
|
+
async encryptRSA(payload, publicKey, exp) {
|
|
113
|
+
await this.__init();
|
|
72
114
|
const symmetricKey = crypto_1.default.randomBytes(32);
|
|
73
115
|
const newPayload = { payload: payload, exp: exp };
|
|
74
116
|
const { iv, encrypted, tag } = this._encrypt('aes-256-gcm', symmetricKey, newPayload);
|
|
@@ -87,7 +129,8 @@ class NodeCrypto {
|
|
|
87
129
|
encryptedKey: encryptedSymmetricKey.toString('base64')
|
|
88
130
|
}, encrypted);
|
|
89
131
|
}
|
|
90
|
-
decryptRSA(privateKey, encryptedKey, encryptedData) {
|
|
132
|
+
async decryptRSA(privateKey, encryptedKey, encryptedData) {
|
|
133
|
+
await this.__init();
|
|
91
134
|
const decryptedSymmetricKey = crypto_1.default.privateDecrypt({
|
|
92
135
|
key: privateKey,
|
|
93
136
|
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
|
|
@@ -95,10 +138,12 @@ class NodeCrypto {
|
|
|
95
138
|
}, Buffer.from(encryptedKey, 'base64'));
|
|
96
139
|
return this._decrypt('aes-256-gcm', decryptedSymmetricKey, encryptedData);
|
|
97
140
|
}
|
|
98
|
-
rsaPrivatePublicKeyGeneration() {
|
|
141
|
+
async rsaPrivatePublicKeyGeneration() {
|
|
142
|
+
await this.__init();
|
|
99
143
|
return this._rsaPrivatePublicKeyGeneration();
|
|
100
144
|
}
|
|
101
|
-
rsaPublicKeyGeneration(privateKeyPem) {
|
|
145
|
+
async rsaPublicKeyGeneration(privateKeyPem) {
|
|
146
|
+
await this.__init();
|
|
102
147
|
return this._rsaPublicKeyGeneration(privateKeyPem);
|
|
103
148
|
}
|
|
104
149
|
}
|
|
@@ -33,23 +33,23 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.
|
|
36
|
+
exports.RuntimeCrypto = void 0;
|
|
37
37
|
const algo_config_1 = require("../config/algo.config");
|
|
38
38
|
const functions_lib_1 = require("../lib/functions.lib");
|
|
39
|
-
class
|
|
39
|
+
class RuntimeCrypto {
|
|
40
40
|
node;
|
|
41
41
|
web;
|
|
42
42
|
async getModule(runtime) {
|
|
43
43
|
if (runtime === 'node' && this.node === undefined) {
|
|
44
|
-
const { NodeCrypto } = await Promise.resolve().then(() => __importStar(require('./node.
|
|
44
|
+
const { NodeCrypto } = await Promise.resolve().then(() => __importStar(require('./node.runtime')));
|
|
45
45
|
this.node = new NodeCrypto();
|
|
46
46
|
}
|
|
47
47
|
else if (runtime === 'web' && this.web === undefined) {
|
|
48
|
-
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.
|
|
48
|
+
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.runtime')));
|
|
49
49
|
this.web = new WebCrypto();
|
|
50
50
|
}
|
|
51
51
|
else if (runtime === 'edge' && this.web === undefined) {
|
|
52
|
-
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.
|
|
52
|
+
const { WebCrypto } = await Promise.resolve().then(() => __importStar(require('./web.runtime')));
|
|
53
53
|
this.web = new WebCrypto();
|
|
54
54
|
}
|
|
55
55
|
}
|
|
@@ -65,13 +65,13 @@ class Crypto {
|
|
|
65
65
|
if (algoData.value !== 'ras+a256gcm') {
|
|
66
66
|
throw new Error(`Algorithm ${algoData.name} is not supported for asymmetric encryption`);
|
|
67
67
|
}
|
|
68
|
-
return this.node.encryptRSA(payload, key, exp);
|
|
68
|
+
return await this.node.encryptRSA(payload, key, exp);
|
|
69
69
|
}
|
|
70
70
|
else {
|
|
71
71
|
if (algoData.value !== 'aes-256-gcm') {
|
|
72
72
|
throw new Error(`Algorithm ${algoData.name} is not supported for symmetric encryption`);
|
|
73
73
|
}
|
|
74
|
-
return this.node.encrypt(algoData.value, key, payload, exp);
|
|
74
|
+
return await this.node.encrypt(algoData.value, key, payload, exp);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
else {
|
|
@@ -101,13 +101,13 @@ class Crypto {
|
|
|
101
101
|
if (algo !== 'RSA+A256GCM') {
|
|
102
102
|
throw new Error(`Algorithm ${algo} is not supported for asymmetric encryption`);
|
|
103
103
|
}
|
|
104
|
-
return this.node.decryptRSA(key, encryptedKey, { iv, encrypted, tag });
|
|
104
|
+
return await this.node.decryptRSA(key, encryptedKey, { iv, encrypted, tag });
|
|
105
105
|
}
|
|
106
106
|
else {
|
|
107
107
|
if (algo !== 'AES-256-GCM') {
|
|
108
108
|
throw new Error(`Algorithm ${algo} is not supported for symmetric encryption`);
|
|
109
109
|
}
|
|
110
|
-
return this.node.decrypt("aes-256-gcm", key, { iv, encrypted, tag });
|
|
110
|
+
return await this.node.decrypt("aes-256-gcm", key, { iv, encrypted, tag });
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
else {
|
|
@@ -129,12 +129,12 @@ class Crypto {
|
|
|
129
129
|
await this.getModule(runtime);
|
|
130
130
|
if (runtime === 'node') {
|
|
131
131
|
if (type === 'keyPair') {
|
|
132
|
-
return this.node.rsaPrivatePublicKeyGeneration();
|
|
132
|
+
return await this.node.rsaPrivatePublicKeyGeneration();
|
|
133
133
|
}
|
|
134
134
|
else {
|
|
135
135
|
if (!privateKeyPem)
|
|
136
136
|
throw new Error('privateKeyPem is required');
|
|
137
|
-
return this.node.rsaPublicKeyGeneration(privateKeyPem);
|
|
137
|
+
return await this.node.rsaPublicKeyGeneration(privateKeyPem);
|
|
138
138
|
}
|
|
139
139
|
}
|
|
140
140
|
else {
|
|
@@ -149,4 +149,4 @@ class Crypto {
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
|
-
exports.
|
|
152
|
+
exports.RuntimeCrypto = RuntimeCrypto;
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|