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