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

remote.ts

214 lines7.2 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​
35export const exchangeRemoteDpopAccessToken = Effect.fn(
36 "clientRuntime.authorization.exchangeRemoteDpopAccessToken",
37)(function* (input: {
38 readonly httpBaseUrl: string;
39 readonly credential: string;
40 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
41 readonly clientMetadata?: AuthClientPresentationMetadata;
42 readonly dpopProof: string;
43 readonly timeoutMs?: number;
44}) {
45 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
46 const response = yield* executeEnvironmentHttpRequest(
47 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
48 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
49 client.auth.token({
50 headers: { dpop: input.dpopProof },
51 payload: {
52 grant_type: AuthTokenExchangeGrantType,
53 subject_token: input.credential,
54 subject_token_type: AuthEnvironmentBootstrapTokenType,
55 requested_token_type: AuthAccessTokenType,
56 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
57 ...clientMetadataTokenExchangeFields(input.clientMetadata),
58 },
59 }),
60 );
61 return response;
62});
63​
64export const bootstrapRemoteBearerSession = Effect.fn(
65 "clientRuntime.authorization.bootstrapRemoteBearerSession",
66)(function* (input: {
67 readonly httpBaseUrl: string;
68 readonly credential: string;
69 readonly scopes?: ReadonlyArray<AuthEnvironmentScope>;
70 readonly clientMetadata?: AuthClientPresentationMetadata;
71 readonly timeoutMs?: number;
72}) {
73 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
74 return yield* executeEnvironmentHttpRequest(
75 environmentEndpointUrl(input.httpBaseUrl, "/oauth/token"),
76 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
77 client.auth.token({
78 headers: {},
79 payload: {
80 grant_type: AuthTokenExchangeGrantType,
81 subject_token: input.credential,
82 subject_token_type: AuthEnvironmentBootstrapTokenType,
83 requested_token_type: AuthAccessTokenType,
84 ...(input.scopes ? { scope: encodeOAuthScope(input.scopes) } : {}),
85 ...clientMetadataTokenExchangeFields(input.clientMetadata),
86 },
87 }),
88 );
89});
90​
91export const fetchRemoteSessionState = Effect.fn(
92 "clientRuntime.authorization.fetchRemoteSessionState",
93)(function* (input: {
94 readonly httpBaseUrl: string;
95 readonly bearerToken: string;
96 readonly timeoutMs?: number;
97}) {
98 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
99 return yield* executeEnvironmentHttpRequest(
100 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"),
101 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
102 client.auth.session({
103 headers: {
104 authorization: `Bearer ${input.bearerToken}`,
105 },
106 }),
107 );
108});
109​
110export const fetchRemoteDpopSessionState = Effect.fn(
111 "clientRuntime.authorization.fetchRemoteDpopSessionState",
112)(function* (input: {
113 readonly httpBaseUrl: string;
114 readonly accessToken: string;
115 readonly dpopProof: string;
116 readonly timeoutMs?: number;
117}) {
118 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
119 return yield* executeEnvironmentHttpRequest(
120 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/session"),
121 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
122 client.auth.session({
123 headers: {
124 authorization: `DPoP ${input.accessToken}`,
125 dpop: input.dpopProof,
126 },
127 }),
128 );
129});
130​
131export const issueRemoteWebSocketTicket = Effect.fn(
132 "clientRuntime.authorization.issueRemoteWebSocketTicket",
133)(function* (input: {
134 readonly httpBaseUrl: string;
135 readonly bearerToken: string;
136 readonly timeoutMs?: number;
137}) {
138 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
139 return yield* executeEnvironmentHttpRequest(
140 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
141 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
142 client.auth.webSocketTicket({
143 headers: {
144 authorization: `Bearer ${input.bearerToken}`,
145 },
146 }),
147 );
148});
149​
150export const issueRemoteDpopWebSocketTicket = Effect.fn(
151 "clientRuntime.authorization.issueRemoteDpopWebSocketTicket",
152)(function* (input: {
153 readonly httpBaseUrl: string;
154 readonly accessToken: string;
155 readonly dpopProof: string;
156 readonly timeoutMs?: number;
157}) {
158 const client = yield* makeEnvironmentHttpApiClient(input.httpBaseUrl);
159 return yield* executeEnvironmentHttpRequest(
160 environmentEndpointUrl(input.httpBaseUrl, "/api/auth/websocket-ticket"),
161 input.timeoutMs ?? DEFAULT_REMOTE_REQUEST_TIMEOUT_MS,
162 client.auth.webSocketTicket({
163 headers: {
164 authorization: `DPoP ${input.accessToken}`,
165 dpop: input.dpopProof,
166 },
167 }),
168 );
169});
170​
171export const resolveRemoteWebSocketConnectionUrl = Effect.fn(
172 "clientRuntime.authorization.resolveRemoteWebSocketConnectionUrl",
173)(function* (input: {
174 readonly wsBaseUrl: string;
175 readonly httpBaseUrl: string;
176 readonly bearerToken: string;
177 readonly timeoutMs?: number;
178}) {
179 const issued = yield* issueRemoteWebSocketTicket({
180 httpBaseUrl: input.httpBaseUrl,
181 bearerToken: input.bearerToken,
182 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
183 });
184​
185 const url = new URL(input.wsBaseUrl);
186 if (url.pathname === "" || url.pathname === "/") {
187 url.pathname = "/ws";
188 }
189 url.searchParams.set("wsTicket", issued.ticket);
190 return url.toString();
191});
192​
193export const resolveRemoteDpopWebSocketConnectionUrl = Effect.fn(
194 "clientRuntime.authorization.resolveRemoteDpopWebSocketConnectionUrl",
195)(function* (input: {
196 readonly wsBaseUrl: string;
197 readonly httpBaseUrl: string;
198 readonly accessToken: string;
199 readonly dpopProof: string;
200 readonly timeoutMs?: number;
201}) {
202 const issued = yield* issueRemoteDpopWebSocketTicket({
203 httpBaseUrl: input.httpBaseUrl,
204 accessToken: input.accessToken,
205 dpopProof: input.dpopProof,
206 ...(input.timeoutMs ? { timeoutMs: input.timeoutMs } : {}),
207 });
208 const url = new URL(input.wsBaseUrl);
209 if (url.pathname === "" || url.pathname === "/") {
210 url.pathname = "/ws";
211 }
212 url.searchParams.set("wsTicket", issued.ticket);
213 return url.toString();
214});