@digilogiclabs/platform-core 1.21.0 → 1.24.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/dist/{ConsoleEmail-DpxhsG9C.d.mts → ConsoleEmail-CgSIvRdj.d.mts} +2 -1
- package/dist/{ConsoleEmail-BbdpINKx.d.ts → ConsoleEmail-iJO7Cb_v.d.ts} +2 -1
- package/dist/{IAI-CWd11dzL.d.mts → IAI-B7pp322X.d.mts} +1 -66
- package/dist/{IAI-CWd11dzL.d.ts → IAI-B7pp322X.d.ts} +1 -66
- package/dist/ICache-BWSuTuhz.d.mts +66 -0
- package/dist/ICache-BWSuTuhz.d.ts +66 -0
- package/dist/{agents-CES-Aq1K.d.mts → agents-C7FPA8y9.d.mts} +2 -1
- package/dist/{agents-C42rGwRl.d.ts → agents-qyQ3BtXy.d.ts} +2 -1
- package/dist/agents.d.mts +3 -2
- package/dist/agents.d.ts +3 -2
- package/dist/auth.d.mts +181 -3
- package/dist/auth.d.ts +181 -3
- package/dist/auth.js +275 -2
- package/dist/auth.js.map +1 -1
- package/dist/auth.mjs +273 -2
- package/dist/auth.mjs.map +1 -1
- package/dist/index.d.mts +8 -6
- package/dist/index.d.ts +8 -6
- package/dist/testing.d.mts +4 -3
- package/dist/testing.d.ts +4 -3
- package/package.json +1 -1
package/dist/auth.mjs
CHANGED
|
@@ -1541,6 +1541,24 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1541
1541
|
const resolvedAuditAction = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.action : auditCfg;
|
|
1542
1542
|
const resolvedAuditResource = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.resource : routeConfig.auditResource;
|
|
1543
1543
|
const resolvedGetResourceId = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.getResourceId : routeConfig.getResourceId;
|
|
1544
|
+
const emitBlockedAudit = async (reason) => {
|
|
1545
|
+
if (!factoryConfig.auditOnBlocked || !factoryConfig.auditLog) return;
|
|
1546
|
+
const action = factoryConfig.blockedAuditAction ?? "unauthorized_access";
|
|
1547
|
+
try {
|
|
1548
|
+
await factoryConfig.auditLog({
|
|
1549
|
+
actor: session?.user ? {
|
|
1550
|
+
id: session.user.id ?? "unknown",
|
|
1551
|
+
type: isLegacyToken ? "admin" : "user",
|
|
1552
|
+
email: session.user.email ?? void 0
|
|
1553
|
+
} : { id: "anonymous", type: "anonymous" },
|
|
1554
|
+
action,
|
|
1555
|
+
resource: resolvedAuditResource ? { type: resolvedAuditResource, id: "unknown" } : void 0,
|
|
1556
|
+
outcome: "blocked",
|
|
1557
|
+
reason
|
|
1558
|
+
});
|
|
1559
|
+
} catch {
|
|
1560
|
+
}
|
|
1561
|
+
};
|
|
1544
1562
|
try {
|
|
1545
1563
|
if (routeConfig.requireAuth || routeConfig.requireAdmin || routeConfig.requireRoles?.length) {
|
|
1546
1564
|
session = await factoryConfig.getSession();
|
|
@@ -1551,6 +1569,7 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1551
1569
|
isAdmin = true;
|
|
1552
1570
|
} else {
|
|
1553
1571
|
log.warn("Unauthorized access attempt");
|
|
1572
|
+
await emitBlockedAudit("No authenticated session or valid token");
|
|
1554
1573
|
return Response.json(
|
|
1555
1574
|
{ error: "Unauthorized" },
|
|
1556
1575
|
{ status: 401, headers: { "X-Request-ID": requestId } }
|
|
@@ -1559,6 +1578,7 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1559
1578
|
}
|
|
1560
1579
|
if (routeConfig.requireAdmin && !isLegacyToken && !isAdmin) {
|
|
1561
1580
|
log.warn("Admin access denied");
|
|
1581
|
+
await emitBlockedAudit("Missing admin role");
|
|
1562
1582
|
return Response.json(
|
|
1563
1583
|
{ error: "Forbidden: Admin access required" },
|
|
1564
1584
|
{ status: 403, headers: { "X-Request-ID": requestId } }
|
|
@@ -1570,21 +1590,30 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1570
1590
|
);
|
|
1571
1591
|
if (!hasRole2) {
|
|
1572
1592
|
log.warn("Role-based access denied");
|
|
1593
|
+
await emitBlockedAudit(
|
|
1594
|
+
`Missing required roles: ${routeConfig.requireRoles.join(", ")}`
|
|
1595
|
+
);
|
|
1573
1596
|
return Response.json(
|
|
1574
1597
|
{ error: "Forbidden: Insufficient permissions" },
|
|
1575
1598
|
{ status: 403, headers: { "X-Request-ID": requestId } }
|
|
1576
1599
|
);
|
|
1577
1600
|
}
|
|
1578
1601
|
}
|
|
1602
|
+
let rateLimitInfo = null;
|
|
1579
1603
|
if (!routeConfig.skipRateLimit && routeConfig.rateLimit && factoryConfig.rateLimiter) {
|
|
1580
1604
|
const userId = session?.user?.id ?? void 0;
|
|
1581
|
-
const
|
|
1605
|
+
const result = await factoryConfig.rateLimiter.enforce(
|
|
1582
1606
|
request,
|
|
1583
1607
|
operation,
|
|
1584
1608
|
routeConfig.rateLimit,
|
|
1585
1609
|
userId
|
|
1586
1610
|
);
|
|
1587
|
-
if (
|
|
1611
|
+
if (result instanceof Response) {
|
|
1612
|
+
return result;
|
|
1613
|
+
}
|
|
1614
|
+
if (result !== null && typeof result === "object") {
|
|
1615
|
+
rateLimitInfo = result;
|
|
1616
|
+
}
|
|
1588
1617
|
}
|
|
1589
1618
|
let validated = {};
|
|
1590
1619
|
if (routeConfig.validate) {
|
|
@@ -1650,6 +1679,20 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1650
1679
|
};
|
|
1651
1680
|
const response = await handler(request, ctx);
|
|
1652
1681
|
response.headers.set("X-Request-ID", requestId);
|
|
1682
|
+
if (rateLimitInfo) {
|
|
1683
|
+
response.headers.set(
|
|
1684
|
+
"X-RateLimit-Limit",
|
|
1685
|
+
String(rateLimitInfo.limit)
|
|
1686
|
+
);
|
|
1687
|
+
response.headers.set(
|
|
1688
|
+
"X-RateLimit-Remaining",
|
|
1689
|
+
String(rateLimitInfo.remaining)
|
|
1690
|
+
);
|
|
1691
|
+
response.headers.set(
|
|
1692
|
+
"X-RateLimit-Reset",
|
|
1693
|
+
String(rateLimitInfo.resetMs)
|
|
1694
|
+
);
|
|
1695
|
+
}
|
|
1653
1696
|
if (resolvedAuditAction && factoryConfig.auditLog && !timedAudit) {
|
|
1654
1697
|
await factoryConfig.auditLog({
|
|
1655
1698
|
actor: {
|
|
@@ -1728,6 +1771,43 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1728
1771
|
handler
|
|
1729
1772
|
);
|
|
1730
1773
|
}
|
|
1774
|
+
function withRoleBasedApi(roles, config, handler) {
|
|
1775
|
+
return createSecureHandler(
|
|
1776
|
+
{
|
|
1777
|
+
rateLimit: factoryConfig.rateLimiter?.authDefault,
|
|
1778
|
+
...config,
|
|
1779
|
+
requireAuth: true,
|
|
1780
|
+
requireRoles: roles
|
|
1781
|
+
},
|
|
1782
|
+
handler
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
function withDebugApi(config, handler) {
|
|
1786
|
+
const { allowInProduction = false, ...routeConfig } = config;
|
|
1787
|
+
const inner = createSecureHandler(
|
|
1788
|
+
{
|
|
1789
|
+
rateLimit: factoryConfig.rateLimiter?.adminDefault,
|
|
1790
|
+
...routeConfig,
|
|
1791
|
+
requireAuth: true,
|
|
1792
|
+
requireAdmin: true
|
|
1793
|
+
},
|
|
1794
|
+
handler
|
|
1795
|
+
);
|
|
1796
|
+
return async (request, routeContext) => {
|
|
1797
|
+
const isProduction = isDev === false;
|
|
1798
|
+
if (isProduction && !allowInProduction) {
|
|
1799
|
+
const requestId = getRequestId(request);
|
|
1800
|
+
return Response.json(
|
|
1801
|
+
{
|
|
1802
|
+
error: "Debug endpoints are disabled in production",
|
|
1803
|
+
code: "DEBUG_DISABLED"
|
|
1804
|
+
},
|
|
1805
|
+
{ status: 403, headers: { "X-Request-ID": requestId } }
|
|
1806
|
+
);
|
|
1807
|
+
}
|
|
1808
|
+
return inner(request, routeContext);
|
|
1809
|
+
};
|
|
1810
|
+
}
|
|
1731
1811
|
function withLegacyAdminApi(config, handler) {
|
|
1732
1812
|
return createSecureHandler(
|
|
1733
1813
|
{
|
|
@@ -1740,11 +1820,91 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1740
1820
|
handler
|
|
1741
1821
|
);
|
|
1742
1822
|
}
|
|
1823
|
+
function withAiApi(config, handler) {
|
|
1824
|
+
return createSecureHandler(
|
|
1825
|
+
{
|
|
1826
|
+
rateLimit: factoryConfig.rateLimiter?.aiDefault,
|
|
1827
|
+
...config,
|
|
1828
|
+
requireAuth: true
|
|
1829
|
+
},
|
|
1830
|
+
handler
|
|
1831
|
+
);
|
|
1832
|
+
}
|
|
1833
|
+
function withCronApi(config, handler) {
|
|
1834
|
+
const { allowVercelCron = true, ...routeConfig } = config;
|
|
1835
|
+
return async (request, routeContext) => {
|
|
1836
|
+
const requestId = getRequestId(request);
|
|
1837
|
+
const operation = routeConfig.operation || `CRON ${new URL(request.url).pathname}`;
|
|
1838
|
+
const log = makeLogger(request, operation);
|
|
1839
|
+
const userAgent = request.headers.get("user-agent") || "";
|
|
1840
|
+
const isVercelCron = allowVercelCron && userAgent.toLowerCase().includes("vercel");
|
|
1841
|
+
const isAdminToken = checkLegacyToken(request);
|
|
1842
|
+
let isCronSecret = false;
|
|
1843
|
+
if (factoryConfig.cronSecret) {
|
|
1844
|
+
try {
|
|
1845
|
+
const contentType = request.headers.get("content-type");
|
|
1846
|
+
if (contentType?.includes("application/json")) {
|
|
1847
|
+
const body = await request.clone().json();
|
|
1848
|
+
if (typeof body.cronSecret === "string" && constantTimeEqual(body.cronSecret, factoryConfig.cronSecret)) {
|
|
1849
|
+
isCronSecret = true;
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
} catch {
|
|
1853
|
+
}
|
|
1854
|
+
if (!isCronSecret) {
|
|
1855
|
+
const qs = new URL(request.url).searchParams.get("cronSecret") || "";
|
|
1856
|
+
if (qs && constantTimeEqual(qs, factoryConfig.cronSecret)) {
|
|
1857
|
+
isCronSecret = true;
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
if (!isVercelCron && !isAdminToken && !isCronSecret) {
|
|
1862
|
+
log.warn("Unauthorized cron access attempt");
|
|
1863
|
+
return Response.json(
|
|
1864
|
+
{ error: "Unauthorized" },
|
|
1865
|
+
{ status: 401, headers: { "X-Request-ID": requestId } }
|
|
1866
|
+
);
|
|
1867
|
+
}
|
|
1868
|
+
log.info("Cron job authorized", {
|
|
1869
|
+
method: isVercelCron ? "vercel_cron" : isAdminToken ? "admin_token" : "cron_secret"
|
|
1870
|
+
});
|
|
1871
|
+
const params = await routeContext.params;
|
|
1872
|
+
const ctx = {
|
|
1873
|
+
session: null,
|
|
1874
|
+
isLegacyToken: isAdminToken,
|
|
1875
|
+
authMethod: isAdminToken ? "legacy_token" : "none",
|
|
1876
|
+
isAdmin: true,
|
|
1877
|
+
// cron implicitly trusted
|
|
1878
|
+
validated: {},
|
|
1879
|
+
logger: log,
|
|
1880
|
+
requestId,
|
|
1881
|
+
params
|
|
1882
|
+
};
|
|
1883
|
+
try {
|
|
1884
|
+
const response = await handler(request, ctx);
|
|
1885
|
+
response.headers.set("X-Request-ID", requestId);
|
|
1886
|
+
return response;
|
|
1887
|
+
} catch (error) {
|
|
1888
|
+
log.error("Cron handler error", {
|
|
1889
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1890
|
+
});
|
|
1891
|
+
const { status, body } = classify(error, isDev);
|
|
1892
|
+
return Response.json(
|
|
1893
|
+
{ error: body.error, code: body.code },
|
|
1894
|
+
{ status, headers: { "X-Request-ID": requestId } }
|
|
1895
|
+
);
|
|
1896
|
+
}
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1743
1899
|
return {
|
|
1744
1900
|
createSecureHandler,
|
|
1745
1901
|
withPublicApi,
|
|
1746
1902
|
withAuthenticatedApi,
|
|
1747
1903
|
withAdminApi,
|
|
1904
|
+
withRoleBasedApi,
|
|
1905
|
+
withDebugApi,
|
|
1906
|
+
withAiApi,
|
|
1907
|
+
withCronApi,
|
|
1748
1908
|
withLegacyAdminApi
|
|
1749
1909
|
};
|
|
1750
1910
|
}
|
|
@@ -2004,6 +2164,115 @@ function createHealthRoute(opts) {
|
|
|
2004
2164
|
};
|
|
2005
2165
|
}
|
|
2006
2166
|
|
|
2167
|
+
// src/interfaces/ILogger.ts
|
|
2168
|
+
var NoopLogger = class {
|
|
2169
|
+
debug() {
|
|
2170
|
+
}
|
|
2171
|
+
info() {
|
|
2172
|
+
}
|
|
2173
|
+
warn() {
|
|
2174
|
+
}
|
|
2175
|
+
error() {
|
|
2176
|
+
}
|
|
2177
|
+
child() {
|
|
2178
|
+
return this;
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
|
|
2182
|
+
// src/auth/cache-helpers.ts
|
|
2183
|
+
var CacheTTL = {
|
|
2184
|
+
/** 5 minutes — query results, dropdown options. */
|
|
2185
|
+
SHORT: 300,
|
|
2186
|
+
/** 1 hour — semi-static reference data, per-user state. */
|
|
2187
|
+
MEDIUM: 3600,
|
|
2188
|
+
/** 6 hours — heavy aggregates, derived state. */
|
|
2189
|
+
LONG: 21600,
|
|
2190
|
+
/** 24 hours — positive AI cache, immutable enrichments. */
|
|
2191
|
+
DAY: 86400,
|
|
2192
|
+
/** 7 days — long-term derived data, hashed content. */
|
|
2193
|
+
WEEK: 604800
|
|
2194
|
+
};
|
|
2195
|
+
function createAppCache(cache, options = {}) {
|
|
2196
|
+
const logger = options.logger ?? new NoopLogger();
|
|
2197
|
+
return {
|
|
2198
|
+
async get(key) {
|
|
2199
|
+
try {
|
|
2200
|
+
return await cache.get(key);
|
|
2201
|
+
} catch (err) {
|
|
2202
|
+
logger.debug("cache.get failed", { key, err: String(err) });
|
|
2203
|
+
return null;
|
|
2204
|
+
}
|
|
2205
|
+
},
|
|
2206
|
+
async set(key, value, ttlSeconds) {
|
|
2207
|
+
try {
|
|
2208
|
+
await cache.set(key, value, ttlSeconds);
|
|
2209
|
+
} catch (err) {
|
|
2210
|
+
logger.debug("cache.set failed", { key, err: String(err) });
|
|
2211
|
+
}
|
|
2212
|
+
},
|
|
2213
|
+
async delete(key) {
|
|
2214
|
+
try {
|
|
2215
|
+
await cache.delete(key);
|
|
2216
|
+
} catch (err) {
|
|
2217
|
+
logger.debug("cache.delete failed", { key, err: String(err) });
|
|
2218
|
+
}
|
|
2219
|
+
},
|
|
2220
|
+
async exists(key) {
|
|
2221
|
+
try {
|
|
2222
|
+
return await cache.exists(key);
|
|
2223
|
+
} catch {
|
|
2224
|
+
return false;
|
|
2225
|
+
}
|
|
2226
|
+
},
|
|
2227
|
+
async getCachedOrFetch(key, fetchFn, ttlSeconds) {
|
|
2228
|
+
try {
|
|
2229
|
+
const cached = await cache.get(key);
|
|
2230
|
+
if (cached !== null) return cached;
|
|
2231
|
+
} catch (err) {
|
|
2232
|
+
logger.debug("getCachedOrFetch read failed; falling through", {
|
|
2233
|
+
key,
|
|
2234
|
+
err: String(err)
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
2237
|
+
const fresh = await fetchFn();
|
|
2238
|
+
try {
|
|
2239
|
+
await cache.set(key, fresh, ttlSeconds);
|
|
2240
|
+
} catch (err) {
|
|
2241
|
+
logger.debug("getCachedOrFetch write-back failed", {
|
|
2242
|
+
key,
|
|
2243
|
+
err: String(err)
|
|
2244
|
+
});
|
|
2245
|
+
}
|
|
2246
|
+
return fresh;
|
|
2247
|
+
},
|
|
2248
|
+
async invalidate(keys) {
|
|
2249
|
+
const list = Array.isArray(keys) ? keys : [keys];
|
|
2250
|
+
let total = 0;
|
|
2251
|
+
for (const key of list) {
|
|
2252
|
+
try {
|
|
2253
|
+
if (key.includes("*")) {
|
|
2254
|
+
total += await cache.deletePattern(key);
|
|
2255
|
+
} else {
|
|
2256
|
+
await cache.delete(key);
|
|
2257
|
+
total += 1;
|
|
2258
|
+
}
|
|
2259
|
+
} catch (err) {
|
|
2260
|
+
logger.debug("invalidate failed for key", { key, err: String(err) });
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
return total;
|
|
2264
|
+
},
|
|
2265
|
+
async warm(key, fetchFn, ttlSeconds) {
|
|
2266
|
+
try {
|
|
2267
|
+
const value = await fetchFn();
|
|
2268
|
+
await cache.set(key, value, ttlSeconds);
|
|
2269
|
+
} catch (err) {
|
|
2270
|
+
logger.debug("warm failed", { key, err: String(err) });
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
};
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2007
2276
|
// src/env.ts
|
|
2008
2277
|
function getRequiredEnv(key) {
|
|
2009
2278
|
const value = process.env[key];
|
|
@@ -2097,6 +2366,7 @@ function getEnvSummary(keys) {
|
|
|
2097
2366
|
export {
|
|
2098
2367
|
ApiError,
|
|
2099
2368
|
ApiErrorCode,
|
|
2369
|
+
CacheTTL,
|
|
2100
2370
|
CommonApiErrors,
|
|
2101
2371
|
CommonRateLimits,
|
|
2102
2372
|
DateRangeSchema,
|
|
@@ -2131,6 +2401,7 @@ export {
|
|
|
2131
2401
|
constantTimeEqual,
|
|
2132
2402
|
containsHtml,
|
|
2133
2403
|
containsUrls,
|
|
2404
|
+
createAppCache,
|
|
2134
2405
|
createAuditActor,
|
|
2135
2406
|
createAuditLogger,
|
|
2136
2407
|
createBetaClient,
|