@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.mjs CHANGED
@@ -1482,6 +1482,11 @@ function createSecureHandlerFactory(factoryConfig) {
1482
1482
  let session = null;
1483
1483
  let isAdmin = false;
1484
1484
  let isLegacyToken = false;
1485
+ let timedAudit;
1486
+ const auditCfg = routeConfig.audit;
1487
+ const resolvedAuditAction = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.action : auditCfg;
1488
+ const resolvedAuditResource = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.resource : routeConfig.auditResource;
1489
+ const resolvedGetResourceId = typeof auditCfg === "object" && auditCfg !== null ? auditCfg.getResourceId : routeConfig.getResourceId;
1485
1490
  try {
1486
1491
  if (routeConfig.requireAuth || routeConfig.requireAdmin || routeConfig.requireRoles?.length) {
1487
1492
  session = await factoryConfig.getSession();
@@ -1549,27 +1554,57 @@ function createSecureHandlerFactory(factoryConfig) {
1549
1554
  }
1550
1555
  validated = result.data;
1551
1556
  }
1557
+ const actorId = isLegacyToken ? "admin_token" : session?.user?.id || "anonymous";
1558
+ const actorType = isLegacyToken ? "admin" : "user";
1559
+ const actorEmail = session?.user?.email ?? void 0;
1560
+ const resourceId = resolvedGetResourceId ? resolvedGetResourceId(
1561
+ request,
1562
+ params,
1563
+ validated
1564
+ ) : void 0;
1565
+ if (resolvedAuditAction && factoryConfig.createTimedAudit) {
1566
+ timedAudit = factoryConfig.createTimedAudit(
1567
+ {
1568
+ action: resolvedAuditAction,
1569
+ resource: resolvedAuditResource ? {
1570
+ type: resolvedAuditResource,
1571
+ id: resourceId
1572
+ } : void 0,
1573
+ actor: {
1574
+ id: actorId,
1575
+ type: actorType,
1576
+ email: actorEmail
1577
+ }
1578
+ },
1579
+ request
1580
+ );
1581
+ }
1582
+ const authMethod = isLegacyToken ? "legacy_token" : session?.user ? "session" : "none";
1552
1583
  const ctx = {
1553
1584
  session,
1554
1585
  isLegacyToken,
1586
+ authMethod,
1555
1587
  isAdmin,
1556
1588
  validated,
1557
1589
  logger: log,
1558
1590
  requestId,
1559
- params
1591
+ params,
1592
+ timedAudit
1560
1593
  };
1561
1594
  const response = await handler(request, ctx);
1562
1595
  response.headers.set("X-Request-ID", requestId);
1563
- if (routeConfig.audit && factoryConfig.auditLog) {
1564
- const actorId = isLegacyToken ? "admin_token" : session?.user?.id || "anonymous";
1596
+ if (resolvedAuditAction && factoryConfig.auditLog && !timedAudit) {
1565
1597
  await factoryConfig.auditLog({
1566
1598
  actor: {
1567
1599
  id: actorId,
1568
- type: isLegacyToken ? "admin" : "user",
1569
- email: session?.user?.email ?? void 0
1600
+ type: actorType,
1601
+ email: actorEmail
1570
1602
  },
1571
- action: routeConfig.audit,
1572
- resource: routeConfig.auditResource ? { type: routeConfig.auditResource, id: "unknown" } : void 0,
1603
+ action: resolvedAuditAction,
1604
+ resource: resolvedAuditResource ? {
1605
+ type: resolvedAuditResource,
1606
+ id: resourceId ?? "unknown"
1607
+ } : void 0,
1573
1608
  outcome: "success"
1574
1609
  }).catch(() => {
1575
1610
  });
@@ -1579,15 +1614,19 @@ function createSecureHandlerFactory(factoryConfig) {
1579
1614
  log.error("Request handler error", {
1580
1615
  error: error instanceof Error ? error.message : String(error)
1581
1616
  });
1582
- if (routeConfig.audit && factoryConfig.auditLog) {
1617
+ const errReason = error instanceof Error ? error.message : "Unknown error";
1618
+ if (timedAudit) {
1619
+ await timedAudit.failure(errReason).catch(() => {
1620
+ });
1621
+ } else if (resolvedAuditAction && factoryConfig.auditLog) {
1583
1622
  await factoryConfig.auditLog({
1584
1623
  actor: {
1585
1624
  id: session?.user?.id || "unknown",
1586
1625
  type: "user"
1587
1626
  },
1588
- action: routeConfig.audit,
1627
+ action: resolvedAuditAction,
1589
1628
  outcome: "failure",
1590
- reason: error instanceof Error ? error.message : "Unknown error"
1629
+ reason: errReason
1591
1630
  }).catch(() => {
1592
1631
  });
1593
1632
  }