Repository navigation
Expand file tree
/
Copy pathsystemConfig.ts
More file actions
421 lines (374 loc) · 13.5 KB
/
Copy pathsystemConfig.ts
File metadata and controls
421 lines (374 loc) · 13.5 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import {
AvailableRolesResponseSchema,
OAuthProviderConfigSchema,
SystemConfigPatchSchema,
SystemConfigSchema,
UpdateSystemConfigResponseSchema,
type OAuthProviderConfig,
type SystemConfigPatch,
type UpdateSystemConfigResponse,
} from "@seamless-auth/types";
import { z } from "zod";
import type { AuthClient } from "./authClient.js";
import { PermissionError } from "./permissionError.js";
import { scrubTokens } from "./redact.js";
import { forbidden } from "./stepUp.js";
export { PermissionError };
// What an instance stores, typed with the shared schema's input side but every key
// optional and others allowed: the instance may be older or newer than this CLI's
// types. Reads are deliberately not parsed with SystemConfigSchema. Its defaults
// would invent keys an older instance does not have, and its value rules (which can
// tighten between versions) would make `config get` fail on a value the instance
// accepted. Only the shape is checked.
export type SystemConfig = Partial<z.input<typeof SystemConfigSchema>> &
Record<string, unknown>;
// What a config file or `config set` supplies, before the instance validates it.
export type SystemConfigInput = Record<string, unknown>;
// The keys `config set` and `config apply` send. The instance's patch schema is
// strict and rejects the whole patch over one key it does not know, so a key goes
// here only once a released auth API accepts it, never on a types bump alone (see
// "Config keys ahead of the API" in AGENTS.md). `satisfies` ties each entry to the
// shared patch schema, and a test fails when that schema gains a key that is in
// neither this list nor NOT_YET_WRITABLE, so a bump cannot add one unnoticed.
export const WRITABLE_KEYS = [
"app_name",
"default_roles",
"available_roles",
"login_methods",
"passkey_login_fallback_enabled",
"prompt_passkey_enrollment",
"phishing_resistant_only",
"oauth_providers",
"lockout_policy",
"authenticator_policy",
"access_token_ttl",
"session_idle_ttl",
"refresh_token_ttl",
"max_concurrent_sessions",
"rate_limit",
"delay_after",
"flow_rate_limits",
"rpid",
"origins",
"magic_link_redirect_uris",
"recovery_codes",
"account_recovery",
"hosted_ui",
"oidc_provider_enabled",
"oidc_issuer",
"allow_client_secrets",
"third_party_clients_enabled",
] as const satisfies readonly (keyof SystemConfigPatch)[];
// In the shared patch schema but not yet accepted by a released auth API this CLI
// can rely on, each with the ticket that tracks it. Move a key to WRITABLE_KEYS
// once it is.
export const NOT_YET_WRITABLE = {
oidc_cli_client_id: "#351",
} as const satisfies Partial<
Record<keyof SystemConfigPatch, string>
>;
const WRITABLE = new Set<string>(WRITABLE_KEYS);
const KNOWN = new Set<string>(Object.keys(SystemConfigSchema.shape));
export class ConfigApiError extends Error {
constructor(message: string) {
super(message);
this.name = "ConfigApiError";
}
}
const ConfigObjectSchema = z.record(z.string(), z.unknown());
export async function getSystemConfig(
client: AuthClient,
): Promise<SystemConfig> {
const res = await client.get<unknown>("/system-config/admin");
if (res.status === 403) throw forbidden(res);
if (!res.ok) {
throw new ConfigApiError(`Could not read system config (${res.status}).`);
}
const parsed = ConfigObjectSchema.safeParse(res.data);
if (!parsed.success) {
throw new ConfigApiError(
"The instance returned a system config this CLI cannot read.",
);
}
return parsed.data as SystemConfig;
}
export async function getRoles(client: AuthClient): Promise<string[]> {
const res = await client.get<unknown>("/system-config/roles");
if (res.status === 403) throw forbidden(res);
if (!res.ok) {
throw new ConfigApiError(`Could not read roles (${res.status}).`);
}
const parsed = AvailableRolesResponseSchema.safeParse(res.data);
if (!parsed.success) {
throw new ConfigApiError("The instance returned a role list this CLI cannot read.");
}
return parsed.data.roles;
}
export type PatchResult = UpdateSystemConfigResponse;
export async function patchSystemConfig(
client: AuthClient,
patch: SystemConfigInput,
): Promise<PatchResult> {
const res = await client.request<{
success?: boolean;
updatedKeys?: string[];
error?: string;
details?: unknown;
}>("/system-config/admin", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(patch),
});
if (res.status === 403) throw forbidden(res);
if (res.status === 400) {
const reason = res.data?.error ?? "Invalid configuration";
const details = res.data?.details
? ` ${JSON.stringify(scrubTokens(res.data.details))}`
: "";
throw new ConfigApiError(`${reason}.${details}`);
}
if (!res.ok) {
throw new ConfigApiError(`Could not update system config (${res.status}).`);
}
// The patch has been applied by now, so an answer this CLI cannot read is reported
// as no keys listed rather than as a failure.
const parsed = UpdateSystemConfigResponseSchema.safeParse(res.data);
return parsed.success ? parsed.data : { success: true, updatedKeys: [] };
}
// A provider as the instance stores it. As with SystemConfig, reads are not parsed
// with OAuthProviderConfigSchema, whose defaults would invent settings; only the id
// every provider has is checked.
export type OAuthProvider = Partial<z.input<typeof OAuthProviderConfigSchema>> &
Pick<OAuthProviderConfig, "id"> &
Record<string, unknown>;
// What `oauth-providers add` and `update` read from the developer, before the
// instance validates it.
export type OAuthProviderInput = Record<string, unknown>;
const StoredProviderSchema = OAuthProviderConfigSchema.pick({ id: true }).loose();
const ProviderListSchema = z.object({ providers: z.array(StoredProviderSchema) });
const ProviderEnvelopeSchema = z.object({ provider: StoredProviderSchema });
const OAUTH_PROVIDERS_PATH = "/system-config/oauth-providers";
function providerMutationError(
res: {
status: number;
data: { error?: string; message?: string; details?: unknown } | null;
},
action: string,
id?: unknown,
): never {
if (res.status === 403) throw forbidden(res);
const label = id ? ` "${String(id)}"` : "";
if (res.status === 404) {
throw new ConfigApiError(`OAuth provider${label} not found.`);
}
if (res.status === 409) {
throw new ConfigApiError(
res.data?.error ?? `OAuth provider${label} already exists.`,
);
}
if (res.status === 400) {
const reason = res.data?.error ?? "Invalid OAuth provider";
const details = res.data?.details
? ` ${JSON.stringify(scrubTokens(res.data.details))}`
: "";
throw new ConfigApiError(`${reason}.${details}`);
}
if (res.status === 502) {
const reason = res.data?.message ? `: ${res.data.message}` : ".";
throw new ConfigApiError(
`Could not read the issuer's discovery document for OAuth provider${label}${reason}`,
);
}
throw new ConfigApiError(`Could not ${action} OAuth provider (${res.status}).`);
}
export async function listOAuthProviders(
client: AuthClient,
): Promise<OAuthProvider[]> {
const res = await client.get<unknown>(OAUTH_PROVIDERS_PATH);
if (res.status === 403) throw forbidden(res);
if (!res.ok) {
throw new ConfigApiError(`Could not list OAuth providers (${res.status}).`);
}
const parsed = ProviderListSchema.safeParse(res.data);
if (!parsed.success) {
throw new ConfigApiError(
"The instance returned an OAuth provider list this CLI cannot read.",
);
}
return parsed.data.providers as OAuthProvider[];
}
// Like a config patch, a provider change has been applied by the time this reads
// the answer, so one it cannot read falls back to what was sent.
function providerFrom(data: unknown): OAuthProvider | undefined {
const parsed = ProviderEnvelopeSchema.safeParse(data);
return parsed.success ? (parsed.data.provider as OAuthProvider) : undefined;
}
export async function createOAuthProvider(
client: AuthClient,
provider: OAuthProviderInput,
): Promise<OAuthProviderInput> {
const res = await client.request<{ error?: string; details?: unknown }>(
OAUTH_PROVIDERS_PATH,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(provider),
},
);
if (res.ok) return providerFrom(res.data) ?? provider;
throw providerMutationError(res, "add", provider.id);
}
export async function updateOAuthProvider(
client: AuthClient,
id: string,
updates: OAuthProviderInput,
): Promise<OAuthProviderInput> {
const res = await client.request<{ error?: string; details?: unknown }>(
`${OAUTH_PROVIDERS_PATH}/${encodeURIComponent(id)}`,
{
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updates),
},
);
if (res.ok) return providerFrom(res.data) ?? updates;
throw providerMutationError(res, "update", id);
}
// Re-reads the issuer's discovery document and drops the instance's cached signing
// keys. Nothing is stored; the answer is the provider as sign-in will now use it.
export async function refreshOAuthProviderDiscovery(
client: AuthClient,
id: string,
): Promise<OAuthProvider> {
const res = await client.request<{
error?: string;
message?: string;
details?: unknown;
}>(`${OAUTH_PROVIDERS_PATH}/${encodeURIComponent(id)}/discovery`, {
method: "POST",
});
if (!res.ok) throw providerMutationError(res, "refresh", id);
const provider = providerFrom(res.data);
if (!provider) {
throw new ConfigApiError(
"The instance returned an OAuth provider this CLI cannot read.",
);
}
return provider;
}
export async function deleteOAuthProvider(
client: AuthClient,
id: string,
): Promise<void> {
const res = await client.request<{ error?: string; details?: unknown }>(
`${OAUTH_PROVIDERS_PATH}/${encodeURIComponent(id)}`,
{ method: "DELETE" },
);
if (res.ok) return;
throw providerMutationError(res, "remove", id);
}
// The writable keys the shared patch schema types as a plain string. Their values are
// never JSON-parsed, so `config set app_name 123` sends the string "123" rather than
// the number 123, and `config set rpid true` sends "true". Everything else is parsed
// as JSON, falling back to the raw string, which is what makes `access_token_ttl 15m`
// work: a TTL is string-typed, but the fallback would have carried it anyway.
const STRING_KEYS = new Set<string>(
WRITABLE_KEYS.filter((key) => {
const field = SystemConfigPatchSchema.shape[key];
return field instanceof z.ZodOptional && field.unwrap() instanceof z.ZodString;
}),
);
export function isStringKey(key: string): boolean {
return STRING_KEYS.has(key);
}
export function parseValue(raw: string, key?: string): unknown {
const trimmed = raw.trim();
if (key !== undefined && STRING_KEYS.has(key)) return trimmed;
try {
return JSON.parse(trimmed);
} catch {
return raw;
}
}
// What `config set` checks before sending, so `config set oidc_provider_enabled yes`
// names the value it wanted rather than surfacing the instance's 400.
const VALUE_RULES: Partial<Record<(typeof WRITABLE_KEYS)[number], string>> = {
oidc_provider_enabled: "true or false",
oidc_issuer: "a URL, or null to clear it",
allow_client_secrets: "true or false",
third_party_clients_enabled: "true or false",
};
export function checkValue(key: string, value: unknown): void {
const rule = VALUE_RULES[key as keyof typeof VALUE_RULES];
if (!rule) return;
const field =
SystemConfigPatchSchema.shape[key as keyof typeof SystemConfigPatchSchema.shape];
if (!field.safeParse(value).success) {
throw new ConfigApiError(`${key} must be ${rule}.`);
}
}
export function isWritableKey(key: string): boolean {
return WRITABLE.has(key);
}
// Whether the shared config schema has this key at all, as opposed to a key the
// instance has but does not let a patch change.
export function isKnownKey(key: string): boolean {
return KNOWN.has(key);
}
export function filterWritable(config: SystemConfigInput): {
patch: SystemConfigInput;
readOnly: string[];
unknown: string[];
} {
const patch: SystemConfigInput = {};
const readOnly: string[] = [];
const unknown: string[] = [];
for (const [key, value] of Object.entries(config)) {
if (WRITABLE.has(key)) patch[key] = value;
else if (KNOWN.has(key)) readOnly.push(key);
else unknown.push(key);
}
return { patch, readOnly, unknown };
}
export function deepEqual(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (a === null || b === null) return a === b;
if (typeof a !== typeof b) return false;
if (Array.isArray(a) || Array.isArray(b)) {
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
return false;
}
return a.every((item, i) => deepEqual(item, b[i]));
}
if (typeof a === "object" && typeof b === "object") {
const aKeys = Object.keys(a as object);
const bKeys = Object.keys(b as object);
if (aKeys.length !== bKeys.length) return false;
return aKeys.every(
(key) =>
Object.prototype.hasOwnProperty.call(b, key) &&
deepEqual(
(a as Record<string, unknown>)[key],
(b as Record<string, unknown>)[key],
),
);
}
return false;
}
export interface ConfigChange {
key: string;
from: unknown;
to: unknown;
}
export function diffConfig(
local: SystemConfigInput,
remote: SystemConfig,
): ConfigChange[] {
const changes: ConfigChange[] = [];
for (const [key, to] of Object.entries(local)) {
if (!deepEqual(remote[key], to)) {
changes.push({ key, from: remote[key], to });
}
}
return changes;
}