Open History to see the last commit
1import {
2 RelayAccessTokenType,
3 RelayApi,
4 type RelayClientEnvironmentRecord,
5 type RelayClientDeviceRecord,
6 RelayConnectEnvironmentEndpoint,
7 type RelayDeviceRegistrationRequest,
8 RelayDpopAccessTokenScope,
9 RelayDpopTokenExchangeGrantType,
10 type RelayEnvironmentConnectRequest,
11 type RelayEnvironmentConnectResponse,
12 type RelayEnvironmentLinkChallengeRequest,
13 type RelayEnvironmentLinkChallengeResponse,
14 type RelayEnvironmentLinkRequest,
15 type RelayEnvironmentLinkResponse,
16 type RelayEnvironmentStatusResponse,
17 RelayExchangeDpopAccessTokenEndpoint,
18 RelayGetEnvironmentStatusEndpoint,
19 RelayJwtSubjectTokenType,
20 type RelayAgentActivitySnapshotResponse,
21 type RelayLiveActivityRegistrationRequest,
22 RelayMobileRegistrationScope,
23 type RelayOkResponse,
24 type RelayPublicClientId,
25 type RelayT3ChatLinkRequestResponse,
26 type RelayT3ChatLinkResponse,
27 RelayRegisterDeviceEndpoint,
28 RelayAgentActivitySnapshotEndpoint,
29 RelayRegisterLiveActivityEndpoint,
30 RelayProtectedError,
31 type RelayProtectedError as RelayProtectedErrorType,
32 RelayUnregisterDeviceEndpoint,
33} from "@t3tools/contracts/relay";
34import { encodeOAuthScope, oauthScopeSetEquals } from "@t3tools/shared/oauthScope";
35import { decodeRelayJwt } from "@t3tools/shared/relayJwt";
36import { withRelayClientTracing } from "@t3tools/shared/relayTracing";
37import { normalizeSecureRelayUrl } from "@t3tools/shared/relayUrl";
38import * as Clock from "effect/Clock";
39import * as Context from "effect/Context";
40import * as Duration from "effect/Duration";
41import * as Effect from "effect/Effect";
42import * as Layer from "effect/Layer";
43import * as Option from "effect/Option";
44import * as Schema from "effect/Schema";
45import * as SynchronizedRef from "effect/SynchronizedRef";
46import * as HttpClientError from "effect/http/HttpClientError";
47import type * as HttpMethod from "effect/http/HttpMethod";
48import * as HttpApiClient from "effect/http-api/HttpApiClient";
49import { NETWORK_BLOCKING_HINT } from "../errors/network.ts";
50
51export interface ManagedRelayDpopProofInput {
52 readonly method: HttpMethod.HttpMethod;
53 readonly url: string;
54 readonly accessToken?: string;
55}
56
57export class ManagedRelayDpopKeyLoadError extends Schema.TaggedError<ManagedRelayDpopKeyLoadError>()(
58 "ManagedRelayDpopKeyLoadError",
59 {
60 keyStore: Schema.Literals(["expo-secure-store", "indexed-db"]),
61 cause: Schema.Defect(),
62 },
63) {
64 override get message(): string {
65 return "Could not load relay DPoP proof key.";
66 }
67}
68
69export class ManagedRelayDpopProofCreationError extends Schema.TaggedError<ManagedRelayDpopProofCreationError>()(
70 "ManagedRelayDpopProofCreationError",
71 {
72 method: Schema.String,
73 url: Schema.String,
74 cause: Schema.Defect(),
75 },
76) {
77 override get message(): string {
78 return `Could not create the relay DPoP proof for ${this.method} ${this.url}.`;
79 }
80}
81
82export const ManagedRelayDpopSignerError = Schema.Union([
83 ManagedRelayDpopKeyLoadError,
84 ManagedRelayDpopProofCreationError,
85]);
86export type ManagedRelayDpopSignerError = typeof ManagedRelayDpopSignerError.Type;
87
88export const ManagedRelayRequestAction = Schema.Literals([
89 "exchange relay DPoP access token",
90 "list relay-managed environments",
91 "list relay client devices",
92 "create relay environment link challenge",
93 "link relay environment",
94 "unlink relay environment",
95 "get relay environment status",
96 "connect relay environment",
97 "register relay mobile device",
98 "unregister relay mobile device",
99 "register relay live activity",
100 "read relay agent activity snapshot",
101 "read linked T3 Chat account",
102 "start T3 Chat linking",
103 "unlink T3 Chat account",
104]);
105export type ManagedRelayRequestAction = typeof ManagedRelayRequestAction.Type;
106
107export const ManagedRelayRequestActivity = Schema.Literals([
108 "Relay DPoP access token exchange",
109 "Relay environment listing",
110 "Relay client device listing",
111 "Relay environment link challenge",
112 "Relay environment linking",
113 "Relay environment unlinking",
114 "Relay environment status request",
115 "Relay environment connection",
116 "Relay mobile device registration",
117 "Relay mobile device unregistration",
118 "Relay Live Activity registration",
119 "Relay agent activity snapshot",
120 "Relay T3 Chat link lookup",
121 "Relay T3 Chat link request",
122 "Relay T3 Chat unlinking",
123]);
124export type ManagedRelayRequestActivity = typeof ManagedRelayRequestActivity.Type;
125
126export class ManagedRelayRequestTimeoutError extends Schema.TaggedError<ManagedRelayRequestTimeoutError>()(
127 "ManagedRelayRequestTimeoutError",
128 {
129 activity: ManagedRelayRequestActivity,
130 timeoutMs: Schema.Number,
131 traceId: Schema.NullOr(Schema.String),
132 },
133) {
134 override get message(): string {
135 return `${this.activity} timed out. ${NETWORK_BLOCKING_HINT}`;
136 }
137}
138
139export class ManagedRelayUrlInvalidError extends Schema.TaggedError<ManagedRelayUrlInvalidError>()(
140 "ManagedRelayUrlInvalidError",
141 {
142 relayUrl: Schema.String,
143 },
144) {
145 override get message(): string {
146 return "Relay URL must be a secure absolute HTTPS origin.";
147 }
148}
149
150export class ManagedRelayRequestFailedError extends Schema.TaggedError<ManagedRelayRequestFailedError>()(
151 "ManagedRelayRequestFailedError",
152 {
153 action: ManagedRelayRequestAction,
154 transportFailed: Schema.optionalKey(Schema.Boolean),
155 cause: Schema.Defect(),
156 relayError: Schema.optional(RelayProtectedError),
157 status: Schema.optional(Schema.Number),
158 traceId: Schema.optional(Schema.String),
159 },
160) {
161 override get message(): string {
162 const message = `Could not ${this.action}.`;
163 return this.transportFailed ? `${message} ${NETWORK_BLOCKING_HINT}` : message;
164 }
165}
166
167export class ManagedRelayAccessTokenScopesUnexpectedError extends Schema.TaggedError<ManagedRelayAccessTokenScopesUnexpectedError>()(
168 "ManagedRelayAccessTokenScopesUnexpectedError",
169 {
170 requestedScopes: Schema.Array(RelayDpopAccessTokenScope),
171 grantedScope: Schema.String,
172 },
173) {
174 override get message(): string {
175 return "Relay granted unexpected DPoP access token scopes.";
176 }
177}
178
179export class ManagedRelayTokenProofCreationError extends Schema.TaggedError<ManagedRelayTokenProofCreationError>()(
180 "ManagedRelayTokenProofCreationError",
181 {
182 method: Schema.String,
183 url: Schema.String,
184 cause: Schema.Defect(),
185 },
186) {
187 override get message(): string {
188 return "Could not create relay token DPoP proof.";
189 }
190}
191
192export class ManagedRelayRequestProofCreationError extends Schema.TaggedError<ManagedRelayRequestProofCreationError>()(
193 "ManagedRelayRequestProofCreationError",
194 {
195 method: Schema.String,
196 url: Schema.String,
197 cause: Schema.Defect(),
198 },
199) {
200 override get message(): string {
201 return "Could not create relay request DPoP proof.";
202 }
203}
204
205export const ManagedRelayClientError = Schema.Union([
206 ManagedRelayUrlInvalidError,
207 ManagedRelayRequestFailedError,
208 ManagedRelayRequestTimeoutError,
209 ManagedRelayAccessTokenScopesUnexpectedError,
210 ManagedRelayDpopKeyLoadError,
211 ManagedRelayTokenProofCreationError,
212 ManagedRelayRequestProofCreationError,
213]);
214export type ManagedRelayClientError = typeof ManagedRelayClientError.Type;
215
216type RelayHttpRequestError =
217 | RelayProtectedErrorType
218 | HttpClientError.HttpClientError
219 | Schema.SchemaError;
220
221export class ManagedRelayDpopSigner extends Context.Service<
222 ManagedRelayDpopSigner,
223 {
224 readonly thumbprint: Effect.Effect<string, ManagedRelayDpopKeyLoadError>;
225 readonly createProof: (
226 input: ManagedRelayDpopProofInput,
227 ) => Effect.Effect<string, ManagedRelayDpopProofCreationError>;
228 }
229>()("@t3tools/client-runtime/relay/managedRelay/ManagedRelayDpopSigner") {}
230
231export const MANAGED_RELAY_REQUEST_TIMEOUT_MS = 10_000;
232
233export interface ManagedRelayAccessTokenCacheEntry {
234 readonly accountId: string;
235 readonly clientId: RelayPublicClientId;
236 readonly relayUrl: string;
237 readonly thumbprint: string;
238 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
239 readonly accessToken: string;
240 readonly expiresAtMillis: number;
241}
242
243export interface ManagedRelayAccessTokenStore {
244 readonly load: Effect.Effect<ReadonlyArray<ManagedRelayAccessTokenCacheEntry>>;
245 readonly save: (entries: ReadonlyArray<ManagedRelayAccessTokenCacheEntry>) => Effect.Effect<void>;
246 readonly clear: Effect.Effect<void>;
247}
248
249export interface ManagedRelayAuthorization {
250 readonly accessToken: string;
251 readonly proof: string;
252 readonly thumbprint: string;
253}
254
255export interface ManagedRelayClientLayerOptions {
256 readonly relayUrl: string;
257 readonly clientId: RelayPublicClientId;
258 readonly accessTokenStore?: ManagedRelayAccessTokenStore;
259}
260
261export class ManagedRelayClient extends Context.Service<
262 ManagedRelayClient,
263 {
264 readonly relayUrl: string;
265 readonly listEnvironments: (input: {
266 readonly clerkToken: string;
267 }) => Effect.Effect<ReadonlyArray<RelayClientEnvironmentRecord>, ManagedRelayClientError>;
268 readonly listDevices: (input: {
269 readonly clerkToken: string;
270 }) => Effect.Effect<ReadonlyArray<RelayClientDeviceRecord>, ManagedRelayClientError>;
271 readonly createEnvironmentLinkChallenge: (input: {
272 readonly clerkToken: string;
273 readonly payload: RelayEnvironmentLinkChallengeRequest;
274 }) => Effect.Effect<RelayEnvironmentLinkChallengeResponse, ManagedRelayClientError>;
275 readonly linkEnvironment: (input: {
276 readonly clerkToken: string;
277 readonly payload: RelayEnvironmentLinkRequest;
278 }) => Effect.Effect<RelayEnvironmentLinkResponse, ManagedRelayClientError>;
279 readonly unlinkEnvironment: (input: {
280 readonly clerkToken: string;
281 readonly environmentId: RelayClientEnvironmentRecord["environmentId"];
282 }) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
283 readonly getT3ChatLink: (input: {
284 readonly clerkToken: string;
285 }) => Effect.Effect<RelayT3ChatLinkResponse, ManagedRelayClientError>;
286 readonly createT3ChatLinkRequest: (input: {
287 readonly clerkToken: string;
288 }) => Effect.Effect<RelayT3ChatLinkRequestResponse, ManagedRelayClientError>;
289 readonly unlinkT3Chat: (input: {
290 readonly clerkToken: string;
291 }) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
292 readonly getEnvironmentStatus: (input: {
293 readonly clerkToken: string;
294 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
295 readonly environmentId: RelayClientEnvironmentRecord["environmentId"];
296 }) => Effect.Effect<RelayEnvironmentStatusResponse, ManagedRelayClientError>;
297 readonly connectEnvironment: (input: {
298 readonly clerkToken: string;
299 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
300 readonly environmentId: RelayClientEnvironmentRecord["environmentId"];
301 readonly deviceId?: string;
302 }) => Effect.Effect<RelayEnvironmentConnectResponse, ManagedRelayClientError>;
303 readonly registerDevice: (input: {
304 readonly clerkToken: string;
305 readonly payload: RelayDeviceRegistrationRequest;
306 }) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
307 readonly unregisterDevice: (input: {
308 readonly clerkToken: string;
309 readonly deviceId: string;
310 }) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
311 readonly registerLiveActivity: (input: {
312 readonly clerkToken: string;
313 readonly payload: RelayLiveActivityRegistrationRequest;
314 }) => Effect.Effect<RelayOkResponse, ManagedRelayClientError>;
315 readonly getAgentActivitySnapshot: (input: {
316 readonly clerkToken: string;
317 }) => Effect.Effect<RelayAgentActivitySnapshotResponse, ManagedRelayClientError>;
318 readonly resetTokenCache: Effect.Effect<void>;
319 }
320>()("@t3tools/client-runtime/relay/managedRelay/ManagedRelayClient") {}
321
322const isRelayProtectedError = Schema.is(RelayProtectedError);
323
324const TRACEPARENT_TRACE_ID = /^[\da-f]{2}-([\da-f]{32})-[\da-f]{16}-[\da-f]{2}$/i;
325
326function untypedResponseFields(cause: RelayHttpRequestError) {
327 const response = HttpClientError.isHttpClientError(cause) ? cause.response : undefined;
328 if (!response) return {};
329 const traceId = TRACEPARENT_TRACE_ID.exec(response.headers.traceparent ?? "")?.[1];
330 return { status: response.status, ...(traceId ? { traceId } : {}) };
331}
332
333function relayRequestError(action: ManagedRelayRequestAction) {
334 return (cause: RelayHttpRequestError): ManagedRelayClientError =>
335 new ManagedRelayRequestFailedError({
336 action,
337 transportFailed:
338 HttpClientError.isHttpClientError(cause) && cause.reason._tag === "TransportError",
339 cause,
340 ...(isRelayProtectedError(cause)
341 ? { relayError: cause, traceId: cause.traceId }
342 : untypedResponseFields(cause)),
343 });
344}
345
346function proofCreationErrorFields(error: ManagedRelayDpopProofCreationError) {
347 return {
348 method: error.method,
349 url: error.url,
350 cause: error,
351 };
352}
353
354function isRejectedDpopAccessToken(error: ManagedRelayClientError): boolean {
355 return (
356 error._tag === "ManagedRelayRequestFailedError" &&
357 error.relayError?._tag === "RelayAuthInvalidError" &&
358 error.relayError.reason === "invalid_bearer"
359 );
360}
361
362function timeoutRelayRequest(activity: ManagedRelayRequestActivity) {
363 return <A, E, R>(
364 effect: Effect.Effect<A, E, R>,
365 ): Effect.Effect<A, E | ManagedRelayClientError, R> =>
366 effect.pipe(
367 Effect.timeoutOption(Duration.millis(MANAGED_RELAY_REQUEST_TIMEOUT_MS)),
368 Effect.flatMap(
369 Option.match({
370 onNone: () =>
371 Effect.currentParentSpan.pipe(
372 Effect.map((span) => span.traceId),
373 Effect.orElseSucceed(() => null),
374 Effect.flatMap((traceId) =>
375 Effect.fail(
376 new ManagedRelayRequestTimeoutError({
377 activity,
378 timeoutMs: MANAGED_RELAY_REQUEST_TIMEOUT_MS,
379 traceId,
380 }),
381 ),
382 ),
383 ),
384 onSome: Effect.succeed,
385 }),
386 ),
387 );
388}
389
390function tokenMatches(
391 token: ManagedRelayAccessTokenCacheEntry,
392 input: {
393 readonly accountId: string;
394 readonly clientId: RelayPublicClientId;
395 readonly relayUrl: string;
396 readonly thumbprint: string;
397 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
398 readonly nowMillis: number;
399 },
400): boolean {
401 return (
402 token.accountId === input.accountId &&
403 token.clientId === input.clientId &&
404 token.relayUrl === input.relayUrl &&
405 token.thumbprint === input.thumbprint &&
406 token.expiresAtMillis > input.nowMillis + 5_000 &&
407 input.scopes.every((scope) => token.scopes.includes(scope))
408 );
409}
410
411function relayAccountId(clerkToken: string): Option.Option<string> {
412 try {
413 return Option.fromNullishOr(decodeRelayJwt(clerkToken).sub).pipe(
414 Option.filter((subject) => subject.length > 0),
415 );
416 } catch {
417 return Option.none();
418 }
419}
420
421function bearerHeaders(clerkToken: string) {
422 return { authorization: `Bearer ${clerkToken}` };
423}
424
425function dpopHeaders(authorization: ManagedRelayAuthorization) {
426 return {
427 authorization: `DPoP ${authorization.accessToken}`,
428 dpop: authorization.proof,
429 };
430}
431
432function disabledManagedRelayClient(relayUrl: string): ManagedRelayClient["Service"] {
433 const unavailable = (spanName: string) =>
434 Effect.fn(spanName)(function* () {
435 return yield* new ManagedRelayUrlInvalidError({ relayUrl });
436 });
437 return ManagedRelayClient.of({
438 relayUrl,
439 listEnvironments: unavailable("clientRuntime.managedRelay.listEnvironments"),
440 listDevices: unavailable("clientRuntime.managedRelay.listDevices"),
441 createEnvironmentLinkChallenge: unavailable(
442 "clientRuntime.managedRelay.createEnvironmentLinkChallenge",
443 ),
444 linkEnvironment: unavailable("clientRuntime.managedRelay.linkEnvironment"),
445 unlinkEnvironment: unavailable("clientRuntime.managedRelay.unlinkEnvironment"),
446 getT3ChatLink: unavailable("clientRuntime.managedRelay.getT3ChatLink"),
447 createT3ChatLinkRequest: unavailable("clientRuntime.managedRelay.createT3ChatLinkRequest"),
448 unlinkT3Chat: unavailable("clientRuntime.managedRelay.unlinkT3Chat"),
449 getEnvironmentStatus: unavailable("clientRuntime.managedRelay.getEnvironmentStatus"),
450 connectEnvironment: unavailable("clientRuntime.managedRelay.connectEnvironment"),
451 registerDevice: unavailable("clientRuntime.managedRelay.registerDevice"),
452 unregisterDevice: unavailable("clientRuntime.managedRelay.unregisterDevice"),
453 registerLiveActivity: unavailable("clientRuntime.managedRelay.registerLiveActivity"),
454 getAgentActivitySnapshot: unavailable("clientRuntime.managedRelay.getAgentActivitySnapshot"),
455 resetTokenCache: Effect.void.pipe(
456 Effect.withSpan("clientRuntime.managedRelay.resetTokenCache"),
457 ),
458 });
459}
460
461/** @public */
462export const make = Effect.fn("ManagedRelayClient.make")(function* (
463 options: ManagedRelayClientLayerOptions,
464) {
465 const relayUrl = normalizeSecureRelayUrl(options.relayUrl);
466 if (relayUrl === null) {
467 return disabledManagedRelayClient(options.relayUrl);
468 }
469 const signer = yield* ManagedRelayDpopSigner;
470 const client = yield* HttpApiClient.make(RelayApi, { baseUrl: relayUrl });
471 const initialTokens = options.accessTokenStore ? yield* options.accessTokenStore.load : [];
472 const cachedTokens = yield* SynchronizedRef.make<
473 ReadonlyArray<ManagedRelayAccessTokenCacheEntry>
474 >(initialTokens.filter((token) => token.clientId === options.clientId));
475 const urlBuilder = HttpApiClient.urlBuilder(RelayApi, { baseUrl: relayUrl });
476
477 type DpopProofTarget = Pick<ManagedRelayDpopProofInput, "method" | "url">;
478 const dpopProofTargets = {
479 exchangeAccessToken: (): DpopProofTarget => ({
480 method: RelayExchangeDpopAccessTokenEndpoint.method,
481 url: urlBuilder.token.exchangeDpopAccessToken(),
482 }),
483 getEnvironmentStatus: (
484 environmentId: RelayClientEnvironmentRecord["environmentId"],
485 ): DpopProofTarget => ({
486 method: RelayGetEnvironmentStatusEndpoint.method,
487 url: urlBuilder.dpopClient.getEnvironmentStatus({ params: { environmentId } }),
488 }),
489 connectEnvironment: (
490 environmentId: RelayClientEnvironmentRecord["environmentId"],
491 ): DpopProofTarget => ({
492 method: RelayConnectEnvironmentEndpoint.method,
493 url: urlBuilder.dpopClient.connectEnvironment({ params: { environmentId } }),
494 }),
495 registerDevice: (): DpopProofTarget => ({
496 method: RelayRegisterDeviceEndpoint.method,
497 url: urlBuilder.mobile.registerDevice(),
498 }),
499 unregisterDevice: (deviceId: string): DpopProofTarget => ({
500 method: RelayUnregisterDeviceEndpoint.method,
501 url: urlBuilder.mobile.unregisterDevice({ params: { deviceId } }),
502 }),
503 getAgentActivitySnapshot: (): DpopProofTarget => ({
504 method: RelayAgentActivitySnapshotEndpoint.method,
505 url: urlBuilder.mobile.getAgentActivitySnapshot(),
506 }),
507 registerLiveActivity: (): DpopProofTarget => ({
508 method: RelayRegisterLiveActivityEndpoint.method,
509 url: urlBuilder.mobile.registerLiveActivity(),
510 }),
511 };
512
513 const exchangeAccessToken = Effect.fn("clientRuntime.managedRelay.exchangeAccessToken")(
514 function* (input: {
515 readonly clerkToken: string;
516 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
517 }) {
518 yield* Effect.annotateCurrentSpan({
519 "relay.client_id": options.clientId,
520 "relay.scopes": input.scopes.join(" "),
521 });
522 const proof = yield* signer
523 .createProof(dpopProofTargets.exchangeAccessToken())
524 .pipe(
525 Effect.mapError(
526 (error) => new ManagedRelayTokenProofCreationError(proofCreationErrorFields(error)),
527 ),
528 );
529 const response = yield* client.token
530 .exchangeDpopAccessToken({
531 headers: { dpop: proof },
532 payload: {
533 grant_type: RelayDpopTokenExchangeGrantType,
534 subject_token: input.clerkToken,
535 subject_token_type: RelayJwtSubjectTokenType,
536 requested_token_type: RelayAccessTokenType,
537 resource: relayUrl,
538 scope: encodeOAuthScope(input.scopes),
539 client_id: options.clientId,
540 },
541 })
542 .pipe(
543 Effect.mapError(relayRequestError("exchange relay DPoP access token")),
544 timeoutRelayRequest("Relay DPoP access token exchange"),
545 );
546 if (!oauthScopeSetEquals(response.scope, input.scopes)) {
547 return yield* new ManagedRelayAccessTokenScopesUnexpectedError({
548 requestedScopes: input.scopes,
549 grantedScope: response.scope,
550 });
551 }
552 return response;
553 },
554 );
555
556 const obtainAccessToken = Effect.fn("clientRuntime.managedRelay.obtainAccessToken")(
557 function* (input: {
558 readonly clerkToken: string;
559 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
560 readonly thumbprint: string;
561 }) {
562 yield* Effect.annotateCurrentSpan({
563 "relay.client_id": options.clientId,
564 "relay.scopes": input.scopes.join(" "),
565 });
566 const nowMillis = yield* Clock.currentTimeMillis;
567 const accountId = relayAccountId(input.clerkToken);
568 if (Option.isNone(accountId)) {
569 yield* Effect.annotateCurrentSpan({
570 "relay.token_cache.result": "bypass",
571 "relay.token_cache.bypass_reason": "invalid_subject_token",
572 });
573 const response = yield* exchangeAccessToken(input);
574 return {
575 accountId: "",
576 clientId: options.clientId,
577 relayUrl,
578 thumbprint: input.thumbprint,
579 scopes: input.scopes,
580 accessToken: response.access_token,
581 expiresAtMillis: nowMillis + response.expires_in * 1_000,
582 } satisfies ManagedRelayAccessTokenCacheEntry;
583 }
584 const match = {
585 accountId: accountId.value,
586 clientId: options.clientId,
587 relayUrl,
588 thumbprint: input.thumbprint,
589 scopes: input.scopes,
590 nowMillis,
591 };
592 const cached = (yield* SynchronizedRef.get(cachedTokens)).find((token) =>
593 tokenMatches(token, match),
594 );
595 if (cached) {
596 yield* Effect.annotateCurrentSpan({
597 "relay.token_cache.result": "hit",
598 });
599 return cached;
600 }
601 return yield* SynchronizedRef.modifyEffect(cachedTokens, (tokens) =>
602 Effect.gen(function* () {
603 const lookupMillis = yield* Clock.currentTimeMillis;
604 const activeTokens = tokens.filter(
605 (token) => token.expiresAtMillis > lookupMillis + 5_000,
606 );
607 const cached = activeTokens.find((token) =>
608 tokenMatches(token, { ...match, nowMillis: lookupMillis }),
609 );
610 if (cached) {
611 yield* Effect.annotateCurrentSpan({
612 "relay.token_cache.result": "hit",
613 });
614 return [cached, activeTokens] as const;
615 }
616 yield* Effect.annotateCurrentSpan({
617 "relay.token_cache.result": "miss",
618 });
619 const response = yield* exchangeAccessToken(input);
620 const next: ManagedRelayAccessTokenCacheEntry = {
621 accountId: accountId.value,
622 clientId: options.clientId,
623 relayUrl,
624 thumbprint: input.thumbprint,
625 scopes: input.scopes,
626 accessToken: response.access_token,
627 expiresAtMillis: lookupMillis + response.expires_in * 1_000,
628 };
629 const nextTokens = [...activeTokens, next];
630 if (options.accessTokenStore) {
631 yield* options.accessTokenStore.save(nextTokens);
632 }
633 return [next, nextTokens] as const;
634 }),
635 ).pipe(Effect.withSpan("clientRuntime.managedRelay.tokenCacheCriticalSection"));
636 },
637 );
638
639 const authorize = Effect.fn("clientRuntime.managedRelay.authorize")(function* (input: {
640 readonly clerkToken: string;
641 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
642 readonly target: DpopProofTarget;
643 }) {
644 yield* Effect.annotateCurrentSpan({
645 "relay.client_id": options.clientId,
646 "relay.scopes": input.scopes.join(" "),
647 "http.request.method": input.target.method,
648 "url.full": input.target.url,
649 });
650 const thumbprint = yield* signer.thumbprint;
651 const token = yield* obtainAccessToken({
652 clerkToken: input.clerkToken,
653 scopes: input.scopes,
654 thumbprint,
655 });
656 const proof = yield* signer
657 .createProof({
658 ...input.target,
659 accessToken: token.accessToken,
660 })
661 .pipe(
662 Effect.mapError(
663 (error) => new ManagedRelayRequestProofCreationError(proofCreationErrorFields(error)),
664 ),
665 );
666 return { accessToken: token.accessToken, proof, thumbprint };
667 });
668
669 const invalidateAccessToken = Effect.fn("clientRuntime.managedRelay.invalidateAccessToken")(
670 function* (accessToken: string) {
671 return yield* SynchronizedRef.modifyEffect(cachedTokens, (tokens) => {
672 const nextTokens = tokens.filter((token) => token.accessToken !== accessToken);
673 if (nextTokens.length === tokens.length) {
674 return Effect.succeed([false, tokens] as const);
675 }
676 return (
677 options.accessTokenStore ? options.accessTokenStore.save(nextTokens) : Effect.void
678 ).pipe(Effect.as([true, nextTokens] as const));
679 });
680 },
681 );
682
683 const runDpopRequest = <A>(
684 input: {
685 readonly clerkToken: string;
686 readonly scopes: ReadonlyArray<RelayDpopAccessTokenScope>;
687 readonly target: DpopProofTarget;
688 },
689 request: (
690 authorization: ManagedRelayAuthorization,
691 ) => Effect.Effect<A, ManagedRelayClientError>,
692 ): Effect.Effect<A, ManagedRelayClientError> => {
693 const attempt = (refreshRejectedToken: boolean): Effect.Effect<A, ManagedRelayClientError> =>
694 authorize(input).pipe(
695 Effect.flatMap((authorization) =>
696 request(authorization).pipe(
697 Effect.catchIf(isRejectedDpopAccessToken, (error) =>
698 invalidateAccessToken(authorization.accessToken).pipe(
699 Effect.tap((invalidated) =>
700 Effect.annotateCurrentSpan({
701 "relay.token_cache.invalidated": invalidated,
702 "relay.token_cache.invalidation_reason": "invalid_bearer",
703 "relay.token_cache.retry_after_invalidation": refreshRejectedToken,
704 }),
705 ),
706 Effect.tap((invalidated) =>
707 invalidated && refreshRejectedToken
708 ? Effect.logWarning(
709 "Relay rejected a cached DPoP access token; refreshing it once.",
710 )
711 : Effect.void,
712 ),
713 Effect.andThen(refreshRejectedToken ? attempt(false) : Effect.fail(error)),
714 ),
715 ),
716 ),
717 ),
718 );
719 return attempt(true);
720 };
721
722 const mobileRegistrationRequest = <A>(
723 input: {
724 readonly clerkToken: string;
725 readonly target: DpopProofTarget;
726 },
727 request: (
728 authorization: ManagedRelayAuthorization,
729 ) => Effect.Effect<A, ManagedRelayClientError>,
730 ) =>
731 runDpopRequest(
732 {
733 ...input,
734 scopes: [RelayMobileRegistrationScope],
735 },
736 request,
737 );
738
739 return ManagedRelayClient.of({
740 relayUrl,
741 listEnvironments: Effect.fnUntraced(
742 function* (input) {
743 return yield* client.client
744 .listEnvironments({ headers: bearerHeaders(input.clerkToken) })
745 .pipe(
746 Effect.map((response) => response.environments),
747 Effect.mapError(relayRequestError("list relay-managed environments")),
748 timeoutRelayRequest("Relay environment listing"),
749 );
750 },
751 Effect.withSpan("clientRuntime.managedRelay.listEnvironments"),
752 withRelayClientTracing,
753 ),
754 listDevices: Effect.fnUntraced(
755 function* (input) {
756 return yield* client.client
757 .listDevicesV2({
758 headers: bearerHeaders(input.clerkToken),
759 })
760 .pipe(
761 Effect.map((response) => response.devices),
762 Effect.mapError(relayRequestError("list relay client devices")),
763 timeoutRelayRequest("Relay client device listing"),
764 );
765 },
766 Effect.withSpan("clientRuntime.managedRelay.listDevices"),
767 withRelayClientTracing,
768 ),
769 createEnvironmentLinkChallenge: Effect.fnUntraced(
770 function* (input) {
771 return yield* client.client
772 .createEnvironmentLinkChallenge({
773 headers: bearerHeaders(input.clerkToken),
774 payload: input.payload,
775 })
776 .pipe(
777 Effect.mapError(relayRequestError("create relay environment link challenge")),
778 timeoutRelayRequest("Relay environment link challenge"),
779 );
780 },
781 Effect.withSpan("clientRuntime.managedRelay.createEnvironmentLinkChallenge"),
782 withRelayClientTracing,
783 ),
784 linkEnvironment: Effect.fnUntraced(
785 function* (input) {
786 return yield* client.client
787 .linkEnvironment({
788 headers: bearerHeaders(input.clerkToken),
789 payload: input.payload,
790 })
791 .pipe(
792 Effect.mapError(relayRequestError("link relay environment")),
793 timeoutRelayRequest("Relay environment linking"),
794 );
795 },
796 Effect.withSpan("clientRuntime.managedRelay.linkEnvironment"),
797 withRelayClientTracing,
798 ),
799 unlinkEnvironment: Effect.fnUntraced(
800 function* (input) {
801 return yield* client.client
802 .unlinkEnvironment({
803 headers: bearerHeaders(input.clerkToken),
804 params: { environmentId: input.environmentId },
805 })
806 .pipe(
807 Effect.mapError(relayRequestError("unlink relay environment")),
808 timeoutRelayRequest("Relay environment unlinking"),
809 );
810 },
811 Effect.withSpan("clientRuntime.managedRelay.unlinkEnvironment"),
812 withRelayClientTracing,
813 ),
814 getT3ChatLink: Effect.fnUntraced(
815 function* (input) {
816 return yield* client.client
817 .getT3ChatLink({ headers: bearerHeaders(input.clerkToken) })
818 .pipe(
819 Effect.mapError(relayRequestError("read linked T3 Chat account")),
820 timeoutRelayRequest("Relay T3 Chat link lookup"),
821 );
822 },
823 Effect.withSpan("clientRuntime.managedRelay.getT3ChatLink"),
824 withRelayClientTracing,
825 ),
826 createT3ChatLinkRequest: Effect.fnUntraced(
827 function* (input) {
828 return yield* client.client
829 .createT3ChatLinkRequest({ headers: bearerHeaders(input.clerkToken) })
830 .pipe(
831 Effect.mapError(relayRequestError("start T3 Chat linking")),
832 timeoutRelayRequest("Relay T3 Chat link request"),
833 );
834 },
835 Effect.withSpan("clientRuntime.managedRelay.createT3ChatLinkRequest"),
836 withRelayClientTracing,
837 ),
838 unlinkT3Chat: Effect.fnUntraced(
839 function* (input) {
840 return yield* client.client
841 .unlinkT3Chat({ headers: bearerHeaders(input.clerkToken) })
842 .pipe(
843 Effect.mapError(relayRequestError("unlink T3 Chat account")),
844 timeoutRelayRequest("Relay T3 Chat unlinking"),
845 );
846 },
847 Effect.withSpan("clientRuntime.managedRelay.unlinkT3Chat"),
848 withRelayClientTracing,
849 ),
850 getEnvironmentStatus: Effect.fnUntraced(
851 function* (input) {
852 yield* Effect.annotateCurrentSpan({
853 "environment.id": input.environmentId,
854 });
855 return yield* runDpopRequest(
856 {
857 clerkToken: input.clerkToken,
858 scopes: input.scopes,
859 target: dpopProofTargets.getEnvironmentStatus(input.environmentId),
860 },
861 (authorization) =>
862 client.dpopClient
863 .getEnvironmentStatus({
864 headers: dpopHeaders(authorization),
865 params: { environmentId: input.environmentId },
866 })
867 .pipe(
868 Effect.mapError(relayRequestError("get relay environment status")),
869 timeoutRelayRequest("Relay environment status request"),
870 ),
871 );
872 },
873 Effect.withSpan("clientRuntime.managedRelay.getEnvironmentStatus"),
874 withRelayClientTracing,
875 ),
876 connectEnvironment: Effect.fnUntraced(
877 function* (input) {
878 yield* Effect.annotateCurrentSpan({
879 "environment.id": input.environmentId,
880 });
881 return yield* runDpopRequest(
882 {
883 clerkToken: input.clerkToken,
884 scopes: input.scopes,
885 target: dpopProofTargets.connectEnvironment(input.environmentId),
886 },
887 (authorization) => {
888 const payload: RelayEnvironmentConnectRequest = {
889 ...(input.deviceId ? { deviceId: input.deviceId } : {}),
890 clientKeyThumbprint: authorization.thumbprint,
891 };
892 return client.dpopClient
893 .connectEnvironment({
894 headers: dpopHeaders(authorization),
895 params: { environmentId: input.environmentId },
896 payload,
897 })
898 .pipe(
899 Effect.mapError(relayRequestError("connect relay environment")),
900 timeoutRelayRequest("Relay environment connection"),
901 );
902 },
903 );
904 },
905 Effect.withSpan("clientRuntime.managedRelay.connectEnvironment"),
906 withRelayClientTracing,
907 ),
908 registerDevice: Effect.fnUntraced(
909 function* (input) {
910 return yield* mobileRegistrationRequest(
911 {
912 clerkToken: input.clerkToken,
913 target: dpopProofTargets.registerDevice(),
914 },
915 (authorization) =>
916 client.mobile
917 .registerDevice({
918 headers: dpopHeaders(authorization),
919 payload: input.payload,
920 })
921 .pipe(
922 Effect.mapError(relayRequestError("register relay mobile device")),
923 timeoutRelayRequest("Relay mobile device registration"),
924 ),
925 );
926 },
927 Effect.withSpan("clientRuntime.managedRelay.registerDevice"),
928 withRelayClientTracing,
929 ),
930 unregisterDevice: Effect.fnUntraced(
931 function* (input) {
932 return yield* mobileRegistrationRequest(
933 {
934 clerkToken: input.clerkToken,
935 target: dpopProofTargets.unregisterDevice(input.deviceId),
936 },
937 (authorization) =>
938 client.mobile
939 .unregisterDevice({
940 headers: dpopHeaders(authorization),
941 params: { deviceId: input.deviceId },
942 })
943 .pipe(
944 Effect.mapError(relayRequestError("unregister relay mobile device")),
945 timeoutRelayRequest("Relay mobile device unregistration"),
946 ),
947 );
948 },
949 Effect.withSpan("clientRuntime.managedRelay.unregisterDevice"),
950 withRelayClientTracing,
951 ),
952 getAgentActivitySnapshot: Effect.fnUntraced(
953 function* (input) {
954 return yield* mobileRegistrationRequest(
955 {
956 clerkToken: input.clerkToken,
957 target: dpopProofTargets.getAgentActivitySnapshot(),
958 },
959 (authorization) =>
960 client.mobile
961 .getAgentActivitySnapshot({
962 headers: dpopHeaders(authorization),
963 })
964 .pipe(
965 Effect.mapError(relayRequestError("read relay agent activity snapshot")),
966 timeoutRelayRequest("Relay agent activity snapshot"),
967 ),
968 );
969 },
970 Effect.withSpan("clientRuntime.managedRelay.getAgentActivitySnapshot"),
971 withRelayClientTracing,
972 ),
973 registerLiveActivity: Effect.fnUntraced(
974 function* (input) {
975 return yield* mobileRegistrationRequest(
976 {
977 clerkToken: input.clerkToken,
978 target: dpopProofTargets.registerLiveActivity(),
979 },
980 (authorization) =>
981 client.mobile
982 .registerLiveActivity({
983 headers: dpopHeaders(authorization),
984 payload: input.payload,
985 })
986 .pipe(
987 Effect.mapError(relayRequestError("register relay live activity")),
988 timeoutRelayRequest("Relay Live Activity registration"),
989 ),
990 );
991 },
992 Effect.withSpan("clientRuntime.managedRelay.registerLiveActivity"),
993 withRelayClientTracing,
994 ),
995 resetTokenCache: SynchronizedRef.set(cachedTokens, []).pipe(
996 Effect.andThen(options.accessTokenStore ? options.accessTokenStore.clear : Effect.void),
997 Effect.withSpan("clientRuntime.managedRelay.resetTokenCache"),
998 withRelayClientTracing,
999 ),
1000 });
1001});
1002
1003export const layer = (options: ManagedRelayClientLayerOptions) =>
1004 Layer.effect(ManagedRelayClient, make(options));