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