@mentra/cloud-client 3.4.0-dev.1085 → 3.4.0-dev.1087
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 +2 -2
- package/src/modules/auth/auth.ts +41 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mentra/cloud-client",
|
|
3
|
-
"version": "3.4.0-dev.
|
|
3
|
+
"version": "3.4.0-dev.1087",
|
|
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.4.0-dev.
|
|
15
|
+
"@mentra/cloud-protocol": "3.4.0-dev.1087",
|
|
16
16
|
"tweetnacl": "^1.0.3"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
package/src/modules/auth/auth.ts
CHANGED
|
@@ -66,10 +66,12 @@ export interface AuthModule {
|
|
|
66
66
|
getCoreToken(opts?: {forceRefresh?: boolean}): Promise<string>
|
|
67
67
|
// a miniapp-scoped token, cached per packageName and re-minted before expiry.
|
|
68
68
|
// Pass `{ fresh: true }` to skip the cache and mint now, for the moments the
|
|
69
|
-
// token's claims must be current (a miniapp opening).
|
|
69
|
+
// token's claims must be current (a miniapp opening). Pass
|
|
70
|
+
// `{ localBuild: true }` for a local or development build: Core marks its
|
|
71
|
+
// token as one, and it is cached apart from the installed miniapp's.
|
|
70
72
|
getMiniappToken(
|
|
71
73
|
packageName: string,
|
|
72
|
-
opts?: {minTtlMs?: number; fresh?: boolean},
|
|
74
|
+
opts?: {minTtlMs?: number; fresh?: boolean; localBuild?: boolean},
|
|
73
75
|
): Promise<{token: string; expiresAt: number}>
|
|
74
76
|
// Core-owned user/oem identity, read from the Core access token.
|
|
75
77
|
// Runtime-only deployments do not expose this surface.
|
|
@@ -159,6 +161,15 @@ interface RuntimeTokenEntry {
|
|
|
159
161
|
expiresAt: number
|
|
160
162
|
}
|
|
161
163
|
|
|
164
|
+
/**
|
|
165
|
+
* The cache key of a package's miniapp token: an installed miniapp's and a local
|
|
166
|
+
* build's are different tokens. The variant comes first and holds no `:`, so no
|
|
167
|
+
* package name makes one key look like another.
|
|
168
|
+
*/
|
|
169
|
+
function miniappTokenKey(packageName: string, localBuild: boolean): string {
|
|
170
|
+
return `${localBuild ? "local" : "installed"}:${packageName}`
|
|
171
|
+
}
|
|
172
|
+
|
|
162
173
|
/**
|
|
163
174
|
* A revoke is worth retrying only when Core gave no definite answer: the
|
|
164
175
|
* transport failed (`status` 0) or the server errored (5xx). A 4xx means Core
|
|
@@ -270,9 +281,9 @@ export class Auth implements AuthModule {
|
|
|
270
281
|
private readonly httpTransport: HttpTransport
|
|
271
282
|
private readonly timers: CloudClientTimers
|
|
272
283
|
|
|
273
|
-
/** Miniapp tokens cached per packageName until near expiry. */
|
|
284
|
+
/** Miniapp tokens cached per packageName and local-build flag (`miniappTokenKey`) until near expiry. */
|
|
274
285
|
private readonly miniappCache = new Map<string, MiniappTokenEntry>()
|
|
275
|
-
/** The newest mint per package
|
|
286
|
+
/** The newest mint per package, flag and session, until it settles. See `mintMiniappToken`. */
|
|
276
287
|
private readonly miniappMints = new Map<string, MiniappMint>()
|
|
277
288
|
/** Core-brokered runtime token cache. */
|
|
278
289
|
private runtimeToken: RuntimeTokenEntry | null = null
|
|
@@ -364,6 +375,11 @@ export class Auth implements AuthModule {
|
|
|
364
375
|
* By default the token is cached per packageName and re-minted only when near
|
|
365
376
|
* expiry, so a steady miniapp does not hit the cloud on every call.
|
|
366
377
|
*
|
|
378
|
+
* `localBuild: true` asks for a local or development build's token, which Core
|
|
379
|
+
* marks as one (and never names a workspace in). It is a different token from
|
|
380
|
+
* the installed miniapp's, so the two are cached and minted apart, and a call
|
|
381
|
+
* never gets the other's.
|
|
382
|
+
*
|
|
367
383
|
* `fresh: true` skips the cache and reaches Core, because what Core puts in a
|
|
368
384
|
* token (the features the user holds, today) can change within its hour. It
|
|
369
385
|
* can share a mint only if that mint's request has not left yet; a mint whose
|
|
@@ -379,34 +395,41 @@ export class Auth implements AuthModule {
|
|
|
379
395
|
*/
|
|
380
396
|
async getMiniappToken(
|
|
381
397
|
packageName: string,
|
|
382
|
-
opts?: {minTtlMs?: number; fresh?: boolean},
|
|
398
|
+
opts?: {minTtlMs?: number; fresh?: boolean; localBuild?: boolean},
|
|
383
399
|
): Promise<{token: string; expiresAt: number}> {
|
|
384
400
|
this.assertSessionActive()
|
|
385
401
|
const generation = this.sessionGeneration
|
|
386
402
|
const fresh = opts?.fresh === true
|
|
403
|
+
const localBuild = opts?.localBuild === true
|
|
387
404
|
if (!fresh) {
|
|
388
405
|
const marginSeconds = this.tokenMarginSeconds(opts?.minTtlMs)
|
|
389
|
-
const cached = this.miniappCache.get(packageName)
|
|
406
|
+
const cached = this.miniappCache.get(miniappTokenKey(packageName, localBuild))
|
|
390
407
|
if (cached && !this.isExpiring(cached.expiresAt, marginSeconds)) {
|
|
391
408
|
return {token: cached.token, expiresAt: cached.expiresAt}
|
|
392
409
|
}
|
|
393
410
|
}
|
|
394
411
|
|
|
395
|
-
const entry = await this.mintMiniappToken(packageName, generation, fresh)
|
|
412
|
+
const entry = await this.mintMiniappToken(packageName, localBuild, generation, fresh)
|
|
396
413
|
return {token: entry.token, expiresAt: entry.expiresAt}
|
|
397
414
|
}
|
|
398
415
|
|
|
399
416
|
/**
|
|
400
|
-
* The mint a caller should wait on. Mints for a package
|
|
401
|
-
* time; `miniappMints` holds the newest link.
|
|
417
|
+
* The mint a caller should wait on. Mints for a package (and local-build flag)
|
|
418
|
+
* form a chain, one at a time; `miniappMints` holds the newest link.
|
|
402
419
|
*
|
|
403
420
|
* - A default caller takes the newest mint, whatever its state.
|
|
404
421
|
* - A fresh caller takes it only while its request is unsent. Otherwise it
|
|
405
422
|
* appends a mint that starts when the newest one settles, win or lose, and
|
|
406
423
|
* becomes the newest, so later fresh callers share it until it too sends.
|
|
407
424
|
*/
|
|
408
|
-
private mintMiniappToken(
|
|
409
|
-
|
|
425
|
+
private mintMiniappToken(
|
|
426
|
+
packageName: string,
|
|
427
|
+
localBuild: boolean,
|
|
428
|
+
generation: number,
|
|
429
|
+
fresh: boolean,
|
|
430
|
+
): Promise<MiniappTokenEntry> {
|
|
431
|
+
const cacheKey = miniappTokenKey(packageName, localBuild)
|
|
432
|
+
const key = `${MINIAPP_FLIGHT_PREFIX}${generation}:${cacheKey}`
|
|
410
433
|
const newest = this.miniappMints.get(key)
|
|
411
434
|
if (newest && (!fresh || !newest.requestSent())) return newest.promise
|
|
412
435
|
|
|
@@ -416,14 +439,16 @@ export class Auth implements AuthModule {
|
|
|
416
439
|
const accessToken = await this.getCoreToken()
|
|
417
440
|
const http = this.requireCoreHttp()
|
|
418
441
|
requestSent = true
|
|
419
|
-
//
|
|
420
|
-
//
|
|
421
|
-
|
|
442
|
+
// The request names the package, and says when it is a local build so
|
|
443
|
+
// Core marks the token. An installed miniapp's request names only the
|
|
444
|
+
// package, as it always has.
|
|
445
|
+
const body = localBuild ? {packageName, localBuild: true} : {packageName}
|
|
446
|
+
const res = await http.post<MiniappTokenEntry>(MINIAPP_TOKEN_PATH, body, {bearer: accessToken})
|
|
422
447
|
|
|
423
448
|
this.assertSessionActive(generation)
|
|
424
449
|
const entry: MiniappTokenEntry = {token: res.token, expiresAt: res.expiresAt}
|
|
425
|
-
this.miniappCache.set(
|
|
426
|
-
this.logger.debug("minted miniapp token", {packageName})
|
|
450
|
+
this.miniappCache.set(cacheKey, entry)
|
|
451
|
+
this.logger.debug("minted miniapp token", {packageName, localBuild})
|
|
427
452
|
return entry
|
|
428
453
|
})()
|
|
429
454
|
const mint: MiniappMint = {promise, requestSent: () => requestSent}
|