Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

remote.ts

232 lines7.9 KB

1import {
2 AuthAccessTokenType,
3 type AuthClientPresentationMetadata,
4 AuthEnvironmentBootstrapTokenType,
5 AuthTokenExchangeGrantType,
6 type AuthEnvironmentScope,
7} from "@t3tools/contracts";
8import { encodeOAuthScope } from "@t3tools/shared/oauthScope";
9import * as Effect from "effect/Effect";
10import { environmentEndpointUrl } from "../environment/endpoint.ts";
11import {
12 executeEnvironmentHttpRequest,
13 makeEnvironmentHttpApiClient,
14 type RemoteEnvironmentRequestError,
15} from "../rpc/http.ts";
16​
17export {
18 RemoteEnvironmentAuthFetchError,
19 RemoteEnvironmentAuthInvalidJsonError,
20 RemoteEnvironmentAuthTimeoutError,
21 RemoteEnvironmentAuthUndeclaredStatusError,
22} from "../rpc/http.ts";
23export type RemoteEnvironmentAuthError = RemoteEnvironmentRequestError;
24​
25const DEFAULT_REMOTE_REQUEST_TIMEOUT_MS = 10_000;
26​
27const clientMetadataTokenExchangeFields = (
28 clientMetadata: AuthClientPresentationMetadata | undefined,
29) => ({
30 ...(clientMetadata?.label ? { client_label: clientMetadata.label } : {}),
31 ...(clientMetadata?.deviceType ? { client_device_type: clientMetadata.deviceType } : {}),
32 ...(clientMetadata?.os ? { client_os: clientMetadata.os } : {}),
33});
34​
35// The server reads these off the /ws upgrade URL next to wsTicket. Optional on
36// both ends: old servers ignore unknown params, old clients never send them.
37export const appendClientConnectionParams = (
38 url: URL,
39 clientMetadata: AuthClientPresentationMetadata | undefined,
40): void => {
41 if (clientMetadata?.surface) {
42 url.searchParams.set("clientSurface", clientMetadata.surface);
43 }
44 if (clientMetadata?.appVersion) {
45 url.searchParams.set("clientAppVersion", clientMetadata.appVersion);
46 }
47};
48​
49export const exchangeRemoteDpopAccessToken = Effect.fn(
50 "clientRuntime.authorization.exchangeRemoteDpopAccessToken",
51)(function* (input: {
52 readonly httpBaseUrl: string;
53 readonly credential: string;
54 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
55 readonly clientMetadata?: AuthClientPresentationMetadata;
56 readonly dpopProof: string;
57 readonly timeoutMs?: number;
58}) {
59 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
60 const response = yield* executeEnvironmentHttpRequest(
61 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
62 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
63 client.auth.token({
64 headers: { dpop: input.dpopProof },
65 payload: {
66 grant_type: AuthTokenExchangeGrantType,
67 subject_token: input.credential,
68 subject_token_type: AuthEnvironmentBootstrapTokenType,
69 requested_token_type: AuthAccessTokenType,
70 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
71 ...clientMetadataTokenExchangeFields(input.clientMetadata),
72 },
73 }),
74 );
75 return response;
76});
77​
78export const bootstrapRemoteBearerSession = Effect.fn(
79 "clientRuntime.authorization.bootstrapRemoteBearerSession",
80)(function* (input: {
81 readonly httpBaseUrl: string;
82 readonly credential: string;
83 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
84 readonly clientMetadata?: AuthClientPresentationMetadata;
85 readonly timeoutMs?: number;
86}) {
87 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
88 return yield* executeEnvironmentHttpRequest(
89 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
90 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
91 client.auth.token({
92 headers: {},
93 payload: {
94 grant_type: AuthTokenExchangeGrantType,
95 subject_token: input.credential,
96 subject_token_type: AuthEnvironmentBootstrapTokenType,
97 requested_token_type: AuthAccessTokenType,
98 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
99 ...clientMetadataTokenExchangeFields(input.clientMetadata),
100 },
101 }),
102 );
103});
104​
105export const fetchRemoteSessionState = Effect.fn(
106 "clientRuntime.authorization.fetchRemoteSessionState",
107)(function* (input: {
108 readonly httpBaseUrl: string;
109 readonly bearerToken: string;
110 readonly timeoutMs?: number;
111}) {
112 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
113 return yield* executeEnvironmentHttpRequest(
114 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"),
115 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
116 client.auth.session({
117 headers: {
118 authorization: `Bearer ${input.bearerToken}`,
119 },
120 }),
121 );
122});
123​
124export const fetchRemoteDpopSessionState = Effect.fn(
125 "clientRuntime.authorization.fetchRemoteDpopSessionState",
126)(function* (input: {
127 readonly httpBaseUrl: string;
128 readonly accessToken: string;
129 readonly dpopProof: string;
130 readonly timeoutMs?: number;
131}) {
132 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
133 return yield* executeEnvironmentHttpRequest(
134 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"),
135 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
136 client.auth.session({
137 headers: {
138 authorization: `DPoP ${input.accessToken}`,
139 dpop: input.dpopProof,
140 },
141 }),
142 );
143});
144​
145export const issueRemoteWebSocketTicket = Effect.fn(
146 "clientRuntime.authorization.issueRemoteWebSocketTicket",
147)(function* (input: {
148 readonly httpBaseUrl: string;
149 readonly bearerToken: string;
150 readonly timeoutMs?: number;
151}) {
152 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
153 return yield* executeEnvironmentHttpRequest(
154 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
155 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
156 client.auth.webSocketTicket({
157 headers: {
158 authorization: `Bearer ${input.bearerToken}`,
159 },
160 }),
161 );
162});
163​
164export const issueRemoteDpopWebSocketTicket = Effect.fn(
165 "clientRuntime.authorization.issueRemoteDpopWebSocketTicket",
166)(function* (input: {
167 readonly httpBaseUrl: string;
168 readonly accessToken: string;
169 readonly dpopProof: string;
170 readonly timeoutMs?: number;
171}) {
172 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
173 return yield* executeEnvironmentHttpRequest(
174 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
175 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
176 client.auth.webSocketTicket({
177 headers: {
178 authorization: `DPoP ${input.accessToken}`,
179 dpop: input.dpopProof,
180 },
181 }),
182 );
183});
184​
185export const resolveRemoteWebSocketConnectionUrl = Effect.fn(
186 "clientRuntime.authorization.resolveRemoteWebSocketConnectionUrl",
187)(function* (input: {
188 readonly wsBaseUrl: string;
189 readonly httpBaseUrl: string;
190 readonly bearerToken: string;
191 readonly clientMetadata?: AuthClientPresentationMetadata;
192 readonly timeoutMs?: number;
193}) {
194 const issued = yield* issueRemoteWebSocketTicket({
195 httpBaseUrl: input.httpBaseUrl,
196 bearerToken: input.bearerToken,
197 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
198 });
199​
200 const url = new URL(input.wsBaseUrl);
201 if (url.pathname === "" || url.pathname === "/") {
202 url.pathname = "/ws";
203 }
204 url.searchParams.set("wsTicket", issued.ticket);
205 appendClientConnectionParams(url, input.clientMetadata);
206 return url.toString();
207});
208​
209export const resolveRemoteDpopWebSocketConnectionUrl = Effect.fn(
210 "clientRuntime.authorization.resolveRemoteDpopWebSocketConnectionUrl",
211)(function* (input: {
212 readonly wsBaseUrl: string;
213 readonly httpBaseUrl: string;
214 readonly accessToken: string;
215 readonly dpopProof: string;
216 readonly clientMetadata?: AuthClientPresentationMetadata;
217 readonly timeoutMs?: number;
218}) {
219 const issued = yield* issueRemoteDpopWebSocketTicket({
220 httpBaseUrl: input.httpBaseUrl,
221 accessToken: input.accessToken,
222 dpopProof: input.dpopProof,
223 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
224 });
225 const url = new URL(input.wsBaseUrl);
226 if (url.pathname === "" || url.pathname === "/") {
227 url.pathname = "/ws";
228 }
229 url.searchParams.set("wsTicket", issued.ticket);
230 appendClientConnectionParams(url, input.clientMetadata);
231 return url.toString();
232});