@mentra/cloud-client 3.3.0-dev.815 → 3.3.0-dev.818

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/cloud-client",
3
- "version": "3.3.0-dev.815",
3
+ "version": "3.3.0-dev.818",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "exports": {
@@ -12,7 +12,7 @@
12
12
  "test": "bun test"
13
13
  },
14
14
  "dependencies": {
15
- "@mentra/cloud-protocol": "3.3.0-dev.815",
15
+ "@mentra/cloud-protocol": "3.3.0-dev.818",
16
16
  "tweetnacl": "^1.0.3"
17
17
  },
18
18
  "devDependencies": {
@@ -93,6 +93,8 @@ const EXPIRY_MARGIN_SECONDS = 60
93
93
  const REVOKE_ATTEMPTS = 3
94
94
  /** Base backoff between revoke attempts in milliseconds; doubles per attempt. */
95
95
  const REVOKE_RETRY_BASE_MS = 250
96
+ /** Whole remote logout operation, including refresh and retries. */
97
+ const REVOKE_TIMEOUT_MS = 10_000
96
98
 
97
99
  /** The cloud's token endpoints, relative to the core base URL. */
98
100
  const EXCHANGE_PATH = "/api/client/auth/exchange"
@@ -312,31 +314,43 @@ export class Auth implements AuthModule {
312
314
  }
313
315
 
314
316
  async clearSession(): Promise<void> {
317
+ const current = this.store.current()
318
+ // Capture the saved refresh token before deleting it, but never reacquire
319
+ // credentials through the upstream provider during logout.
320
+ const refreshSnapshot = this.store.refreshToken()
315
321
  this.sessionGeneration += 1
316
322
  this.sessionCleared = true
317
- let revokeError: unknown
323
+ this.runtimeToken = null
324
+ this.miniappCache.clear()
325
+ this.expiredFired = false
326
+ // Start local deletion in the same synchronous prefix as invalidation;
327
+ // only local storage work may precede it, never a network operation.
328
+ const localClear = this.store.clear()
329
+ const [refreshToken] = await Promise.all([refreshSnapshot, localClear])
330
+ const controller = new AbortController()
331
+ let timeout: unknown
318
332
  try {
319
- await this.revokeCoreSession()
320
- } catch (err) {
321
- revokeError = err
333
+ await Promise.race([
334
+ this.revokeCoreSession(current, refreshToken, controller.signal),
335
+ new Promise<never>((_, reject) => {
336
+ timeout = this.timers.setTimeout(() => {
337
+ controller.abort()
338
+ reject(new Error("Core session revocation timed out"))
339
+ }, REVOKE_TIMEOUT_MS)
340
+ }),
341
+ ])
342
+ } catch (error) {
322
343
  this.logger.warn("Core session revocation failed during local logout", {
323
- error: (err as Error)?.message,
344
+ error: (error as Error)?.message,
324
345
  })
346
+ throw new SessionRevocationError("Core session revocation failed; local credentials were cleared", {cause: error})
325
347
  } finally {
326
- await this.store.clear()
327
- this.runtimeToken = null
328
- this.miniappCache.clear()
329
- this.expiredFired = false
330
- }
331
- if (revokeError) {
332
- throw new SessionRevocationError("Core session revocation failed; local credentials were cleared", {
333
- cause: revokeError,
334
- })
348
+ this.timers.clearTimeout(timeout)
335
349
  }
336
350
  }
337
351
 
338
352
  /**
339
- * Revoke the Core session server-side before local credentials are dropped.
353
+ * Revoke the captured Core session after local credentials are dropped.
340
354
  *
341
355
  * Core verifies the presented access token with no clock tolerance, so a
342
356
  * cached token inside the usual expiry margin is refreshed first rather than
@@ -345,23 +359,26 @@ export class Auth implements AuthModule {
345
359
  * not. Any failure propagates so the caller can tell revocation did not
346
360
  * complete.
347
361
  */
348
- private async revokeCoreSession(): Promise<void> {
362
+ private async revokeCoreSession(
363
+ current: ReturnType<TokenStore["current"]>,
364
+ refreshToken: string | null,
365
+ signal: AbortSignal,
366
+ ): Promise<void> {
349
367
  if (!this.coreConfig || !this.http) return
350
- const current = this.store.current()
351
- const refreshToken = await this.store.refreshToken()
352
368
  let accessToken: string | null = null
353
369
  if (current && !this.isExpiring(current.exp)) {
354
370
  accessToken = current.accessToken
355
371
  } else if (refreshToken) {
356
372
  const body = new URLSearchParams({grant_type: "refresh_token", refresh_token: refreshToken})
357
- const tokens = await this.postForm(REFRESH_PATH, body, "logout refresh")
373
+ const tokens = await this.postForm(REFRESH_PATH, body, "logout refresh", signal)
358
374
  accessToken = tokens.access_token
359
375
  }
360
376
  if (!accessToken) return
361
377
 
362
378
  for (let attempt = 0; ; attempt++) {
379
+ if (signal.aborted) throw new Error("Core session revocation timed out")
363
380
  try {
364
- await this.http.post(REVOKE_PATH, {}, {bearer: accessToken})
381
+ await this.http.post(REVOKE_PATH, {}, {bearer: accessToken, signal})
365
382
  return
366
383
  } catch (err) {
367
384
  if (attempt + 1 >= REVOKE_ATTEMPTS || !isRetryableRevokeError(err)) throw err
@@ -597,13 +614,20 @@ export class Auth implements AuthModule {
597
614
  * subject/refresh token in the body, not as a Bearer header. We never log the
598
615
  * body: it carries a token.
599
616
  */
600
- private async postForm(path: string, body: URLSearchParams, label: string): Promise<TokenResponse> {
617
+ private async postForm(
618
+ path: string,
619
+ body: URLSearchParams,
620
+ label: string,
621
+ signal?: AbortSignal,
622
+ ): Promise<TokenResponse> {
601
623
  const url = this.joinUrl(path)
602
624
  const res = await this.httpTransport(url, {
603
625
  method: "POST",
604
626
  headers: {"Content-Type": "application/x-www-form-urlencoded"},
605
627
  body: body.toString(),
628
+ ...(signal ? {signal} : {}),
606
629
  })
630
+ if (signal?.aborted) throw new Error("Core session revocation timed out")
607
631
 
608
632
  if (!res.ok) {
609
633
  // The body may carry an RFC `{ error, error_description }`, but we keep the