Open History to see the last commit
1import {
2 AuthAccessTokenType,
3 type AuthClientPresentationMetadata,
4 AuthEnvironmentBootstrapTokenType,
5 AuthTokenExchangeGrantType,
6 type ClientConnectionMethod,
7 type AuthEnvironmentScope,
8} from "@t3tools/contracts";
9import { encodeOAuthScope } from "@t3tools/shared/oauthScope";
10import * as Effect from "effect/Effect";
11import { environmentEndpointUrl } from "../environment/endpoint.ts";
12import {
13 executeEnvironmentHttpRequest,
14 makeEnvironmentHttpApiGroupClient,
15 type RemoteEnvironmentRequestError,
16} from "../rpc/http.ts";
17
18export {
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 const displayOs = clientMetadata?.os;
31 return {
32 ...(clientMetadata?.label ? { client_label: clientMetadata.label } : {}),
33 ...(clientMetadata?.deviceType ? { client_device_type: clientMetadata.deviceType } : {}),
34 ...(displayOs && displayOs !== "unknown" && displayOs !== "other"
35 ? { client_os: displayOs }
36 : {}),
37 };
38};
39
40export const appendClientConnectionParams = (
41 url: URL,
42 clientMetadata: AuthClientPresentationMetadata | undefined,
43 connectionMethod?: ClientConnectionMethod,
44): void => {
45 if (clientMetadata?.surface) {
46 url.searchParams.set("clientSurface", clientMetadata.surface);
47 }
48 if (clientMetadata?.appVersion) {
49 url.searchParams.set("clientAppVersion", clientMetadata.appVersion);
50 }
51 if (clientMetadata?.deviceType) {
52 const deviceType =
53 clientMetadata.deviceType === "mobile"
54 ? "phone"
55 : clientMetadata.deviceType === "desktop" || clientMetadata.deviceType === "tablet"
56 ? clientMetadata.deviceType
57 : "unknown";
58 url.searchParams.set("clientDeviceType", deviceType);
59 }
60 if (clientMetadata?.os) {
61 url.searchParams.set("clientOs", clientMetadata.os);
62 }
63 if (clientMetadata?.surface === "web") {
64 if (clientMetadata.webDeployment) {
65 url.searchParams.set("clientWebDeployment", clientMetadata.webDeployment);
66 }
67 if (clientMetadata.browser) {
68 url.searchParams.set("clientBrowser", clientMetadata.browser);
69 }
70 }
71 if (clientMetadata?.surface === "mobile") {
72 if (clientMetadata.osMajorVersion !== undefined) {
73 url.searchParams.set("clientOsMajorVersion", String(clientMetadata.osMajorVersion));
74 }
75 if (clientMetadata.deviceModel) {
76 url.searchParams.set("clientDeviceModel", clientMetadata.deviceModel);
77 }
78 }
79 if (connectionMethod) {
80 url.searchParams.set("connectionMethod", connectionMethod);
81 }
82};
83
84export const exchangeRemoteDpopAccessToken = Effect.fn(
85 "clientRuntime.authorization.exchangeRemoteDpopAccessToken",
86)(function* (input: {
87 readonly httpBaseUrl: string;
88 readonly credential: string;
89 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
90 readonly clientMetadata?: AuthClientPresentationMetadata;
91 readonly dpopProof: string;
92 readonly timeoutMs?: number;
93}) {
94 const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
95 const response = yield* executeEnvironmentHttpRequest(
96 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
97 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
98 client.token({
99 headers: { dpop: input.dpopProof },
100 payload: {
101 grant_type: AuthTokenExchangeGrantType,
102 subject_token: input.credential,
103 subject_token_type: AuthEnvironmentBootstrapTokenType,
104 requested_token_type: AuthAccessTokenType,
105 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
106 ...clientMetadataTokenExchangeFields(input.clientMetadata),
107 },
108 }),
109 );
110 return response;
111});
112
113export const bootstrapRemoteBearerSession = Effect.fn(
114 "clientRuntime.authorization.bootstrapRemoteBearerSession",
115)(function* (input: {
116 readonly httpBaseUrl: string;
117 readonly credential: string;
118 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
119 readonly clientMetadata?: AuthClientPresentationMetadata;
120 readonly timeoutMs?: number;
121}) {
122 const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
123 return yield* executeEnvironmentHttpRequest(
124 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
125 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
126 client.token({
127 headers: {},
128 payload: {
129 grant_type: AuthTokenExchangeGrantType,
130 subject_token: input.credential,
131 subject_token_type: AuthEnvironmentBootstrapTokenType,
132 requested_token_type: AuthAccessTokenType,
133 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
134 ...clientMetadataTokenExchangeFields(input.clientMetadata),
135 },
136 }),
137 );
138});
139
140export const fetchRemoteSessionState = Effect.fn(
141 "clientRuntime.authorization.fetchRemoteSessionState",
142)(function* (input: {
143 readonly httpBaseUrl: string;
144 readonly bearerToken: string;
145 readonly timeoutMs?: number;
146}) {
147 const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
148 return yield* executeEnvironmentHttpRequest(
149 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"),
150 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
151 client.session({
152 headers: {
153 authorization: `Bearer ${input.bearerToken}`,
154 },
155 }),
156 );
157});
158
159export const issueRemoteWebSocketTicket = Effect.fn(
160 "clientRuntime.authorization.issueRemoteWebSocketTicket",
161)(function* (input: {
162 readonly httpBaseUrl: string;
163 readonly bearerToken: string;
164 readonly timeoutMs?: number;
165}) {
166 const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
167 return yield* executeEnvironmentHttpRequest(
168 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
169 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
170 client.webSocketTicket({
171 headers: {
172 authorization: `Bearer ${input.bearerToken}`,
173 },
174 }),
175 );
176});
177
178export const issueRemoteDpopWebSocketTicket = Effect.fn(
179 "clientRuntime.authorization.issueRemoteDpopWebSocketTicket",
180)(function* (input: {
181 readonly httpBaseUrl: string;
182 readonly accessToken: string;
183 readonly dpopProof: string;
184 readonly timeoutMs?: number;
185}) {
186 const client = yield* makeEnvironmentHttpApiGroupClient(input.httpBaseUrl, "auth");
187 return yield* executeEnvironmentHttpRequest(
188 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
189 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
190 client.webSocketTicket({
191 headers: {
192 authorization: `DPoP ${input.accessToken}`,
193 dpop: input.dpopProof,
194 },
195 }),
196 );
197});
198
199export const resolveRemoteWebSocketConnectionUrl = Effect.fn(
200 "clientRuntime.authorization.resolveRemoteWebSocketConnectionUrl",
201)(function* (input: {
202 readonly wsBaseUrl: string;
203 readonly httpBaseUrl: string;
204 readonly bearerToken: string;
205 readonly clientMetadata?: AuthClientPresentationMetadata;
206 readonly connectionMethod?: ClientConnectionMethod;
207 readonly timeoutMs?: number;
208}) {
209 const issued = yield* issueRemoteWebSocketTicket({
210 httpBaseUrl: input.httpBaseUrl,
211 bearerToken: input.bearerToken,
212 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
213 });
214
215 const url = new URL(input.wsBaseUrl);
216 if (url.pathname === "" || url.pathname === "/") {
217 url.pathname = "/ws";
218 }
219 url.searchParams.set("wsTicket", issued.ticket);
220 appendClientConnectionParams(url, input.clientMetadata, input.connectionMethod);
221 return url.toString();
222});
223
224export const resolveRemoteDpopWebSocketConnectionUrl = Effect.fn(
225 "clientRuntime.authorization.resolveRemoteDpopWebSocketConnectionUrl",
226)(function* (input: {
227 readonly wsBaseUrl: string;
228 readonly httpBaseUrl: string;
229 readonly accessToken: string;
230 readonly dpopProof: string;
231 readonly clientMetadata?: AuthClientPresentationMetadata;
232 readonly connectionMethod?: ClientConnectionMethod;
233 readonly timeoutMs?: number;
234}) {
235 const issued = yield* issueRemoteDpopWebSocketTicket({
236 httpBaseUrl: input.httpBaseUrl,
237 accessToken: input.accessToken,
238 dpopProof: input.dpopProof,
239 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
240 });
241 const url = new URL(input.wsBaseUrl);
242 if (url.pathname === "" || url.pathname === "/") {
243 url.pathname = "/ws";
244 }
245 url.searchParams.set("wsTicket", issued.ticket);
246 appendClientConnectionParams(url, input.clientMetadata, input.connectionMethod);
247 return url.toString();
248});