@digilogiclabs/platform-core 1.21.0 → 1.23.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 +149 -1
- package/dist/auth.d.ts +149 -1
- package/dist/auth.js +253 -0
- package/dist/auth.js.map +1 -1
- package/dist/auth.mjs +251 -0
- 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,6 +1590,9 @@ 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 } }
|
|
@@ -1728,6 +1751,43 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1728
1751
|
handler
|
|
1729
1752
|
);
|
|
1730
1753
|
}
|
|
1754
|
+
function withRoleBasedApi(roles, config, handler) {
|
|
1755
|
+
return createSecureHandler(
|
|
1756
|
+
{
|
|
1757
|
+
rateLimit: factoryConfig.rateLimiter?.authDefault,
|
|
1758
|
+
...config,
|
|
1759
|
+
requireAuth: true,
|
|
1760
|
+
requireRoles: roles
|
|
1761
|
+
},
|
|
1762
|
+
handler
|
|
1763
|
+
);
|
|
1764
|
+
}
|
|
1765
|
+
function withDebugApi(config, handler) {
|
|
1766
|
+
const { allowInProduction = false, ...routeConfig } = config;
|
|
1767
|
+
const inner = createSecureHandler(
|
|
1768
|
+
{
|
|
1769
|
+
rateLimit: factoryConfig.rateLimiter?.adminDefault,
|
|
1770
|
+
...routeConfig,
|
|
1771
|
+
requireAuth: true,
|
|
1772
|
+
requireAdmin: true
|
|
1773
|
+
},
|
|
1774
|
+
handler
|
|
1775
|
+
);
|
|
1776
|
+
return async (request, routeContext) => {
|
|
1777
|
+
const isProduction = isDev === false;
|
|
1778
|
+
if (isProduction && !allowInProduction) {
|
|
1779
|
+
const requestId = getRequestId(request);
|
|
1780
|
+
return Response.json(
|
|
1781
|
+
{
|
|
1782
|
+
error: "Debug endpoints are disabled in production",
|
|
1783
|
+
code: "DEBUG_DISABLED"
|
|
1784
|
+
},
|
|
1785
|
+
{ status: 403, headers: { "X-Request-ID": requestId } }
|
|
1786
|
+
);
|
|
1787
|
+
}
|
|
1788
|
+
return inner(request, routeContext);
|
|
1789
|
+
};
|
|
1790
|
+
}
|
|
1731
1791
|
function withLegacyAdminApi(config, handler) {
|
|
1732
1792
|
return createSecureHandler(
|
|
1733
1793
|
{
|
|
@@ -1740,11 +1800,91 @@ function createSecureHandlerFactory(factoryConfig) {
|
|
|
1740
1800
|
handler
|
|
1741
1801
|
);
|
|
1742
1802
|
}
|
|
1803
|
+
function withAiApi(config, handler) {
|
|
1804
|
+
return createSecureHandler(
|
|
1805
|
+
{
|
|
1806
|
+
rateLimit: factoryConfig.rateLimiter?.aiDefault,
|
|
1807
|
+
...config,
|
|
1808
|
+
requireAuth: true
|
|
1809
|
+
},
|
|
1810
|
+
handler
|
|
1811
|
+
);
|
|
1812
|
+
}
|
|
1813
|
+
function withCronApi(config, handler) {
|
|
1814
|
+
const { allowVercelCron = true, ...routeConfig } = config;
|
|
1815
|
+
return async (request, routeContext) => {
|
|
1816
|
+
const requestId = getRequestId(request);
|
|
1817
|
+
const operation = routeConfig.operation || `CRON ${new URL(request.url).pathname}`;
|
|
1818
|
+
const log = makeLogger(request, operation);
|
|
1819
|
+
const userAgent = request.headers.get("user-agent") || "";
|
|
1820
|
+
const isVercelCron = allowVercelCron && userAgent.toLowerCase().includes("vercel");
|
|
1821
|
+
const isAdminToken = checkLegacyToken(request);
|
|
1822
|
+
let isCronSecret = false;
|
|
1823
|
+
if (factoryConfig.cronSecret) {
|
|
1824
|
+
try {
|
|
1825
|
+
const contentType = request.headers.get("content-type");
|
|
1826
|
+
if (contentType?.includes("application/json")) {
|
|
1827
|
+
const body = await request.clone().json();
|
|
1828
|
+
if (typeof body.cronSecret === "string" && constantTimeEqual(body.cronSecret, factoryConfig.cronSecret)) {
|
|
1829
|
+
isCronSecret = true;
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
} catch {
|
|
1833
|
+
}
|
|
1834
|
+
if (!isCronSecret) {
|
|
1835
|
+
const qs = new URL(request.url).searchParams.get("cronSecret") || "";
|
|
1836
|
+
if (qs && constantTimeEqual(qs, factoryConfig.cronSecret)) {
|
|
1837
|
+
isCronSecret = true;
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
if (!isVercelCron && !isAdminToken && !isCronSecret) {
|
|
1842
|
+
log.warn("Unauthorized cron access attempt");
|
|
1843
|
+
return Response.json(
|
|
1844
|
+
{ error: "Unauthorized" },
|
|
1845
|
+
{ status: 401, headers: { "X-Request-ID": requestId } }
|
|
1846
|
+
);
|
|
1847
|
+
}
|
|
1848
|
+
log.info("Cron job authorized", {
|
|
1849
|
+
method: isVercelCron ? "vercel_cron" : isAdminToken ? "admin_token" : "cron_secret"
|
|
1850
|
+
});
|
|
1851
|
+
const params = await routeContext.params;
|
|
1852
|
+
const ctx = {
|
|
1853
|
+
session: null,
|
|
1854
|
+
isLegacyToken: isAdminToken,
|
|
1855
|
+
authMethod: isAdminToken ? "legacy_token" : "none",
|
|
1856
|
+
isAdmin: true,
|
|
1857
|
+
// cron implicitly trusted
|
|
1858
|
+
validated: {},
|
|
1859
|
+
logger: log,
|
|
1860
|
+
requestId,
|
|
1861
|
+
params
|
|
1862
|
+
};
|
|
1863
|
+
try {
|
|
1864
|
+
const response = await handler(request, ctx);
|
|
1865
|
+
response.headers.set("X-Request-ID", requestId);
|
|
1866
|
+
return response;
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
log.error("Cron handler error", {
|
|
1869
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1870
|
+
});
|
|
1871
|
+
const { status, body } = classify(error, isDev);
|
|
1872
|
+
return Response.json(
|
|
1873
|
+
{ error: body.error, code: body.code },
|
|
1874
|
+
{ status, headers: { "X-Request-ID": requestId } }
|
|
1875
|
+
);
|
|
1876
|
+
}
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1743
1879
|
return {
|
|
1744
1880
|
createSecureHandler,
|
|
1745
1881
|
withPublicApi,
|
|
1746
1882
|
withAuthenticatedApi,
|
|
1747
1883
|
withAdminApi,
|
|
1884
|
+
withRoleBasedApi,
|
|
1885
|
+
withDebugApi,
|
|
1886
|
+
withAiApi,
|
|
1887
|
+
withCronApi,
|
|
1748
1888
|
withLegacyAdminApi
|
|
1749
1889
|
};
|
|
1750
1890
|
}
|
|
@@ -2004,6 +2144,115 @@ function createHealthRoute(opts) {
|
|
|
2004
2144
|
};
|
|
2005
2145
|
}
|
|
2006
2146
|
|
|
2147
|
+
// src/interfaces/ILogger.ts
|
|
2148
|
+
var NoopLogger = class {
|
|
2149
|
+
debug() {
|
|
2150
|
+
}
|
|
2151
|
+
info() {
|
|
2152
|
+
}
|
|
2153
|
+
warn() {
|
|
2154
|
+
}
|
|
2155
|
+
error() {
|
|
2156
|
+
}
|
|
2157
|
+
child() {
|
|
2158
|
+
return this;
|
|
2159
|
+
}
|
|
2160
|
+
};
|
|
2161
|
+
|
|
2162
|
+
// src/auth/cache-helpers.ts
|
|
2163
|
+
var CacheTTL = {
|
|
2164
|
+
/** 5 minutes — query results, dropdown options. */
|
|
2165
|
+
SHORT: 300,
|
|
2166
|
+
/** 1 hour — semi-static reference data, per-user state. */
|
|
2167
|
+
MEDIUM: 3600,
|
|
2168
|
+
/** 6 hours — heavy aggregates, derived state. */
|
|
2169
|
+
LONG: 21600,
|
|
2170
|
+
/** 24 hours — positive AI cache, immutable enrichments. */
|
|
2171
|
+
DAY: 86400,
|
|
2172
|
+
/** 7 days — long-term derived data, hashed content. */
|
|
2173
|
+
WEEK: 604800
|
|
2174
|
+
};
|
|
2175
|
+
function createAppCache(cache, options = {}) {
|
|
2176
|
+
const logger = options.logger ?? new NoopLogger();
|
|
2177
|
+
return {
|
|
2178
|
+
async get(key) {
|
|
2179
|
+
try {
|
|
2180
|
+
return await cache.get(key);
|
|
2181
|
+
} catch (err) {
|
|
2182
|
+
logger.debug("cache.get failed", { key, err: String(err) });
|
|
2183
|
+
return null;
|
|
2184
|
+
}
|
|
2185
|
+
},
|
|
2186
|
+
async set(key, value, ttlSeconds) {
|
|
2187
|
+
try {
|
|
2188
|
+
await cache.set(key, value, ttlSeconds);
|
|
2189
|
+
} catch (err) {
|
|
2190
|
+
logger.debug("cache.set failed", { key, err: String(err) });
|
|
2191
|
+
}
|
|
2192
|
+
},
|
|
2193
|
+
async delete(key) {
|
|
2194
|
+
try {
|
|
2195
|
+
await cache.delete(key);
|
|
2196
|
+
} catch (err) {
|
|
2197
|
+
logger.debug("cache.delete failed", { key, err: String(err) });
|
|
2198
|
+
}
|
|
2199
|
+
},
|
|
2200
|
+
async exists(key) {
|
|
2201
|
+
try {
|
|
2202
|
+
return await cache.exists(key);
|
|
2203
|
+
} catch {
|
|
2204
|
+
return false;
|
|
2205
|
+
}
|
|
2206
|
+
},
|
|
2207
|
+
async getCachedOrFetch(key, fetchFn, ttlSeconds) {
|
|
2208
|
+
try {
|
|
2209
|
+
const cached = await cache.get(key);
|
|
2210
|
+
if (cached !== null) return cached;
|
|
2211
|
+
} catch (err) {
|
|
2212
|
+
logger.debug("getCachedOrFetch read failed; falling through", {
|
|
2213
|
+
key,
|
|
2214
|
+
err: String(err)
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
const fresh = await fetchFn();
|
|
2218
|
+
try {
|
|
2219
|
+
await cache.set(key, fresh, ttlSeconds);
|
|
2220
|
+
} catch (err) {
|
|
2221
|
+
logger.debug("getCachedOrFetch write-back failed", {
|
|
2222
|
+
key,
|
|
2223
|
+
err: String(err)
|
|
2224
|
+
});
|
|
2225
|
+
}
|
|
2226
|
+
return fresh;
|
|
2227
|
+
},
|
|
2228
|
+
async invalidate(keys) {
|
|
2229
|
+
const list = Array.isArray(keys) ? keys : [keys];
|
|
2230
|
+
let total = 0;
|
|
2231
|
+
for (const key of list) {
|
|
2232
|
+
try {
|
|
2233
|
+
if (key.includes("*")) {
|
|
2234
|
+
total += await cache.deletePattern(key);
|
|
2235
|
+
} else {
|
|
2236
|
+
await cache.delete(key);
|
|
2237
|
+
total += 1;
|
|
2238
|
+
}
|
|
2239
|
+
} catch (err) {
|
|
2240
|
+
logger.debug("invalidate failed for key", { key, err: String(err) });
|
|
2241
|
+
}
|
|
2242
|
+
}
|
|
2243
|
+
return total;
|
|
2244
|
+
},
|
|
2245
|
+
async warm(key, fetchFn, ttlSeconds) {
|
|
2246
|
+
try {
|
|
2247
|
+
const value = await fetchFn();
|
|
2248
|
+
await cache.set(key, value, ttlSeconds);
|
|
2249
|
+
} catch (err) {
|
|
2250
|
+
logger.debug("warm failed", { key, err: String(err) });
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
};
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2007
2256
|
// src/env.ts
|
|
2008
2257
|
function getRequiredEnv(key) {
|
|
2009
2258
|
const value = process.env[key];
|
|
@@ -2097,6 +2346,7 @@ function getEnvSummary(keys) {
|
|
|
2097
2346
|
export {
|
|
2098
2347
|
ApiError,
|
|
2099
2348
|
ApiErrorCode,
|
|
2349
|
+
CacheTTL,
|
|
2100
2350
|
CommonApiErrors,
|
|
2101
2351
|
CommonRateLimits,
|
|
2102
2352
|
DateRangeSchema,
|
|
@@ -2131,6 +2381,7 @@ export {
|
|
|
2131
2381
|
constantTimeEqual,
|
|
2132
2382
|
containsHtml,
|
|
2133
2383
|
containsUrls,
|
|
2384
|
+
createAppCache,
|
|
2134
2385
|
createAuditActor,
|
|
2135
2386
|
createAuditLogger,
|
|
2136
2387
|
createBetaClient,
|