@digilogiclabs/platform-core 1.15.0 → 1.17.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.js CHANGED
@@ -1594,6 +1594,11 @@ function createSecureHandlerFactory(factoryConfig) {
1594
1594
  let session = null;
1595
1595
  let isAdmin = false;
1596
1596
  let isLegacyToken = false;
1597
+ let timedAudit;
1598
+ const auditCfg = routeConfig.audit;
1599
+ const resolvedAuditAction = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.action : auditCfg;
1600
+ const resolvedAuditResource = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.resource : routeConfig.auditResource;
1601
+ const resolvedGetResourceId = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.getResourceId : routeConfig.getResourceId;
1597
1602
  try {
1598
1603
  if (routeConfig.requireAuth || routeConfig.requireAdmin || routeConfig.requireRoles?.length) {
1599
1604
  session = await factoryConfig.getSession();
@@ -1661,27 +1666,57 @@ function createSecureHandlerFactory(factoryConfig) {
1661
1666
  }
1662
1667
  validated = result.data;
1663
1668
  }
1669
+ const actorId = isLegacyToken ? "admin_token" : session?.user?.id || "anonymous";
1670
+ const actorType = isLegacyToken ? "admin" : "user";
1671
+ const actorEmail = session?.user?.email ?? void 0;
1672
+ const resourceId = resolvedGetResourceId ? resolvedGetResourceId(
1673
+ request,
1674
+ params,
1675
+ validated
1676
+ ) : void 0;
1677
+ if (resolvedAuditAction && factoryConfig.createTimedAudit) {
1678
+ timedAudit = factoryConfig.createTimedAudit(
1679
+ {
1680
+ action: resolvedAuditAction,
1681
+ resource: resolvedAuditResource ? {
1682
+ type: resolvedAuditResource,
1683
+ id: resourceId
1684
+ } : void 0,
1685
+ actor: {
1686
+ id: actorId,
1687
+ type: actorType,
1688
+ email: actorEmail
1689
+ }
1690
+ },
1691
+ request
1692
+ );
1693
+ }
1694
+ const authMethod = isLegacyToken ? "legacy_token" : session?.user ? "session" : "none";
1664
1695
  const ctx = {
1665
1696
  session,
1666
1697
  isLegacyToken,
1698
+ authMethod,
1667
1699
  isAdmin,
1668
1700
  validated,
1669
1701
  logger: log,
1670
1702
  requestId,
1671
- params
1703
+ params,
1704
+ timedAudit
1672
1705
  };
1673
1706
  const response = await handler(request, ctx);
1674
1707
  response.headers.set("X-Request-ID", requestId);
1675
- if (routeConfig.audit && factoryConfig.auditLog) {
1676
- const actorId = isLegacyToken ? "admin_token" : session?.user?.id || "anonymous";
1708
+ if (resolvedAuditAction && factoryConfig.auditLog && !timedAudit) {
1677
1709
  await factoryConfig.auditLog({
1678
1710
  actor: {
1679
1711
  id: actorId,
1680
- type: isLegacyToken ? "admin" : "user",
1681
- email: session?.user?.email ?? void 0
1712
+ type: actorType,
1713
+ email: actorEmail
1682
1714
  },
1683
- action: routeConfig.audit,
1684
- resource: routeConfig.auditResource ? { type: routeConfig.auditResource, id: "unknown" } : void 0,
1715
+ action: resolvedAuditAction,
1716
+ resource: resolvedAuditResource ? {
1717
+ type: resolvedAuditResource,
1718
+ id: resourceId ?? "unknown"
1719
+ } : void 0,
1685
1720
  outcome: "success"
1686
1721
  }).catch(() => {
1687
1722
  });
@@ -1691,15 +1726,19 @@ function createSecureHandlerFactory(factoryConfig) {
1691
1726
  log.error("Request handler error", {
1692
1727
  error: error instanceof Error ? error.message : String(error)
1693
1728
  });
1694
- if (routeConfig.audit && factoryConfig.auditLog) {
1729
+ const errReason = error instanceof Error ? error.message : "Unknown error";
1730
+ if (timedAudit) {
1731
+ await timedAudit.failure(errReason).catch(() => {
1732
+ });
1733
+ } else if (resolvedAuditAction && factoryConfig.auditLog) {
1695
1734
  await factoryConfig.auditLog({
1696
1735
  actor: {
1697
1736
  id: session?.user?.id || "unknown",
1698
1737
  type: "user"
1699
1738
  },
1700
- action: routeConfig.audit,
1739
+ action: resolvedAuditAction,
1701
1740
  outcome: "failure",
1702
- reason: error instanceof Error ? error.message : "Unknown error"
1741
+ reason: errReason
1703
1742
  }).catch(() => {
1704
1743
  });
1705
1744
  }