@better-auth/api-key 1.6.12 → 1.6.13
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/client.mjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as API_KEY_ERROR_CODES, t as PACKAGE_VERSION } from "./version-
|
|
1
|
+
import { n as API_KEY_ERROR_CODES, t as PACKAGE_VERSION } from "./version-BGdGIPst.mjs";
|
|
2
2
|
import { createAuthEndpoint, createAuthMiddleware } from "@better-auth/core/api";
|
|
3
3
|
import { base64Url } from "@better-auth/utils/base64";
|
|
4
4
|
import { createHash } from "@better-auth/utils/hash";
|
|
@@ -1632,10 +1632,17 @@ function isRateLimited(apiKey, opts) {
|
|
|
1632
1632
|
}
|
|
1633
1633
|
//#endregion
|
|
1634
1634
|
//#region src/routes/verify-api-key.ts
|
|
1635
|
-
async function validateApiKey({
|
|
1636
|
-
const apiKey = await getApiKey$1(ctx,
|
|
1635
|
+
async function validateApiKey({ key, ctx, lookupOpts, configurations, schema, permissions, expectedConfigId, runCustomValidator }) {
|
|
1636
|
+
const apiKey = await getApiKey$1(ctx, lookupOpts.disableKeyHashing ? key : await defaultKeyHasher(key), lookupOpts);
|
|
1637
1637
|
if (!apiKey) throw APIError$1.from("UNAUTHORIZED", API_KEY_ERROR_CODES.INVALID_API_KEY);
|
|
1638
|
-
if (
|
|
1638
|
+
if (expectedConfigId !== void 0 && !configIdMatches(apiKey.configId, expectedConfigId)) throw APIError$1.from("UNAUTHORIZED", API_KEY_ERROR_CODES.INVALID_API_KEY);
|
|
1639
|
+
const opts = resolveConfiguration(ctx.context, configurations, apiKey.configId);
|
|
1640
|
+
if (runCustomValidator && opts.customAPIKeyValidator) {
|
|
1641
|
+
if (!await opts.customAPIKeyValidator({
|
|
1642
|
+
ctx,
|
|
1643
|
+
key
|
|
1644
|
+
})) throw APIError$1.from("UNAUTHORIZED", API_KEY_ERROR_CODES.KEY_NOT_FOUND);
|
|
1645
|
+
}
|
|
1639
1646
|
if (apiKey.enabled === false) throw APIError$1.from("UNAUTHORIZED", API_KEY_ERROR_CODES.KEY_DISABLED);
|
|
1640
1647
|
if (apiKey.expiresAt) {
|
|
1641
1648
|
if (Date.now() > new Date(apiKey.expiresAt).getTime()) {
|
|
@@ -1765,10 +1772,13 @@ async function validateApiKey({ hashedKey, ctx, opts, schema, permissions }) {
|
|
|
1765
1772
|
newApiKey = await performUpdate();
|
|
1766
1773
|
if (!newApiKey) throw APIError$1.from("INTERNAL_SERVER_ERROR", API_KEY_ERROR_CODES.FAILED_TO_UPDATE_API_KEY);
|
|
1767
1774
|
}
|
|
1768
|
-
return
|
|
1775
|
+
return {
|
|
1776
|
+
apiKey: newApiKey,
|
|
1777
|
+
opts
|
|
1778
|
+
};
|
|
1769
1779
|
}
|
|
1770
1780
|
const verifyApiKeyBodySchema = z.object({
|
|
1771
|
-
configId: z.string().meta({ description: "
|
|
1781
|
+
configId: z.string().meta({ description: "Configuration ID to scope verification to. When omitted, the key is validated against its own configuration." }).optional(),
|
|
1772
1782
|
key: z.string().meta({ description: "The key to verify" }),
|
|
1773
1783
|
permissions: z.record(z.string(), z.array(z.string())).meta({ description: "The permissions to verify." }).optional()
|
|
1774
1784
|
});
|
|
@@ -1779,7 +1789,7 @@ function verifyApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
|
|
|
1779
1789
|
}, async (ctx) => {
|
|
1780
1790
|
const { configId, key } = ctx.body;
|
|
1781
1791
|
const lookupOpts = resolveConfiguration(ctx.context, configurations, configId);
|
|
1782
|
-
if (lookupOpts.customAPIKeyValidator) {
|
|
1792
|
+
if (configId !== void 0 && lookupOpts.customAPIKeyValidator) {
|
|
1783
1793
|
if (!await lookupOpts.customAPIKeyValidator({
|
|
1784
1794
|
ctx,
|
|
1785
1795
|
key
|
|
@@ -1792,17 +1802,22 @@ function verifyApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
|
|
|
1792
1802
|
key: null
|
|
1793
1803
|
});
|
|
1794
1804
|
}
|
|
1795
|
-
const hashed = lookupOpts.disableKeyHashing ? key : await defaultKeyHasher(key);
|
|
1796
1805
|
let apiKey = null;
|
|
1806
|
+
let opts;
|
|
1797
1807
|
try {
|
|
1798
|
-
|
|
1799
|
-
|
|
1808
|
+
const result = await validateApiKey({
|
|
1809
|
+
key,
|
|
1800
1810
|
permissions: ctx.body.permissions,
|
|
1801
1811
|
ctx,
|
|
1802
|
-
|
|
1803
|
-
|
|
1812
|
+
lookupOpts,
|
|
1813
|
+
configurations,
|
|
1814
|
+
schema,
|
|
1815
|
+
expectedConfigId: configId,
|
|
1816
|
+
runCustomValidator: configId === void 0
|
|
1804
1817
|
});
|
|
1805
|
-
|
|
1818
|
+
apiKey = result.apiKey;
|
|
1819
|
+
opts = result.opts;
|
|
1820
|
+
if (opts.deferUpdates) ctx.context.runInBackground(deleteAllExpiredApiKeys(ctx.context).catch((err) => {
|
|
1806
1821
|
ctx.context.logger.error("Failed to delete expired API keys:", err);
|
|
1807
1822
|
}));
|
|
1808
1823
|
} catch (error) {
|
|
@@ -1829,7 +1844,6 @@ function verifyApiKey({ configurations, schema, deleteAllExpiredApiKeys }) {
|
|
|
1829
1844
|
key: 1,
|
|
1830
1845
|
permissions: void 0
|
|
1831
1846
|
};
|
|
1832
|
-
const opts = apiKey ? resolveConfiguration(ctx.context, configurations, apiKey.configId) : lookupOpts;
|
|
1833
1847
|
let migratedMetadata = null;
|
|
1834
1848
|
if (apiKey) migratedMetadata = await migrateDoubleStringifiedMetadata(ctx, apiKey, opts);
|
|
1835
1849
|
returningApiKey.permissions = returningApiKey.permissions ? safeJSONParse(returningApiKey.permissions) : null;
|
|
@@ -2158,11 +2172,13 @@ function apiKey(_configurations, _options) {
|
|
|
2158
2172
|
key
|
|
2159
2173
|
})) throw APIError.from("FORBIDDEN", API_KEY_ERROR_CODES.INVALID_API_KEY);
|
|
2160
2174
|
}
|
|
2161
|
-
const apiKey = await validateApiKey({
|
|
2162
|
-
|
|
2175
|
+
const { apiKey } = await validateApiKey({
|
|
2176
|
+
key,
|
|
2163
2177
|
ctx,
|
|
2164
|
-
|
|
2165
|
-
|
|
2178
|
+
lookupOpts: config,
|
|
2179
|
+
configurations,
|
|
2180
|
+
schema,
|
|
2181
|
+
expectedConfigId: config.configId
|
|
2166
2182
|
});
|
|
2167
2183
|
const cleanupTask = deleteAllExpiredApiKeys(ctx.context).catch((err) => {
|
|
2168
2184
|
ctx.context.logger.error("Failed to delete expired API keys:", err);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/api-key",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.13",
|
|
4
4
|
"description": "API Key plugin for Better Auth.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -61,14 +61,14 @@
|
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"tsdown": "0.21.1",
|
|
64
|
-
"@better-auth/core": "1.6.
|
|
65
|
-
"better-auth": "1.6.
|
|
64
|
+
"@better-auth/core": "1.6.13",
|
|
65
|
+
"better-auth": "1.6.13"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"@better-auth/utils": "0.4.1",
|
|
69
69
|
"better-call": "1.3.5",
|
|
70
|
-
"@better-auth/core": "^1.6.
|
|
71
|
-
"better-auth": "^1.6.
|
|
70
|
+
"@better-auth/core": "^1.6.13",
|
|
71
|
+
"better-auth": "^1.6.13"
|
|
72
72
|
},
|
|
73
73
|
"scripts": {
|
|
74
74
|
"build": "tsdown",
|