@lightsparkdev/core 0.2.4 → 0.3.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/CHANGELOG.md +12 -0
- package/README.md +0 -2
- package/dist/index.cjs +30 -14
- package/dist/index.d.ts +31 -16
- package/dist/index.js +29 -14
- package/package.json +1 -2
- package/src/crypto/KeyOrAlias.ts +18 -0
- package/src/crypto/NodeKeyCache.ts +11 -10
- package/src/crypto/crypto.ts +24 -18
- package/src/crypto/index.ts +1 -0
- package/src/requester/Query.ts +3 -0
- package/src/requester/Requester.ts +9 -4
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
DefaultCrypto: () => DefaultCrypto,
|
|
34
|
+
KeyOrAlias: () => KeyOrAlias,
|
|
34
35
|
LightsparkAuthException: () => LightsparkAuthException_default,
|
|
35
36
|
LightsparkException: () => LightsparkException_default,
|
|
36
37
|
LightsparkSigningException: () => LightsparkSigningException_default,
|
|
@@ -279,22 +280,27 @@ var getNonce = async () => {
|
|
|
279
280
|
const nonceSt = await getRandomValues32(new Uint32Array(1));
|
|
280
281
|
return Number(nonceSt);
|
|
281
282
|
};
|
|
282
|
-
var sign = async (
|
|
283
|
+
var sign = async (keyOrAlias, data) => {
|
|
284
|
+
if (typeof keyOrAlias === "string") {
|
|
285
|
+
throw new LightsparkSigningException_default(
|
|
286
|
+
"Key alias not supported for default crypto."
|
|
287
|
+
);
|
|
288
|
+
}
|
|
283
289
|
const cryptoImpl = await getCrypto();
|
|
284
290
|
return await cryptoImpl.subtle.sign(
|
|
285
291
|
{
|
|
286
292
|
name: "RSA-PSS",
|
|
287
293
|
saltLength: 32
|
|
288
294
|
},
|
|
289
|
-
|
|
295
|
+
keyOrAlias,
|
|
290
296
|
data
|
|
291
297
|
);
|
|
292
298
|
};
|
|
293
|
-
var importPrivateSigningKey = async (keyData
|
|
299
|
+
var importPrivateSigningKey = async (keyData) => {
|
|
294
300
|
const cryptoImpl = await getCrypto();
|
|
295
301
|
return await cryptoImpl.subtle.importKey(
|
|
296
302
|
/*format*/
|
|
297
|
-
|
|
303
|
+
"pkcs8",
|
|
298
304
|
/*keyData*/
|
|
299
305
|
keyData,
|
|
300
306
|
/*algorithm*/
|
|
@@ -317,6 +323,12 @@ var DefaultCrypto = {
|
|
|
317
323
|
importPrivateSigningKey
|
|
318
324
|
};
|
|
319
325
|
|
|
326
|
+
// src/crypto/KeyOrAlias.ts
|
|
327
|
+
var KeyOrAlias = {
|
|
328
|
+
key: (key) => ({ key }),
|
|
329
|
+
alias: (alias) => ({ alias })
|
|
330
|
+
};
|
|
331
|
+
|
|
320
332
|
// src/crypto/NodeKeyCache.ts
|
|
321
333
|
var import_auto_bind = __toESM(require("auto-bind"), 1);
|
|
322
334
|
var NodeKeyCache = class {
|
|
@@ -326,13 +338,14 @@ var NodeKeyCache = class {
|
|
|
326
338
|
(0, import_auto_bind.default)(this);
|
|
327
339
|
}
|
|
328
340
|
idToKey;
|
|
329
|
-
async loadKey(id,
|
|
330
|
-
|
|
341
|
+
async loadKey(id, keyOrAlias) {
|
|
342
|
+
if (keyOrAlias.alias !== void 0) {
|
|
343
|
+
this.idToKey.set(id, keyOrAlias.alias);
|
|
344
|
+
return keyOrAlias.alias;
|
|
345
|
+
}
|
|
346
|
+
const decoded = b64decode(this.stripPemTags(keyOrAlias.key));
|
|
331
347
|
try {
|
|
332
|
-
const key = await this.cryptoImpl.importPrivateSigningKey(
|
|
333
|
-
decoded,
|
|
334
|
-
format
|
|
335
|
-
);
|
|
348
|
+
const key = await this.cryptoImpl.importPrivateSigningKey(decoded);
|
|
336
349
|
this.idToKey.set(id, key);
|
|
337
350
|
return key;
|
|
338
351
|
} catch (e) {
|
|
@@ -393,7 +406,8 @@ var Requester = class {
|
|
|
393
406
|
const data = await this.makeRawRequest(
|
|
394
407
|
query.queryPayload,
|
|
395
408
|
query.variables || {},
|
|
396
|
-
query.signingNodeId
|
|
409
|
+
query.signingNodeId,
|
|
410
|
+
!!query.skipAuth
|
|
397
411
|
);
|
|
398
412
|
return query.constructObject(data);
|
|
399
413
|
}
|
|
@@ -429,7 +443,7 @@ var Requester = class {
|
|
|
429
443
|
})
|
|
430
444
|
);
|
|
431
445
|
}
|
|
432
|
-
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0) {
|
|
446
|
+
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
433
447
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
434
448
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
435
449
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -455,12 +469,13 @@ var Requester = class {
|
|
|
455
469
|
};
|
|
456
470
|
const browserUserAgent = typeof navigator !== "undefined" ? navigator.userAgent : "";
|
|
457
471
|
const sdkUserAgent = this.getSdkUserAgent();
|
|
458
|
-
const
|
|
472
|
+
const baseHeaders = {
|
|
459
473
|
"Content-Type": "application/json",
|
|
460
474
|
[LIGHTSPARK_BETA_HEADER_KEY]: LIGHTSPARK_BETA_HEADER_VALUE,
|
|
461
475
|
"X-Lightspark-SDK": sdkUserAgent,
|
|
462
476
|
"User-Agent": browserUserAgent || sdkUserAgent
|
|
463
|
-
}
|
|
477
|
+
};
|
|
478
|
+
const headers = skipAuth ? baseHeaders : await this.authProvider.addAuthHeaders(baseHeaders);
|
|
464
479
|
bodyData = await this.addSigningDataIfNeeded(
|
|
465
480
|
bodyData,
|
|
466
481
|
headers,
|
|
@@ -622,6 +637,7 @@ var isType = (typename) => (node) => {
|
|
|
622
637
|
// Annotate the CommonJS export names for ESM import in node:
|
|
623
638
|
0 && (module.exports = {
|
|
624
639
|
DefaultCrypto,
|
|
640
|
+
KeyOrAlias,
|
|
625
641
|
LightsparkAuthException,
|
|
626
642
|
LightsparkException,
|
|
627
643
|
LightsparkSigningException,
|
package/dist/index.d.ts
CHANGED
|
@@ -27,36 +27,49 @@ declare class LightsparkSigningException extends LightsparkException {
|
|
|
27
27
|
constructor(message: string, extraInfo?: any);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
type GeneratedKeyPair = {
|
|
31
|
+
publicKey: CryptoKey | string;
|
|
32
|
+
privateKey: CryptoKey | string;
|
|
33
|
+
keyAlias?: string;
|
|
34
|
+
};
|
|
30
35
|
type CryptoInterface = {
|
|
31
36
|
decryptSecretWithNodePassword: (cipher: string, encryptedSecret: string, nodePassword: string) => Promise<ArrayBuffer | null>;
|
|
32
|
-
generateSigningKeyPair: () => Promise<
|
|
33
|
-
publicKey: CryptoKey | string;
|
|
34
|
-
privateKey: CryptoKey | string;
|
|
35
|
-
}>;
|
|
37
|
+
generateSigningKeyPair: () => Promise<GeneratedKeyPair>;
|
|
36
38
|
serializeSigningKey: (key: CryptoKey | string, format: "pkcs8" | "spki") => Promise<ArrayBuffer>;
|
|
37
39
|
getNonce: () => Promise<number>;
|
|
38
|
-
sign: (
|
|
39
|
-
importPrivateSigningKey: (keyData: Uint8Array
|
|
40
|
+
sign: (keyOrAlias: CryptoKey | string, data: Uint8Array) => Promise<ArrayBuffer>;
|
|
41
|
+
importPrivateSigningKey: (keyData: Uint8Array) => Promise<CryptoKey | string>;
|
|
40
42
|
};
|
|
41
43
|
declare function decryptSecretWithNodePassword(cipher: string, encryptedSecret: string, nodePassword: string): Promise<ArrayBuffer | null>;
|
|
42
44
|
declare const DefaultCrypto: {
|
|
43
45
|
decryptSecretWithNodePassword: typeof decryptSecretWithNodePassword;
|
|
44
|
-
generateSigningKeyPair: () => Promise<
|
|
45
|
-
publicKey: string;
|
|
46
|
-
privateKey: string;
|
|
47
|
-
}>;
|
|
46
|
+
generateSigningKeyPair: () => Promise<GeneratedKeyPair>;
|
|
48
47
|
serializeSigningKey: (key: CryptoKey | string, format: "pkcs8" | "spki") => Promise<ArrayBuffer>;
|
|
49
48
|
getNonce: () => Promise<number>;
|
|
50
|
-
sign: (
|
|
51
|
-
importPrivateSigningKey: (keyData: Uint8Array
|
|
49
|
+
sign: (keyOrAlias: CryptoKey | string, data: Uint8Array) => Promise<ArrayBuffer>;
|
|
50
|
+
importPrivateSigningKey: (keyData: Uint8Array) => Promise<CryptoKey | string>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
type OnlyKey = {
|
|
54
|
+
key: string;
|
|
55
|
+
alias?: never;
|
|
56
|
+
};
|
|
57
|
+
type OnlyAlias = {
|
|
58
|
+
key?: never;
|
|
59
|
+
alias: string;
|
|
60
|
+
};
|
|
61
|
+
type KeyOrAliasType = OnlyKey | OnlyAlias;
|
|
62
|
+
declare const KeyOrAlias: {
|
|
63
|
+
key: (key: string) => OnlyKey;
|
|
64
|
+
alias: (alias: string) => OnlyAlias;
|
|
52
65
|
};
|
|
53
66
|
|
|
54
67
|
declare class NodeKeyCache {
|
|
55
68
|
private readonly cryptoImpl;
|
|
56
69
|
private idToKey;
|
|
57
70
|
constructor(cryptoImpl?: CryptoInterface);
|
|
58
|
-
loadKey(id: string,
|
|
59
|
-
getKey(id: string): CryptoKey |
|
|
71
|
+
loadKey(id: string, keyOrAlias: KeyOrAliasType): Promise<CryptoKey | string | null>;
|
|
72
|
+
getKey(id: string): CryptoKey | string | undefined;
|
|
60
73
|
hasKey(id: string): boolean;
|
|
61
74
|
private stripPemTags;
|
|
62
75
|
}
|
|
@@ -72,6 +85,8 @@ type Query<T> = {
|
|
|
72
85
|
constructObject: (rawData: any) => T;
|
|
73
86
|
/** The id of the node that will be used to sign the query. **/
|
|
74
87
|
signingNodeId?: string;
|
|
88
|
+
/** True if auth headers should be omitted for this query. **/
|
|
89
|
+
skipAuth?: boolean;
|
|
75
90
|
};
|
|
76
91
|
|
|
77
92
|
declare class Requester {
|
|
@@ -89,7 +104,7 @@ declare class Requester {
|
|
|
89
104
|
}): Observable<any>;
|
|
90
105
|
makeRawRequest(queryPayload: string, variables?: {
|
|
91
106
|
[key: string]: any;
|
|
92
|
-
}, signingNodeId?: string | undefined): Promise<any | null>;
|
|
107
|
+
}, signingNodeId?: string | undefined, skipAuth?: boolean): Promise<any | null>;
|
|
93
108
|
private getSdkUserAgent;
|
|
94
109
|
private addSigningDataIfNeeded;
|
|
95
110
|
}
|
|
@@ -163,4 +178,4 @@ declare const isType: <T extends string>(typename: T) => <N extends {
|
|
|
163
178
|
__typename: T;
|
|
164
179
|
}>;
|
|
165
180
|
|
|
166
|
-
export { AuthProvider, ById, CryptoInterface, DefaultCrypto, ExpandRecursively, LightsparkAuthException, LightsparkException, LightsparkSigningException, Maybe, NodeKeyCache, OmitTypename, Query, Requester, ServerEnvironment, StubAuthProvider, apiDomainForEnvironment, b64decode, b64encode, convertCurrencyAmount, isBrowser, isNode, isType, urlsafe_b64decode };
|
|
181
|
+
export { AuthProvider, ById, CryptoInterface, DefaultCrypto, ExpandRecursively, GeneratedKeyPair, KeyOrAlias, KeyOrAliasType, LightsparkAuthException, LightsparkException, LightsparkSigningException, Maybe, NodeKeyCache, OmitTypename, Query, Requester, ServerEnvironment, StubAuthProvider, apiDomainForEnvironment, b64decode, b64encode, convertCurrencyAmount, isBrowser, isNode, isType, urlsafe_b64decode };
|
package/dist/index.js
CHANGED
|
@@ -228,22 +228,27 @@ var getNonce = async () => {
|
|
|
228
228
|
const nonceSt = await getRandomValues32(new Uint32Array(1));
|
|
229
229
|
return Number(nonceSt);
|
|
230
230
|
};
|
|
231
|
-
var sign = async (
|
|
231
|
+
var sign = async (keyOrAlias, data) => {
|
|
232
|
+
if (typeof keyOrAlias === "string") {
|
|
233
|
+
throw new LightsparkSigningException_default(
|
|
234
|
+
"Key alias not supported for default crypto."
|
|
235
|
+
);
|
|
236
|
+
}
|
|
232
237
|
const cryptoImpl = await getCrypto();
|
|
233
238
|
return await cryptoImpl.subtle.sign(
|
|
234
239
|
{
|
|
235
240
|
name: "RSA-PSS",
|
|
236
241
|
saltLength: 32
|
|
237
242
|
},
|
|
238
|
-
|
|
243
|
+
keyOrAlias,
|
|
239
244
|
data
|
|
240
245
|
);
|
|
241
246
|
};
|
|
242
|
-
var importPrivateSigningKey = async (keyData
|
|
247
|
+
var importPrivateSigningKey = async (keyData) => {
|
|
243
248
|
const cryptoImpl = await getCrypto();
|
|
244
249
|
return await cryptoImpl.subtle.importKey(
|
|
245
250
|
/*format*/
|
|
246
|
-
|
|
251
|
+
"pkcs8",
|
|
247
252
|
/*keyData*/
|
|
248
253
|
keyData,
|
|
249
254
|
/*algorithm*/
|
|
@@ -266,6 +271,12 @@ var DefaultCrypto = {
|
|
|
266
271
|
importPrivateSigningKey
|
|
267
272
|
};
|
|
268
273
|
|
|
274
|
+
// src/crypto/KeyOrAlias.ts
|
|
275
|
+
var KeyOrAlias = {
|
|
276
|
+
key: (key) => ({ key }),
|
|
277
|
+
alias: (alias) => ({ alias })
|
|
278
|
+
};
|
|
279
|
+
|
|
269
280
|
// src/crypto/NodeKeyCache.ts
|
|
270
281
|
import autoBind from "auto-bind";
|
|
271
282
|
var NodeKeyCache = class {
|
|
@@ -275,13 +286,14 @@ var NodeKeyCache = class {
|
|
|
275
286
|
autoBind(this);
|
|
276
287
|
}
|
|
277
288
|
idToKey;
|
|
278
|
-
async loadKey(id,
|
|
279
|
-
|
|
289
|
+
async loadKey(id, keyOrAlias) {
|
|
290
|
+
if (keyOrAlias.alias !== void 0) {
|
|
291
|
+
this.idToKey.set(id, keyOrAlias.alias);
|
|
292
|
+
return keyOrAlias.alias;
|
|
293
|
+
}
|
|
294
|
+
const decoded = b64decode(this.stripPemTags(keyOrAlias.key));
|
|
280
295
|
try {
|
|
281
|
-
const key = await this.cryptoImpl.importPrivateSigningKey(
|
|
282
|
-
decoded,
|
|
283
|
-
format
|
|
284
|
-
);
|
|
296
|
+
const key = await this.cryptoImpl.importPrivateSigningKey(decoded);
|
|
285
297
|
this.idToKey.set(id, key);
|
|
286
298
|
return key;
|
|
287
299
|
} catch (e) {
|
|
@@ -342,7 +354,8 @@ var Requester = class {
|
|
|
342
354
|
const data = await this.makeRawRequest(
|
|
343
355
|
query.queryPayload,
|
|
344
356
|
query.variables || {},
|
|
345
|
-
query.signingNodeId
|
|
357
|
+
query.signingNodeId,
|
|
358
|
+
!!query.skipAuth
|
|
346
359
|
);
|
|
347
360
|
return query.constructObject(data);
|
|
348
361
|
}
|
|
@@ -378,7 +391,7 @@ var Requester = class {
|
|
|
378
391
|
})
|
|
379
392
|
);
|
|
380
393
|
}
|
|
381
|
-
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0) {
|
|
394
|
+
async makeRawRequest(queryPayload, variables = {}, signingNodeId = void 0, skipAuth = false) {
|
|
382
395
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
383
396
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
384
397
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -404,12 +417,13 @@ var Requester = class {
|
|
|
404
417
|
};
|
|
405
418
|
const browserUserAgent = typeof navigator !== "undefined" ? navigator.userAgent : "";
|
|
406
419
|
const sdkUserAgent = this.getSdkUserAgent();
|
|
407
|
-
const
|
|
420
|
+
const baseHeaders = {
|
|
408
421
|
"Content-Type": "application/json",
|
|
409
422
|
[LIGHTSPARK_BETA_HEADER_KEY]: LIGHTSPARK_BETA_HEADER_VALUE,
|
|
410
423
|
"X-Lightspark-SDK": sdkUserAgent,
|
|
411
424
|
"User-Agent": browserUserAgent || sdkUserAgent
|
|
412
|
-
}
|
|
425
|
+
};
|
|
426
|
+
const headers = skipAuth ? baseHeaders : await this.authProvider.addAuthHeaders(baseHeaders);
|
|
413
427
|
bodyData = await this.addSigningDataIfNeeded(
|
|
414
428
|
bodyData,
|
|
415
429
|
headers,
|
|
@@ -570,6 +584,7 @@ var isType = (typename) => (node) => {
|
|
|
570
584
|
};
|
|
571
585
|
export {
|
|
572
586
|
DefaultCrypto,
|
|
587
|
+
KeyOrAlias,
|
|
573
588
|
LightsparkAuthException_default as LightsparkAuthException,
|
|
574
589
|
LightsparkException_default as LightsparkException,
|
|
575
590
|
LightsparkSigningException_default as LightsparkSigningException,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightsparkdev/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Lightspark JS SDK",
|
|
5
5
|
"author": "Lightspark Inc.",
|
|
6
6
|
"keywords": [
|
|
@@ -58,7 +58,6 @@
|
|
|
58
58
|
},
|
|
59
59
|
"license": "Apache-2.0",
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@openid/appauth": "^1.3.1",
|
|
62
61
|
"auto-bind": "^5.0.1",
|
|
63
62
|
"crypto": "^1.0.1",
|
|
64
63
|
"crypto-browserify": "^3.12.0",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
|
|
2
|
+
|
|
3
|
+
type OnlyKey = {
|
|
4
|
+
key: string;
|
|
5
|
+
alias?: never;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type OnlyAlias = {
|
|
9
|
+
key?: never;
|
|
10
|
+
alias: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type KeyOrAliasType = OnlyKey | OnlyAlias;
|
|
14
|
+
|
|
15
|
+
export const KeyOrAlias = {
|
|
16
|
+
key: (key: string): OnlyKey => ({ key }),
|
|
17
|
+
alias: (alias: string): OnlyAlias => ({ alias }),
|
|
18
|
+
};
|
|
@@ -4,9 +4,10 @@ import autoBind from "auto-bind";
|
|
|
4
4
|
|
|
5
5
|
import { b64decode } from "../utils/base64.js";
|
|
6
6
|
import { CryptoInterface, DefaultCrypto } from "./crypto.js";
|
|
7
|
+
import { KeyOrAliasType } from "./KeyOrAlias.js";
|
|
7
8
|
|
|
8
9
|
class NodeKeyCache {
|
|
9
|
-
private idToKey: Map<string, CryptoKey |
|
|
10
|
+
private idToKey: Map<string, CryptoKey | string>;
|
|
10
11
|
constructor(private readonly cryptoImpl: CryptoInterface = DefaultCrypto) {
|
|
11
12
|
this.idToKey = new Map();
|
|
12
13
|
autoBind(this);
|
|
@@ -14,15 +15,15 @@ class NodeKeyCache {
|
|
|
14
15
|
|
|
15
16
|
public async loadKey(
|
|
16
17
|
id: string,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
keyOrAlias: KeyOrAliasType
|
|
19
|
+
): Promise<CryptoKey | string | null> {
|
|
20
|
+
if (keyOrAlias.alias !== undefined) {
|
|
21
|
+
this.idToKey.set(id, keyOrAlias.alias);
|
|
22
|
+
return keyOrAlias.alias;
|
|
23
|
+
}
|
|
24
|
+
const decoded = b64decode(this.stripPemTags(keyOrAlias.key));
|
|
21
25
|
try {
|
|
22
|
-
const key = await this.cryptoImpl.importPrivateSigningKey(
|
|
23
|
-
decoded,
|
|
24
|
-
format
|
|
25
|
-
);
|
|
26
|
+
const key = await this.cryptoImpl.importPrivateSigningKey(decoded);
|
|
26
27
|
this.idToKey.set(id, key);
|
|
27
28
|
return key;
|
|
28
29
|
} catch (e) {
|
|
@@ -31,7 +32,7 @@ class NodeKeyCache {
|
|
|
31
32
|
return null;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
public getKey(id: string): CryptoKey |
|
|
35
|
+
public getKey(id: string): CryptoKey | string | undefined {
|
|
35
36
|
return this.idToKey.get(id);
|
|
36
37
|
}
|
|
37
38
|
|
package/src/crypto/crypto.ts
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
import LightsparkException from "../LightsparkException.js";
|
|
3
3
|
|
|
4
4
|
import { b64decode } from "../utils/base64.js";
|
|
5
|
+
import LightsparkSigningException from "./LightsparkSigningException.js";
|
|
6
|
+
|
|
7
|
+
export type GeneratedKeyPair = {
|
|
8
|
+
publicKey: CryptoKey | string;
|
|
9
|
+
privateKey: CryptoKey | string;
|
|
10
|
+
keyAlias?: string;
|
|
11
|
+
};
|
|
5
12
|
|
|
6
13
|
export type CryptoInterface = {
|
|
7
14
|
decryptSecretWithNodePassword: (
|
|
@@ -10,10 +17,7 @@ export type CryptoInterface = {
|
|
|
10
17
|
nodePassword: string
|
|
11
18
|
) => Promise<ArrayBuffer | null>;
|
|
12
19
|
|
|
13
|
-
generateSigningKeyPair: () => Promise<
|
|
14
|
-
publicKey: CryptoKey | string;
|
|
15
|
-
privateKey: CryptoKey | string;
|
|
16
|
-
}>;
|
|
20
|
+
generateSigningKeyPair: () => Promise<GeneratedKeyPair>;
|
|
17
21
|
|
|
18
22
|
serializeSigningKey: (
|
|
19
23
|
key: CryptoKey | string,
|
|
@@ -22,12 +26,12 @@ export type CryptoInterface = {
|
|
|
22
26
|
|
|
23
27
|
getNonce: () => Promise<number>;
|
|
24
28
|
|
|
25
|
-
sign: (
|
|
29
|
+
sign: (
|
|
30
|
+
keyOrAlias: CryptoKey | string,
|
|
31
|
+
data: Uint8Array
|
|
32
|
+
) => Promise<ArrayBuffer>;
|
|
26
33
|
|
|
27
|
-
importPrivateSigningKey: (
|
|
28
|
-
keyData: Uint8Array,
|
|
29
|
-
format: "pkcs8" | "spki"
|
|
30
|
-
) => Promise<CryptoKey | Uint8Array>;
|
|
34
|
+
importPrivateSigningKey: (keyData: Uint8Array) => Promise<CryptoKey | string>;
|
|
31
35
|
};
|
|
32
36
|
|
|
33
37
|
const getCrypto = () => {
|
|
@@ -175,9 +179,7 @@ async function decryptSecretWithNodePassword(
|
|
|
175
179
|
return decryptedValue;
|
|
176
180
|
}
|
|
177
181
|
|
|
178
|
-
const generateSigningKeyPair = async (): Promise<
|
|
179
|
-
CryptoKeyPair | { publicKey: string; privateKey: string }
|
|
180
|
-
> => {
|
|
182
|
+
const generateSigningKeyPair = async (): Promise<GeneratedKeyPair> => {
|
|
181
183
|
const cryptoImpl = await getCrypto();
|
|
182
184
|
return await cryptoImpl.subtle.generateKey(
|
|
183
185
|
/*algorithm:*/ {
|
|
@@ -209,27 +211,31 @@ const getNonce = async () => {
|
|
|
209
211
|
};
|
|
210
212
|
|
|
211
213
|
const sign = async (
|
|
212
|
-
|
|
214
|
+
keyOrAlias: CryptoKey | string,
|
|
213
215
|
data: Uint8Array
|
|
214
216
|
): Promise<ArrayBuffer> => {
|
|
217
|
+
if (typeof keyOrAlias === "string") {
|
|
218
|
+
throw new LightsparkSigningException(
|
|
219
|
+
"Key alias not supported for default crypto."
|
|
220
|
+
);
|
|
221
|
+
}
|
|
215
222
|
const cryptoImpl = await getCrypto();
|
|
216
223
|
return await cryptoImpl.subtle.sign(
|
|
217
224
|
{
|
|
218
225
|
name: "RSA-PSS",
|
|
219
226
|
saltLength: 32,
|
|
220
227
|
},
|
|
221
|
-
|
|
228
|
+
keyOrAlias as CryptoKey,
|
|
222
229
|
data
|
|
223
230
|
);
|
|
224
231
|
};
|
|
225
232
|
|
|
226
233
|
const importPrivateSigningKey = async (
|
|
227
|
-
keyData: Uint8Array
|
|
228
|
-
|
|
229
|
-
): Promise<CryptoKey | Uint8Array> => {
|
|
234
|
+
keyData: Uint8Array
|
|
235
|
+
): Promise<CryptoKey | string> => {
|
|
230
236
|
const cryptoImpl = await getCrypto();
|
|
231
237
|
return await cryptoImpl.subtle.importKey(
|
|
232
|
-
/*format*/
|
|
238
|
+
/*format*/ "pkcs8",
|
|
233
239
|
/*keyData*/ keyData,
|
|
234
240
|
/*algorithm*/ {
|
|
235
241
|
name: "RSA-PSS",
|
package/src/crypto/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
|
|
2
2
|
|
|
3
3
|
export * from "./crypto.js";
|
|
4
|
+
export * from "./KeyOrAlias.js";
|
|
4
5
|
export { default as LightsparkSigningException } from "./LightsparkSigningException.js";
|
|
5
6
|
export { default as NodeKeyCache } from "./NodeKeyCache.js";
|
package/src/requester/Query.ts
CHANGED
|
@@ -53,7 +53,8 @@ class Requester {
|
|
|
53
53
|
const data = await this.makeRawRequest(
|
|
54
54
|
query.queryPayload,
|
|
55
55
|
query.variables || {},
|
|
56
|
-
query.signingNodeId
|
|
56
|
+
query.signingNodeId,
|
|
57
|
+
!!query.skipAuth
|
|
57
58
|
);
|
|
58
59
|
return query.constructObject(data);
|
|
59
60
|
}
|
|
@@ -99,7 +100,8 @@ class Requester {
|
|
|
99
100
|
public async makeRawRequest(
|
|
100
101
|
queryPayload: string,
|
|
101
102
|
variables: { [key: string]: any } = {},
|
|
102
|
-
signingNodeId: string | undefined = undefined
|
|
103
|
+
signingNodeId: string | undefined = undefined,
|
|
104
|
+
skipAuth: boolean = false
|
|
103
105
|
): Promise<any | null> {
|
|
104
106
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
105
107
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
@@ -128,12 +130,15 @@ class Requester {
|
|
|
128
130
|
const browserUserAgent =
|
|
129
131
|
typeof navigator !== "undefined" ? navigator.userAgent : "";
|
|
130
132
|
const sdkUserAgent = this.getSdkUserAgent();
|
|
131
|
-
const
|
|
133
|
+
const baseHeaders = {
|
|
132
134
|
"Content-Type": "application/json",
|
|
133
135
|
[LIGHTSPARK_BETA_HEADER_KEY]: LIGHTSPARK_BETA_HEADER_VALUE,
|
|
134
136
|
"X-Lightspark-SDK": sdkUserAgent,
|
|
135
137
|
"User-Agent": browserUserAgent || sdkUserAgent,
|
|
136
|
-
}
|
|
138
|
+
};
|
|
139
|
+
const headers = skipAuth
|
|
140
|
+
? baseHeaders
|
|
141
|
+
: await this.authProvider.addAuthHeaders(baseHeaders);
|
|
137
142
|
bodyData = await this.addSigningDataIfNeeded(
|
|
138
143
|
bodyData,
|
|
139
144
|
headers,
|