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

auth.ts

344 lines12.8 KB

1import * as Schema from "effect/Schema";
2import * as HttpApiSchema from "effect/unstable/httpapi/HttpApiSchema";
3​
4import { AuthSessionId, TrimmedNonEmptyString } from "./baseSchemas.ts";
5​
6/**
7 * Declares the server's overall authentication posture.
8 *
9 * This is a high-level policy label that tells clients how the environment is
10 * expected to be accessed, not a transport detail and not an exhaustive list
11 * of every accepted credential.
12 *
13 * Typical usage:
14 * - rendered in auth/pairing UI so the user understands what kind of
15 * environment they are connecting to
16 * - used by clients to decide whether silent desktop bootstrap is expected or
17 * whether an explicit pairing flow should be shown
18 *
19 * Meanings:
20 * - `desktop-managed-local`: local desktop-managed environment with narrow
21 * trusted bootstrap, intended to avoid login prompts on the same machine
22 * - `loopback-browser`: standalone local server intended for browser pairing on
23 * the same machine
24 * - `remote-reachable`: environment intended to be reached from other devices
25 * or networks, where explicit pairing/auth is expected
26 * - `unsafe-no-auth`: intentionally unauthenticated mode; this is an explicit
27 * unsafe escape hatch, not a normal deployment mode
28 */
29export const ServerAuthPolicy = Schema.Literals([
30 "desktop-managed-local",
31 "loopback-browser",
32 "remote-reachable",
33 "unsafe-no-auth",
34]);
35export type ServerAuthPolicy = typeof ServerAuthPolicy.Type;
36​
37/**
38 * A credential type that can be exchanged for a real authenticated session.
39 *
40 * Bootstrap methods are for establishing trust at the start of a connection or
41 * pairing flow. They are not the long-lived credential used for ordinary
42 * authenticated HTTP / WebSocket traffic after pairing succeeds.
43 *
44 * Current methods:
45 * - `desktop-bootstrap`: a trusted local desktop handoff, used so the desktop
46 * shell can pair the renderer without a login screen
47 * - `one-time-token`: a short-lived pairing token, suitable for manual pairing
48 * flows such as `/pair?token=...`
49 */
50export const ServerAuthBootstrapMethod = Schema.Literals(["desktop-bootstrap", "one-time-token"]);
51export type ServerAuthBootstrapMethod = typeof ServerAuthBootstrapMethod.Type;
52​
53/**
54 * A credential type accepted for steady-state authenticated requests after a
55 * client has already paired.
56 *
57 * These methods are used by the server-wide auth layer for privileged HTTP and
58 * WebSocket access. They are distinct from bootstrap methods so clients can
59 * reason clearly about "pair first, then use session auth".
60 *
61 * Current methods:
62 * - `browser-session-cookie`: cookie-backed browser session, used by the web
63 * app after bootstrap/pairing
64 * - `bearer-access-token`: scoped token suitable for non-cookie or
65 * non-browser clients
66 * - `dpop-access-token`: scoped proof-of-possession token used by managed
67 * relay connections
68 */
69export const ServerAuthSessionMethod = Schema.Literals([
70 "browser-session-cookie",
71 "bearer-access-token",
72 "dpop-access-token",
73]);
74export type ServerAuthSessionMethod = typeof ServerAuthSessionMethod.Type;
75​
76export const AuthOrchestrationReadScope = "orchestration:read" as const;
77export const AuthOrchestrationOperateScope = "orchestration:operate" as const;
78export const AuthTerminalOperateScope = "terminal:operate" as const;
79export const AuthReviewWriteScope = "review:write" as const;
80export const AuthAccessReadScope = "access:read" as const;
81export const AuthAccessWriteScope = "access:write" as const;
82export const AuthRelayReadScope = "relay:read" as const;
83export const AuthRelayWriteScope = "relay:write" as const;
84export const AuthEnvironmentScope = Schema.Literals([
85 AuthOrchestrationReadScope,
86 AuthOrchestrationOperateScope,
87 AuthTerminalOperateScope,
88 AuthReviewWriteScope,
89 AuthAccessReadScope,
90 AuthAccessWriteScope,
91 AuthRelayReadScope,
92 AuthRelayWriteScope,
93]);
94export type AuthEnvironmentScope = typeof AuthEnvironmentScope.Type;
95export const AuthEnvironmentScopes = Schema.Array(AuthEnvironmentScope);
96export type AuthEnvironmentScopes = typeof AuthEnvironmentScopes.Type;
97​
98export const AuthStandardClientScopes = [
99 AuthOrchestrationReadScope,
100 AuthOrchestrationOperateScope,
101 AuthTerminalOperateScope,
102 AuthReviewWriteScope,
103 AuthRelayReadScope,
104] as const;
105export const AuthAdministrativeScopes = [
106 ...AuthStandardClientScopes,
107 AuthAccessReadScope,
108 AuthAccessWriteScope,
109 AuthRelayWriteScope,
110] as const;
111​
112export const AuthTokenExchangeGrantType =
113 "urn:ietf:params:oauth:grant-type:token-exchange" as const;
114export const AuthAccessTokenType = "urn:ietf:params:oauth:token-type:access_token" as const;
115export const AuthEnvironmentBootstrapTokenType =
116 "urn:t3:params:oauth:token-type:environment-bootstrap" as const;
117​
118/**
119 * Server-advertised auth capabilities for a specific execution environment.
120 *
121 * Clients should treat this as the authoritative description of how that
122 * environment expects to be paired and how authenticated requests should be
123 * made afterward.
124 *
125 * Field meanings:
126 * - `policy`: high-level auth posture for the environment
127 * - `bootstrapMethods`: pairing/bootstrap methods the server is currently
128 * willing to accept
129 * - `sessionMethods`: authenticated request/session methods the server supports
130 * once pairing is complete
131 * - `sessionCookieName`: cookie name clients should expect when
132 * `browser-session-cookie` is in use
133 *
134 * This descriptor is intentionally capability-oriented. It lets clients choose
135 * the right UX without embedding server-specific auth logic or assuming a
136 * single access method.
137 */
138export const ServerAuthDescriptor = Schema.Struct({
139 policy: ServerAuthPolicy,
140 bootstrapMethods: Schema.Array(ServerAuthBootstrapMethod),
141 sessionMethods: Schema.Array(ServerAuthSessionMethod),
142 sessionCookieName: TrimmedNonEmptyString,
143});
144export type ServerAuthDescriptor = typeof ServerAuthDescriptor.Type;
145​
146export const AuthBrowserSessionRequest = Schema.Struct({
147 credential: TrimmedNonEmptyString,
148});
149export type AuthBrowserSessionRequest = typeof AuthBrowserSessionRequest.Type;
150​
151export const AuthBrowserSessionResult = Schema.Struct({
152 authenticated: Schema.Literal(true),
153 scopes: AuthEnvironmentScopes,
154 sessionMethod: ServerAuthSessionMethod,
155 expiresAt: Schema.DateTimeUtc,
156});
157export type AuthBrowserSessionResult = typeof AuthBrowserSessionResult.Type;
158​
159export const AuthClientMetadataDeviceType = Schema.Literals([
160 "desktop",
161 "mobile",
162 "tablet",
163 "bot",
164 "unknown",
165]);
166export type AuthClientMetadataDeviceType = typeof AuthClientMetadataDeviceType.Type;
167​
168export const AuthClientPresentationMetadata = Schema.Struct({
169 label: Schema.optionalKey(TrimmedNonEmptyString),
170 deviceType: Schema.optionalKey(AuthClientMetadataDeviceType),
171 os: Schema.optionalKey(TrimmedNonEmptyString),
172});
173export type AuthClientPresentationMetadata = typeof AuthClientPresentationMetadata.Type;
174​
175export const AuthTokenExchangeRequest = Schema.Struct({
176 grant_type: Schema.Literal(AuthTokenExchangeGrantType),
177 subject_token: TrimmedNonEmptyString,
178 subject_token_type: Schema.Literal(AuthEnvironmentBootstrapTokenType),
179 requested_token_type: Schema.Literal(AuthAccessTokenType),
180 scope: Schema.optionalKey(TrimmedNonEmptyString),
181 client_label: Schema.optionalKey(TrimmedNonEmptyString),
182 client_device_type: Schema.optionalKey(AuthClientMetadataDeviceType),
183 client_os: Schema.optionalKey(TrimmedNonEmptyString),
184}).pipe(HttpApiSchema.asFormUrlEncoded());
185export type AuthTokenExchangeRequest = typeof AuthTokenExchangeRequest.Type;
186​
187export const AuthAccessTokenResult = Schema.Struct({
188 access_token: TrimmedNonEmptyString,
189 issued_token_type: Schema.Literal(AuthAccessTokenType),
190 token_type: Schema.Literals(["Bearer", "DPoP"]),
191 expires_in: Schema.Number,
192 scope: TrimmedNonEmptyString,
193});
194export type AuthAccessTokenResult = typeof AuthAccessTokenResult.Type;
195​
196export const AuthWebSocketTicketResult = Schema.Struct({
197 ticket: TrimmedNonEmptyString,
198 expiresAt: Schema.DateTimeUtc,
199});
200export type AuthWebSocketTicketResult = typeof AuthWebSocketTicketResult.Type;
201​
202export const AuthPairingCredentialResult = Schema.Struct({
203 id: TrimmedNonEmptyString,
204 credential: TrimmedNonEmptyString,
205 label: Schema.optionalKey(TrimmedNonEmptyString),
206 expiresAt: Schema.DateTimeUtc,
207});
208export type AuthPairingCredentialResult = typeof AuthPairingCredentialResult.Type;
209​
210export const AuthPairingLink = Schema.Struct({
211 id: TrimmedNonEmptyString,
212 credential: TrimmedNonEmptyString,
213 scopes: AuthEnvironmentScopes,
214 subject: TrimmedNonEmptyString,
215 label: Schema.optionalKey(TrimmedNonEmptyString),
216 createdAt: Schema.DateTimeUtc,
217 expiresAt: Schema.DateTimeUtc,
218});
219export type AuthPairingLink = typeof AuthPairingLink.Type;
220​
221export const AuthClientMetadata = Schema.Struct({
222 label: Schema.optionalKey(TrimmedNonEmptyString),
223 ipAddress: Schema.optionalKey(TrimmedNonEmptyString),
224 userAgent: Schema.optionalKey(TrimmedNonEmptyString),
225 deviceType: AuthClientMetadataDeviceType,
226 os: Schema.optionalKey(TrimmedNonEmptyString),
227 browser: Schema.optionalKey(TrimmedNonEmptyString),
228});
229export type AuthClientMetadata = typeof AuthClientMetadata.Type;
230​
231export const AuthClientSession = Schema.Struct({
232 sessionId: AuthSessionId,
233 subject: TrimmedNonEmptyString,
234 scopes: AuthEnvironmentScopes,
235 method: ServerAuthSessionMethod,
236 client: AuthClientMetadata,
237 issuedAt: Schema.DateTimeUtc,
238 expiresAt: Schema.DateTimeUtc,
239 lastConnectedAt: Schema.NullOr(Schema.DateTimeUtc),
240 connected: Schema.Boolean,
241 current: Schema.Boolean,
242});
243export type AuthClientSession = typeof AuthClientSession.Type;
244​
245export const AuthAccessSnapshot = Schema.Struct({
246 pairingLinks: Schema.Array(AuthPairingLink),
247 clientSessions: Schema.Array(AuthClientSession),
248});
249export type AuthAccessSnapshot = typeof AuthAccessSnapshot.Type;
250​
251export const AuthAccessStreamSnapshotEvent = Schema.Struct({
252 version: Schema.Literal(1),
253 revision: Schema.Number,
254 type: Schema.Literal("snapshot"),
255 payload: AuthAccessSnapshot,
256});
257export type AuthAccessStreamSnapshotEvent = typeof AuthAccessStreamSnapshotEvent.Type;
258​
259export const AuthAccessStreamPairingLinkUpsertedEvent = Schema.Struct({
260 version: Schema.Literal(1),
261 revision: Schema.Number,
262 type: Schema.Literal("pairingLinkUpserted"),
263 payload: AuthPairingLink,
264});
265export type AuthAccessStreamPairingLinkUpsertedEvent =
266 typeof AuthAccessStreamPairingLinkUpsertedEvent.Type;
267​
268export const AuthAccessStreamPairingLinkRemovedEvent = Schema.Struct({
269 version: Schema.Literal(1),
270 revision: Schema.Number,
271 type: Schema.Literal("pairingLinkRemoved"),
272 payload: Schema.Struct({
273 id: TrimmedNonEmptyString,
274 }),
275});
276export type AuthAccessStreamPairingLinkRemovedEvent =
277 typeof AuthAccessStreamPairingLinkRemovedEvent.Type;
278​
279export class AuthAccessStreamError extends Schema.TaggedErrorClass<AuthAccessStreamError>()(
280 "AuthAccessStreamError",
281 {
282 message: Schema.String,
283 },
284) {}
285​
286export class EnvironmentAuthorizationError extends Schema.TaggedErrorClass<EnvironmentAuthorizationError>()(
287 "EnvironmentAuthorizationError",
288 {
289 message: Schema.String,
290 requiredScope: AuthEnvironmentScope,
291 },
292) {}
293​
294export const AuthAccessStreamClientUpsertedEvent = Schema.Struct({
295 version: Schema.Literal(1),
296 revision: Schema.Number,
297 type: Schema.Literal("clientUpserted"),
298 payload: AuthClientSession,
299});
300export type AuthAccessStreamClientUpsertedEvent = typeof AuthAccessStreamClientUpsertedEvent.Type;
301​
302export const AuthAccessStreamClientRemovedEvent = Schema.Struct({
303 version: Schema.Literal(1),
304 revision: Schema.Number,
305 type: Schema.Literal("clientRemoved"),
306 payload: Schema.Struct({
307 sessionId: AuthSessionId,
308 }),
309});
310export type AuthAccessStreamClientRemovedEvent = typeof AuthAccessStreamClientRemovedEvent.Type;
311​
312export const AuthAccessStreamEvent = Schema.Union([
313 AuthAccessStreamSnapshotEvent,
314 AuthAccessStreamPairingLinkUpsertedEvent,
315 AuthAccessStreamPairingLinkRemovedEvent,
316 AuthAccessStreamClientUpsertedEvent,
317 AuthAccessStreamClientRemovedEvent,
318]);
319export type AuthAccessStreamEvent = typeof AuthAccessStreamEvent.Type;
320​
321export const AuthRevokePairingLinkInput = Schema.Struct({
322 id: TrimmedNonEmptyString,
323});
324export type AuthRevokePairingLinkInput = typeof AuthRevokePairingLinkInput.Type;
325​
326export const AuthRevokeClientSessionInput = Schema.Struct({
327 sessionId: AuthSessionId,
328});
329export type AuthRevokeClientSessionInput = typeof AuthRevokeClientSessionInput.Type;
330​
331export const AuthCreatePairingCredentialInput = Schema.Struct({
332 label: Schema.optionalKey(TrimmedNonEmptyString),
333 scopes: Schema.optionalKey(AuthEnvironmentScopes),
334});
335export type AuthCreatePairingCredentialInput = typeof AuthCreatePairingCredentialInput.Type;
336​
337export const AuthSessionState = Schema.Struct({
338 authenticated: Schema.Boolean,
339 auth: ServerAuthDescriptor,
340 scopes: Schema.optionalKey(AuthEnvironmentScopes),
341 sessionMethod: Schema.optionalKey(ServerAuthSessionMethod),
342 expiresAt: Schema.optionalKey(Schema.DateTimeUtc),
343});
344export type AuthSessionState = typeof AuthSessionState.Type;