import {z} from 'zod' export type JumpcloudPasswordPasswordPolicy = {allowUnenrolledMFAPasswordReset?: boolean | undefined, allowUsernameSubstring?: boolean | undefined, daysAfterExpirationToSelfRecover?: number | undefined, daysBeforeExpirationToForceReset?: number | undefined, default?: boolean | undefined, description?: string | undefined, disallowCommonlyUsedPasswords?: boolean | undefined, disallowCustomBannedPasswords?: boolean | undefined, disallowNameSubstring?: boolean | undefined, disallowSequentialOrRepetitiveChars?: boolean | undefined, displayComplexityOnResetScreen?: boolean | undefined, effectiveDate?: string | undefined, enableDaysAfterExpirationToSelfRecover?: boolean | undefined, enableDaysBeforeExpirationToForceReset?: boolean | undefined, enableLockoutTimeInSeconds?: boolean | undefined, enableMaxHistory?: boolean | undefined, enableMaxLoginAttempts?: boolean | undefined, enableMinChangePeriodInDays?: boolean | undefined, enableMinLength?: boolean | undefined, enableNameConsecutiveChars?: boolean | undefined, enablePasswordExpirationInDays?: boolean | undefined, enableRecoveryEmail?: boolean | undefined, enableResetLockoutCounter?: boolean | undefined, lockoutTimeInSeconds?: number | undefined, maxHistory?: number | undefined, maxLoginAttempts?: number | undefined, minChangePeriodInDays?: number | undefined, minLength?: number | undefined, name?: string | undefined, nameConsecutiveChars?: number | undefined, needsLowercase?: boolean | undefined, needsNumeric?: boolean | undefined, needsSymbolic?: boolean | undefined, needsUppercase?: boolean | undefined, passwordExpirationInDays?: number | undefined, precedence?: number | undefined, resetLockoutCounterMinutes?: number | undefined}; export type UpdateApiPasswordpoliciesObjectIdArgs = {objectId: string, body: {groupIds?: Array | undefined, organizationObjectId?: string | undefined, policy?: JumpcloudPasswordPasswordPolicy | undefined}}; export type UpdateApiPasswordpoliciesObjectIdOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudPasswordGroupResponse = z.object({groupId: z.string().optional(), name: z.string().optional()}); export const jumpcloudPasswordPasswordPolicy = z.object({allowUnenrolledMFAPasswordReset: z.boolean().optional(), allowUsernameSubstring: z.boolean().optional(), daysAfterExpirationToSelfRecover: z.number().int().optional(), daysBeforeExpirationToForceReset: z.number().int().optional(), default: z.boolean().optional(), description: z.string().optional(), disallowCommonlyUsedPasswords: z.boolean().optional(), disallowCustomBannedPasswords: z.boolean().optional(), disallowNameSubstring: z.boolean().optional(), disallowSequentialOrRepetitiveChars: z.boolean().optional(), displayComplexityOnResetScreen: z.boolean().optional(), effectiveDate: z.string().optional(), enableDaysAfterExpirationToSelfRecover: z.boolean().optional(), enableDaysBeforeExpirationToForceReset: z.boolean().optional(), enableLockoutTimeInSeconds: z.boolean().optional(), enableMaxHistory: z.boolean().optional(), enableMaxLoginAttempts: z.boolean().optional(), enableMinChangePeriodInDays: z.boolean().optional(), enableMinLength: z.boolean().optional(), enableNameConsecutiveChars: z.boolean().optional(), enablePasswordExpirationInDays: z.boolean().optional(), enableRecoveryEmail: z.boolean().optional(), enableResetLockoutCounter: z.boolean().optional(), lockoutTimeInSeconds: z.number().int().optional(), maxHistory: z.number().int().optional(), maxLoginAttempts: z.number().int().optional(), minChangePeriodInDays: z.number().int().optional(), minLength: z.number().int().optional(), name: z.string().optional(), nameConsecutiveChars: z.number().int().optional(), needsLowercase: z.boolean().optional(), needsNumeric: z.boolean().optional(), needsSymbolic: z.boolean().optional(), needsUppercase: z.boolean().optional(), passwordExpirationInDays: z.number().int().optional(), precedence: z.number().int().optional(), resetLockoutCounterMinutes: z.number().int().optional()}); export const jumpcloudPasswordResponse = z.object({cached: z.boolean().optional(), groups: z.array(jumpcloudPasswordGroupResponse).optional(), objectId: z.string().optional(), organizationObjectId: z.string().optional(), policy: jumpcloudPasswordPasswordPolicy.optional()}); 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 }; /** * Update Password Policy * * PUT /passwordpolicies/{objectId} */ export const updateApiPasswordpoliciesObjectId = async (args: UpdateApiPasswordpoliciesObjectIdArgs, options?: UpdateApiPasswordpoliciesObjectIdOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/passwordpolicies/${encodeURIComponent(String(args.objectId))}${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(jumpcloudPasswordResponse, await res.json()) };