Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

13.0 KB
Raw Blame
import { getTokenSessionKey, getTokenUserId, tokensBelongToSameUser } from 'features/auth/store/authSlice';

const AUTH_GENERATION_KEY = 'auth_generation';
const MEDIA_AUTH_LOCK = 'invokeai-media-auth';
const FALLBACK_LOCK_PREFIX = `${MEDIA_AUTH_LOCK}:`;
const FALLBACK_LOCK_LEASE_MS = 30_000;
const FALLBACK_LOCK_POLL_MS = 10;
// Shared with webv2's IdentityTokenAdapter: each rotation has a distinct key and { at, userId } value.
const PASSWORD_CHANGE_PREFIX = 'auth_token_rotation:';
const PASSWORD_CHANGE_WAIT_MS = 30_000;
const PASSWORD_CHANGE_POLL_MS = 100;

// Bound on the media-cookie sync fetch made while holding the media-auth lock.
export const MEDIA_COOKIE_SYNC_TIMEOUT_MS = 10_000;

// The middleware mints a refreshed token on every successful mutating request, but
// token lifetimes are measured in days — re-committing (and re-syncing the media
// cookie over the network) once a minute is plenty, and it keeps bulk operations from
// paying one serialized cookie round trip per request.
const TOKEN_REFRESH_THROTTLE_MS = 60_000;
let lastTokenRefreshAcceptedAt = 0;

export const isTokenRefreshThrottled = () => Date.now() - lastTokenRefreshAcceptedAt < TOKEN_REFRESH_THROTTLE_MS;

export const markTokenRefreshAccepted = () => {
  lastTokenRefreshAcceptedAt = Date.now();
};

/** True when `refreshedToken` is the same user's replacement that advances the revocation epoch. */
const isEpochReplacement = (requestToken: string, refreshedToken: string): boolean =>
  tokensBelongToSameUser(requestToken, refreshedToken) &&
  getTokenSessionKey(requestToken) !== getTokenSessionKey(refreshedToken);

export const shouldThrottleRefreshedToken = (requestToken: string, refreshedToken: string): boolean => {
  if (!isTokenRefreshThrottled()) {
    return false;
  }
  // An epoch-changing replacement is the only credential that remains valid after revocation.
  // Keep every other replacement on the normal sliding-refresh throttle.
  return !isEpochReplacement(requestToken, refreshedToken);
};

type FallbackLockTicket = {
  choosing: boolean;
  expiresAt: number;
  owner: string;
  ticket: number;
};

const getAuthGeneration = () => {
  const value = Number(localStorage.getItem(AUTH_GENERATION_KEY) ?? 0);
  return Number.isSafeInteger(value) && value >= 0 ? value : 0;
};

export const captureAuthGeneration = () => getAuthGeneration();

export const beginAuthTransition = () => {
  const next = getAuthGeneration() + 1;
  localStorage.setItem(AUTH_GENERATION_KEY, String(next));
  return next;
};

/**
 * True when `refreshedToken`, returned for a request sent with `requestToken`, may replace the
 * stored token.
 *
 * Byte equality with the stored token rejects refreshes that belong to a different login: a
 * logout, another user logging in, or another tab adopting a new token. A routine sliding-window
 * refresh of the same session committed in the meantime is not one of those, but it changes the
 * bytes too. For an epoch-advancing replacement (a password change) that would drop the only
 * credential that survives the revocation, so it is also accepted when the stored token is the
 * same session (user id and epoch) as `requestToken`. It is then strictly newer than both. The
 * generation check still rejects any login transition that started after the request was sent.
 */
export const shouldAcceptRefreshedToken = (
  requestToken: string,
  requestGeneration: number,
  refreshedToken?: string
): boolean => {
  if (getAuthGeneration() !== requestGeneration) {
    return false;
  }
  const storedToken = localStorage.getItem('auth_token');
  if (storedToken === requestToken) {
    return true;
  }
  return (
    refreshedToken !== undefined &&
    isEpochReplacement(requestToken, refreshedToken) &&
    getTokenSessionKey(storedToken) === getTokenSessionKey(requestToken)
  );
};

/**
 * True when a 401 for a request that carried `requestToken` should end the live session.
 *
 * A 401 is evidence about the credential that was *sent*, and about nothing else. By the time
 * one lands, the tab may already belong to someone else: `setCredentials` (a login here) and
 * `externalTokenAdopted` (a login in another tab — localStorage is shared) both swap the token
 * synchronously while earlier requests are still in flight. Ending the session on such a 401
 * destroys the session that just replaced it, and the user who never issued the request is the
 * one logged out. So the token that was sent must still be the live one.
 *
 * Byte equality, deliberately — and note this is the opposite of what `isSameAuthContext` wants.
 * A sliding-window refresh must NOT pass here: it mints a new token for the same login, and a
 * 401 for the token it replaced says nothing about whether the replacement is still good. That
 * costs nothing, because the session ending is not a one-shot event to be caught — if it really
 * has ended, the next request carries the live token and its 401 ends it here.
 *
 * A falsy `requestToken` never qualifies: unauthenticated requests (client_state probes during
 * page load, the setup-status query) 401 routinely and must not log anyone out. Truthiness
 * rather than a null check, to match the condition `dynamicBaseQuery` actually sends the header
 * under — an empty-string token sets no `Authorization` header, so its 401 proves nothing about
 * any session, yet it is equal to the stored empty string.
 */
export const shouldEndSessionForUnauthorized = (requestToken: string | null): boolean =>
  !!requestToken && localStorage.getItem('auth_token') === requestToken;

/** The session an operation started under. See `isSameAuthContext`. */
export type AuthContext = {
  token: string | null;
};

export const captureAuthContext = (): AuthContext => ({
  token: localStorage.getItem('auth_token'),
});

/**
 * True while the session an operation began under is still the live one.
 *
 * Requests read the bearer token out of localStorage at send time (`dynamicBaseQuery`), so an
 * operation that issues several of them does not carry one identity: log out and back in as
 * someone else midway and the remaining requests go out as the new user. Nothing stops that on
 * its own — RTK Query's `resetApiState` clears the store, not a `queryFn` that is already
 * running. Callers that fan a single user action out into a sequence of requests must therefore
 * capture the context up front and check it before each one.
 *
 * The comparison is on the identity the token carries, not on the token itself and not on the
 * auth generation counter:
 * - Not the bytes, because the sliding-window refresh mints a fresh token for the same login
 *   mid-operation, and aborting on that would abandon a batch for a routine event.
 * - Not the generation counter, because `beginAuthTransition` bumps it when a login or logout
 *   *request is sent*, before anything has changed and regardless of whether it succeeds. A
 *   login in a second tab — even one that fails, or one that signs the same user back in —
 *   would abort an unrelated batch in this one. Identity answers the question the counter was
 *   standing in for, and answers it correctly: whoever the next chunk would be sent as is read
 *   fresh from localStorage every time.
 *
 * `sessionExpiredLogout` removes the token with no request at all, so the token comparison is
 * what catches it. Two nulls compare equal, which keeps deployments that never issue a token —
 * the single-user default — out of this entirely.
 */
export const isSameAuthContext = (context: AuthContext): boolean => {
  const token = localStorage.getItem('auth_token');
  return token === context.token || tokensBelongToSameUser(context.token, token);
};

const getFallbackLockTickets = (): FallbackLockTicket[] => {
  const tickets: FallbackLockTicket[] = [];
  const now = Date.now();
  for (let index = 0; index < localStorage.length; index++) {
    const key = localStorage.key(index);
    if (!key?.startsWith(FALLBACK_LOCK_PREFIX)) {
      continue;
    }
    try {
      const ticket = JSON.parse(localStorage.getItem(key) ?? '') as FallbackLockTicket;
      if (
        ticket.owner &&
        Number.isSafeInteger(ticket.ticket) &&
        ticket.ticket >= 0 &&
        Number.isFinite(ticket.expiresAt) &&
        ticket.expiresAt > now
      ) {
        tickets.push(ticket);
      }
    } catch {
      // Ignore malformed or stale lock records.
    }
  }
  return tickets;
};

const delay = (milliseconds: number) =>
  new Promise<void>((resolve) => {
    setTimeout(resolve, milliseconds);
  });

/** Publish a bounded, cross-tab-visible intent before sending a password-changing request. */
export const beginPasswordChange = (requestToken: string): (() => void) => {
  const userId = getTokenUserId(requestToken);
  // The announcement coordinates a request, not authorization. Never persist bearer bytes.
  if (userId === null) {
    return () => {};
  }
  const key = `${PASSWORD_CHANGE_PREFIX}${globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random()}`}`;
  try {
    localStorage.setItem(key, JSON.stringify({ at: Date.now(), userId }));
  } catch {
    // The password request must not fail just because other tabs cannot see its announcement.
  }
  return () => {
    try {
      localStorage.removeItem(key);
    } catch {
      // An abandoned marker expires even if storage becomes unavailable during cleanup.
    }
  };
};

const hasPendingPasswordChange = (requestToken: string, firstSeenAt: number): boolean => {
  const userId = getTokenUserId(requestToken);
  if (userId === null) {
    return false;
  }
  for (let index = 0; index < localStorage.length; index++) {
    const key = localStorage.key(index);
    if (!key?.startsWith(PASSWORD_CHANGE_PREFIX)) {
      continue;
    }
    try {
      const marker: unknown = JSON.parse(localStorage.getItem(key) ?? '');
      if (
        typeof marker === 'object' &&
        marker !== null &&
        'userId' in marker &&
        marker.userId === userId &&
        'at' in marker &&
        typeof marker.at === 'number' &&
        Number.isFinite(marker.at) &&
        Math.min(marker.at, firstSeenAt) + PASSWORD_CHANGE_WAIT_MS > Date.now()
      ) {
        return true;
      }
    } catch {
      // Ignore a damaged marker; it is not evidence of an in-flight replacement.
    }
  }
  return false;
};

/** Defer an old-token 401 until the password-change result can commit its replacement. */
export const waitForPasswordChange = async (requestToken: string, requestGeneration: number): Promise<void> => {
  const firstSeenAt = Date.now();
  // Marker timestamps are wall-clock values shared across tabs; the overall wait cap must
  // still hold when the system clock changes while this request is pending.
  const deadline = performance.now() + PASSWORD_CHANGE_WAIT_MS;
  while (
    shouldEndSessionForUnauthorized(requestToken) &&
    getAuthGeneration() === requestGeneration &&
    hasPendingPasswordChange(requestToken, firstSeenAt) &&
    performance.now() < deadline
  ) {
    await delay(Math.min(PASSWORD_CHANGE_POLL_MS, deadline - performance.now()));
  }
};

export const createMediaAuthLock = (owner: string) => {
  const key = `${FALLBACK_LOCK_PREFIX}${owner}`;
  let localQueue = Promise.resolve();

  const run = async <T>(callback: () => T | PromiseLike<T>): Promise<T> => {
    const writeTicket = (ticket: number, choosing: boolean) => {
      localStorage.setItem(
        key,
        JSON.stringify({ choosing, expiresAt: Date.now() + FALLBACK_LOCK_LEASE_MS, owner, ticket })
      );
    };

    writeTicket(0, true);
    const ticket = Math.max(0, ...getFallbackLockTickets().map((entry) => entry.ticket)) + 1;
    writeTicket(ticket, false);

    let lastRenewedAt = Date.now();
    while (
      getFallbackLockTickets().some(
        (entry) =>
          entry.owner !== owner &&
          (entry.choosing || entry.ticket < ticket || (entry.ticket === ticket && entry.owner < owner))
      )
    ) {
      await delay(FALLBACK_LOCK_POLL_MS);
      // Renew the waiter's own lease while queueing: tickets are stamped with a 30s
      // expiry at acquisition, and a waiter that outlives it vanishes from other tabs'
      // views — a later-ticket tab would then enter the critical section alongside it.
      if (Date.now() - lastRenewedAt >= FALLBACK_LOCK_LEASE_MS / 3) {
        writeTicket(ticket, false);
        lastRenewedAt = Date.now();
      }
    }

    const heartbeat = setInterval(() => writeTicket(ticket, false), FALLBACK_LOCK_LEASE_MS / 3);
    try {
      return await callback();
    } finally {
      clearInterval(heartbeat);
      localStorage.removeItem(key);
    }
  };

  return <T>(callback: () => T | PromiseLike<T>): Promise<T> => {
    const result = localQueue.then(
      () => run(callback),
      () => run(callback)
    );
    localQueue = result.then(
      () => undefined,
      () => undefined
    );
    return result;
  };
};

const fallbackMediaAuthLock = createMediaAuthLock(
  globalThis.crypto?.randomUUID?.() ?? `${Date.now()}-${Math.random()}`
);

export const runWithMediaAuthLock = <T>(callback: () => T | PromiseLike<T>): Promise<T> => {
  if (typeof navigator !== 'undefined' && navigator.locks) {
    return navigator.locks.request(MEDIA_AUTH_LOCK, callback) as Promise<T>;
  }
  return fallbackMediaAuthLock(callback);
};