@oxy.so/core 7.11.1 → 7.12.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.
@@ -8,12 +8,21 @@
8
8
  import { jwtDecode } from 'jwt-decode';
9
9
  import { normalizeUserIdentity } from '../utils/userIdentity.js';
10
10
  import { OxyApiError } from '../OxyServices.errors.js';
11
+ import { checkContract, holderContextEvidenceV1ResponseCheck } from '@oxy.so/contracts/client';
12
+ /** How long a refusal (disabled, rate limited, unreachable) is remembered per installation. */
13
+ const EVIDENCE_REFUSAL_MS = 5000;
14
+ const unavailable = (reason) => ({
15
+ status: 'unavailable',
16
+ reason,
17
+ });
11
18
  export class SessionApi {
12
19
  constructor(ctx) {
13
20
  this.ctx = ctx;
14
21
  this.deviceCredentialProvider = null;
15
22
  this.decodedFor = null;
16
23
  this.decoded = null;
24
+ /** One server round trip per installed bearer; the key is the opaque generation. */
25
+ this.evidence = new WeakMap();
17
26
  }
18
27
  // ── Local state ──────────────────────────────────────────────────────────
19
28
  /** The current access token, or `null` when signed out. */
@@ -118,6 +127,115 @@ export class SessionApi {
118
127
  return null;
119
128
  }
120
129
  }
130
+ // ── Holder-context evidence (ADR 0053) ───────────────────────────────────
131
+ /**
132
+ * Ask the server which shared-device context the INSTALLED bearer belongs to,
133
+ * and bind the answer to that exact installation.
134
+ *
135
+ * The result is honored only if the credential generation captured before
136
+ * the request is still current when the answer is committed, so a refresh,
137
+ * setter, account or actor switch, cross-tab change, restore or A to B to A
138
+ * during the request yields `changed`. The answer must also agree with the
139
+ * claims of the bearer that was captured; a disagreement is
140
+ * `binding_unavailable`. Nothing here refreshes, clears or mints a credential,
141
+ * and no outcome is a revocation verdict: callers keep their existing checks.
142
+ * Concurrent callers share one request per installation.
143
+ */
144
+ observeHolderEvidence() {
145
+ const generation = this.ctx.http.captureCredentialGeneration();
146
+ const token = this.ctx.http.getAccessToken();
147
+ if (!generation || !token)
148
+ return Promise.resolve(unavailable('no_credential'));
149
+ const known = this.evidence.get(generation);
150
+ if (known) {
151
+ const refused = known.settledAt !== undefined;
152
+ if (!refused || Date.now() - known.settledAt < EVIDENCE_REFUSAL_MS) {
153
+ return known.result.then((result) => result.status === 'ok' && result.evidence.expiresAtMs <= Date.now()
154
+ ? this.refreshEvidence(generation, token)
155
+ : result);
156
+ }
157
+ }
158
+ return this.refreshEvidence(generation, token);
159
+ }
160
+ /** Whether evidence still names the installed bearer and has not expired. */
161
+ isHolderEvidenceCurrent(evidence) {
162
+ return (Date.now() < evidence.expiresAtMs &&
163
+ this.ctx.http.isCredentialGenerationCurrent(evidence.generation));
164
+ }
165
+ refreshEvidence(generation, token) {
166
+ const entry = {
167
+ result: this.acquireEvidence(generation, token).then((result) => {
168
+ if (result.status !== 'ok')
169
+ entry.settledAt = Date.now();
170
+ return result;
171
+ }),
172
+ };
173
+ this.evidence.set(generation, entry);
174
+ return entry.result;
175
+ }
176
+ async acquireEvidence(generation, token) {
177
+ let claims;
178
+ try {
179
+ claims = jwtDecode(token);
180
+ }
181
+ catch {
182
+ return unavailable('unsupported');
183
+ }
184
+ const proof = await this.readDeviceProof();
185
+ if (!proof)
186
+ return unavailable('no_holder');
187
+ let response;
188
+ try {
189
+ response = await this.ctx.request('POST', '/session/device/context-evidence-v1', proof, {
190
+ cache: false,
191
+ retry: false,
192
+ expectedCredentialGeneration: generation,
193
+ });
194
+ }
195
+ catch (error) {
196
+ if (error?.code === 'AUTH_SESSION_CHANGED') {
197
+ return unavailable('changed');
198
+ }
199
+ const status = error instanceof OxyApiError ? error.status : 0;
200
+ if (status === 403)
201
+ return unavailable('unsupported');
202
+ if (status === 409)
203
+ return unavailable('binding_unavailable');
204
+ return unavailable('unavailable');
205
+ }
206
+ const parsed = checkContract(holderContextEvidenceV1ResponseCheck, response);
207
+ if (!parsed)
208
+ return unavailable('unavailable');
209
+ // Commit synchronously: nothing may replace the bearer between this check
210
+ // and the evidence becoming visible to its caller.
211
+ if (!this.ctx.http.isCredentialGenerationCurrent(generation))
212
+ return unavailable('changed');
213
+ const expiresAtMs = Date.parse(parsed.expiresAt);
214
+ if (parsed.deviceId !== proof.deviceId ||
215
+ parsed.accountId !== claims.userId ||
216
+ parsed.sessionId !== claims.sessionId ||
217
+ (claims.act?.sub !== undefined && parsed.actorId !== claims.act.sub) ||
218
+ (claims.device_context_id !== undefined && parsed.contextId !== claims.device_context_id)) {
219
+ return unavailable('binding_unavailable');
220
+ }
221
+ if (!Number.isFinite(expiresAtMs) || expiresAtMs <= Date.now()) {
222
+ return unavailable('unavailable');
223
+ }
224
+ const evidence = Object.freeze({
225
+ generation,
226
+ identity: Object.freeze({
227
+ deviceId: parsed.deviceId,
228
+ deviceSessionId: parsed.deviceSessionId,
229
+ contextId: parsed.contextId,
230
+ actorId: parsed.actorId,
231
+ accountId: parsed.accountId,
232
+ sessionId: parsed.sessionId,
233
+ }),
234
+ deviceRevision: parsed.revision,
235
+ expiresAtMs,
236
+ });
237
+ return { status: 'ok', evidence };
238
+ }
121
239
  // ── The server session ───────────────────────────────────────────────────
122
240
  /** Ask the server whether the current access token is valid. */
123
241
  async validateToken() {
package/dist/esm/index.js CHANGED
@@ -30,6 +30,7 @@ import './crypto/polyfill.js';
30
30
  // ---------------------------------------------------------------------------
31
31
  export { OxyServices, OXY_API_URL, OXY_CLOUD_URL } from './OxyServices.js';
32
32
  export { OxyApiError, OxyAuthenticationError, OxyAuthenticationTimeoutError, AssetUrlResolutionError, ServiceAssetMetadataError, } from './OxyServices.errors.js';
33
+ export { sameHolderIdentity } from './api/holderEvidence.js';
33
34
  export { ASSET_MULTIPART_UPLOAD_MAX_BYTES, assetUrlCacheTTL, MAX_UPLOAD_BYTES } from './api/assets.js';
34
35
  export { DirectUploadInterruptedError } from './api/directUpload.js';
35
36
  export { buildUserDid } from './api/identity.js';
@@ -22,6 +22,18 @@ export type AccessTokenProvider = () => string | null;
22
22
  * — `null` to send it anonymous. See {@link HttpService.setAnonymousAuthProvider}.
23
23
  */
24
24
  export type AnonymousAuthProvider = () => Promise<string | null>;
25
+ declare const credentialGenerationBrand: unique symbol;
26
+ /**
27
+ * An opaque, immutable token for ONE installation of a bearer in a client.
28
+ *
29
+ * `TokenStore` swaps it on every write path (setter, refresh, account or actor
30
+ * switch, cross-tab or restored token, clear), so two installations of the same
31
+ * bytes (A, B, A) are different generations. It names a local sequence only: it
32
+ * is not evidence of an account, a scope or authorization.
33
+ */
34
+ export type CredentialGeneration = {
35
+ readonly [credentialGenerationBrand]: true;
36
+ };
25
37
  /** A trusted platform transport, for example Expo's streaming fetch. */
26
38
  export type ResponseTransport = (input: string, init: RequestInit) => Promise<Response>;
27
39
  /**
@@ -44,6 +56,13 @@ export interface ResponseRequest {
44
56
  /** Same response contract, but rejects locally when the SDK has no bearer. */
45
57
  export type AuthenticatedResponseRequest = ResponseRequest;
46
58
  export interface RequestOptions {
59
+ /**
60
+ * Bind a write to ONE installation of the source's bearer. The transport
61
+ * rejects (`AUTH_SESSION_CHANGED`) when the generation was replaced before
62
+ * dispatch, after queueing, or before the result is returned. Implies no cache,
63
+ * deduplication or retry, and a 401 neither refreshes nor clears the session.
64
+ */
65
+ expectedCredentialGeneration?: CredentialGeneration;
47
66
  cache?: boolean;
48
67
  cacheTTL?: number;
49
68
  deduplicate?: boolean;
@@ -228,6 +247,7 @@ export declare class HttpService {
228
247
  * bearer — and through the token listeners, the account — back.
229
248
  */
230
249
  private sessionEpoch;
250
+ private readonly credentialSource;
231
251
  /** Preserve a known device across legacy rotations which omit the claim. */
232
252
  private sessionDeviceId;
233
253
  /** Preserve known actor and context hints across rotations without those claims. */
@@ -258,7 +278,7 @@ export declare class HttpService {
258
278
  */
259
279
  private _tokenChangeListeners;
260
280
  private requestMetrics;
261
- constructor(config: OxyConfig);
281
+ constructor(config: OxyConfig, credentialSource?: HttpService | null);
262
282
  private syncAccessTokenFromProvider;
263
283
  /**
264
284
  * Robust FormData detection that works in browser, React Native, and
@@ -521,6 +541,10 @@ export declare class HttpService {
521
541
  * plants tokens itself (the device-secret arm) compares it across its awaits.
522
542
  */
523
543
  getSessionEpoch(): number;
544
+ /** Capture only the local generation; the caller must separately bind authority. */
545
+ captureCredentialGeneration(): CredentialGeneration | null;
546
+ isCredentialGenerationCurrent(generation: CredentialGeneration): boolean;
547
+ private assertCredentialGeneration;
524
548
  /** Whether {@link endSession} ran and no token has been planted since. */
525
549
  hasSessionEnded(): boolean;
526
550
  setAuthRefreshHandler(handler: AuthRefreshHandler | null): void;
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Holder-context evidence: the server's point-in-time observation of which
3
+ * shared-device context a bearer is installed in, bound to ONE installation of
4
+ * that bearer in this client (ADR 0053).
5
+ *
6
+ * Evidence is never authorization. It lets a caller that already captured a
7
+ * viewer intent prove, before a write is dispatched, that the bearer the write
8
+ * will carry belongs to that viewer's account and actor, and then pin the write
9
+ * to the exact installation with `expectedCredentialGeneration`. Anything that
10
+ * cannot be proven is `unavailable`, and the caller keeps its existing checks.
11
+ */
12
+ import type { HolderContextEvidenceV1Response } from '@oxy.so/contracts';
13
+ import type { CredentialGeneration } from '../HttpService';
14
+ /** The identifiers the server observed; none of them is a credential. */
15
+ export type HolderEvidenceIdentity = Readonly<Pick<HolderContextEvidenceV1Response, 'deviceId' | 'deviceSessionId' | 'contextId' | 'actorId' | 'accountId' | 'sessionId'>>;
16
+ export interface HolderEvidence {
17
+ /** The installation of the bearer this evidence was observed for. */
18
+ readonly generation: CredentialGeneration;
19
+ readonly identity: HolderEvidenceIdentity;
20
+ /** The device-session revision the server observed. */
21
+ readonly deviceRevision: number;
22
+ /** Epoch milliseconds: the earlier of the bearer and session expiry. */
23
+ readonly expiresAtMs: number;
24
+ }
25
+ /**
26
+ * Why no evidence exists. Every reason means "keep the existing checks": none of
27
+ * them says the session is revoked, and none may clear or refresh credentials.
28
+ *
29
+ * - `no_credential`: no bearer is installed, or it comes from a pull-only provider.
30
+ * - `no_holder`: this client holds no device credential to prove the holder with.
31
+ * - `changed`: the bearer was replaced while the evidence was being acquired.
32
+ * - `unsupported`: a delegated, OAuth or legacy bearer, or the endpoint is not offered.
33
+ * - `binding_unavailable`: the server could not match holder, context and bearer.
34
+ * - `unavailable`: the endpoint is off, rate limited, unreachable or answered wrongly.
35
+ */
36
+ export type HolderEvidenceUnavailableReason = 'no_credential' | 'no_holder' | 'changed' | 'unsupported' | 'binding_unavailable' | 'unavailable';
37
+ export type HolderEvidenceResult = {
38
+ readonly status: 'ok';
39
+ readonly evidence: HolderEvidence;
40
+ } | {
41
+ readonly status: 'unavailable';
42
+ readonly reason: HolderEvidenceUnavailableReason;
43
+ };
44
+ /** The one comparison that decides whether two observations name the same viewer context. */
45
+ export declare function sameHolderIdentity(a: HolderEvidenceIdentity, b: HolderEvidenceIdentity): boolean;
@@ -2,6 +2,7 @@ import type { AccountActorChain, DeviceProof } from '@oxy.so/contracts';
2
2
  import type { OxyContext } from '../client/context';
3
3
  import type { ClientSession } from '../models/session';
4
4
  import type { User } from '../models/interfaces';
5
+ import type { HolderEvidence, HolderEvidenceResult } from './holderEvidence';
5
6
  /**
6
7
  * Reads the device credential this client holds, if any — `{ deviceId,
7
8
  * deviceSecret }` from the provider's auth store. See
@@ -29,6 +30,8 @@ export declare class SessionApi {
29
30
  private deviceCredentialProvider;
30
31
  private decodedFor;
31
32
  private decoded;
33
+ /** One server round trip per installed bearer; the key is the opaque generation. */
34
+ private readonly evidence;
32
35
  constructor(ctx: OxyContext);
33
36
  /** The current access token, or `null` when signed out. */
34
37
  get accessToken(): string | null;
@@ -75,6 +78,24 @@ export declare class SessionApi {
75
78
  * that cannot be read only means the sign-in gets its own device.
76
79
  */
77
80
  readDeviceProof(): Promise<DeviceProof | null>;
81
+ /**
82
+ * Ask the server which shared-device context the INSTALLED bearer belongs to,
83
+ * and bind the answer to that exact installation.
84
+ *
85
+ * The result is honored only if the credential generation captured before
86
+ * the request is still current when the answer is committed, so a refresh,
87
+ * setter, account or actor switch, cross-tab change, restore or A to B to A
88
+ * during the request yields `changed`. The answer must also agree with the
89
+ * claims of the bearer that was captured; a disagreement is
90
+ * `binding_unavailable`. Nothing here refreshes, clears or mints a credential,
91
+ * and no outcome is a revocation verdict: callers keep their existing checks.
92
+ * Concurrent callers share one request per installation.
93
+ */
94
+ observeHolderEvidence(): Promise<HolderEvidenceResult>;
95
+ /** Whether evidence still names the installed bearer and has not expired. */
96
+ isHolderEvidenceCurrent(evidence: HolderEvidence): boolean;
97
+ private refreshEvidence;
98
+ private acquireEvidence;
78
99
  /** Ask the server whether the current access token is valid. */
79
100
  validateToken(): Promise<boolean>;
80
101
  /**
@@ -24,7 +24,9 @@ export { OxyServices, OXY_API_URL, OXY_CLOUD_URL } from './OxyServices';
24
24
  export type { OxyConfig, LinkedHttpClient } from './OxyServices';
25
25
  export { OxyApiError, OxyAuthenticationError, OxyAuthenticationTimeoutError, AssetUrlResolutionError, ServiceAssetMetadataError, } from './OxyServices.errors';
26
26
  export type { HttpMethod } from './client/context';
27
- export type { RequestOptions, ResponsePayload, ResponseRequest, ResponseTransport, AuthenticatedResponseRequest, AuthRefreshReason, AuthRefreshHandler, } from './HttpService';
27
+ export type { RequestOptions, ResponsePayload, ResponseRequest, ResponseTransport, AuthenticatedResponseRequest, AuthRefreshReason, AuthRefreshHandler, CredentialGeneration, } from './HttpService';
28
+ export { sameHolderIdentity } from './api/holderEvidence';
29
+ export type { HolderEvidence, HolderEvidenceIdentity, HolderEvidenceResult, HolderEvidenceUnavailableReason, } from './api/holderEvidence';
28
30
  export type { SessionApi, DeviceCredentialProvider, SessionValidation } from './api/session';
29
31
  export type { AuthApi, ChallengeResponse, RegistrationRequest, ChallengeVerifyRequest, PublicKeyCheckResponse, OAuthUserInfoResponse, OAuthTokenExchangeResult, CommonsSignInHandle, CommonsSignInStatus, CommonsSignInPurpose, CommonsOAuthContext, CommonsApprovalInfo, CommonsApprovalSubjectAccount, CommonsSignInActionResult, CommonsOAuthFinalizeResult, CommonsDeliveryResult, SignInDeviceOptions, ClaimedSession, } from './api/auth';
30
32
  export type { UsersApi, ResolveExternalUserInput, DeleteAccountProof } from './api/users';
@@ -17,7 +17,7 @@ export declare class ServerSafetyApi extends SafetyApi {
17
17
  /** The original personal bearer, a verified MCP connection proof, or explicit anonymous access. */
18
18
  profileAccess(input: ProfileAccessInput, options?: SafetyRequester): Promise<{
19
19
  allowed: boolean;
20
- reason: "blocked" | "private" | "public" | "owner" | "unavailable";
20
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner";
21
21
  policyVersion: "1";
22
22
  }>;
23
23
  profileAccessBatch(input: ProfileAccessBatchInput, options: SafetyRequester): Promise<{
@@ -25,14 +25,14 @@ export declare class ServerSafetyApi extends SafetyApi {
25
25
  subjectAccountId: string;
26
26
  decision: {
27
27
  allowed: boolean;
28
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
28
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
29
29
  policyVersion: "2";
30
30
  };
31
31
  }[];
32
32
  }>;
33
33
  messageAccess(input: MessageAccessInput, options: SafetyRequester): Promise<{
34
34
  allowed: boolean;
35
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
35
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
36
36
  policyVersion: "2";
37
37
  }>;
38
38
  messageAccessBatch(input: MessageAccessBatchInput, options: SafetyRequester): Promise<{
@@ -40,7 +40,7 @@ export declare class ServerSafetyApi extends SafetyApi {
40
40
  resourceId: string;
41
41
  decision: {
42
42
  allowed: boolean;
43
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
43
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
44
44
  policyVersion: "2";
45
45
  };
46
46
  }[];
@@ -60,7 +60,7 @@ export declare class ServerSafetyApi extends SafetyApi {
60
60
  resourceId: string;
61
61
  decision: {
62
62
  allowed: boolean;
63
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
63
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
64
64
  policyVersion: "2";
65
65
  };
66
66
  }[];
@@ -70,7 +70,7 @@ export declare class ServerSafetyApi extends SafetyApi {
70
70
  targetAccountId: string;
71
71
  decision: {
72
72
  allowed: boolean;
73
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
73
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
74
74
  policyVersion: "2";
75
75
  };
76
76
  }[];
@@ -96,7 +96,7 @@ export declare class ServerSafetyApi extends SafetyApi {
96
96
  }>;
97
97
  contentAccess(input: ContentAccessInput, options: SafetyRequester): Promise<{
98
98
  allowed: boolean;
99
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
99
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
100
100
  policyVersion: "2";
101
101
  }>;
102
102
  contentAccessBatch(input: ContentAccessBatchInput, options: SafetyRequester): Promise<{
@@ -104,14 +104,14 @@ export declare class ServerSafetyApi extends SafetyApi {
104
104
  resourceId: string;
105
105
  decision: {
106
106
  allowed: boolean;
107
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
107
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
108
108
  policyVersion: "2";
109
109
  };
110
110
  }[];
111
111
  }>;
112
112
  deliveryAccess(input: DeliveryAccessInput): Promise<{
113
113
  allowed: boolean;
114
- reason: "blocked" | "private" | "public" | "owner" | "unavailable" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
114
+ reason: "blocked" | "private" | "public" | "unavailable" | "owner" | "allowed" | "unsupported_consumer" | "contact_not_approved" | "mature_content" | "unrated_content" | "approval_required";
115
115
  policyVersion: "2";
116
116
  }>;
117
117
  requestPurchaseApproval(input: PurchaseEffects, options: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxy.so/core",
3
- "version": "7.11.1",
3
+ "version": "7.12.0",
4
4
  "description": "OxyHQ SDK Foundation — API client, authentication, cryptographic identity, and shared utilities",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -181,7 +181,7 @@
181
181
  "dependencies": {
182
182
  "@noble/ciphers": "^1.3.0",
183
183
  "@noble/hashes": "^1.8.0",
184
- "@oxy.so/contracts": "6.3.0",
184
+ "@oxy.so/contracts": "6.4.0",
185
185
  "@oxy.so/protocol": "^1.5.1",
186
186
  "@oxy.so/telemetry": "^2.1.0",
187
187
  "@scure/bip39": "^1.6.0",
@@ -56,6 +56,21 @@ export type AccessTokenProvider = () => string | null;
56
56
  */
57
57
  export type AnonymousAuthProvider = () => Promise<string | null>;
58
58
 
59
+ declare const credentialGenerationBrand: unique symbol;
60
+ /**
61
+ * An opaque, immutable token for ONE installation of a bearer in a client.
62
+ *
63
+ * `TokenStore` swaps it on every write path (setter, refresh, account or actor
64
+ * switch, cross-tab or restored token, clear), so two installations of the same
65
+ * bytes (A, B, A) are different generations. It names a local sequence only: it
66
+ * is not evidence of an account, a scope or authorization.
67
+ */
68
+ export type CredentialGeneration = { readonly [credentialGenerationBrand]: true };
69
+
70
+ function newCredentialGeneration(): CredentialGeneration {
71
+ return Object.freeze({}) as CredentialGeneration;
72
+ }
73
+
59
74
  /** Comparison only fences local async work; it never validates token authority. */
60
75
  function sameTokenSession(left: string | null, right: string | null): boolean {
61
76
  if (left === right) return true;
@@ -190,6 +205,13 @@ interface FormDataLike {
190
205
  }
191
206
 
192
207
  export interface RequestOptions {
208
+ /**
209
+ * Bind a write to ONE installation of the source's bearer. The transport
210
+ * rejects (`AUTH_SESSION_CHANGED`) when the generation was replaced before
211
+ * dispatch, after queueing, or before the result is returned. Implies no cache,
212
+ * deduplication or retry, and a 401 neither refreshes nor clears the session.
213
+ */
214
+ expectedCredentialGeneration?: CredentialGeneration;
193
215
  cache?: boolean;
194
216
  cacheTTL?: number;
195
217
  deduplicate?: boolean;
@@ -434,16 +456,29 @@ const CACHE_SIZE_WARNING_THROTTLE_MS = 60000;
434
456
  */
435
457
  class TokenStore {
436
458
  private accessToken: string | null = null;
459
+ private generation: CredentialGeneration = newCredentialGeneration();
437
460
 
438
461
  setTokens(accessToken: string): void {
462
+ this.generation = newCredentialGeneration();
439
463
  this.accessToken = accessToken;
440
464
  }
441
465
 
466
+ /** The identity of the bearer's current installation, never of its bytes. */
467
+ getGeneration(): CredentialGeneration {
468
+ return this.generation;
469
+ }
470
+
471
+ /** Retire the current generation without touching the bearer (provider swaps). */
472
+ invalidateGeneration(): void {
473
+ this.generation = newCredentialGeneration();
474
+ }
475
+
442
476
  getAccessToken(): string | null {
443
477
  return this.accessToken;
444
478
  }
445
479
 
446
480
  clearTokens(): void {
481
+ this.generation = newCredentialGeneration();
447
482
  this.accessToken = null;
448
483
  }
449
484
 
@@ -576,6 +611,7 @@ export class HttpService {
576
611
  * bearer — and through the token listeners, the account — back.
577
612
  */
578
613
  private sessionEpoch = 0;
614
+ private readonly credentialSource: HttpService | null;
579
615
  /** Preserve a known device across legacy rotations which omit the claim. */
580
616
  private sessionDeviceId: string | null = null;
581
617
  /** Preserve known actor and context hints across rotations without those claims. */
@@ -618,8 +654,9 @@ export class HttpService {
618
654
  averageResponseTime: 0,
619
655
  };
620
656
 
621
- constructor(config: OxyConfig) {
657
+ constructor(config: OxyConfig, credentialSource: HttpService | null = null) {
622
658
  this.config = config;
659
+ this.credentialSource = credentialSource;
623
660
  this.baseURL = config.baseURL;
624
661
  this.tokenStore = new TokenStore();
625
662
 
@@ -760,6 +797,21 @@ export class HttpService {
760
797
  */
761
798
  async request<T = unknown>(config: RequestConfig): Promise<ResponsePayload<T>> {
762
799
  if (config.signal?.aborted) throw createCancelledError('Request cancelled before it was sent');
800
+ if (config.expectedCredentialGeneration) {
801
+ // The prototype is deliberately limited to writes; it does not
802
+ // yet define leases for cached/deduplicated reads or raw response paths.
803
+ if (
804
+ !['POST', 'PUT', 'PATCH', 'DELETE'].includes(config.method) ||
805
+ config.skipAuth ||
806
+ Object.keys(config.headers ?? {}).some((key) => key.toLowerCase() === 'authorization')
807
+ ) {
808
+ throw new Error('A credential generation requires an implicitly authenticated write');
809
+ }
810
+ (this.credentialSource ?? this).assertCredentialGeneration(
811
+ config.expectedCredentialGeneration,
812
+ );
813
+ config = { ...config, cache: false, deduplicate: false, retry: false };
814
+ }
763
815
  // The provider can change independently of the last request. Synchronize
764
816
  // before deriving the cache and deduplication identity, including hits.
765
817
  const explicitAuthorization = Object.keys(config.headers ?? {}).some(
@@ -878,6 +930,15 @@ export class HttpService {
878
930
  const authHeader = usesSessionAuth
879
931
  ? await awaitRequestSignal(this.getAuthHeader(), signal)
880
932
  : null;
933
+ const assertExpectedCredential = () => {
934
+ if (config.expectedCredentialGeneration) {
935
+ (this.credentialSource ?? this).assertCredentialGeneration(
936
+ config.expectedCredentialGeneration,
937
+ authHeader,
938
+ );
939
+ }
940
+ };
941
+ assertExpectedCredential();
881
942
  const isOriginalSession = () => {
882
943
  if (this.sessionEpoch !== contextEpoch) return false;
883
944
  const currentToken = this.syncAccessTokenFromProvider();
@@ -929,6 +990,7 @@ export class HttpService {
929
990
  if (usesSessionAuth && !isOriginalSession()) {
930
991
  throw new OxyAuthenticationError('The request session changed', 'AUTH_SESSION_CHANGED');
931
992
  }
993
+ assertExpectedCredential();
932
994
 
933
995
  /**
934
996
  * Whether OUR timer fired, so a timeout can be told apart from a
@@ -1042,6 +1104,9 @@ export class HttpService {
1042
1104
  throw new Error('Requester proofs require a transport that rejects redirects');
1043
1105
  }
1044
1106
 
1107
+ // Header and source generation are checked together in this synchronous
1108
+ // dispatch segment, after the preflight and any queue wait.
1109
+ assertExpectedCredential();
1045
1110
  const response = useXhrForUpload
1046
1111
  ? await this.uploadViaXHR(
1047
1112
  fullUrl,
@@ -1065,6 +1130,13 @@ export class HttpService {
1065
1130
  // it, including on every throw between here and there.
1066
1131
  if (timeoutId) clearTimeout(timeoutId);
1067
1132
 
1133
+ try {
1134
+ assertExpectedCredential();
1135
+ } catch (error) {
1136
+ void discardResponseBody(response);
1137
+ throw error;
1138
+ }
1139
+
1068
1140
  if (usesSessionAuth && !isOriginalSession()) {
1069
1141
  void discardResponseBody(response);
1070
1142
  throw new OxyAuthenticationError('The request session changed', 'AUTH_SESSION_CHANGED');
@@ -1077,6 +1149,7 @@ export class HttpService {
1077
1149
  // session routes; the refresh handler owns session rotation.
1078
1150
  if (
1079
1151
  response.status === 401 &&
1152
+ !config.expectedCredentialGeneration &&
1080
1153
  !config._isAuthRetry &&
1081
1154
  usesSessionAuth &&
1082
1155
  isOriginalSession()
@@ -1218,12 +1291,14 @@ export class HttpService {
1218
1291
  if (usesSessionAuth && !isOriginalSession()) {
1219
1292
  throw new OxyAuthenticationError('The request session changed', 'AUTH_SESSION_CHANGED');
1220
1293
  }
1294
+ assertExpectedCredential();
1221
1295
  const duration = Date.now() - startTime;
1222
1296
  this.updateMetrics(true, duration);
1223
1297
  this.config.onRequestEnd?.(url, method, duration, true);
1224
1298
 
1225
1299
  return responseData as T;
1226
1300
  } catch (error: unknown) {
1301
+ assertExpectedCredential();
1227
1302
  const duration = Date.now() - startTime;
1228
1303
  this.updateMetrics(false, duration);
1229
1304
  // A body reader can reject or finish parsing an error after the viewer
@@ -1348,6 +1423,7 @@ export class HttpService {
1348
1423
  if (usesSessionAuth && !isOriginalSession()) {
1349
1424
  throw new OxyAuthenticationError('The request session changed', 'AUTH_SESSION_CHANGED');
1350
1425
  }
1426
+ assertExpectedCredential();
1351
1427
 
1352
1428
  // Cache the result if caching is enabled
1353
1429
  if (cache && cacheKey && lease.valid && result) {
@@ -2192,6 +2268,36 @@ export class HttpService {
2192
2268
  return this.sessionEpoch;
2193
2269
  }
2194
2270
 
2271
+ /** Capture only the local generation; the caller must separately bind authority. */
2272
+ captureCredentialGeneration(): CredentialGeneration | null {
2273
+ // A pull-only provider can change A→B→A between reads without notifying us.
2274
+ if (this.accessTokenProvider) return null;
2275
+ return this.tokenStore.getAccessToken() ? this.tokenStore.getGeneration() : null;
2276
+ }
2277
+
2278
+ isCredentialGenerationCurrent(generation: CredentialGeneration): boolean {
2279
+ return (
2280
+ !this.accessTokenProvider &&
2281
+ this.tokenStore.getAccessToken() !== null &&
2282
+ generation === this.tokenStore.getGeneration()
2283
+ );
2284
+ }
2285
+
2286
+ private assertCredentialGeneration(
2287
+ generation: CredentialGeneration,
2288
+ selectedHeader?: string | null,
2289
+ ): void {
2290
+ const token = this.tokenStore.getAccessToken();
2291
+ if (
2292
+ this.accessTokenProvider ||
2293
+ !token ||
2294
+ generation !== this.tokenStore.getGeneration() ||
2295
+ (selectedHeader !== undefined && selectedHeader !== `Bearer ${token}`)
2296
+ ) {
2297
+ throw new OxyAuthenticationError('The request credential changed', 'AUTH_SESSION_CHANGED');
2298
+ }
2299
+ }
2300
+
2195
2301
  /** Whether {@link endSession} ran and no token has been planted since. */
2196
2302
  hasSessionEnded(): boolean {
2197
2303
  return this.sessionEnded;
@@ -2202,6 +2308,7 @@ export class HttpService {
2202
2308
  }
2203
2309
 
2204
2310
  setAccessTokenProvider(provider: AccessTokenProvider | null): void {
2311
+ this.tokenStore.invalidateGeneration();
2205
2312
  this.accessTokenProvider = provider;
2206
2313
  }
2207
2314
 
@@ -159,7 +159,10 @@ export class OxyServices {
159
159
  * Query). Pass `enableCache: true` to opt in.
160
160
  */
161
161
  createLinkedClient(config: OxyConfig): LinkedHttpClient {
162
- const client = new HttpService({ ...config, enableCache: config.enableCache ?? false });
162
+ const client = new HttpService(
163
+ { ...config, enableCache: config.enableCache ?? false },
164
+ this.http,
165
+ );
163
166
 
164
167
  const syncToken = (accessToken: string | null): void => {
165
168
  const current = client.getAccessToken();