cursedauth 0.5.1 → 0.7.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/README.md +16 -1
- package/dist/offlineGrant.d.ts +127 -0
- package/dist/offlineGrant.d.ts.map +1 -0
- package/dist/offlineGrant.js +104 -0
- package/dist/offlineGrant.js.map +1 -0
- package/package.json +14 -2
- package/src/offlineGrant.ts +153 -0
package/README.md
CHANGED
|
@@ -43,14 +43,29 @@ logins and nothing else. The provider is a separate service reached over HTTPS a
|
|
|
43
43
|
| `cursedauth/content-census` | server | answer "what has this account made here?" by COUNT |
|
|
44
44
|
| `cursedauth/account-slots` | both | several accounts signed in on one browser |
|
|
45
45
|
| `cursedauth/sso` | both | the grant: its wire shape, its claims, its verification |
|
|
46
|
+
| `cursedauth/session-claims` | server | the bearer-token half: `SESSION_CLAIMS_KEY`, `SessionClaims`, `createRequireSession`. **Worker-safe** — reaches no `bun:sqlite` |
|
|
46
47
|
| `cursedauth/constant-time` | both | `constantTimeEqual` — compare two secrets without leaking where they differ |
|
|
48
|
+
| `cursedauth/offline-grant` | both | the bounded offline window: how long a device with no network may keep using cached content |
|
|
47
49
|
| `cursedauth/auth` | browser | `startSsoSignIn`, `useSsoAutoSignIn`, session hooks |
|
|
48
50
|
| `cursedauth/account-ui` | browser | the account menu |
|
|
49
51
|
| `cursedauth/account-slot-client` | browser | slot switching in the page |
|
|
50
52
|
| `cursedauth/testing` | server | the grant→callback→cookie harness, for a consumer's own suite |
|
|
51
53
|
|
|
52
54
|
🔴 The barrel reaches `bun:sqlite`. A browser bundle imports the three browser
|
|
53
|
-
subpaths and never the root
|
|
55
|
+
subpaths and never the root — **and so does a Cloudflare Worker**, which is why
|
|
56
|
+
`cursedauth/session-claims` exists. `apps/patterns` could not `wrangler deploy` at
|
|
57
|
+
all on 2026-09-18 (`Could not resolve "bun:sqlite"`, naming `dist/userSessions.js`)
|
|
58
|
+
because one string constant was reachable only through the barrel. Which subpaths
|
|
59
|
+
may reach `bun:sqlite` is now a CHECK — `src/subpathsReachNoBunBuiltin.test.ts` —
|
|
60
|
+
and that allowlist may only shrink.
|
|
61
|
+
|
|
62
|
+
🔴 **Two different things are called a "grant" here, and only one is a
|
|
63
|
+
credential.** `cursedauth/sso` has *the* grant: signed by the accounts service,
|
|
64
|
+
single-use, and it authorizes a sign-in. `cursedauth/offline-grant` has an
|
|
65
|
+
`OfflineGrant`: a local stamp saying "this device completed a verified session
|
|
66
|
+
for account X at time T", which authorizes nothing, is never sent anywhere, and
|
|
67
|
+
only bounds how long already-cached bytes may be replayed while there is no
|
|
68
|
+
network to re-check. Separate subpaths, no import between them.
|
|
54
69
|
|
|
55
70
|
🔴 **This table is a CHECK, not prose** — `src/publishShape.test.ts` fails if an
|
|
56
71
|
export subpath is missing a row. A subpath nobody can find is a subpath every app
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The offline validity window — the bounded answer to "a disabled account cannot
|
|
3
|
+
* be revoked on a device that is offline".
|
|
4
|
+
*
|
|
5
|
+
* Cached bytes cannot be revoked and auth's revocation ticket cannot reach an
|
|
6
|
+
* offline device, so we do NOT pretend a disable is instantaneous. Instead every
|
|
7
|
+
* successful ONLINE session verification stamps a grant; offline use is allowed
|
|
8
|
+
* only while that stamp is recent. A reconnect re-verifies and either refreshes
|
|
9
|
+
* the stamp (still valid) or drops the grant and purges the cache (revoked).
|
|
10
|
+
*
|
|
11
|
+
* This module is pure: it never touches storage, `Date` or the network — the
|
|
12
|
+
* caller passes `now` and the persisted grant. That is what makes the window a
|
|
13
|
+
* unit-testable decision rather than a scattering of `Date.now()` comparisons.
|
|
14
|
+
* It imports nothing, so it is a leaf on both sides of the wire.
|
|
15
|
+
*
|
|
16
|
+
* ## 🔴 This is NOT the SSO grant
|
|
17
|
+
*
|
|
18
|
+
* `cursedauth/sso` publishes *the grant*: a signed, single-use credential the
|
|
19
|
+
* accounts service mints, which authorizes a sign-in. An {@link OfflineGrant} is
|
|
20
|
+
* the opposite kind of object — a local stamp that authorizes nothing and is
|
|
21
|
+
* never sent anywhere. Same word, two meanings, and the distinction is the whole
|
|
22
|
+
* safety argument below, so the two live on separate subpaths and never import
|
|
23
|
+
* each other.
|
|
24
|
+
*
|
|
25
|
+
* ## Where it came from
|
|
26
|
+
*
|
|
27
|
+
* Built, tested and justified in `roms` as `src/offline/validity.ts`, which is
|
|
28
|
+
* still where the trade-off is written down: `apps/roms/docs/offline.md`
|
|
29
|
+
* §"Constraint 2 — offline play vs. account disable: a bounded window". Lifted
|
|
30
|
+
* here on 2026-09-17 because the window is a property of a *session*, the
|
|
31
|
+
* revocation argument is auth's, and `music` was named as the second app that
|
|
32
|
+
* wants it — one app's `src/offline/` is unreachable from every other app.
|
|
33
|
+
*
|
|
34
|
+
* The shell/service-worker/cache half of `roms`' offline work deliberately did
|
|
35
|
+
* NOT come with it: `swRouting`, the outbox and quota relief are shaped around
|
|
36
|
+
* ROM bytes and save states, and whether they generalise is a question to answer
|
|
37
|
+
* with a second consumer in hand.
|
|
38
|
+
*
|
|
39
|
+
* One rename on the way over, and nothing else: `isOfflinePlayAllowed` is
|
|
40
|
+
* {@link isOfflineGrantValid} here, because a library that knows nothing about
|
|
41
|
+
* games should not decide whether "play" is allowed. Every other name, every
|
|
42
|
+
* default and every spec is the one `roms` reasoned about.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* The maximum time a device may use cached content while offline, measured from
|
|
46
|
+
* the last successful online session check. Seven days: long enough for a normal
|
|
47
|
+
* trip, short enough that a revoked account cannot indefinitely retain access by
|
|
48
|
+
* never reconnecting. See the trade-off write-up in `apps/roms/docs/offline.md`.
|
|
49
|
+
*
|
|
50
|
+
* 🔴 **A consumer may pass a SHORTER `maxWindowMs`, never a longer one by
|
|
51
|
+
* default.** This number *is* the revocation argument: the exposure a disabled
|
|
52
|
+
* account keeps is exactly this window, and it was justified against cached game
|
|
53
|
+
* bytes on a device that may never reconnect. Raising the shared default would
|
|
54
|
+
* widen every app's exposure at once, on no app's reasoning.
|
|
55
|
+
*/
|
|
56
|
+
export declare const OFFLINE_MAX_WINDOW_MS: number;
|
|
57
|
+
/**
|
|
58
|
+
* Proof, stamped at the last successful ONLINE session verification, that this
|
|
59
|
+
* device was a valid session for `accountId` at `lastVerifiedAt`. Persisted
|
|
60
|
+
* locally (e.g. localStorage). It is not a credential — it grants nothing on its
|
|
61
|
+
* own; it only bounds how long already-cached bytes may be replayed offline.
|
|
62
|
+
*/
|
|
63
|
+
export interface OfflineGrant {
|
|
64
|
+
accountId: string;
|
|
65
|
+
/** Epoch ms of the last successful online session check. */
|
|
66
|
+
lastVerifiedAt: number;
|
|
67
|
+
/**
|
|
68
|
+
* The account's email, purely so a cold OFFLINE load can put a name in the
|
|
69
|
+
* header instead of a uuid (`apps/roms/src/offlineAuth.ts` is the app that
|
|
70
|
+
* does exactly this). Optional: grants written before 2026-08-05 have none,
|
|
71
|
+
* and nothing may depend on it — it is a label, never a credential and never
|
|
72
|
+
* an identity check.
|
|
73
|
+
*/
|
|
74
|
+
email?: string;
|
|
75
|
+
}
|
|
76
|
+
export type OfflineValidity =
|
|
77
|
+
/** No grant on this device — it never signed in. Offline use is refused. */
|
|
78
|
+
{
|
|
79
|
+
state: "no-grant";
|
|
80
|
+
}
|
|
81
|
+
/** Within the window — offline use allowed. */
|
|
82
|
+
| {
|
|
83
|
+
state: "valid";
|
|
84
|
+
remainingMs: number;
|
|
85
|
+
}
|
|
86
|
+
/** The window has lapsed — offline use refused until a reconnect re-checks. */
|
|
87
|
+
| {
|
|
88
|
+
state: "expired";
|
|
89
|
+
expiredForMs: number;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* How much of the offline window is left, clamped to zero. Negative inputs
|
|
93
|
+
* (a grant stamped in the "future" by a skewed clock) are treated as fully spent
|
|
94
|
+
* rather than trusted, so clock games cannot extend the window.
|
|
95
|
+
*/
|
|
96
|
+
export declare function offlineWindowRemainingMs(grant: OfflineGrant, now: number, maxWindowMs?: number): number;
|
|
97
|
+
/** The full validity verdict for a (possibly absent) grant at time `now`. */
|
|
98
|
+
export declare function offlineValidity(grant: OfflineGrant | null | undefined, now: number, maxWindowMs?: number): OfflineValidity;
|
|
99
|
+
/**
|
|
100
|
+
* Convenience: is offline use permitted on the strength of this grant right now?
|
|
101
|
+
*
|
|
102
|
+
* (`roms` calls this `isOfflinePlayAllowed`; it is the same predicate.)
|
|
103
|
+
*/
|
|
104
|
+
export declare function isOfflineGrantValid(grant: OfflineGrant | null | undefined, now: number, maxWindowMs?: number): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* The outcome of an online reconnect check. `revoked` means auth reported the
|
|
107
|
+
* account disabled or the session no longer valid; `verified` means it is still
|
|
108
|
+
* this account and still valid.
|
|
109
|
+
*/
|
|
110
|
+
export type ReconnectCheck = {
|
|
111
|
+
verified: true;
|
|
112
|
+
accountId: string;
|
|
113
|
+
at: number;
|
|
114
|
+
} | {
|
|
115
|
+
revoked: true;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Fold a fresh online check into the stored grant — the stamp-on-successful-
|
|
119
|
+
* verification hook, and the only way a grant is ever created or refreshed.
|
|
120
|
+
*
|
|
121
|
+
* - verified → a refreshed grant stamped `at` (the window resets).
|
|
122
|
+
* - revoked, OR verified as a DIFFERENT account than the one that cached the
|
|
123
|
+
* bytes → `null`. The caller MUST then purge its content cache: a null grant is
|
|
124
|
+
* the signal that whatever is cached must no longer be usable.
|
|
125
|
+
*/
|
|
126
|
+
export declare function reconcileGrant(previous: OfflineGrant | null | undefined, check: ReconnectCheck): OfflineGrant | null;
|
|
127
|
+
//# sourceMappingURL=offlineGrant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"offlineGrant.d.ts","sourceRoot":"","sources":["../src/offlineGrant.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH;;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B,YAAY,GAAG,IAAI,CAMrB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The offline validity window — the bounded answer to "a disabled account cannot
|
|
3
|
+
* be revoked on a device that is offline".
|
|
4
|
+
*
|
|
5
|
+
* Cached bytes cannot be revoked and auth's revocation ticket cannot reach an
|
|
6
|
+
* offline device, so we do NOT pretend a disable is instantaneous. Instead every
|
|
7
|
+
* successful ONLINE session verification stamps a grant; offline use is allowed
|
|
8
|
+
* only while that stamp is recent. A reconnect re-verifies and either refreshes
|
|
9
|
+
* the stamp (still valid) or drops the grant and purges the cache (revoked).
|
|
10
|
+
*
|
|
11
|
+
* This module is pure: it never touches storage, `Date` or the network — the
|
|
12
|
+
* caller passes `now` and the persisted grant. That is what makes the window a
|
|
13
|
+
* unit-testable decision rather than a scattering of `Date.now()` comparisons.
|
|
14
|
+
* It imports nothing, so it is a leaf on both sides of the wire.
|
|
15
|
+
*
|
|
16
|
+
* ## 🔴 This is NOT the SSO grant
|
|
17
|
+
*
|
|
18
|
+
* `cursedauth/sso` publishes *the grant*: a signed, single-use credential the
|
|
19
|
+
* accounts service mints, which authorizes a sign-in. An {@link OfflineGrant} is
|
|
20
|
+
* the opposite kind of object — a local stamp that authorizes nothing and is
|
|
21
|
+
* never sent anywhere. Same word, two meanings, and the distinction is the whole
|
|
22
|
+
* safety argument below, so the two live on separate subpaths and never import
|
|
23
|
+
* each other.
|
|
24
|
+
*
|
|
25
|
+
* ## Where it came from
|
|
26
|
+
*
|
|
27
|
+
* Built, tested and justified in `roms` as `src/offline/validity.ts`, which is
|
|
28
|
+
* still where the trade-off is written down: `apps/roms/docs/offline.md`
|
|
29
|
+
* §"Constraint 2 — offline play vs. account disable: a bounded window". Lifted
|
|
30
|
+
* here on 2026-09-17 because the window is a property of a *session*, the
|
|
31
|
+
* revocation argument is auth's, and `music` was named as the second app that
|
|
32
|
+
* wants it — one app's `src/offline/` is unreachable from every other app.
|
|
33
|
+
*
|
|
34
|
+
* The shell/service-worker/cache half of `roms`' offline work deliberately did
|
|
35
|
+
* NOT come with it: `swRouting`, the outbox and quota relief are shaped around
|
|
36
|
+
* ROM bytes and save states, and whether they generalise is a question to answer
|
|
37
|
+
* with a second consumer in hand.
|
|
38
|
+
*
|
|
39
|
+
* One rename on the way over, and nothing else: `isOfflinePlayAllowed` is
|
|
40
|
+
* {@link isOfflineGrantValid} here, because a library that knows nothing about
|
|
41
|
+
* games should not decide whether "play" is allowed. Every other name, every
|
|
42
|
+
* default and every spec is the one `roms` reasoned about.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* The maximum time a device may use cached content while offline, measured from
|
|
46
|
+
* the last successful online session check. Seven days: long enough for a normal
|
|
47
|
+
* trip, short enough that a revoked account cannot indefinitely retain access by
|
|
48
|
+
* never reconnecting. See the trade-off write-up in `apps/roms/docs/offline.md`.
|
|
49
|
+
*
|
|
50
|
+
* 🔴 **A consumer may pass a SHORTER `maxWindowMs`, never a longer one by
|
|
51
|
+
* default.** This number *is* the revocation argument: the exposure a disabled
|
|
52
|
+
* account keeps is exactly this window, and it was justified against cached game
|
|
53
|
+
* bytes on a device that may never reconnect. Raising the shared default would
|
|
54
|
+
* widen every app's exposure at once, on no app's reasoning.
|
|
55
|
+
*/
|
|
56
|
+
export const OFFLINE_MAX_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
57
|
+
/**
|
|
58
|
+
* How much of the offline window is left, clamped to zero. Negative inputs
|
|
59
|
+
* (a grant stamped in the "future" by a skewed clock) are treated as fully spent
|
|
60
|
+
* rather than trusted, so clock games cannot extend the window.
|
|
61
|
+
*/
|
|
62
|
+
export function offlineWindowRemainingMs(grant, now, maxWindowMs = OFFLINE_MAX_WINDOW_MS) {
|
|
63
|
+
const elapsed = now - grant.lastVerifiedAt;
|
|
64
|
+
if (elapsed < 0)
|
|
65
|
+
return 0; // future-stamped grant: don't trust it
|
|
66
|
+
return Math.max(0, maxWindowMs - elapsed);
|
|
67
|
+
}
|
|
68
|
+
/** The full validity verdict for a (possibly absent) grant at time `now`. */
|
|
69
|
+
export function offlineValidity(grant, now, maxWindowMs = OFFLINE_MAX_WINDOW_MS) {
|
|
70
|
+
if (!grant)
|
|
71
|
+
return { state: "no-grant" };
|
|
72
|
+
const remainingMs = offlineWindowRemainingMs(grant, now, maxWindowMs);
|
|
73
|
+
if (remainingMs > 0)
|
|
74
|
+
return { state: "valid", remainingMs };
|
|
75
|
+
const elapsed = now - grant.lastVerifiedAt;
|
|
76
|
+
return { state: "expired", expiredForMs: Math.max(0, elapsed - maxWindowMs) };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Convenience: is offline use permitted on the strength of this grant right now?
|
|
80
|
+
*
|
|
81
|
+
* (`roms` calls this `isOfflinePlayAllowed`; it is the same predicate.)
|
|
82
|
+
*/
|
|
83
|
+
export function isOfflineGrantValid(grant, now, maxWindowMs = OFFLINE_MAX_WINDOW_MS) {
|
|
84
|
+
return offlineValidity(grant, now, maxWindowMs).state === "valid";
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Fold a fresh online check into the stored grant — the stamp-on-successful-
|
|
88
|
+
* verification hook, and the only way a grant is ever created or refreshed.
|
|
89
|
+
*
|
|
90
|
+
* - verified → a refreshed grant stamped `at` (the window resets).
|
|
91
|
+
* - revoked, OR verified as a DIFFERENT account than the one that cached the
|
|
92
|
+
* bytes → `null`. The caller MUST then purge its content cache: a null grant is
|
|
93
|
+
* the signal that whatever is cached must no longer be usable.
|
|
94
|
+
*/
|
|
95
|
+
export function reconcileGrant(previous, check) {
|
|
96
|
+
if ("revoked" in check)
|
|
97
|
+
return null;
|
|
98
|
+
// A device that comes back as a different account must not inherit the prior
|
|
99
|
+
// account's offline window over the prior account's cached library.
|
|
100
|
+
if (previous && previous.accountId !== check.accountId)
|
|
101
|
+
return null;
|
|
102
|
+
return { accountId: check.accountId, lastVerifiedAt: check.at };
|
|
103
|
+
}
|
|
104
|
+
//# sourceMapping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B;IAE3C,OAAO,eAAe,CAAC,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC,KAAK,KAAK,OAAO,CAAC;AACnE,CAAC;AASD;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAC7B,QAAyC,EACzC,KAAqB;IAErB,IAAI,SAAS,IAAI,KAAK;QAAE,OAAO,IAAI,CAAC;IACpC,6EAA6E;IAC7E,oEAAoE;IACpE,IAAI,QAAQ,IAAI,QAAQ,CAAC,SAAS,KAAK,KAAK,CAAC,SAAS;QAAE,OAAO,IAAI,CAAC;IACpE,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,cAAc,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;AACjE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedauth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "The app side of fleet SSO — sign in through the accounts service, keep a local snapshot of who signed in, and gate the app on it. Bun + Hono + React, one SQLite file per app.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -71,12 +71,24 @@
|
|
|
71
71
|
"source": "./src/ssoTestHarness.ts",
|
|
72
72
|
"import": "./dist/ssoTestHarness.js"
|
|
73
73
|
},
|
|
74
|
+
"./session-claims": {
|
|
75
|
+
"types": "./dist/auth.server.d.ts",
|
|
76
|
+
"bun": "./src/auth.server.ts",
|
|
77
|
+
"source": "./src/auth.server.ts",
|
|
78
|
+
"import": "./dist/auth.server.js"
|
|
79
|
+
},
|
|
74
80
|
"./constant-time": {
|
|
75
81
|
"types": "./dist/constantTime.d.ts",
|
|
76
82
|
"bun": "./src/constantTime.ts",
|
|
77
83
|
"source": "./src/constantTime.ts",
|
|
78
84
|
"import": "./dist/constantTime.js"
|
|
79
85
|
},
|
|
86
|
+
"./offline-grant": {
|
|
87
|
+
"types": "./dist/offlineGrant.d.ts",
|
|
88
|
+
"bun": "./src/offlineGrant.ts",
|
|
89
|
+
"source": "./src/offlineGrant.ts",
|
|
90
|
+
"import": "./dist/offlineGrant.js"
|
|
91
|
+
},
|
|
80
92
|
"./package.json": "./package.json"
|
|
81
93
|
},
|
|
82
94
|
"files": [
|
|
@@ -88,7 +100,7 @@
|
|
|
88
100
|
"LICENSE"
|
|
89
101
|
],
|
|
90
102
|
"scripts": {
|
|
91
|
-
"paths": "bun
|
|
103
|
+
"paths": "bun run scripts/paths.ts",
|
|
92
104
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
93
105
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
94
106
|
"test": "bun test src",
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The offline validity window — the bounded answer to "a disabled account cannot
|
|
3
|
+
* be revoked on a device that is offline".
|
|
4
|
+
*
|
|
5
|
+
* Cached bytes cannot be revoked and auth's revocation ticket cannot reach an
|
|
6
|
+
* offline device, so we do NOT pretend a disable is instantaneous. Instead every
|
|
7
|
+
* successful ONLINE session verification stamps a grant; offline use is allowed
|
|
8
|
+
* only while that stamp is recent. A reconnect re-verifies and either refreshes
|
|
9
|
+
* the stamp (still valid) or drops the grant and purges the cache (revoked).
|
|
10
|
+
*
|
|
11
|
+
* This module is pure: it never touches storage, `Date` or the network — the
|
|
12
|
+
* caller passes `now` and the persisted grant. That is what makes the window a
|
|
13
|
+
* unit-testable decision rather than a scattering of `Date.now()` comparisons.
|
|
14
|
+
* It imports nothing, so it is a leaf on both sides of the wire.
|
|
15
|
+
*
|
|
16
|
+
* ## 🔴 This is NOT the SSO grant
|
|
17
|
+
*
|
|
18
|
+
* `cursedauth/sso` publishes *the grant*: a signed, single-use credential the
|
|
19
|
+
* accounts service mints, which authorizes a sign-in. An {@link OfflineGrant} is
|
|
20
|
+
* the opposite kind of object — a local stamp that authorizes nothing and is
|
|
21
|
+
* never sent anywhere. Same word, two meanings, and the distinction is the whole
|
|
22
|
+
* safety argument below, so the two live on separate subpaths and never import
|
|
23
|
+
* each other.
|
|
24
|
+
*
|
|
25
|
+
* ## Where it came from
|
|
26
|
+
*
|
|
27
|
+
* Built, tested and justified in `roms` as `src/offline/validity.ts`, which is
|
|
28
|
+
* still where the trade-off is written down: `apps/roms/docs/offline.md`
|
|
29
|
+
* §"Constraint 2 — offline play vs. account disable: a bounded window". Lifted
|
|
30
|
+
* here on 2026-09-17 because the window is a property of a *session*, the
|
|
31
|
+
* revocation argument is auth's, and `music` was named as the second app that
|
|
32
|
+
* wants it — one app's `src/offline/` is unreachable from every other app.
|
|
33
|
+
*
|
|
34
|
+
* The shell/service-worker/cache half of `roms`' offline work deliberately did
|
|
35
|
+
* NOT come with it: `swRouting`, the outbox and quota relief are shaped around
|
|
36
|
+
* ROM bytes and save states, and whether they generalise is a question to answer
|
|
37
|
+
* with a second consumer in hand.
|
|
38
|
+
*
|
|
39
|
+
* One rename on the way over, and nothing else: `isOfflinePlayAllowed` is
|
|
40
|
+
* {@link isOfflineGrantValid} here, because a library that knows nothing about
|
|
41
|
+
* games should not decide whether "play" is allowed. Every other name, every
|
|
42
|
+
* default and every spec is the one `roms` reasoned about.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The maximum time a device may use cached content while offline, measured from
|
|
47
|
+
* the last successful online session check. Seven days: long enough for a normal
|
|
48
|
+
* trip, short enough that a revoked account cannot indefinitely retain access by
|
|
49
|
+
* never reconnecting. See the trade-off write-up in `apps/roms/docs/offline.md`.
|
|
50
|
+
*
|
|
51
|
+
* 🔴 **A consumer may pass a SHORTER `maxWindowMs`, never a longer one by
|
|
52
|
+
* default.** This number *is* the revocation argument: the exposure a disabled
|
|
53
|
+
* account keeps is exactly this window, and it was justified against cached game
|
|
54
|
+
* bytes on a device that may never reconnect. Raising the shared default would
|
|
55
|
+
* widen every app's exposure at once, on no app's reasoning.
|
|
56
|
+
*/
|
|
57
|
+
export const OFFLINE_MAX_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Proof, stamped at the last successful ONLINE session verification, that this
|
|
61
|
+
* device was a valid session for `accountId` at `lastVerifiedAt`. Persisted
|
|
62
|
+
* locally (e.g. localStorage). It is not a credential — it grants nothing on its
|
|
63
|
+
* own; it only bounds how long already-cached bytes may be replayed offline.
|
|
64
|
+
*/
|
|
65
|
+
export interface OfflineGrant {
|
|
66
|
+
accountId: string;
|
|
67
|
+
/** Epoch ms of the last successful online session check. */
|
|
68
|
+
lastVerifiedAt: number;
|
|
69
|
+
/**
|
|
70
|
+
* The account's email, purely so a cold OFFLINE load can put a name in the
|
|
71
|
+
* header instead of a uuid (`apps/roms/src/offlineAuth.ts` is the app that
|
|
72
|
+
* does exactly this). Optional: grants written before 2026-08-05 have none,
|
|
73
|
+
* and nothing may depend on it — it is a label, never a credential and never
|
|
74
|
+
* an identity check.
|
|
75
|
+
*/
|
|
76
|
+
email?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type OfflineValidity =
|
|
80
|
+
/** No grant on this device — it never signed in. Offline use is refused. */
|
|
81
|
+
| { state: "no-grant" }
|
|
82
|
+
/** Within the window — offline use allowed. */
|
|
83
|
+
| { state: "valid"; remainingMs: number }
|
|
84
|
+
/** The window has lapsed — offline use refused until a reconnect re-checks. */
|
|
85
|
+
| { state: "expired"; expiredForMs: number };
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* How much of the offline window is left, clamped to zero. Negative inputs
|
|
89
|
+
* (a grant stamped in the "future" by a skewed clock) are treated as fully spent
|
|
90
|
+
* rather than trusted, so clock games cannot extend the window.
|
|
91
|
+
*/
|
|
92
|
+
export function offlineWindowRemainingMs(
|
|
93
|
+
grant: OfflineGrant,
|
|
94
|
+
now: number,
|
|
95
|
+
maxWindowMs: number = OFFLINE_MAX_WINDOW_MS,
|
|
96
|
+
): number {
|
|
97
|
+
const elapsed = now - grant.lastVerifiedAt;
|
|
98
|
+
if (elapsed < 0) return 0; // future-stamped grant: don't trust it
|
|
99
|
+
return Math.max(0, maxWindowMs - elapsed);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The full validity verdict for a (possibly absent) grant at time `now`. */
|
|
103
|
+
export function offlineValidity(
|
|
104
|
+
grant: OfflineGrant | null | undefined,
|
|
105
|
+
now: number,
|
|
106
|
+
maxWindowMs: number = OFFLINE_MAX_WINDOW_MS,
|
|
107
|
+
): OfflineValidity {
|
|
108
|
+
if (!grant) return { state: "no-grant" };
|
|
109
|
+
const remainingMs = offlineWindowRemainingMs(grant, now, maxWindowMs);
|
|
110
|
+
if (remainingMs > 0) return { state: "valid", remainingMs };
|
|
111
|
+
const elapsed = now - grant.lastVerifiedAt;
|
|
112
|
+
return { state: "expired", expiredForMs: Math.max(0, elapsed - maxWindowMs) };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Convenience: is offline use permitted on the strength of this grant right now?
|
|
117
|
+
*
|
|
118
|
+
* (`roms` calls this `isOfflinePlayAllowed`; it is the same predicate.)
|
|
119
|
+
*/
|
|
120
|
+
export function isOfflineGrantValid(
|
|
121
|
+
grant: OfflineGrant | null | undefined,
|
|
122
|
+
now: number,
|
|
123
|
+
maxWindowMs: number = OFFLINE_MAX_WINDOW_MS,
|
|
124
|
+
): boolean {
|
|
125
|
+
return offlineValidity(grant, now, maxWindowMs).state === "valid";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The outcome of an online reconnect check. `revoked` means auth reported the
|
|
130
|
+
* account disabled or the session no longer valid; `verified` means it is still
|
|
131
|
+
* this account and still valid.
|
|
132
|
+
*/
|
|
133
|
+
export type ReconnectCheck = { verified: true; accountId: string; at: number } | { revoked: true };
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fold a fresh online check into the stored grant — the stamp-on-successful-
|
|
137
|
+
* verification hook, and the only way a grant is ever created or refreshed.
|
|
138
|
+
*
|
|
139
|
+
* - verified → a refreshed grant stamped `at` (the window resets).
|
|
140
|
+
* - revoked, OR verified as a DIFFERENT account than the one that cached the
|
|
141
|
+
* bytes → `null`. The caller MUST then purge its content cache: a null grant is
|
|
142
|
+
* the signal that whatever is cached must no longer be usable.
|
|
143
|
+
*/
|
|
144
|
+
export function reconcileGrant(
|
|
145
|
+
previous: OfflineGrant | null | undefined,
|
|
146
|
+
check: ReconnectCheck,
|
|
147
|
+
): OfflineGrant | null {
|
|
148
|
+
if ("revoked" in check) return null;
|
|
149
|
+
// A device that comes back as a different account must not inherit the prior
|
|
150
|
+
// account's offline window over the prior account's cached library.
|
|
151
|
+
if (previous && previous.accountId !== check.accountId) return null;
|
|
152
|
+
return { accountId: check.accountId, lastVerifiedAt: check.at };
|
|
153
|
+
}
|