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

auth.ts

266 lines9.8 KB

1import * as Schema from "effect/Schema";
2​
3import { AuthSessionId, TrimmedNonEmptyString } from "./baseSchemas.ts";
4​
5/**
6 * Declares the server's overall authentication posture.
7 *
8 * This is a high-level policy label that tells clients how the environment is
9 * expected to be accessed, not a transport detail and not an exhaustive list
10 * of every accepted credential.
11 *
12 * Typical usage:
13 * - rendered in auth/pairing UI so the user understands what kind of
14 * environment they are connecting to
15 * - used by clients to decide whether silent desktop bootstrap is expected or
16 * whether an explicit pairing flow should be shown
17 *
18 * Meanings:
19 * - `desktop-managed-local`: local desktop-managed environment with narrow
20 * trusted bootstrap, intended to avoid login prompts on the same machine
21 * - `loopback-browser`: standalone local server intended for browser pairing on
22 * the same machine
23 * - `remote-reachable`: environment intended to be reached from other devices
24 * or networks, where explicit pairing/auth is expected
25 * - `unsafe-no-auth`: intentionally unauthenticated mode; this is an explicit
26 * unsafe escape hatch, not a normal deployment mode
27 */
28export const ServerAuthPolicy = Schema.Literals([
29 "desktop-managed-local",
30 "loopback-browser",
31 "remote-reachable",
32 "unsafe-no-auth",
33]);
34export type ServerAuthPolicy = typeof ServerAuthPolicy.Type;
35​
36/**
37 * A credential type that can be exchanged for a real authenticated session.
38 *
39 * Bootstrap methods are for establishing trust at the start of a connection or
40 * pairing flow. They are not the long-lived credential used for ordinary
41 * authenticated HTTP / WebSocket traffic after pairing succeeds.
42 *
43 * Current methods:
44 * - `desktop-bootstrap`: a trusted local desktop handoff, used so the desktop
45 * shell can pair the renderer without a login screen
46 * - `one-time-token`: a short-lived pairing token, suitable for manual pairing
47 * flows such as `/pair?token=...`
48 */
49export const ServerAuthBootstrapMethod = Schema.Literals(["desktop-bootstrap", "one-time-token"]);
50export type ServerAuthBootstrapMethod = typeof ServerAuthBootstrapMethod.Type;
51​
52/**
53 * A credential type accepted for steady-state authenticated requests after a
54 * client has already paired.
55 *
56 * These methods are used by the server-wide auth layer for privileged HTTP and
57 * WebSocket access. They are distinct from bootstrap methods so clients can
58 * reason clearly about "pair first, then use session auth".
59 *
60 * Current methods:
61 * - `browser-session-cookie`: cookie-backed browser session, used by the web
62 * app after bootstrap/pairing
63 * - `bearer-session-token`: token-based session suitable for non-cookie or
64 * non-browser clients
65 */
66export const ServerAuthSessionMethod = Schema.Literals([
67 "browser-session-cookie",
68 "bearer-session-token",
69]);
70export type ServerAuthSessionMethod = typeof ServerAuthSessionMethod.Type;
71​
72export const AuthSessionRole = Schema.Literals(["owner", "client"]);
73export type AuthSessionRole = typeof AuthSessionRole.Type;
74​
75/**
76 * Server-advertised auth capabilities for a specific execution environment.
77 *
78 * Clients should treat this as the authoritative description of how that
79 * environment expects to be paired and how authenticated requests should be
80 * made afterward.
81 *
82 * Field meanings:
83 * - `policy`: high-level auth posture for the environment
84 * - `bootstrapMethods`: pairing/bootstrap methods the server is currently
85 * willing to accept
86 * - `sessionMethods`: authenticated request/session methods the server supports
87 * once pairing is complete
88 * - `sessionCookieName`: cookie name clients should expect when
89 * `browser-session-cookie` is in use
90 *
91 * This descriptor is intentionally capability-oriented. It lets clients choose
92 * the right UX without embedding server-specific auth logic or assuming a
93 * single access method.
94 */
95export const ServerAuthDescriptor = Schema.Struct({
96 policy: ServerAuthPolicy,
97 bootstrapMethods: Schema.Array(ServerAuthBootstrapMethod),
98 sessionMethods: Schema.Array(ServerAuthSessionMethod),
99 sessionCookieName: TrimmedNonEmptyString,
100});
101export type ServerAuthDescriptor = typeof ServerAuthDescriptor.Type;
102​
103export const AuthBootstrapInput = Schema.Struct({
104 credential: TrimmedNonEmptyString,
105});
106export type AuthBootstrapInput = typeof AuthBootstrapInput.Type;
107​
108export const AuthBootstrapResult = Schema.Struct({
109 authenticated: Schema.Literal(true),
110 role: AuthSessionRole,
111 sessionMethod: ServerAuthSessionMethod,
112 expiresAt: Schema.DateTimeUtcFromString,
113});
114export type AuthBootstrapResult = typeof AuthBootstrapResult.Type;
115​
116export const AuthBearerBootstrapResult = Schema.Struct({
117 authenticated: Schema.Literal(true),
118 role: AuthSessionRole,
119 sessionMethod: Schema.Literal("bearer-session-token"),
120 expiresAt: Schema.DateTimeUtcFromString,
121 sessionToken: TrimmedNonEmptyString,
122});
123export type AuthBearerBootstrapResult = typeof AuthBearerBootstrapResult.Type;
124​
125export const AuthWebSocketTokenResult = Schema.Struct({
126 token: TrimmedNonEmptyString,
127 expiresAt: Schema.DateTimeUtcFromString,
128});
129export type AuthWebSocketTokenResult = typeof AuthWebSocketTokenResult.Type;
130​
131export const AuthPairingCredentialResult = Schema.Struct({
132 id: TrimmedNonEmptyString,
133 credential: TrimmedNonEmptyString,
134 label: Schema.optionalKey(TrimmedNonEmptyString),
135 expiresAt: Schema.DateTimeUtcFromString,
136});
137export type AuthPairingCredentialResult = typeof AuthPairingCredentialResult.Type;
138​
139export const AuthPairingLink = Schema.Struct({
140 id: TrimmedNonEmptyString,
141 credential: TrimmedNonEmptyString,
142 role: AuthSessionRole,
143 subject: TrimmedNonEmptyString,
144 label: Schema.optionalKey(TrimmedNonEmptyString),
145 createdAt: Schema.DateTimeUtcFromString,
146 expiresAt: Schema.DateTimeUtcFromString,
147});
148export type AuthPairingLink = typeof AuthPairingLink.Type;
149​
150export const AuthClientMetadataDeviceType = Schema.Literals([
151 "desktop",
152 "mobile",
153 "tablet",
154 "bot",
155 "unknown",
156]);
157export type AuthClientMetadataDeviceType = typeof AuthClientMetadataDeviceType.Type;
158​
159export const AuthClientMetadata = Schema.Struct({
160 label: Schema.optionalKey(TrimmedNonEmptyString),
161 ipAddress: Schema.optionalKey(TrimmedNonEmptyString),
162 userAgent: Schema.optionalKey(TrimmedNonEmptyString),
163 deviceType: AuthClientMetadataDeviceType,
164 os: Schema.optionalKey(TrimmedNonEmptyString),
165 browser: Schema.optionalKey(TrimmedNonEmptyString),
166});
167export type AuthClientMetadata = typeof AuthClientMetadata.Type;
168​
169export const AuthClientSession = Schema.Struct({
170 sessionId: AuthSessionId,
171 subject: TrimmedNonEmptyString,
172 role: AuthSessionRole,
173 method: ServerAuthSessionMethod,
174 client: AuthClientMetadata,
175 issuedAt: Schema.DateTimeUtcFromString,
176 expiresAt: Schema.DateTimeUtcFromString,
177 lastConnectedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
178 connected: Schema.Boolean,
179 current: Schema.Boolean,
180});
181export type AuthClientSession = typeof AuthClientSession.Type;
182​
183export const AuthAccessSnapshot = Schema.Struct({
184 pairingLinks: Schema.Array(AuthPairingLink),
185 clientSessions: Schema.Array(AuthClientSession),
186});
187export type AuthAccessSnapshot = typeof AuthAccessSnapshot.Type;
188​
189export const AuthAccessStreamSnapshotEvent = Schema.Struct({
190 version: Schema.Literal(1),
191 revision: Schema.Number,
192 type: Schema.Literal("snapshot"),
193 payload: AuthAccessSnapshot,
194});
195export type AuthAccessStreamSnapshotEvent = typeof AuthAccessStreamSnapshotEvent.Type;
196​
197export const AuthAccessStreamPairingLinkUpsertedEvent = Schema.Struct({
198 version: Schema.Literal(1),
199 revision: Schema.Number,
200 type: Schema.Literal("pairingLinkUpserted"),
201 payload: AuthPairingLink,
202});
203export type AuthAccessStreamPairingLinkUpsertedEvent =
204 typeof AuthAccessStreamPairingLinkUpsertedEvent.Type;
205​
206export const AuthAccessStreamPairingLinkRemovedEvent = Schema.Struct({
207 version: Schema.Literal(1),
208 revision: Schema.Number,
209 type: Schema.Literal("pairingLinkRemoved"),
210 payload: Schema.Struct({
211 id: TrimmedNonEmptyString,
212 }),
213});
214export type AuthAccessStreamPairingLinkRemovedEvent =
215 typeof AuthAccessStreamPairingLinkRemovedEvent.Type;
216​
217export const AuthAccessStreamClientUpsertedEvent = Schema.Struct({
218 version: Schema.Literal(1),
219 revision: Schema.Number,
220 type: Schema.Literal("clientUpserted"),
221 payload: AuthClientSession,
222});
223export type AuthAccessStreamClientUpsertedEvent = typeof AuthAccessStreamClientUpsertedEvent.Type;
224​
225export const AuthAccessStreamClientRemovedEvent = Schema.Struct({
226 version: Schema.Literal(1),
227 revision: Schema.Number,
228 type: Schema.Literal("clientRemoved"),
229 payload: Schema.Struct({
230 sessionId: AuthSessionId,
231 }),
232});
233export type AuthAccessStreamClientRemovedEvent = typeof AuthAccessStreamClientRemovedEvent.Type;
234​
235export const AuthAccessStreamEvent = Schema.Union([
236 AuthAccessStreamSnapshotEvent,
237 AuthAccessStreamPairingLinkUpsertedEvent,
238 AuthAccessStreamPairingLinkRemovedEvent,
239 AuthAccessStreamClientUpsertedEvent,
240 AuthAccessStreamClientRemovedEvent,
241]);
242export type AuthAccessStreamEvent = typeof AuthAccessStreamEvent.Type;
243​
244export const AuthRevokePairingLinkInput = Schema.Struct({
245 id: TrimmedNonEmptyString,
246});
247export type AuthRevokePairingLinkInput = typeof AuthRevokePairingLinkInput.Type;
248​
249export const AuthRevokeClientSessionInput = Schema.Struct({
250 sessionId: AuthSessionId,
251});
252export type AuthRevokeClientSessionInput = typeof AuthRevokeClientSessionInput.Type;
253​
254export const AuthCreatePairingCredentialInput = Schema.Struct({
255 label: Schema.optionalKey(TrimmedNonEmptyString),
256});
257export type AuthCreatePairingCredentialInput = typeof AuthCreatePairingCredentialInput.Type;
258​
259export const AuthSessionState = Schema.Struct({
260 authenticated: Schema.Boolean,
261 auth: ServerAuthDescriptor,
262 role: Schema.optionalKey(AuthSessionRole),
263 sessionMethod: Schema.optionalKey(ServerAuthSessionMethod),
264 expiresAt: Schema.optionalKey(Schema.DateTimeUtcFromString),
265});
266export type AuthSessionState = typeof AuthSessionState.Type;