Skip to content

Commit e0ffe69

Browse files
fix(client-runtime): fall back to the bare ticket path on older servers
A server that predates ticket audiences has no /:use route and answers 404. The client now retries the bare path there, with its own proof for the DPoP case since the proof names the path. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 890d9d0 commit e0ffe69

2 files changed

Lines changed: 89 additions & 14 deletions

File tree

‎packages/client-runtime/src/authorization/remote.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,37 @@ describe("remote environment authorization", () => {
251251
}),
252252
);
253253

254+
it.effect("falls back to the bare ticket path on a server without the audience route", () =>
255+
Effect.gen(function* () {
256+
const fetch = recordedFetch(
257+
Response.json({ message: "Not Found" }, { status: 404 }),
258+
Response.json({ ticket: "old-ticket", expiresAt: "2026-05-01T12:05:00.000Z" }),
259+
);
260+
const proofs: Array<{ method: string; url: string; accessToken: string }> = [];
261+
262+
const url = yield* resolveRemoteDpopWebSocketConnectionUrl({
263+
wsBaseUrl: "wss://remote.example.com/",
264+
httpBaseUrl: "https://remote.example.com/",
265+
accessToken: "dpop-access-token",
266+
createProof: (input) => {
267+
proofs.push(input);
268+
return Effect.succeed(`proof-${proofs.length}`);
269+
},
270+
}).pipe(provideRemoteHttp(fetch.fetchFn));
271+
272+
expect(fetch.calls.map(([target]) => String(target))).toEqual([
273+
"https://remote.example.com/api/auth/websocket-ticket/rpc",
274+
"https://remote.example.com/api/auth/websocket-ticket",
275+
]);
276+
expect(proofs.map((proof) => proof.url)).toEqual([
277+
"https://remote.example.com/api/auth/websocket-ticket/rpc",
278+
"https://remote.example.com/api/auth/websocket-ticket",
279+
"https://remote.example.com/ws",
280+
]);
281+
expect(url).toContain("wsTicket=old-ticket");
282+
}),
283+
);
284+
254285
it.effect("names the signed-in Connect account and this device by hash only", () =>
255286
Effect.gen(function* () {
256287
const fetch = recordedFetch(

‎packages/client-runtime/src/authorization/remote.ts‎

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
AuthTokenExchangeGrantType,
66
type ClientConnectionMethod,
77
type AuthEnvironmentScope,
8+
type AuthWebSocketTicketResult,
89
type AuthWebSocketTicketUse,
910
} from "@t3tools/contracts";
1011
import { encodeOAuthScope } from "@t3tools/shared/oauthScope";
@@ -16,6 +17,7 @@ import * as ClientCapabilities from "../platform/capabilities.ts";
1617
import {
1718
executeEnvironmentHttpRequest,
1819
makeEnvironmentHttpApiGroupClient,
20+
RemoteEnvironmentAuthUndeclaredStatusError,
1921
type RemoteEnvironmentRequestError,
2022
} from "../rpc/http.ts";
2123

@@ -186,6 +188,27 @@ export const fetchRemoteSessionState = Effect.fn(
186188
const webSocketTicketPath = (use: AuthWebSocketTicketUse | undefined) =>
187189
use === undefined ? "/api/auth/websocket-ticket" : `/api/auth/websocket-ticket/${use}`;
188190

191+
/**
192+
* A server that predates ticket audiences has no `/:use` route and answers 404.
193+
* Its tickets are the unbound kind it always issued, so the bare path is the
194+
* right request there, not an error.
195+
*/
196+
const issueWebSocketTicketWithFallback = <E, R>(
197+
use: AuthWebSocketTicketUse | undefined,
198+
issue: (
199+
use: AuthWebSocketTicketUse | undefined,
200+
) => Effect.Effect<AuthWebSocketTicketResult, E, R>,
201+
) =>
202+
use === undefined
203+
? issue(undefined)
204+
: issue(use).pipe(
205+
Effect.catchIf(
206+
(error) =>
207+
error instanceof RemoteEnvironmentAuthUndeclaredStatusError && error.status === 404,
208+
() => issue(undefined),
209+
),
210+
);
211+
189212
export const issueRemoteWebSocketTicket = Effect.fn(
190213
"clientRuntime.authorization.issueRemoteWebSocketTicket",
191214
)(function* (input: {
@@ -196,32 +219,46 @@ export const issueRemoteWebSocketTicket = Effect.fn(
196219
}) {
197220
const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
198221
const headers = { authorization: `Bearer ${input.bearerToken}` };
199-
return yield* executeEnvironmentHttpRequest(
200-
environmentEndpointUrl(input.httpBaseUrl, webSocketTicketPath(input.use)),
201-
input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
202-
input.use === undefined
203-
? client.webSocketTicket({ headers })
204-
: client.webSocketTicketFor({ headers, params: { use: input.use } }),
222+
return yield* issueWebSocketTicketWithFallback(input.use, (use) =>
223+
executeEnvironmentHttpRequest(
224+
environmentEndpointUrl(input.httpBaseUrl, webSocketTicketPath(use)),
225+
input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
226+
use === undefined
227+
? client.webSocketTicket({ headers })
228+
: client.webSocketTicketFor({ headers, params: { use } }),
229+
),
205230
);
206231
});
207232

208233
export const issueRemoteDpopWebSocketTicket = Effect.fn(
209234
"clientRuntime.authorization.issueRemoteDpopWebSocketTicket",
210-
)(function* (input: {
235+
)(function* <E = never>(input: {
211236
readonly httpBaseUrl: string;
212237
readonly accessToken: string;
213238
readonly dpopProof: string;
214239
readonly use?: AuthWebSocketTicketUse;
240+
/** Proof for the bare path, used only when the server lacks the `/:use` route. */
241+
readonly fallbackProof?: Effect.Effect<string, E>;
215242
readonly timeoutMs?: number;
216243
}) {
217244
const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
218-
const headers = { authorization: `DPoP ${input.accessToken}`, dpop: input.dpopProof };
219-
return yield* executeEnvironmentHttpRequest(
220-
environmentEndpointUrl(input.httpBaseUrl, webSocketTicketPath(input.use)),
221-
input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
222-
input.use === undefined
223-
? client.webSocketTicket({ headers })
224-
: client.webSocketTicketFor({ headers, params: { use: input.use } }),
245+
const fallbackProof = input.fallbackProof;
246+
return yield* issueWebSocketTicketWithFallback(
247+
fallbackProof === undefined ? undefined : input.use,
248+
(use) =>
249+
Effect.gen(function* () {
250+
// The proof names the path it signs, so the fallback request needs its own.
251+
const dpopProof =
252+
use === input.use || fallbackProof === undefined ? input.dpopProof : yield* fallbackProof;
253+
const headers = { authorization: `DPoP ${input.accessToken}`, dpop: dpopProof };
254+
return yield* executeEnvironmentHttpRequest(
255+
environmentEndpointUrl(input.httpBaseUrl, webSocketTicketPath(use)),
256+
input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
257+
use === undefined
258+
? client.webSocketTicket({ headers })
259+
: client.webSocketTicketFor({ headers, params: { use } }),
260+
);
261+
}),
225262
);
226263
});
227264

@@ -291,6 +328,13 @@ export const resolveRemoteDpopWebSocketConnectionUrl = Effect.fn(
291328
accessToken: input.accessToken,
292329
dpopProof: ticketProof,
293330
use: "rpc",
331+
fallbackProof: Effect.suspend(() =>
332+
input.createProof({
333+
method: "POST",
334+
url: environmentEndpointUrl(input.httpBaseUrl, webSocketTicketPath(undefined)),
335+
accessToken: input.accessToken,
336+
}),
337+
),
294338
...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
295339
});
296340
const url = rpcSocketUrl(input.wsBaseUrl);

0 commit comments

Comments
 (0)