55 AuthTokenExchangeGrantType ,
66 type ClientConnectionMethod ,
77 type AuthEnvironmentScope ,
8+ type AuthWebSocketTicketResult ,
89 type AuthWebSocketTicketUse ,
910} from "@t3tools/contracts" ;
1011import { encodeOAuthScope } from "@t3tools/shared/oauthScope" ;
@@ -16,6 +17,7 @@ import * as ClientCapabilities from "../platform/capabilities.ts";
1617import {
1718 executeEnvironmentHttpRequest ,
1819 makeEnvironmentHttpApiGroupClient ,
20+ RemoteEnvironmentAuthUndeclaredStatusError ,
1921 type RemoteEnvironmentRequestError ,
2022} from "../rpc/http.ts" ;
2123
@@ -186,6 +188,27 @@ export const fetchRemoteSessionState = Effect.fn(
186188const 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+
189212export 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
208233export 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