@digilogiclabs/platform-core 1.23.0 → 1.25.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/auth.mjs CHANGED
@@ -1541,9 +1541,8 @@ 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";
1544
+ const writeBlockedAudit = async (action, reason, metadata) => {
1545
+ if (!factoryConfig.auditLog) return;
1547
1546
  try {
1548
1547
  await factoryConfig.auditLog({
1549
1548
  actor: session?.user ? {
@@ -1554,11 +1553,19 @@ function createSecureHandlerFactory(factoryConfig) {
1554
1553
  action,
1555
1554
  resource: resolvedAuditResource ? { type: resolvedAuditResource, id: "unknown" } : void 0,
1556
1555
  outcome: "blocked",
1557
- reason
1556
+ reason,
1557
+ metadata
1558
1558
  });
1559
1559
  } catch {
1560
1560
  }
1561
1561
  };
1562
+ const emitBlockedAudit = async (reason) => {
1563
+ if (!factoryConfig.auditOnBlocked) return;
1564
+ await writeBlockedAudit(
1565
+ factoryConfig.blockedAuditAction ?? "unauthorized_access",
1566
+ reason
1567
+ );
1568
+ };
1562
1569
  try {
1563
1570
  if (routeConfig.requireAuth || routeConfig.requireAdmin || routeConfig.requireRoles?.length) {
1564
1571
  session = await factoryConfig.getSession();
@@ -1599,15 +1606,21 @@ function createSecureHandlerFactory(factoryConfig) {
1599
1606
  );
1600
1607
  }
1601
1608
  }
1609
+ let rateLimitInfo = null;
1602
1610
  if (!routeConfig.skipRateLimit && routeConfig.rateLimit && factoryConfig.rateLimiter) {
1603
1611
  const userId = session?.user?.id ?? void 0;
1604
- const blocked = await factoryConfig.rateLimiter.enforce(
1612
+ const result = await factoryConfig.rateLimiter.enforce(
1605
1613
  request,
1606
1614
  operation,
1607
1615
  routeConfig.rateLimit,
1608
1616
  userId
1609
1617
  );
1610
- if (blocked) return blocked;
1618
+ if (result instanceof Response) {
1619
+ return result;
1620
+ }
1621
+ if (result !== null && typeof result === "object") {
1622
+ rateLimitInfo = result;
1623
+ }
1611
1624
  }
1612
1625
  let validated = {};
1613
1626
  if (routeConfig.validate) {
@@ -1627,6 +1640,13 @@ function createSecureHandlerFactory(factoryConfig) {
1627
1640
  const result = routeConfig.validate.safeParse(input);
1628
1641
  if (!result.success) {
1629
1642
  const fieldErrors = result.error?.flatten ? result.error.flatten().fieldErrors : void 0;
1643
+ if (factoryConfig.auditOnInvalidInput && resolvedAuditAction) {
1644
+ await writeBlockedAudit(
1645
+ factoryConfig.invalidInputAuditAction ?? "invalid_input",
1646
+ "Input validation failed",
1647
+ fieldErrors ? { errors: fieldErrors } : void 0
1648
+ );
1649
+ }
1630
1650
  return Response.json(
1631
1651
  { error: "Validation error", errors: fieldErrors },
1632
1652
  { status: 400, headers: { "X-Request-ID": requestId } }
@@ -1673,6 +1693,20 @@ function createSecureHandlerFactory(factoryConfig) {
1673
1693
  };
1674
1694
  const response = await handler(request, ctx);
1675
1695
  response.headers.set("X-Request-ID", requestId);
1696
+ if (rateLimitInfo) {
1697
+ response.headers.set(
1698
+ "X-RateLimit-Limit",
1699
+ String(rateLimitInfo.limit)
1700
+ );
1701
+ response.headers.set(
1702
+ "X-RateLimit-Remaining",
1703
+ String(rateLimitInfo.remaining)
1704
+ );
1705
+ response.headers.set(
1706
+ "X-RateLimit-Reset",
1707
+ String(rateLimitInfo.resetMs)
1708
+ );
1709
+ }
1676
1710
  if (resolvedAuditAction && factoryConfig.auditLog && !timedAudit) {
1677
1711
  await factoryConfig.auditLog({
1678
1712
  actor: {