nodestack-client 2.0.0 → 2.1.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/README.md +52 -0
- package/dist/Client.d.ts +1 -0
- package/dist/Client.d.ts.map +1 -1
- package/dist/auth/PkceUtil.d.ts +9 -0
- package/dist/auth/PkceUtil.d.ts.map +1 -0
- package/dist/index.cjs +261 -1
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +261 -1
- package/dist/index.mjs.map +3 -3
- package/dist/services/RecordService.d.ts +18 -1
- package/dist/services/RecordService.d.ts.map +1 -1
- package/dist/types.d.ts +64 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +50 -50
package/dist/index.mjs
CHANGED
|
@@ -340,6 +340,175 @@ var RecordService = class extends BaseService {
|
|
|
340
340
|
}
|
|
341
341
|
return result;
|
|
342
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Returns a list of configured and enabled OAuth2 authentication providers.
|
|
345
|
+
*/
|
|
346
|
+
async listAuthMethods(redirectUri) {
|
|
347
|
+
return this.send(
|
|
348
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/auth-methods`,
|
|
349
|
+
{
|
|
350
|
+
method: "GET",
|
|
351
|
+
query: redirectUri ? { redirectUri } : void 0
|
|
352
|
+
}
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Authenticates against an auth collection using OAuth2 with PKCE code exchange.
|
|
357
|
+
* On success, automatically persists the token and record to authStore.
|
|
358
|
+
*/
|
|
359
|
+
async authWithOAuth2(options) {
|
|
360
|
+
let code = options.code;
|
|
361
|
+
let codeVerifier = options.codeVerifier;
|
|
362
|
+
if ((options.urlCallback || options.popup || typeof window !== "undefined" && !code && !codeVerifier) && (!code || !codeVerifier)) {
|
|
363
|
+
const authMethods = await this.listAuthMethods(options.redirectUrl);
|
|
364
|
+
const provider = authMethods.authProviders.find(
|
|
365
|
+
(p) => p.name.toLowerCase() === options.provider.toLowerCase()
|
|
366
|
+
);
|
|
367
|
+
if (!provider) {
|
|
368
|
+
throw new Error(`OAuth provider '${options.provider}' is not enabled or configured.`);
|
|
369
|
+
}
|
|
370
|
+
codeVerifier = provider.codeVerifier;
|
|
371
|
+
const expectedState = provider.state;
|
|
372
|
+
let expectedOrigin = null;
|
|
373
|
+
if (typeof window !== "undefined" && window.location) {
|
|
374
|
+
if (options.redirectUrl) {
|
|
375
|
+
try {
|
|
376
|
+
expectedOrigin = new URL(options.redirectUrl, window.location.href).origin;
|
|
377
|
+
} catch {
|
|
378
|
+
expectedOrigin = window.location.origin;
|
|
379
|
+
}
|
|
380
|
+
} else {
|
|
381
|
+
expectedOrigin = window.location.origin;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
let popup = options.popup || null;
|
|
385
|
+
if (options.urlCallback) {
|
|
386
|
+
const cbResult = options.urlCallback(provider.authUrl);
|
|
387
|
+
if (!popup && cbResult && typeof cbResult.postMessage === "function") {
|
|
388
|
+
popup = cbResult;
|
|
389
|
+
}
|
|
390
|
+
} else if (typeof window !== "undefined" && typeof window.open === "function") {
|
|
391
|
+
popup = window.open(provider.authUrl, "_blank", "width=600,height=700");
|
|
392
|
+
if (!popup) {
|
|
393
|
+
throw new Error("Failed to open OAuth popup window. It may have been blocked by the browser.");
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return new Promise((resolve, reject) => {
|
|
397
|
+
if (typeof window !== "undefined" && window.addEventListener) {
|
|
398
|
+
let pollTimer = null;
|
|
399
|
+
const cleanup = () => {
|
|
400
|
+
if (pollTimer) {
|
|
401
|
+
clearInterval(pollTimer);
|
|
402
|
+
pollTimer = null;
|
|
403
|
+
}
|
|
404
|
+
if (typeof window !== "undefined" && window.removeEventListener) {
|
|
405
|
+
window.removeEventListener("message", handler);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
const handler = async (event) => {
|
|
409
|
+
if (!event.data || typeof event.data !== "object" || event.data.type !== "nodestack_oauth_callback") {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (expectedOrigin && event.origin !== expectedOrigin) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (popup) {
|
|
416
|
+
if (event.source !== popup) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
} else if (event.source === window) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (expectedState && event.data.state !== expectedState) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
cleanup();
|
|
426
|
+
if (popup && typeof popup.close === "function") {
|
|
427
|
+
try {
|
|
428
|
+
popup.close();
|
|
429
|
+
} catch {
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if (event.data.error) {
|
|
433
|
+
const errMsg = typeof event.data.error === "string" ? event.data.error : event.data.error.message || event.data.error_description || JSON.stringify(event.data.error);
|
|
434
|
+
return reject(new Error(errMsg));
|
|
435
|
+
}
|
|
436
|
+
if (!event.data.code) {
|
|
437
|
+
return reject(new Error("OAuth callback message is missing authorization code."));
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
const res = await this.authWithOAuth2({
|
|
441
|
+
provider: options.provider,
|
|
442
|
+
code: event.data.code,
|
|
443
|
+
codeVerifier,
|
|
444
|
+
redirectUrl: options.redirectUrl,
|
|
445
|
+
createData: options.createData,
|
|
446
|
+
state: expectedState
|
|
447
|
+
});
|
|
448
|
+
resolve(res);
|
|
449
|
+
} catch (err) {
|
|
450
|
+
reject(err);
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
window.addEventListener("message", handler);
|
|
454
|
+
if (popup) {
|
|
455
|
+
pollTimer = setInterval(() => {
|
|
456
|
+
if (popup && popup.closed) {
|
|
457
|
+
cleanup();
|
|
458
|
+
reject(new Error("The OAuth popup window was closed before completing authentication."));
|
|
459
|
+
}
|
|
460
|
+
}, 500);
|
|
461
|
+
}
|
|
462
|
+
} else {
|
|
463
|
+
reject(new Error("urlCallback requires code and codeVerifier in non-browser environments."));
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
if (!code || !codeVerifier) {
|
|
468
|
+
throw new Error("Both code and codeVerifier are required for OAuth2 authentication");
|
|
469
|
+
}
|
|
470
|
+
const result = await this.send(
|
|
471
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/auth-with-oauth2`,
|
|
472
|
+
{
|
|
473
|
+
method: "POST",
|
|
474
|
+
body: {
|
|
475
|
+
provider: options.provider,
|
|
476
|
+
code,
|
|
477
|
+
codeVerifier,
|
|
478
|
+
state: options.state,
|
|
479
|
+
redirectUrl: options.redirectUrl,
|
|
480
|
+
createData: options.createData
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
);
|
|
484
|
+
if (result && result.token) {
|
|
485
|
+
this.client.authStore.save(result.token, result.record);
|
|
486
|
+
}
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Lists external social identities linked to a specific record.
|
|
491
|
+
*/
|
|
492
|
+
async listExternalAuths(recordId) {
|
|
493
|
+
return this.send(
|
|
494
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/records/${encodeURIComponent(recordId)}/external-auths`,
|
|
495
|
+
{
|
|
496
|
+
method: "GET"
|
|
497
|
+
}
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Unlinks an external social identity from a record.
|
|
502
|
+
*/
|
|
503
|
+
async unlinkExternalAuth(recordId, provider) {
|
|
504
|
+
await this.send(
|
|
505
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/records/${encodeURIComponent(recordId)}/external-auths/${encodeURIComponent(provider)}`,
|
|
506
|
+
{
|
|
507
|
+
method: "DELETE"
|
|
508
|
+
}
|
|
509
|
+
);
|
|
510
|
+
return true;
|
|
511
|
+
}
|
|
343
512
|
/**
|
|
344
513
|
* Exports records from the collection as CSV or JSON.
|
|
345
514
|
*/
|
|
@@ -1127,6 +1296,7 @@ var AppService = class extends BaseService {
|
|
|
1127
1296
|
var NodeStackClient = class _NodeStackClient {
|
|
1128
1297
|
baseUrl;
|
|
1129
1298
|
authStore;
|
|
1299
|
+
apiKey;
|
|
1130
1300
|
files;
|
|
1131
1301
|
realtime;
|
|
1132
1302
|
admins;
|
|
@@ -1144,6 +1314,7 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1144
1314
|
constructor(baseUrl = "/", options = {}) {
|
|
1145
1315
|
this.baseUrl = (baseUrl || "/").replace(/\/+$/, "");
|
|
1146
1316
|
this.authStore = options.authStore || new LocalAuthStore();
|
|
1317
|
+
this.apiKey = options.apiKey;
|
|
1147
1318
|
this.customFetch = options.fetch;
|
|
1148
1319
|
this.defaultHeaders = options.headers || {};
|
|
1149
1320
|
this.defaultTimeout = options.timeout || 12e4;
|
|
@@ -1207,7 +1378,9 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1207
1378
|
...this.defaultHeaders,
|
|
1208
1379
|
...options.headers || {}
|
|
1209
1380
|
};
|
|
1210
|
-
if (this.
|
|
1381
|
+
if (this.apiKey && !headers["Authorization"] && !headers["authorization"] && !headers["x-api-key"] && !headers["X-API-Key"]) {
|
|
1382
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
1383
|
+
} else if (this.authStore.token && !headers["Authorization"] && !headers["authorization"]) {
|
|
1211
1384
|
headers["Authorization"] = `Bearer ${this.authStore.token}`;
|
|
1212
1385
|
}
|
|
1213
1386
|
if (this.appSlug && !headers["x-nodestack-app"] && !headers["X-NodeStack-App"]) {
|
|
@@ -1350,6 +1523,92 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1350
1523
|
var MemoryAuthStore = class extends BaseAuthStore {
|
|
1351
1524
|
// Inherits default in-memory behavior of BaseAuthStore
|
|
1352
1525
|
};
|
|
1526
|
+
|
|
1527
|
+
// src/auth/PkceUtil.ts
|
|
1528
|
+
var PkceUtil = class _PkceUtil {
|
|
1529
|
+
static generateVerifier(length = 64) {
|
|
1530
|
+
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
1531
|
+
const clampedLength = Math.max(43, Math.min(128, length));
|
|
1532
|
+
const randomBytes = new Uint8Array(clampedLength);
|
|
1533
|
+
const g = typeof globalThis !== "undefined" ? globalThis : {};
|
|
1534
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
1535
|
+
crypto.getRandomValues(randomBytes);
|
|
1536
|
+
} else if (g.crypto && g.crypto.getRandomValues) {
|
|
1537
|
+
g.crypto.getRandomValues(randomBytes);
|
|
1538
|
+
} else {
|
|
1539
|
+
let filled = false;
|
|
1540
|
+
try {
|
|
1541
|
+
const req = g.require;
|
|
1542
|
+
if (typeof req === "function") {
|
|
1543
|
+
req("crypto").randomFillSync(randomBytes);
|
|
1544
|
+
filled = true;
|
|
1545
|
+
}
|
|
1546
|
+
} catch {
|
|
1547
|
+
}
|
|
1548
|
+
if (!filled) {
|
|
1549
|
+
throw new Error(
|
|
1550
|
+
"A cryptographically secure random number generator (crypto.getRandomValues or node:crypto) is required to generate a PKCE code_verifier."
|
|
1551
|
+
);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
let result = "";
|
|
1555
|
+
for (let i = 0; i < clampedLength; i++) {
|
|
1556
|
+
result += charset[randomBytes[i] % charset.length];
|
|
1557
|
+
}
|
|
1558
|
+
return result;
|
|
1559
|
+
}
|
|
1560
|
+
static async generateChallenge(verifier) {
|
|
1561
|
+
const encoder = new TextEncoder();
|
|
1562
|
+
const data = encoder.encode(verifier);
|
|
1563
|
+
const g = typeof globalThis !== "undefined" ? globalThis : {};
|
|
1564
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
1565
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
1566
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1567
|
+
} else if (g.crypto && g.crypto.subtle) {
|
|
1568
|
+
const hash = await g.crypto.subtle.digest("SHA-256", data);
|
|
1569
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1570
|
+
}
|
|
1571
|
+
try {
|
|
1572
|
+
const req = g.require;
|
|
1573
|
+
if (typeof req === "function") {
|
|
1574
|
+
const hash = req("crypto").createHash("sha256").update(data).digest();
|
|
1575
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1576
|
+
}
|
|
1577
|
+
} catch {
|
|
1578
|
+
}
|
|
1579
|
+
throw new Error("Web Crypto API (crypto.subtle) is not available to compute SHA-256 code challenge.");
|
|
1580
|
+
}
|
|
1581
|
+
static base64UrlEncode(bytes) {
|
|
1582
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1583
|
+
let base64 = "";
|
|
1584
|
+
const byteLength = bytes.byteLength;
|
|
1585
|
+
const byteRemainder = byteLength % 3;
|
|
1586
|
+
const mainLength = byteLength - byteRemainder;
|
|
1587
|
+
let a, b, c, d;
|
|
1588
|
+
let chunk;
|
|
1589
|
+
for (let i = 0; i < mainLength; i += 3) {
|
|
1590
|
+
chunk = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
|
|
1591
|
+
a = (chunk & 16515072) >> 18;
|
|
1592
|
+
b = (chunk & 258048) >> 12;
|
|
1593
|
+
c = (chunk & 4032) >> 6;
|
|
1594
|
+
d = chunk & 63;
|
|
1595
|
+
base64 += chars[a] + chars[b] + chars[c] + chars[d];
|
|
1596
|
+
}
|
|
1597
|
+
if (byteRemainder === 1) {
|
|
1598
|
+
chunk = bytes[mainLength];
|
|
1599
|
+
a = (chunk & 252) >> 2;
|
|
1600
|
+
b = (chunk & 3) << 4;
|
|
1601
|
+
base64 += chars[a] + chars[b] + "==";
|
|
1602
|
+
} else if (byteRemainder === 2) {
|
|
1603
|
+
chunk = bytes[mainLength] << 8 | bytes[mainLength + 1];
|
|
1604
|
+
a = (chunk & 64512) >> 10;
|
|
1605
|
+
b = (chunk & 1008) >> 4;
|
|
1606
|
+
c = (chunk & 15) << 2;
|
|
1607
|
+
base64 += chars[a] + chars[b] + chars[c] + "=";
|
|
1608
|
+
}
|
|
1609
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1610
|
+
}
|
|
1611
|
+
};
|
|
1353
1612
|
export {
|
|
1354
1613
|
AdminService,
|
|
1355
1614
|
AppService,
|
|
@@ -1362,6 +1621,7 @@ export {
|
|
|
1362
1621
|
LocalAuthStore,
|
|
1363
1622
|
MemoryAuthStore,
|
|
1364
1623
|
NodeStackClient,
|
|
1624
|
+
PkceUtil,
|
|
1365
1625
|
RealtimeService,
|
|
1366
1626
|
RecordService,
|
|
1367
1627
|
SnapshotService,
|