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