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