jsonauthtoken 3.0.1 → 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.
@@ -1,30 +1,31 @@
1
1
  export declare class NodeCrypto {
2
- constructor();
2
+ private crypto;
3
+ private __init;
3
4
  private _encrypt;
4
5
  private _decrypt;
5
6
  private _rsaPublicKeyGeneration;
6
7
  private _rsaPrivatePublicKeyGeneration;
7
- 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>;
8
9
  decrypt<T>(algo: 'aes-256-gcm', key: string, encryptedData: {
9
10
  iv: string;
10
11
  encrypted: string;
11
12
  tag: string;
12
- }): {
13
+ }): Promise<{
13
14
  payload: T;
14
15
  exp: number;
15
- };
16
- encryptRSA(payload: any, publicKey: string, exp: number): string;
16
+ }>;
17
+ encryptRSA(payload: any, publicKey: string, exp: number): Promise<string>;
17
18
  decryptRSA<T>(privateKey: string, encryptedKey: string, encryptedData: {
18
19
  iv: string;
19
20
  encrypted: string;
20
21
  tag: string;
21
- }): {
22
+ }): Promise<{
22
23
  payload: T;
23
24
  exp: number;
24
- };
25
- rsaPrivatePublicKeyGeneration(): {
25
+ }>;
26
+ rsaPrivatePublicKeyGeneration(): Promise<{
26
27
  privateKey: string;
27
28
  publicKey: string;
28
- };
29
- rsaPublicKeyGeneration(privateKeyPem: string): string | Buffer<ArrayBufferLike>;
29
+ }>;
30
+ rsaPublicKeyGeneration(privateKeyPem: string): Promise<string | Buffer<ArrayBufferLike>>;
30
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,7 +40,11 @@ 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 {
10
- constructor() {
43
+ crypto;
44
+ async __init() {
45
+ if (!this.crypto) {
46
+ return await Promise.resolve().then(() => __importStar(require('crypto')));
47
+ }
11
48
  }
12
49
  _encrypt(algorithm, key, payload) {
13
50
  const iv = crypto_1.default.randomBytes(12);
@@ -53,8 +90,9 @@ class NodeCrypto {
53
90
  });
54
91
  return { privateKey, publicKey };
55
92
  }
56
- encrypt(algo, key, payload, exp) {
57
- const keyHash = crypto_1.default.createHash('sha256').update(key).digest();
93
+ async encrypt(algo, key, payload, exp) {
94
+ await this.__init();
95
+ const keyHash = this.crypto.createHash('sha256').update(key).digest();
58
96
  const newPayload = { payload: payload, exp: exp };
59
97
  const { iv, encrypted, tag } = this._encrypt(algo, keyHash, newPayload);
60
98
  return (0, functions_lib_1.tokenFormatCreate)({
@@ -66,11 +104,13 @@ class NodeCrypto {
66
104
  tag
67
105
  }, encrypted);
68
106
  }
69
- decrypt(algo, key, encryptedData) {
107
+ async decrypt(algo, key, encryptedData) {
108
+ await this.__init();
70
109
  const keyHash = crypto_1.default.createHash('sha256').update(key).digest();
71
110
  return this._decrypt(algo, keyHash, encryptedData);
72
111
  }
73
- encryptRSA(payload, publicKey, exp) {
112
+ async encryptRSA(payload, publicKey, exp) {
113
+ await this.__init();
74
114
  const symmetricKey = crypto_1.default.randomBytes(32);
75
115
  const newPayload = { payload: payload, exp: exp };
76
116
  const { iv, encrypted, tag } = this._encrypt('aes-256-gcm', symmetricKey, newPayload);
@@ -89,7 +129,8 @@ class NodeCrypto {
89
129
  encryptedKey: encryptedSymmetricKey.toString('base64')
90
130
  }, encrypted);
91
131
  }
92
- decryptRSA(privateKey, encryptedKey, encryptedData) {
132
+ async decryptRSA(privateKey, encryptedKey, encryptedData) {
133
+ await this.__init();
93
134
  const decryptedSymmetricKey = crypto_1.default.privateDecrypt({
94
135
  key: privateKey,
95
136
  padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
@@ -97,10 +138,12 @@ class NodeCrypto {
97
138
  }, Buffer.from(encryptedKey, 'base64'));
98
139
  return this._decrypt('aes-256-gcm', decryptedSymmetricKey, encryptedData);
99
140
  }
100
- rsaPrivatePublicKeyGeneration() {
141
+ async rsaPrivatePublicKeyGeneration() {
142
+ await this.__init();
101
143
  return this._rsaPrivatePublicKeyGeneration();
102
144
  }
103
- rsaPublicKeyGeneration(privateKeyPem) {
145
+ async rsaPublicKeyGeneration(privateKeyPem) {
146
+ await this.__init();
104
147
  return this._rsaPublicKeyGeneration(privateKeyPem);
105
148
  }
106
149
  }
@@ -65,13 +65,13 @@ class RuntimeCrypto {
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 RuntimeCrypto {
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 RuntimeCrypto {
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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsonauthtoken",
3
- "version": "3.0.1",
3
+ "version": "3.0.2",
4
4
  "description": "jsonauthtoken is a JavaScript/TypeScript library to secure authentication.",
5
5
  "main": "dist/index.js",
6
6
  "repository": {