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

auth.ts

348 lines13.0 KB

1import * as Schema from "effect/Schema";
2import * as HttpApiSchema from "effect/unstable/httpapi/HttpApiSchema";
3​
4import { AuthSessionId, ClientSurface, 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 osMajorVersion: Schema.optionalKey(Schema.Int),
173 deviceModel: Schema.optionalKey(TrimmedNonEmptyString),
174 surface: Schema.optionalKey(ClientSurface),
175 appVersion: Schema.optionalKey(TrimmedNonEmptyString),
176});
177export type AuthClientPresentationMetadata = typeof AuthClientPresentationMetadata.Type;
178​
179export const AuthTokenExchangeRequest = Schema.Struct({
180 grant_type: Schema.Literal(AuthTokenExchangeGrantType),
181 subject_token: TrimmedNonEmptyString,
182 subject_token_type: Schema.Literal(AuthEnvironmentBootstrapTokenType),
183 requested_token_type: Schema.Literal(AuthAccessTokenType),
184 scope: Schema.optionalKey(TrimmedNonEmptyString),
185 client_label: Schema.optionalKey(TrimmedNonEmptyString),
186 client_device_type: Schema.optionalKey(AuthClientMetadataDeviceType),
187 client_os: Schema.optionalKey(TrimmedNonEmptyString),
188}).pipe(HttpApiSchema.asFormUrlEncoded());
189export type AuthTokenExchangeRequest = typeof AuthTokenExchangeRequest.Type;
190​
191export const AuthAccessTokenResult = Schema.Struct({
192 access_token: TrimmedNonEmptyString,
193 issued_token_type: Schema.Literal(AuthAccessTokenType),
194 token_type: Schema.Literals(["Bearer", "DPoP"]),
195 expires_in: Schema.Number,
196 scope: TrimmedNonEmptyString,
197});
198export type AuthAccessTokenResult = typeof AuthAccessTokenResult.Type;
199​
200export const AuthWebSocketTicketResult = Schema.Struct({
201 ticket: TrimmedNonEmptyString,
202 expiresAt: Schema.DateTimeUtc,
203});
204export type AuthWebSocketTicketResult = typeof AuthWebSocketTicketResult.Type;
205​
206export const AuthPairingCredentialResult = Schema.Struct({
207 id: TrimmedNonEmptyString,
208 credential: TrimmedNonEmptyString,
209 label: Schema.optionalKey(TrimmedNonEmptyString),
210 expiresAt: Schema.DateTimeUtc,
211});
212export type AuthPairingCredentialResult = typeof AuthPairingCredentialResult.Type;
213​
214export const AuthPairingLink = Schema.Struct({
215 id: TrimmedNonEmptyString,
216 credential: TrimmedNonEmptyString,
217 scopes: AuthEnvironmentScopes,
218 subject: TrimmedNonEmptyString,
219 label: Schema.optionalKey(TrimmedNonEmptyString),
220 createdAt: Schema.DateTimeUtc,
221 expiresAt: Schema.DateTimeUtc,
222});
223export type AuthPairingLink = typeof AuthPairingLink.Type;
224​
225export const AuthClientMetadata = Schema.Struct({
226 label: Schema.optionalKey(TrimmedNonEmptyString),
227 ipAddress: Schema.optionalKey(TrimmedNonEmptyString),
228 userAgent: Schema.optionalKey(TrimmedNonEmptyString),
229 deviceType: AuthClientMetadataDeviceType,
230 os: Schema.optionalKey(TrimmedNonEmptyString),
231 browser: Schema.optionalKey(TrimmedNonEmptyString),
232});
233export type AuthClientMetadata = typeof AuthClientMetadata.Type;
234​
235export const AuthClientSession = Schema.Struct({
236 sessionId: AuthSessionId,
237 subject: TrimmedNonEmptyString,
238 scopes: AuthEnvironmentScopes,
239 method: ServerAuthSessionMethod,
240 client: AuthClientMetadata,
241 issuedAt: Schema.DateTimeUtc,
242 expiresAt: Schema.DateTimeUtc,
243 lastConnectedAt: Schema.NullOr(Schema.DateTimeUtc),
244 connected: Schema.Boolean,
245 current: Schema.Boolean,
246});
247export type AuthClientSession = typeof AuthClientSession.Type;
248​
249export const AuthAccessSnapshot = Schema.Struct({
250 pairingLinks: Schema.Array(AuthPairingLink),
251 clientSessions: Schema.Array(AuthClientSession),
252});
253export type AuthAccessSnapshot = typeof AuthAccessSnapshot.Type;
254​
255export const AuthAccessStreamSnapshotEvent = Schema.Struct({
256 version: Schema.Literal(1),
257 revision: Schema.Number,
258 type: Schema.Literal("snapshot"),
259 payload: AuthAccessSnapshot,
260});
261export type AuthAccessStreamSnapshotEvent = typeof AuthAccessStreamSnapshotEvent.Type;
262​
263export const AuthAccessStreamPairingLinkUpsertedEvent = Schema.Struct({
264 version: Schema.Literal(1),
265 revision: Schema.Number,
266 type: Schema.Literal("pairingLinkUpserted"),
267 payload: AuthPairingLink,
268});
269export type AuthAccessStreamPairingLinkUpsertedEvent =
270 typeof AuthAccessStreamPairingLinkUpsertedEvent.Type;
271​
272export const AuthAccessStreamPairingLinkRemovedEvent = Schema.Struct({
273 version: Schema.Literal(1),
274 revision: Schema.Number,
275 type: Schema.Literal("pairingLinkRemoved"),
276 payload: Schema.Struct({
277 id: TrimmedNonEmptyString,
278 }),
279});
280export type AuthAccessStreamPairingLinkRemovedEvent =
281 typeof AuthAccessStreamPairingLinkRemovedEvent.Type;
282​
283export class AuthAccessStreamError extends Schema.TaggedErrorClass<AuthAccessStreamError>()(
284 "AuthAccessStreamError",
285 {
286 message: Schema.String,
287 },
288) {}
289​
290export class EnvironmentAuthorizationError extends Schema.TaggedErrorClass<EnvironmentAuthorizationError>()(
291 "EnvironmentAuthorizationError",
292 {
293 message: Schema.String,
294 requiredScope: AuthEnvironmentScope,
295 },
296) {}
297​
298export const AuthAccessStreamClientUpsertedEvent = Schema.Struct({
299 version: Schema.Literal(1),
300 revision: Schema.Number,
301 type: Schema.Literal("clientUpserted"),
302 payload: AuthClientSession,
303});
304export type AuthAccessStreamClientUpsertedEvent = typeof AuthAccessStreamClientUpsertedEvent.Type;
305​
306export const AuthAccessStreamClientRemovedEvent = Schema.Struct({
307 version: Schema.Literal(1),
308 revision: Schema.Number,
309 type: Schema.Literal("clientRemoved"),
310 payload: Schema.Struct({
311 sessionId: AuthSessionId,
312 }),
313});
314export type AuthAccessStreamClientRemovedEvent = typeof AuthAccessStreamClientRemovedEvent.Type;
315​
316export const AuthAccessStreamEvent = Schema.Union([
317 AuthAccessStreamSnapshotEvent,
318 AuthAccessStreamPairingLinkUpsertedEvent,
319 AuthAccessStreamPairingLinkRemovedEvent,
320 AuthAccessStreamClientUpsertedEvent,
321 AuthAccessStreamClientRemovedEvent,
322]);
323export type AuthAccessStreamEvent = typeof AuthAccessStreamEvent.Type;
324​
325export const AuthRevokePairingLinkInput = Schema.Struct({
326 id: TrimmedNonEmptyString,
327});
328export type AuthRevokePairingLinkInput = typeof AuthRevokePairingLinkInput.Type;
329​
330export const AuthRevokeClientSessionInput = Schema.Struct({
331 sessionId: AuthSessionId,
332});
333export type AuthRevokeClientSessionInput = typeof AuthRevokeClientSessionInput.Type;
334​
335export const AuthCreatePairingCredentialInput = Schema.Struct({
336 label: Schema.optionalKey(TrimmedNonEmptyString),
337 scopes: Schema.optionalKey(AuthEnvironmentScopes),
338});
339export type AuthCreatePairingCredentialInput = typeof AuthCreatePairingCredentialInput.Type;
340​
341export const AuthSessionState = Schema.Struct({
342 authenticated: Schema.Boolean,
343 auth: ServerAuthDescriptor,
344 scopes: Schema.optionalKey(AuthEnvironmentScopes),
345 sessionMethod: Schema.optionalKey(ServerAuthSessionMethod),
346 expiresAt: Schema.optionalKey(Schema.DateTimeUtc),
347});
348export type AuthSessionState = typeof AuthSessionState.Type;