import {z} from 'zod' export type LockoutBackoff = 'linear' | 'exponential'; export type LockoutPolicy = {backoff?: LockoutBackoff | undefined, bucket_size?: number | undefined, max_secs?: number | undefined, start_secs?: number | undefined, threshold?: number | undefined}; export type PasswordPolicy = {apply_to_root?: boolean | undefined, cookie_max_age_secs?: number | undefined, history_count?: number | undefined, history_max_retained?: number | undefined, lockout?: LockoutPolicy | undefined, max_length?: number | undefined, min_length?: number | undefined, require_digit?: boolean | undefined, require_lowercase?: boolean | undefined, require_special?: boolean | undefined, require_uppercase?: boolean | undefined, rotation_days?: number | undefined, rotation_warning_days?: number | undefined, special_char_set?: string | undefined}; export type UpdateApiApiOrgIdSettingsPasswordPolicyArgs = {org_id: string, body: PasswordPolicy}; export type UpdateApiApiOrgIdSettingsPasswordPolicyOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const lockoutBackoff = z.enum(["linear", "exponential"]); export const lockoutPolicy = z.object({backoff: lockoutBackoff.optional(), bucket_size: z.number().int().gte(0).optional(), max_secs: z.number().int().gte(0).optional(), start_secs: z.number().int().gte(0).optional(), threshold: z.number().int().gte(0).optional()}); export const passwordPolicy = z.object({apply_to_root: z.boolean().optional(), cookie_max_age_secs: z.number().int().gte(0).optional(), history_count: z.number().int().gte(0).optional(), history_max_retained: z.number().int().gte(0).optional(), lockout: lockoutPolicy.optional(), max_length: z.number().int().gte(0).optional(), min_length: z.number().int().gte(0).optional(), require_digit: z.boolean().optional(), require_lowercase: z.boolean().optional(), require_special: z.boolean().optional(), require_uppercase: z.boolean().optional(), rotation_days: z.number().int().gte(0).optional(), rotation_warning_days: z.number().int().gte(0).optional(), special_char_set: z.string().optional()}); export const setPasswordPolicyResponse = z.object({policy: passwordPolicy, users_flagged: z.number().int().gte(0)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Configure the password policy * * PUT /api/{org_id}/settings/password_policy */ export const updateApiApiOrgIdSettingsPasswordPolicy = async (args: UpdateApiApiOrgIdSettingsPasswordPolicyArgs, options: UpdateApiApiOrgIdSettingsPasswordPolicyOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/settings/password_policy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(setPasswordPolicyResponse, await res.json()) };