@absolutejs/auth 0.48.3 → 0.48.5

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/index.js CHANGED
@@ -3148,6 +3148,7 @@ var getStatusFromSource = async ({
3148
3148
  }
3149
3149
  return {
3150
3150
  error: null,
3151
+ impersonator: userSession?.impersonator,
3151
3152
  user: userSession?.user ?? null
3152
3153
  };
3153
3154
  };
@@ -8827,7 +8828,7 @@ var userStatus = ({
8827
8828
  statusRoute = "/oauth2/status",
8828
8829
  onStatus
8829
8830
  }) => new Elysia28().use(sessionStore()).get(statusRoute, async ({ status, cookie: { user_session_id }, store: { session } }) => {
8830
- const { user, error } = await getStatusFromSource({
8831
+ const { user, impersonator, error } = await getStatusFromSource({
8831
8832
  authSessionStore,
8832
8833
  session,
8833
8834
  user_session_id
@@ -8836,11 +8837,11 @@ var userStatus = ({
8836
8837
  return status(error.code, error.message);
8837
8838
  }
8838
8839
  try {
8839
- await onStatus?.({ user });
8840
+ await onStatus?.({ impersonator, user });
8840
8841
  } catch (err) {
8841
8842
  return err instanceof Error ? status("Internal Server Error", `Error: ${err.message} - ${err.stack ?? ""}`) : status("Internal Server Error", `Unknown Error: ${String(err)}`);
8842
8843
  }
8843
- return { user };
8844
+ return { impersonator, user };
8844
8845
  }, { cookie: t25.Cookie({ user_session_id: userSessionIdTypebox }) });
8845
8846
 
8846
8847
  // src/scim/routes.ts
@@ -22365,6 +22366,18 @@ var createInMemoryLinkedProviderStores = (input = {}) => {
22365
22366
  // src/session/impersonation.ts
22366
22367
  init_constants();
22367
22368
  var DEFAULT_IMPERSONATION_TTL_MS = MILLISECONDS_IN_AN_HOUR;
22369
+ var restoreCookieTo = (cookie, target, cookieSecure) => {
22370
+ if (target === undefined) {
22371
+ cookie.remove();
22372
+ return;
22373
+ }
22374
+ cookie.set({
22375
+ httpOnly: true,
22376
+ sameSite: "lax",
22377
+ secure: resolveCookieSecure(cookieSecure),
22378
+ value: target
22379
+ });
22380
+ };
22368
22381
  var endImpersonation = async ({
22369
22382
  authSessionStore,
22370
22383
  cookie,
@@ -22382,6 +22395,8 @@ var endImpersonation = async ({
22382
22395
  userSessionId: currentId
22383
22396
  });
22384
22397
  const impersonator = current?.impersonator;
22398
+ if (impersonator === undefined)
22399
+ return { restored: false };
22385
22400
  if (authSessionStore)
22386
22401
  await authSessionStore.removeSession(currentId);
22387
22402
  else
@@ -22411,6 +22426,7 @@ var endImpersonation = async ({
22411
22426
  };
22412
22427
  var isImpersonating = (session) => session?.impersonator !== undefined;
22413
22428
  var startImpersonation = async ({
22429
+ allowNested = false,
22414
22430
  authSessionStore,
22415
22431
  cookie,
22416
22432
  cookieSecure,
@@ -22421,11 +22437,24 @@ var startImpersonation = async ({
22421
22437
  sessionDurationMs = DEFAULT_IMPERSONATION_TTL_MS,
22422
22438
  user
22423
22439
  }) => {
22440
+ const callerSessionId = cookie.value;
22441
+ if (!allowNested && callerSessionId !== undefined) {
22442
+ const caller = await loadSessionFromSource({
22443
+ authSessionStore,
22444
+ removeExpired: false,
22445
+ session: inMemorySession,
22446
+ userSessionId: callerSessionId
22447
+ });
22448
+ if (isImpersonating(caller)) {
22449
+ throw new Error("startImpersonation: caller session is already impersonating; nested impersonation is disabled (pass allowNested to override)");
22450
+ }
22451
+ }
22424
22452
  const stamp = {
22425
22453
  actorEmail: impersonator.actorEmail,
22426
22454
  actorId: impersonator.actorId,
22455
+ readOnly: impersonator.readOnly,
22427
22456
  reason: impersonator.reason,
22428
- returnToSessionId: cookie.value,
22457
+ returnToSessionId: callerSessionId,
22429
22458
  startedAt: Date.now()
22430
22459
  };
22431
22460
  const sessionId = await promoteToSession({
@@ -22437,12 +22466,21 @@ var startImpersonation = async ({
22437
22466
  sessionDurationMs,
22438
22467
  user
22439
22468
  });
22440
- await emit?.({
22441
- at: Date.now(),
22442
- metadata: { actorId: stamp.actorId, reason: stamp.reason },
22443
- type: "impersonation_started",
22444
- userId: getUserId?.(user)
22445
- });
22469
+ try {
22470
+ await emit?.({
22471
+ at: Date.now(),
22472
+ metadata: { actorId: stamp.actorId, reason: stamp.reason },
22473
+ type: "impersonation_started",
22474
+ userId: getUserId?.(user)
22475
+ });
22476
+ } catch (auditError) {
22477
+ if (authSessionStore)
22478
+ await authSessionStore.removeSession(sessionId);
22479
+ else
22480
+ delete inMemorySession[sessionId];
22481
+ restoreCookieTo(cookie, stamp.returnToSessionId, cookieSecure);
22482
+ throw auditError;
22483
+ }
22446
22484
  return sessionId;
22447
22485
  };
22448
22486
  // src/session/anonymous.ts
@@ -27586,5 +27624,5 @@ export {
27586
27624
  AuthIdentityConflictError
27587
27625
  };
27588
27626
 
27589
- //# debugId=DCFE29575482C4FF64756E2164756E21
27627
+ //# debugId=4AE8923415556A3664756E2164756E21
27590
27628
  //# sourceMappingURL=index.js.map