Open History to see the last commit
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 */
67export const ServerAuthSessionMethod = Schema.Literals([
68 "browser-session-cookie",
69 "bearer-access-token",
70]);
71export type ServerAuthSessionMethod = typeof ServerAuthSessionMethod.Type;
72
73export const AuthOrchestrationReadScope = "orchestration:read" as const;
74export const AuthOrchestrationOperateScope = "orchestration:operate" as const;
75export const AuthTerminalOperateScope = "terminal:operate" as const;
76export const AuthReviewWriteScope = "review:write" as const;
77export const AuthAccessReadScope = "access:read" as const;
78export const AuthAccessWriteScope = "access:write" as const;
79export const AuthRelayReadScope = "relay:read" as const;
80export const AuthRelayWriteScope = "relay:write" as const;
81export const AuthEnvironmentScope = Schema.Literals([
82 AuthOrchestrationReadScope,
83 AuthOrchestrationOperateScope,
84 AuthTerminalOperateScope,
85 AuthReviewWriteScope,
86 AuthAccessReadScope,
87 AuthAccessWriteScope,
88 AuthRelayReadScope,
89 AuthRelayWriteScope,
90]);
91export type AuthEnvironmentScope = typeof AuthEnvironmentScope.Type;
92export const AuthEnvironmentScopes = Schema.Array(AuthEnvironmentScope);
93export type AuthEnvironmentScopes = typeof AuthEnvironmentScopes.Type;
94
95export const AuthStandardClientScopes = [
96 AuthOrchestrationReadScope,
97 AuthOrchestrationOperateScope,
98 AuthTerminalOperateScope,
99 AuthReviewWriteScope,
100 AuthRelayReadScope,
101] as const;
102export const AuthAdministrativeScopes = [
103 ...AuthStandardClientScopes,
104 AuthAccessReadScope,
105 AuthAccessWriteScope,
106 AuthRelayWriteScope,
107] as const;
108
109export const AuthTokenExchangeGrantType =
110 "urn:ietf:params:oauth:grant-type:token-exchange" as const;
111export const AuthAccessTokenType = "urn:ietf:params:oauth:token-type:access_token" as const;
112export const AuthEnvironmentBootstrapTokenType =
113 "urn:t3:params:oauth:token-type:environment-bootstrap" as const;
114
115/**
116 * Server-advertised auth capabilities for a specific execution environment.
117 *
118 * Clients should treat this as the authoritative description of how that
119 * environment expects to be paired and how authenticated requests should be
120 * made afterward.
121 *
122 * Field meanings:
123 * - `policy`: high-level auth posture for the environment
124 * - `bootstrapMethods`: pairing/bootstrap methods the server is currently
125 * willing to accept
126 * - `sessionMethods`: authenticated request/session methods the server supports
127 * once pairing is complete
128 * - `sessionCookieName`: cookie name clients should expect when
129 * `browser-session-cookie` is in use
130 *
131 * This descriptor is intentionally capability-oriented. It lets clients choose
132 * the right UX without embedding server-specific auth logic or assuming a
133 * single access method.
134 */
135export const ServerAuthDescriptor = Schema.Struct({
136 policy: ServerAuthPolicy,
137 bootstrapMethods: Schema.Array(ServerAuthBootstrapMethod),
138 sessionMethods: Schema.Array(ServerAuthSessionMethod),
139 sessionCookieName: TrimmedNonEmptyString,
140});
141export type ServerAuthDescriptor = typeof ServerAuthDescriptor.Type;
142
143export const AuthBrowserSessionRequest = Schema.Struct({
144 credential: TrimmedNonEmptyString,
145});
146export type AuthBrowserSessionRequest = typeof AuthBrowserSessionRequest.Type;
147
148export const AuthBrowserSessionResult = Schema.Struct({
149 authenticated: Schema.Literal(true),
150 scopes: AuthEnvironmentScopes,
151 sessionMethod: ServerAuthSessionMethod,
152 expiresAt: Schema.DateTimeUtc,
153});
154export type AuthBrowserSessionResult = typeof AuthBrowserSessionResult.Type;
155
156export const AuthClientMetadataDeviceType = Schema.Literals([
157 "desktop",
158 "mobile",
159 "tablet",
160 "bot",
161 "unknown",
162]);
163export type AuthClientMetadataDeviceType = typeof AuthClientMetadataDeviceType.Type;
164
165export const AuthClientPresentationMetadata = Schema.Struct({
166 label: Schema.optionalKey(TrimmedNonEmptyString),
167 deviceType: Schema.optionalKey(AuthClientMetadataDeviceType),
168 os: Schema.optionalKey(TrimmedNonEmptyString),
169});
170export type AuthClientPresentationMetadata = typeof AuthClientPresentationMetadata.Type;
171
172export const AuthTokenExchangeRequest = Schema.Struct({
173 grant_type: Schema.Literal(AuthTokenExchangeGrantType),
174 subject_token: TrimmedNonEmptyString,
175 subject_token_type: Schema.Literal(AuthEnvironmentBootstrapTokenType),
176 requested_token_type: Schema.Literal(AuthAccessTokenType),
177 scope: Schema.optionalKey(TrimmedNonEmptyString),
178 client_label: Schema.optionalKey(TrimmedNonEmptyString),
179 client_device_type: Schema.optionalKey(AuthClientMetadataDeviceType),
180 client_os: Schema.optionalKey(TrimmedNonEmptyString),
181}).pipe(HttpApiSchema.asFormUrlEncoded());
182export type AuthTokenExchangeRequest = typeof AuthTokenExchangeRequest.Type;
183
184export const AuthAccessTokenResult = Schema.Struct({
185 access_token: TrimmedNonEmptyString,
186 issued_token_type: Schema.Literal(AuthAccessTokenType),
187 token_type: Schema.Literal("Bearer"),
188 expires_in: Schema.Number,
189 scope: TrimmedNonEmptyString,
190});
191export type AuthAccessTokenResult = typeof AuthAccessTokenResult.Type;
192
193export const AuthWebSocketTicketResult = Schema.Struct({
194 ticket: TrimmedNonEmptyString,
195 expiresAt: Schema.DateTimeUtc,
196});
197export type AuthWebSocketTicketResult = typeof AuthWebSocketTicketResult.Type;
198
199export const AuthPairingCredentialResult = Schema.Struct({
200 id: TrimmedNonEmptyString,
201 credential: TrimmedNonEmptyString,
202 label: Schema.optionalKey(TrimmedNonEmptyString),
203 expiresAt: Schema.DateTimeUtc,
204});
205export type AuthPairingCredentialResult = typeof AuthPairingCredentialResult.Type;
206
207export const AuthPairingLink = Schema.Struct({
208 id: TrimmedNonEmptyString,
209 credential: TrimmedNonEmptyString,
210 scopes: AuthEnvironmentScopes,
211 subject: TrimmedNonEmptyString,
212 label: Schema.optionalKey(TrimmedNonEmptyString),
213 createdAt: Schema.DateTimeUtc,
214 expiresAt: Schema.DateTimeUtc,
215});
216export type AuthPairingLink = typeof AuthPairingLink.Type;
217
218export const AuthClientMetadata = Schema.Struct({
219 label: Schema.optionalKey(TrimmedNonEmptyString),
220 ipAddress: Schema.optionalKey(TrimmedNonEmptyString),
221 userAgent: Schema.optionalKey(TrimmedNonEmptyString),
222 deviceType: AuthClientMetadataDeviceType,
223 os: Schema.optionalKey(TrimmedNonEmptyString),
224 browser: Schema.optionalKey(TrimmedNonEmptyString),
225});
226export type AuthClientMetadata = typeof AuthClientMetadata.Type;
227
228export const AuthClientSession = Schema.Struct({
229 sessionId: AuthSessionId,
230 subject: TrimmedNonEmptyString,
231 scopes: AuthEnvironmentScopes,
232 method: ServerAuthSessionMethod,
233 client: AuthClientMetadata,
234 issuedAt: Schema.DateTimeUtc,
235 expiresAt: Schema.DateTimeUtc,
236 lastConnectedAt: Schema.NullOr(Schema.DateTimeUtc),
237 connected: Schema.Boolean,
238 current: Schema.Boolean,
239});
240export type AuthClientSession = typeof AuthClientSession.Type;
241
242export const AuthAccessSnapshot = Schema.Struct({
243 pairingLinks: Schema.Array(AuthPairingLink),
244 clientSessions: Schema.Array(AuthClientSession),
245});
246export type AuthAccessSnapshot = typeof AuthAccessSnapshot.Type;
247
248export const AuthAccessStreamSnapshotEvent = Schema.Struct({
249 version: Schema.Literal(1),
250 revision: Schema.Number,
251 type: Schema.Literal("snapshot"),
252 payload: AuthAccessSnapshot,
253});
254export type AuthAccessStreamSnapshotEvent = typeof AuthAccessStreamSnapshotEvent.Type;
255
256export const AuthAccessStreamPairingLinkUpsertedEvent = Schema.Struct({
257 version: Schema.Literal(1),
258 revision: Schema.Number,
259 type: Schema.Literal("pairingLinkUpserted"),
260 payload: AuthPairingLink,
261});
262export type AuthAccessStreamPairingLinkUpsertedEvent =
263 typeof AuthAccessStreamPairingLinkUpsertedEvent.Type;
264
265export const AuthAccessStreamPairingLinkRemovedEvent = Schema.Struct({
266 version: Schema.Literal(1),
267 revision: Schema.Number,
268 type: Schema.Literal("pairingLinkRemoved"),
269 payload: Schema.Struct({
270 id: TrimmedNonEmptyString,
271 }),
272});
273export type AuthAccessStreamPairingLinkRemovedEvent =
274 typeof AuthAccessStreamPairingLinkRemovedEvent.Type;
275
276export class AuthAccessStreamError extends Schema.TaggedErrorClass<AuthAccessStreamError>()(
277 "AuthAccessStreamError",
278 {
279 message: Schema.String,
280 },
281) {}
282
283export class EnvironmentAuthorizationError extends Schema.TaggedErrorClass<EnvironmentAuthorizationError>()(
284 "EnvironmentAuthorizationError",
285 {
286 message: Schema.String,
287 requiredScope: AuthEnvironmentScope,
288 },
289) {}
290
291export const AuthAccessStreamClientUpsertedEvent = Schema.Struct({
292 version: Schema.Literal(1),
293 revision: Schema.Number,
294 type: Schema.Literal("clientUpserted"),
295 payload: AuthClientSession,
296});
297export type AuthAccessStreamClientUpsertedEvent = typeof AuthAccessStreamClientUpsertedEvent.Type;
298
299export const AuthAccessStreamClientRemovedEvent = Schema.Struct({
300 version: Schema.Literal(1),
301 revision: Schema.Number,
302 type: Schema.Literal("clientRemoved"),
303 payload: Schema.Struct({
304 sessionId: AuthSessionId,
305 }),
306});
307export type AuthAccessStreamClientRemovedEvent = typeof AuthAccessStreamClientRemovedEvent.Type;
308
309export const AuthAccessStreamEvent = Schema.Union([
310 AuthAccessStreamSnapshotEvent,
311 AuthAccessStreamPairingLinkUpsertedEvent,
312 AuthAccessStreamPairingLinkRemovedEvent,
313 AuthAccessStreamClientUpsertedEvent,
314 AuthAccessStreamClientRemovedEvent,
315]);
316export type AuthAccessStreamEvent = typeof AuthAccessStreamEvent.Type;
317
318export const AuthRevokePairingLinkInput = Schema.Struct({
319 id: TrimmedNonEmptyString,
320});
321export type AuthRevokePairingLinkInput = typeof AuthRevokePairingLinkInput.Type;
322
323export const AuthRevokeClientSessionInput = Schema.Struct({
324 sessionId: AuthSessionId,
325});
326export type AuthRevokeClientSessionInput = typeof AuthRevokeClientSessionInput.Type;
327
328export const AuthCreatePairingCredentialInput = Schema.Struct({
329 label: Schema.optionalKey(TrimmedNonEmptyString),
330 scopes: Schema.optionalKey(AuthEnvironmentScopes),
331});
332export type AuthCreatePairingCredentialInput = typeof AuthCreatePairingCredentialInput.Type;
333
334export const AuthSessionState = Schema.Struct({
335 authenticated: Schema.Boolean,
336 auth: ServerAuthDescriptor,
337 scopes: Schema.optionalKey(AuthEnvironmentScopes),
338 sessionMethod: Schema.optionalKey(ServerAuthSessionMethod),
339 expiresAt: Schema.optionalKey(Schema.DateTimeUtc),
340});
341export type AuthSessionState = typeof AuthSessionState.Type;