@basis-theory/basis-theory-js 1.16.0 → 1.20.0
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/BasisTheory.d.ts +4 -9
- package/BasisTheory.js +4 -15
- package/common/utils.js +9 -3
- package/elements/services/tokenize.js +1 -1
- package/index.d.ts +0 -1
- package/index.js +0 -14
- package/package.json +2 -2
- package/tokenize/BasisTheoryTokenize.js +3 -5
- package/types.d.ts +1 -9
- package/encryption/BasisTheoryEncryption.d.ts +0 -12
- package/encryption/BasisTheoryEncryption.js +0 -51
- package/encryption/BasisTheoryEncryptionAdapters.d.ts +0 -8
- package/encryption/BasisTheoryEncryptionAdapters.js +0 -33
- package/encryption/index.d.ts +0 -2
- package/encryption/index.js +0 -31
- package/encryption/providers/browser.d.ts +0 -2
- package/encryption/providers/browser.js +0 -98
- package/encryption/providers/node.d.ts +0 -2
- package/encryption/providers/node.js +0 -73
- package/encryption/providers/utils.d.ts +0 -3
- package/encryption/providers/utils.js +0 -24
- package/encryption/types.d.ts +0 -13
- package/encryption/types.js +0 -1
package/BasisTheory.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import type { AtomicBanks as ElementsAtomicBanks, AtomicCards as ElementsAtomicCards, BasisTheoryElements, CardElement, CustomizableElementOptions, ElementType, TextElement,
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
1
|
+
import type { AtomicBanks as ElementsAtomicBanks, AtomicCards as ElementsAtomicCards, BasisTheoryElements, CardElement, CustomizableElementOptions, ElementType, TextElement, TokenizeData as ElementsTokenizeData, Tokens as ElementsTokens } from '@basis-theory/basis-theory-elements-interfaces/elements';
|
|
2
|
+
import type { TokenizeData } from '@basis-theory/basis-theory-elements-interfaces/models';
|
|
3
|
+
import type { Applications, AtomicBanks, AtomicCards, BasisTheory as IBasisTheory, BasisTheoryInit, Logs, Permissions, ReactorFormulas, Reactors, RequestOptions, Tenants, Tokens } from '@basis-theory/basis-theory-elements-interfaces/sdk';
|
|
4
4
|
import type { BasisTheoryInitOptions, BasisTheoryInitOptionsWithElements, BasisTheoryInitOptionsWithoutElements } from './types';
|
|
5
5
|
export declare class BasisTheory implements BasisTheoryInit, IBasisTheory, BasisTheoryElements {
|
|
6
6
|
private _initStatus;
|
|
7
7
|
private _initOptions?;
|
|
8
8
|
private _tokens?;
|
|
9
9
|
private _tokenize?;
|
|
10
|
-
private _encryption?;
|
|
11
10
|
private _elements?;
|
|
12
11
|
private _applications?;
|
|
13
12
|
private _tenants?;
|
|
@@ -20,14 +19,10 @@ export declare class BasisTheory implements BasisTheoryInit, IBasisTheory, Basis
|
|
|
20
19
|
init(apiKey: string, options?: BasisTheoryInitOptionsWithoutElements): Promise<IBasisTheory>;
|
|
21
20
|
init(apiKey: string, options: BasisTheoryInitOptionsWithElements): Promise<IBasisTheory & BasisTheoryElements>;
|
|
22
21
|
createElement(type: ElementType, options: CustomizableElementOptions): CardElement | TextElement;
|
|
22
|
+
tokenize(tokens: TokenizeData & ElementsTokenizeData, options?: RequestOptions): Promise<TokenizeData>;
|
|
23
23
|
private loadElements;
|
|
24
24
|
get initOptions(): Required<BasisTheoryInitOptions>;
|
|
25
25
|
get tokens(): Tokens & ElementsTokens;
|
|
26
|
-
get tokenize(): Tokenize & ElementsTokenize;
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated
|
|
29
|
-
*/
|
|
30
|
-
get encryption(): BasisTheoryEncryptionAdapters;
|
|
31
26
|
get applications(): Applications;
|
|
32
27
|
get tenants(): Tenants;
|
|
33
28
|
get logs(): Logs;
|
package/BasisTheory.js
CHANGED
|
@@ -15,8 +15,6 @@ var _elements = require("./elements");
|
|
|
15
15
|
|
|
16
16
|
var _constants2 = require("./elements/constants");
|
|
17
17
|
|
|
18
|
-
var _BasisTheoryEncryptionAdapters = require("./encryption/BasisTheoryEncryptionAdapters");
|
|
19
|
-
|
|
20
18
|
var _logs = require("./logs");
|
|
21
19
|
|
|
22
20
|
var _permissions = require("./permissions");
|
|
@@ -110,7 +108,6 @@ class BasisTheory {
|
|
|
110
108
|
apiKey,
|
|
111
109
|
baseURL: new URL(_constants.CLIENT_BASE_PATHS.permissions, baseUrl).toString()
|
|
112
110
|
});
|
|
113
|
-
this._encryption = new _BasisTheoryEncryptionAdapters.BasisTheoryEncryptionAdapters();
|
|
114
111
|
this._initStatus = 'done';
|
|
115
112
|
} catch (error) {
|
|
116
113
|
this._initStatus = 'error';
|
|
@@ -131,6 +128,10 @@ class BasisTheory {
|
|
|
131
128
|
return this._elements.createElement(type, options);
|
|
132
129
|
}
|
|
133
130
|
|
|
131
|
+
tokenize(tokens, options) {
|
|
132
|
+
return (0, _common.assertInit)(this._tokenize).tokenize(tokens, options);
|
|
133
|
+
}
|
|
134
|
+
|
|
134
135
|
async loadElements(apiKey) {
|
|
135
136
|
let elementsBaseUrl;
|
|
136
137
|
|
|
@@ -156,18 +157,6 @@ class BasisTheory {
|
|
|
156
157
|
return (0, _common.assertInit)(this._tokens);
|
|
157
158
|
}
|
|
158
159
|
|
|
159
|
-
get tokenize() {
|
|
160
|
-
return (0, _common.assertInit)(this._tokenize);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* @deprecated
|
|
164
|
-
*/
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
get encryption() {
|
|
168
|
-
return (0, _common.assertInit)(this._encryption);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
160
|
get applications() {
|
|
172
161
|
return (0, _common.assertInit)(this._applications);
|
|
173
162
|
}
|
package/common/utils.js
CHANGED
|
@@ -246,11 +246,12 @@ const getQueryParams = query => {
|
|
|
246
246
|
if (keys.length) {
|
|
247
247
|
const params = new URLSearchParams();
|
|
248
248
|
|
|
249
|
-
const appendSafe = (key, value) => {
|
|
250
|
-
const type = typeof value;
|
|
249
|
+
const appendSafe = (key, value, isNested = false) => {
|
|
250
|
+
const type = typeof value;
|
|
251
|
+
const formattedKey = isNested ? key : (0, _snakeCase.snakeCase)(key); // eslint-disable-next-line unicorn/no-null
|
|
251
252
|
|
|
252
253
|
if (value === null || ['boolean', 'number', 'string'].includes(type)) {
|
|
253
|
-
params.append(
|
|
254
|
+
params.append(formattedKey, value);
|
|
254
255
|
}
|
|
255
256
|
};
|
|
256
257
|
|
|
@@ -261,6 +262,11 @@ const getQueryParams = query => {
|
|
|
261
262
|
value.forEach(aValue => {
|
|
262
263
|
appendSafe(String(key), aValue);
|
|
263
264
|
});
|
|
265
|
+
} else if (value && typeof value === 'object') {
|
|
266
|
+
const objectKeys = Object.keys(value);
|
|
267
|
+
objectKeys.forEach(objectKey => {
|
|
268
|
+
appendSafe(`${key}.${objectKey}`, value[objectKey], true);
|
|
269
|
+
});
|
|
264
270
|
} else {
|
|
265
271
|
appendSafe(String(key), value);
|
|
266
272
|
}
|
|
@@ -10,7 +10,7 @@ var _tokenize = require("../../tokenize");
|
|
|
10
10
|
const delegateTokenize = elements => class BasisTheoryTokenizesElementsDelegate extends _tokenize.BasisTheoryTokenize {
|
|
11
11
|
tokenize(payload, requestOptions) {
|
|
12
12
|
if (elements !== null && elements !== void 0 && elements.hasElement(payload)) {
|
|
13
|
-
return elements.tokenize
|
|
13
|
+
return elements.tokenize(payload, requestOptions);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
return super.tokenize(payload, requestOptions);
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -22,20 +22,6 @@ Object.keys(_BasisTheory).forEach(function (key) {
|
|
|
22
22
|
});
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
var _encryption = require("./encryption");
|
|
26
|
-
|
|
27
|
-
Object.keys(_encryption).forEach(function (key) {
|
|
28
|
-
if (key === "default" || key === "__esModule") return;
|
|
29
|
-
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
30
|
-
if (key in exports && exports[key] === _encryption[key]) return;
|
|
31
|
-
Object.defineProperty(exports, key, {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
get: function () {
|
|
34
|
-
return _encryption[key];
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
25
|
var _applications = require("./applications");
|
|
40
26
|
|
|
41
27
|
Object.keys(_applications).forEach(function (key) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basis-theory/basis-theory-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.20.0",
|
|
4
4
|
"repository": "https://github.com/Basis-Theory/basis-theory-js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"!*.bundle.js"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@basis-theory/basis-theory-elements-interfaces": "^2.
|
|
18
|
+
"@basis-theory/basis-theory-elements-interfaces": "^3.2.0",
|
|
19
19
|
"axios": "^0.21.2",
|
|
20
20
|
"camelcase-keys": "^6.2.2",
|
|
21
21
|
"snake-case": "^3.0.4",
|
|
@@ -7,16 +7,14 @@ exports.BasisTheoryTokenize = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _common = require("../common");
|
|
9
9
|
|
|
10
|
-
var _utils = require("../common/utils");
|
|
11
|
-
|
|
12
10
|
var _service = require("../service");
|
|
13
11
|
|
|
14
12
|
class BasisTheoryTokenize extends _service.BasisTheoryService {
|
|
15
13
|
tokenize(tokens, options = {}) {
|
|
16
14
|
return this.client.post('/', tokens, (0, _common.createRequestConfig)(options, {
|
|
17
|
-
transformRequest:
|
|
18
|
-
transformResponse:
|
|
19
|
-
})).then(
|
|
15
|
+
transformRequest: _common.proxyRaw,
|
|
16
|
+
transformResponse: _common.proxyRaw
|
|
17
|
+
})).then(_common.dataExtractor);
|
|
20
18
|
}
|
|
21
19
|
|
|
22
20
|
}
|
package/types.d.ts
CHANGED
|
@@ -7,14 +7,6 @@ declare type ClientsBasePathMap = {
|
|
|
7
7
|
declare type Providers = 'BROWSER' | 'NODE';
|
|
8
8
|
declare const algorithm: readonly ["RSA", "AES"];
|
|
9
9
|
declare type Algorithm = typeof algorithm[number];
|
|
10
|
-
interface EncryptionProviderOptions {
|
|
11
|
-
defaultKeySize: number;
|
|
12
|
-
keyExpirationInDays: number;
|
|
13
|
-
}
|
|
14
|
-
interface EncryptionOptions {
|
|
15
|
-
algorithm: Algorithm;
|
|
16
|
-
options?: EncryptionProviderOptions;
|
|
17
|
-
}
|
|
18
10
|
interface BasisTheoryInitOptions {
|
|
19
11
|
apiBaseUrl?: string;
|
|
20
12
|
}
|
|
@@ -31,4 +23,4 @@ declare global {
|
|
|
31
23
|
}
|
|
32
24
|
}
|
|
33
25
|
export { algorithm };
|
|
34
|
-
export type { InitStatus, Clients, ClientsBasePathMap, Providers, Algorithm,
|
|
26
|
+
export type { InitStatus, Clients, ClientsBasePathMap, Providers, Algorithm, BasisTheoryInitOptions, BasisTheoryInitOptionsWithoutElements, BasisTheoryInitOptionsWithElements, };
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Providers } from '../types';
|
|
2
|
-
import { EncryptionOptions } from '../types';
|
|
3
|
-
import type { EncryptionAdapter, KeyPair } from './types';
|
|
4
|
-
export declare class BasisTheoryEncryption implements EncryptionAdapter {
|
|
5
|
-
private readonly adapter;
|
|
6
|
-
constructor(encryptionProvider: Providers);
|
|
7
|
-
init(encryptionOptions: EncryptionOptions): void;
|
|
8
|
-
get name(): string;
|
|
9
|
-
generateKeys(): Promise<KeyPair | string | unknown>;
|
|
10
|
-
encrypt(encryptionKey: string, plainTextData: string): Promise<string>;
|
|
11
|
-
decrypt(decryptionKey: string, cipherTextData: string): Promise<string>;
|
|
12
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.BasisTheoryEncryption = void 0;
|
|
7
|
-
|
|
8
|
-
var _browser = require("./providers/browser");
|
|
9
|
-
|
|
10
|
-
var _node = require("./providers/node");
|
|
11
|
-
|
|
12
|
-
class BasisTheoryEncryption {
|
|
13
|
-
constructor(encryptionProvider) {
|
|
14
|
-
switch (encryptionProvider) {
|
|
15
|
-
case 'BROWSER':
|
|
16
|
-
this.adapter = _browser.browserAdapter;
|
|
17
|
-
break;
|
|
18
|
-
|
|
19
|
-
case 'NODE':
|
|
20
|
-
this.adapter = _node.nodeAdapter;
|
|
21
|
-
break;
|
|
22
|
-
|
|
23
|
-
default:
|
|
24
|
-
throw new Error(`No adapter found for provider name "${encryptionProvider}"`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
init(encryptionOptions) {
|
|
29
|
-
return this.adapter.init(encryptionOptions);
|
|
30
|
-
} // eslint-disable-next-line accessor-pairs
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
get name() {
|
|
34
|
-
return this.adapter.name;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
generateKeys() {
|
|
38
|
-
return this.adapter.generateKeys();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
encrypt(encryptionKey, plainTextData) {
|
|
42
|
-
return this.adapter.encrypt(encryptionKey, plainTextData);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
decrypt(decryptionKey, cipherTextData) {
|
|
46
|
-
return this.adapter.decrypt(decryptionKey, cipherTextData);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
exports.BasisTheoryEncryption = BasisTheoryEncryption;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { BasisTheoryEncryption } from './BasisTheoryEncryption';
|
|
2
|
-
export declare class BasisTheoryEncryptionAdapters {
|
|
3
|
-
private _nodeEncryption?;
|
|
4
|
-
private _browserEncryption?;
|
|
5
|
-
constructor();
|
|
6
|
-
get browserEncryption(): BasisTheoryEncryption;
|
|
7
|
-
get nodeEncryption(): BasisTheoryEncryption;
|
|
8
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.BasisTheoryEncryptionAdapters = void 0;
|
|
7
|
-
|
|
8
|
-
var _common = require("../common");
|
|
9
|
-
|
|
10
|
-
var _BasisTheoryEncryption = require("./BasisTheoryEncryption");
|
|
11
|
-
|
|
12
|
-
class BasisTheoryEncryptionAdapters {
|
|
13
|
-
constructor() {
|
|
14
|
-
if (typeof window === 'undefined') {
|
|
15
|
-
this._nodeEncryption = new _BasisTheoryEncryption.BasisTheoryEncryption('NODE');
|
|
16
|
-
} else {
|
|
17
|
-
this._browserEncryption = new _BasisTheoryEncryption.BasisTheoryEncryption('BROWSER');
|
|
18
|
-
}
|
|
19
|
-
} // eslint-disable-next-line accessor-pairs
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
get browserEncryption() {
|
|
23
|
-
return (0, _common.assertInit)(this._browserEncryption);
|
|
24
|
-
} // eslint-disable-next-line accessor-pairs
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
get nodeEncryption() {
|
|
28
|
-
return (0, _common.assertInit)(this._nodeEncryption);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
exports.BasisTheoryEncryptionAdapters = BasisTheoryEncryptionAdapters;
|
package/encryption/index.d.ts
DELETED
package/encryption/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
var _BasisTheoryEncryption = require("./BasisTheoryEncryption");
|
|
8
|
-
|
|
9
|
-
Object.keys(_BasisTheoryEncryption).forEach(function (key) {
|
|
10
|
-
if (key === "default" || key === "__esModule") return;
|
|
11
|
-
if (key in exports && exports[key] === _BasisTheoryEncryption[key]) return;
|
|
12
|
-
Object.defineProperty(exports, key, {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () {
|
|
15
|
-
return _BasisTheoryEncryption[key];
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
var _types = require("./types");
|
|
21
|
-
|
|
22
|
-
Object.keys(_types).forEach(function (key) {
|
|
23
|
-
if (key === "default" || key === "__esModule") return;
|
|
24
|
-
if (key in exports && exports[key] === _types[key]) return;
|
|
25
|
-
Object.defineProperty(exports, key, {
|
|
26
|
-
enumerable: true,
|
|
27
|
-
get: function () {
|
|
28
|
-
return _types[key];
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
});
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.browserAdapter = void 0;
|
|
7
|
-
|
|
8
|
-
var _utils = require("./utils");
|
|
9
|
-
|
|
10
|
-
let signAlgorithm;
|
|
11
|
-
let algorithm;
|
|
12
|
-
|
|
13
|
-
const init = browserEncryption => {
|
|
14
|
-
var _browserEncryption$op, _browserEncryption$op2, _browserEncryption$al;
|
|
15
|
-
|
|
16
|
-
signAlgorithm = {
|
|
17
|
-
name: 'RSA-OAEP',
|
|
18
|
-
modulusLength: (_browserEncryption$op = browserEncryption === null || browserEncryption === void 0 ? void 0 : (_browserEncryption$op2 = browserEncryption.options) === null || _browserEncryption$op2 === void 0 ? void 0 : _browserEncryption$op2.defaultKeySize) !== null && _browserEncryption$op !== void 0 ? _browserEncryption$op : 4096,
|
|
19
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
20
|
-
hash: 'SHA-256'
|
|
21
|
-
};
|
|
22
|
-
algorithm = (_browserEncryption$al = browserEncryption === null || browserEncryption === void 0 ? void 0 : browserEncryption.algorithm) !== null && _browserEncryption$al !== void 0 ? _browserEncryption$al : 'RSA';
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const convertBinaryToPem = (binaryData, label) => {
|
|
26
|
-
const base64Cert = (0, _utils.arrayBufferToBase64String)(binaryData);
|
|
27
|
-
let pemCert = `-----BEGIN ${label} KEY-----\r\n`;
|
|
28
|
-
let nextIndex = 0;
|
|
29
|
-
|
|
30
|
-
while (nextIndex < base64Cert.length) {
|
|
31
|
-
pemCert += nextIndex + 64 <= base64Cert.length ? `${base64Cert.slice(nextIndex, nextIndex + 64)}\r\n` : `${base64Cert.slice(nextIndex)}\r\n`;
|
|
32
|
-
nextIndex += 64;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
pemCert += `-----END ${label} KEY-----\r\n`;
|
|
36
|
-
return pemCert;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const convertPemToBinary = (pem, label) => {
|
|
40
|
-
const lines = pem.split('\n');
|
|
41
|
-
let encoded = '';
|
|
42
|
-
|
|
43
|
-
for (const line of lines) {
|
|
44
|
-
if (line.trim().length > 0 && !line.includes(`-BEGIN ${label} KEY-`) && !line.includes(`-END ${label} KEY-`)) {
|
|
45
|
-
encoded = encoded + line.trim();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return (0, _utils.base64StringToArrayBuffer)(encoded);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const generateRSAKeys = async () => {
|
|
53
|
-
const keyPair = await window.crypto.subtle.generateKey(signAlgorithm, true, ['encrypt', 'decrypt']);
|
|
54
|
-
const exportedPublic = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
|
|
55
|
-
const exportedPrivate = await window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
|
|
56
|
-
return {
|
|
57
|
-
publicKey: convertBinaryToPem(exportedPublic, 'PUBLIC'),
|
|
58
|
-
privateKey: convertBinaryToPem(exportedPrivate, 'PRIVATE')
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const generateKeyMap = {
|
|
63
|
-
RSA: generateRSAKeys,
|
|
64
|
-
AES: () => Promise.reject()
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const generateKeys = () => generateKeyMap[algorithm]();
|
|
68
|
-
|
|
69
|
-
const loadPublicKey = pem => window.crypto.subtle.importKey('spki', convertPemToBinary(pem, 'PUBLIC'), signAlgorithm, true, ['encrypt']);
|
|
70
|
-
|
|
71
|
-
const loadPrivateKey = pem => window.crypto.subtle.importKey('pkcs8', convertPemToBinary(pem, 'PRIVATE'), signAlgorithm, true, ['decrypt']);
|
|
72
|
-
|
|
73
|
-
const encrypt = async (publicKey, data) => {
|
|
74
|
-
const key = await loadPublicKey(publicKey);
|
|
75
|
-
const encrypted = await window.crypto.subtle.encrypt({
|
|
76
|
-
name: signAlgorithm.name
|
|
77
|
-
}, key, // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
78
|
-
new TextEncoder().encode(data).buffer);
|
|
79
|
-
return (0, _utils.arrayBufferToBase64String)(encrypted);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const decrypt = async (privateKey, data) => {
|
|
83
|
-
const key = await loadPrivateKey(privateKey);
|
|
84
|
-
const decrypted = await window.crypto.subtle.decrypt({
|
|
85
|
-
name: signAlgorithm.name
|
|
86
|
-
}, key, (0, _utils.base64StringToArrayBuffer)(data)); // eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
87
|
-
|
|
88
|
-
return new TextDecoder().decode(decrypted);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const browserAdapter = {
|
|
92
|
-
name: 'browser',
|
|
93
|
-
generateKeys,
|
|
94
|
-
init,
|
|
95
|
-
encrypt,
|
|
96
|
-
decrypt
|
|
97
|
-
};
|
|
98
|
-
exports.browserAdapter = browserAdapter;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.nodeAdapter = void 0;
|
|
7
|
-
|
|
8
|
-
var _crypto = require("crypto");
|
|
9
|
-
|
|
10
|
-
let keySize;
|
|
11
|
-
let algorithm;
|
|
12
|
-
|
|
13
|
-
const init = nodeEncryption => {
|
|
14
|
-
var _nodeEncryption$optio, _nodeEncryption$optio2, _nodeEncryption$algor;
|
|
15
|
-
|
|
16
|
-
keySize = (_nodeEncryption$optio = nodeEncryption === null || nodeEncryption === void 0 ? void 0 : (_nodeEncryption$optio2 = nodeEncryption.options) === null || _nodeEncryption$optio2 === void 0 ? void 0 : _nodeEncryption$optio2.defaultKeySize) !== null && _nodeEncryption$optio !== void 0 ? _nodeEncryption$optio : 4096;
|
|
17
|
-
algorithm = (_nodeEncryption$algor = nodeEncryption === null || nodeEncryption === void 0 ? void 0 : nodeEncryption.algorithm) !== null && _nodeEncryption$algor !== void 0 ? _nodeEncryption$algor : 'RSA';
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const generateRSAKeys = () => {
|
|
21
|
-
const {
|
|
22
|
-
publicKey,
|
|
23
|
-
privateKey
|
|
24
|
-
} = (0, _crypto.generateKeyPairSync)('rsa', {
|
|
25
|
-
modulusLength: keySize,
|
|
26
|
-
publicKeyEncoding: {
|
|
27
|
-
type: 'spki',
|
|
28
|
-
format: 'pem'
|
|
29
|
-
},
|
|
30
|
-
privateKeyEncoding: {
|
|
31
|
-
type: 'pkcs8',
|
|
32
|
-
format: 'pem'
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
return Promise.resolve({
|
|
36
|
-
publicKey,
|
|
37
|
-
privateKey
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const generateKeyMap = {
|
|
42
|
-
RSA: generateRSAKeys,
|
|
43
|
-
AES: () => Promise.reject()
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const generateKeys = () => generateKeyMap[algorithm]();
|
|
47
|
-
|
|
48
|
-
const encrypt = (publicKey, data) => {
|
|
49
|
-
const encrypted = (0, _crypto.publicEncrypt)({
|
|
50
|
-
key: publicKey,
|
|
51
|
-
padding: _crypto.constants.RSA_PKCS1_OAEP_PADDING,
|
|
52
|
-
oaepHash: 'sha256'
|
|
53
|
-
}, Buffer.from(data));
|
|
54
|
-
return Promise.resolve(encrypted.toString('base64'));
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const decrypt = (privateKey, data) => {
|
|
58
|
-
const decrypted = (0, _crypto.privateDecrypt)({
|
|
59
|
-
key: privateKey,
|
|
60
|
-
padding: _crypto.constants.RSA_PKCS1_OAEP_PADDING,
|
|
61
|
-
oaepHash: 'sha256'
|
|
62
|
-
}, Buffer.from(data, 'base64'));
|
|
63
|
-
return Promise.resolve(decrypted.toString());
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const nodeAdapter = {
|
|
67
|
-
name: 'node',
|
|
68
|
-
generateKeys,
|
|
69
|
-
init,
|
|
70
|
-
encrypt,
|
|
71
|
-
decrypt
|
|
72
|
-
};
|
|
73
|
-
exports.nodeAdapter = nodeAdapter;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.base64StringToArrayBuffer = exports.arrayBufferToBase64String = void 0;
|
|
7
|
-
|
|
8
|
-
const arrayBufferToBase64String = arrayBuffer => window.btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
|
9
|
-
|
|
10
|
-
exports.arrayBufferToBase64String = arrayBufferToBase64String;
|
|
11
|
-
|
|
12
|
-
const base64StringToArrayBuffer = b64str => {
|
|
13
|
-
const binary = window.atob(b64str);
|
|
14
|
-
const len = binary.length;
|
|
15
|
-
const bytes = new Uint8Array(len);
|
|
16
|
-
|
|
17
|
-
for (let i = 0; i < len; i++) {
|
|
18
|
-
bytes[i] = binary.charCodeAt(i);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return bytes.buffer;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
exports.base64StringToArrayBuffer = base64StringToArrayBuffer;
|
package/encryption/types.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { EncryptionOptions } from '../types';
|
|
2
|
-
interface KeyPair {
|
|
3
|
-
publicKey: string;
|
|
4
|
-
privateKey: string;
|
|
5
|
-
}
|
|
6
|
-
interface EncryptionAdapter {
|
|
7
|
-
name: string;
|
|
8
|
-
generateKeys(): Promise<KeyPair | string | unknown>;
|
|
9
|
-
init(encryptionOptions: EncryptionOptions): void;
|
|
10
|
-
encrypt(encryptionKey: unknown, plainTextData: string): Promise<string>;
|
|
11
|
-
decrypt(decryptionKey: unknown, cipherTextData: string): Promise<string>;
|
|
12
|
-
}
|
|
13
|
-
export { KeyPair, EncryptionAdapter };
|
package/encryption/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|