@nauth-toolkit/client 0.3.0 → 0.3.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.cjs +190 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +157 -1
- package/dist/index.d.ts +157 -1
- package/dist/index.mjs +188 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
23
23
|
var index_exports = {};
|
|
24
24
|
__export(index_exports, {
|
|
25
25
|
AdminOperations: () => AdminOperations,
|
|
26
|
+
ApiKeyOperations: () => ApiKeyOperations,
|
|
26
27
|
AuthAuditEventType: () => AuthAuditEventType,
|
|
27
28
|
AuthChallenge: () => AuthChallenge,
|
|
28
29
|
BrowserStorage: () => BrowserStorage,
|
|
@@ -33,6 +34,7 @@ __export(index_exports, {
|
|
|
33
34
|
NAuthClient: () => NAuthClient,
|
|
34
35
|
NAuthClientError: () => NAuthClientError,
|
|
35
36
|
NAuthErrorCode: () => NAuthErrorCode,
|
|
37
|
+
apiKeyHeader: () => apiKeyHeader,
|
|
36
38
|
defaultAdminEndpoints: () => defaultAdminEndpoints,
|
|
37
39
|
defaultEndpoints: () => defaultEndpoints,
|
|
38
40
|
getChallengeInstructions: () => getChallengeInstructions,
|
|
@@ -1478,19 +1480,182 @@ var AdminOperations = class {
|
|
|
1478
1480
|
}
|
|
1479
1481
|
};
|
|
1480
1482
|
|
|
1483
|
+
// src/core/api-key-operations.ts
|
|
1484
|
+
var hasWindow2 = () => typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
1485
|
+
function apiKeyHeader(key, headerName = "X-API-Key") {
|
|
1486
|
+
return { [headerName]: key };
|
|
1487
|
+
}
|
|
1488
|
+
var ApiKeyOperations = class {
|
|
1489
|
+
/**
|
|
1490
|
+
* @param config - Resolved client configuration
|
|
1491
|
+
* @param basePath - Base path for API-key endpoints (default: `{baseUrl}/api-keys`)
|
|
1492
|
+
*/
|
|
1493
|
+
constructor(config, basePath) {
|
|
1494
|
+
__publicField(this, "config");
|
|
1495
|
+
__publicField(this, "basePath");
|
|
1496
|
+
this.config = config;
|
|
1497
|
+
this.basePath = basePath ?? `${config.baseUrl}/api-keys`;
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* Create a new API key. The plaintext key is returned once.
|
|
1501
|
+
*
|
|
1502
|
+
* @param request - Key options (mandatory expiry; optional IP allowlist)
|
|
1503
|
+
*/
|
|
1504
|
+
async create(request) {
|
|
1505
|
+
return this.post(this.basePath, request);
|
|
1506
|
+
}
|
|
1507
|
+
/**
|
|
1508
|
+
* List the current user's API keys (sanitized).
|
|
1509
|
+
*/
|
|
1510
|
+
async list() {
|
|
1511
|
+
const res = await this.get(this.basePath);
|
|
1512
|
+
return res.apiKeys ?? [];
|
|
1513
|
+
}
|
|
1514
|
+
/**
|
|
1515
|
+
* Update a key's label and/or IP allowlist.
|
|
1516
|
+
*
|
|
1517
|
+
* @param keyId - External key identifier
|
|
1518
|
+
* @param request - Fields to update
|
|
1519
|
+
*/
|
|
1520
|
+
async update(keyId, request) {
|
|
1521
|
+
return this.patch(`${this.basePath}/${encodeURIComponent(keyId)}`, request);
|
|
1522
|
+
}
|
|
1523
|
+
/**
|
|
1524
|
+
* Revoke (soft-delete) a key.
|
|
1525
|
+
*
|
|
1526
|
+
* @param keyId - External key identifier
|
|
1527
|
+
*/
|
|
1528
|
+
async revoke(keyId) {
|
|
1529
|
+
return this.post(`${this.basePath}/${encodeURIComponent(keyId)}/revoke`, {});
|
|
1530
|
+
}
|
|
1531
|
+
/**
|
|
1532
|
+
* Permanently delete a key.
|
|
1533
|
+
*
|
|
1534
|
+
* @param keyId - External key identifier
|
|
1535
|
+
*/
|
|
1536
|
+
async remove(keyId) {
|
|
1537
|
+
return this.delete(`${this.basePath}/${encodeURIComponent(keyId)}`);
|
|
1538
|
+
}
|
|
1539
|
+
// ==========================================================================
|
|
1540
|
+
// Internal request helpers (mirror AdminOperations)
|
|
1541
|
+
// ==========================================================================
|
|
1542
|
+
async buildHeaders(method) {
|
|
1543
|
+
const headers = { ...this.config.headers };
|
|
1544
|
+
if (method !== "GET") {
|
|
1545
|
+
headers["Content-Type"] = "application/json";
|
|
1546
|
+
}
|
|
1547
|
+
if (this.config.tokenDelivery === "json") {
|
|
1548
|
+
try {
|
|
1549
|
+
const token = await this.config.storage.getItem("nauth_access_token");
|
|
1550
|
+
if (token) {
|
|
1551
|
+
headers["Authorization"] = `Bearer ${token}`;
|
|
1552
|
+
}
|
|
1553
|
+
} catch {
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
const mutating = ["POST", "PATCH", "DELETE"];
|
|
1557
|
+
if (this.config.tokenDelivery === "cookies" && hasWindow2() && mutating.includes(method)) {
|
|
1558
|
+
const csrfToken = this.getCsrfToken();
|
|
1559
|
+
if (csrfToken) {
|
|
1560
|
+
headers[this.config.csrf.headerName] = csrfToken;
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
return headers;
|
|
1564
|
+
}
|
|
1565
|
+
getCsrfToken() {
|
|
1566
|
+
if (!hasWindow2() || typeof document === "undefined") return null;
|
|
1567
|
+
const match = document.cookie.match(new RegExp(`(^| )${this.config.csrf.cookieName}=([^;]+)`));
|
|
1568
|
+
return match ? decodeURIComponent(match[2]) : null;
|
|
1569
|
+
}
|
|
1570
|
+
async get(path) {
|
|
1571
|
+
const headers = await this.buildHeaders("GET");
|
|
1572
|
+
const credentials = this.config.tokenDelivery === "cookies" ? "include" : "omit";
|
|
1573
|
+
try {
|
|
1574
|
+
const response = await this.config.httpAdapter.request({ method: "GET", url: path, headers, credentials });
|
|
1575
|
+
return response.data;
|
|
1576
|
+
} catch (error) {
|
|
1577
|
+
throw this.handleError(error);
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
async post(path, body) {
|
|
1581
|
+
const headers = await this.buildHeaders("POST");
|
|
1582
|
+
const credentials = this.config.tokenDelivery === "cookies" ? "include" : "omit";
|
|
1583
|
+
try {
|
|
1584
|
+
const response = await this.config.httpAdapter.request({
|
|
1585
|
+
method: "POST",
|
|
1586
|
+
url: path,
|
|
1587
|
+
headers,
|
|
1588
|
+
body,
|
|
1589
|
+
credentials
|
|
1590
|
+
});
|
|
1591
|
+
return response.data;
|
|
1592
|
+
} catch (error) {
|
|
1593
|
+
throw this.handleError(error);
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
async patch(path, body) {
|
|
1597
|
+
const headers = await this.buildHeaders("PATCH");
|
|
1598
|
+
const credentials = this.config.tokenDelivery === "cookies" ? "include" : "omit";
|
|
1599
|
+
try {
|
|
1600
|
+
const response = await this.config.httpAdapter.request({
|
|
1601
|
+
method: "PATCH",
|
|
1602
|
+
url: path,
|
|
1603
|
+
headers,
|
|
1604
|
+
body,
|
|
1605
|
+
credentials
|
|
1606
|
+
});
|
|
1607
|
+
return response.data;
|
|
1608
|
+
} catch (error) {
|
|
1609
|
+
throw this.handleError(error);
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
async delete(path) {
|
|
1613
|
+
const headers = await this.buildHeaders("DELETE");
|
|
1614
|
+
const credentials = this.config.tokenDelivery === "cookies" ? "include" : "omit";
|
|
1615
|
+
try {
|
|
1616
|
+
const response = await this.config.httpAdapter.request({
|
|
1617
|
+
method: "DELETE",
|
|
1618
|
+
url: path,
|
|
1619
|
+
headers,
|
|
1620
|
+
credentials
|
|
1621
|
+
});
|
|
1622
|
+
return response.data;
|
|
1623
|
+
} catch (error) {
|
|
1624
|
+
throw this.handleError(error);
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
handleError(error) {
|
|
1628
|
+
if (error instanceof NAuthClientError) {
|
|
1629
|
+
return error;
|
|
1630
|
+
}
|
|
1631
|
+
if (error && typeof error === "object" && "response" in error) {
|
|
1632
|
+
const httpError = error;
|
|
1633
|
+
const status = httpError.response?.status ?? 500;
|
|
1634
|
+
const errorData = typeof httpError.response?.data === "object" && httpError.response.data !== null ? httpError.response.data : {};
|
|
1635
|
+
const code = typeof errorData["code"] === "string" ? errorData["code"] : "INTERNAL_ERROR" /* INTERNAL_ERROR */;
|
|
1636
|
+
const message = typeof errorData["message"] === "string" ? errorData["message"] : `Request failed with status ${status}`;
|
|
1637
|
+
return new NAuthClientError(code, message, { statusCode: status, details: errorData });
|
|
1638
|
+
}
|
|
1639
|
+
return new NAuthClientError(
|
|
1640
|
+
"INTERNAL_ERROR" /* INTERNAL_ERROR */,
|
|
1641
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
1642
|
+
);
|
|
1643
|
+
}
|
|
1644
|
+
};
|
|
1645
|
+
|
|
1481
1646
|
// src/core/client.ts
|
|
1482
1647
|
var USER_KEY2 = "nauth_user";
|
|
1483
1648
|
var CHALLENGE_KEY2 = "nauth_challenge_session";
|
|
1484
1649
|
var OAUTH_STATE_KEY2 = "nauth_oauth_state";
|
|
1485
|
-
var
|
|
1650
|
+
var hasWindow3 = () => typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined";
|
|
1486
1651
|
var getOauthStorage = (mainStorage) => {
|
|
1487
|
-
if (
|
|
1652
|
+
if (hasWindow3() && typeof window.sessionStorage !== "undefined") {
|
|
1488
1653
|
return new BrowserStorage(window.sessionStorage);
|
|
1489
1654
|
}
|
|
1490
1655
|
return mainStorage;
|
|
1491
1656
|
};
|
|
1492
1657
|
var defaultStorage = () => {
|
|
1493
|
-
if (
|
|
1658
|
+
if (hasWindow3() && typeof window.localStorage !== "undefined") {
|
|
1494
1659
|
return new BrowserStorage();
|
|
1495
1660
|
}
|
|
1496
1661
|
return new InMemoryStorage();
|
|
@@ -1540,6 +1705,20 @@ var NAuthClient = class {
|
|
|
1540
1705
|
* ```
|
|
1541
1706
|
*/
|
|
1542
1707
|
__publicField(this, "admin");
|
|
1708
|
+
/**
|
|
1709
|
+
* API key management operations (create/list/update/revoke/delete).
|
|
1710
|
+
*
|
|
1711
|
+
* Calls your backend's API-key endpoints (default base path `{baseUrl}/api-keys`).
|
|
1712
|
+
*
|
|
1713
|
+
* @example
|
|
1714
|
+
* ```typescript
|
|
1715
|
+
* const { key } = await client.apiKeys.create({ expiresInDays: 90, allowedIps: ['203.0.113.0/24'] });
|
|
1716
|
+
* const keys = await client.apiKeys.list();
|
|
1717
|
+
* await client.apiKeys.update(keyId, { allowedIps: ['203.0.113.5'] });
|
|
1718
|
+
* await client.apiKeys.revoke(keyId);
|
|
1719
|
+
* ```
|
|
1720
|
+
*/
|
|
1721
|
+
__publicField(this, "apiKeys");
|
|
1543
1722
|
/**
|
|
1544
1723
|
* Handle cross-tab storage updates.
|
|
1545
1724
|
*/
|
|
@@ -1562,7 +1741,8 @@ var NAuthClient = class {
|
|
|
1562
1741
|
if (this.config.admin) {
|
|
1563
1742
|
this.admin = new AdminOperations(this.config);
|
|
1564
1743
|
}
|
|
1565
|
-
|
|
1744
|
+
this.apiKeys = new ApiKeyOperations(this.config);
|
|
1745
|
+
if (hasWindow3()) {
|
|
1566
1746
|
window.addEventListener("storage", this.handleStorageEvent);
|
|
1567
1747
|
}
|
|
1568
1748
|
}
|
|
@@ -1570,7 +1750,7 @@ var NAuthClient = class {
|
|
|
1570
1750
|
* Clean up resources.
|
|
1571
1751
|
*/
|
|
1572
1752
|
dispose() {
|
|
1573
|
-
if (
|
|
1753
|
+
if (hasWindow3()) {
|
|
1574
1754
|
window.removeEventListener("storage", this.handleStorageEvent);
|
|
1575
1755
|
}
|
|
1576
1756
|
}
|
|
@@ -2232,7 +2412,7 @@ var NAuthClient = class {
|
|
|
2232
2412
|
*/
|
|
2233
2413
|
async loginWithSocial(provider, options) {
|
|
2234
2414
|
this.eventEmitter.emit({ type: "oauth:started", data: { provider }, timestamp: Date.now() });
|
|
2235
|
-
if (
|
|
2415
|
+
if (hasWindow3()) {
|
|
2236
2416
|
const startPath = this.config.endpoints.socialRedirectStart.replace(":provider", provider);
|
|
2237
2417
|
const fullUrl = this.buildUrl(startPath);
|
|
2238
2418
|
const startUrl = new URL(fullUrl);
|
|
@@ -2566,7 +2746,7 @@ var NAuthClient = class {
|
|
|
2566
2746
|
}
|
|
2567
2747
|
}
|
|
2568
2748
|
const mutatingMethods = ["POST", "PUT", "PATCH", "DELETE"];
|
|
2569
|
-
if (this.config.tokenDelivery === "cookies" &&
|
|
2749
|
+
if (this.config.tokenDelivery === "cookies" && hasWindow3() && mutatingMethods.includes(method)) {
|
|
2570
2750
|
const csrfToken = this.getCsrfToken();
|
|
2571
2751
|
if (csrfToken) {
|
|
2572
2752
|
headers[this.config.csrf.headerName] = csrfToken;
|
|
@@ -2579,7 +2759,7 @@ var NAuthClient = class {
|
|
|
2579
2759
|
* @private
|
|
2580
2760
|
*/
|
|
2581
2761
|
getCsrfToken() {
|
|
2582
|
-
if (!
|
|
2762
|
+
if (!hasWindow3() || typeof document === "undefined") return null;
|
|
2583
2763
|
const match = document.cookie.match(new RegExp(`(^| )${this.config.csrf.cookieName}=([^;]+)`));
|
|
2584
2764
|
return match ? decodeURIComponent(match[2]) : null;
|
|
2585
2765
|
}
|
|
@@ -2760,6 +2940,7 @@ function isOTPChallenge(challenge) {
|
|
|
2760
2940
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2761
2941
|
0 && (module.exports = {
|
|
2762
2942
|
AdminOperations,
|
|
2943
|
+
ApiKeyOperations,
|
|
2763
2944
|
AuthAuditEventType,
|
|
2764
2945
|
AuthChallenge,
|
|
2765
2946
|
BrowserStorage,
|
|
@@ -2770,6 +2951,7 @@ function isOTPChallenge(challenge) {
|
|
|
2770
2951
|
NAuthClient,
|
|
2771
2952
|
NAuthClientError,
|
|
2772
2953
|
NAuthErrorCode,
|
|
2954
|
+
apiKeyHeader,
|
|
2773
2955
|
defaultAdminEndpoints,
|
|
2774
2956
|
defaultEndpoints,
|
|
2775
2957
|
getChallengeInstructions,
|