import {z} from 'zod' export type AuthnPolicyObligations = {mfa?: ({required?: boolean | undefined, interval?: ({preset?: string | undefined, custom?: ({value?: number | undefined, unit?: string | undefined}) | undefined}) | undefined}) | undefined, userVerification?: ({requirement?: ('none' | 'optional' | 'required') | undefined}) | undefined, mfaFactors?: (Array<{type?: ('DURT' | 'WEBAUTHN' | 'PUSH' | 'DUO' | 'TOTP' | 'SMS_OTP') | undefined}>) | undefined}; export type AuthnPolicyHelpLink = {text?: string | undefined, url?: string | undefined}; export type AuthnPolicyCustomErrorMessage = {message: string, enabled: boolean, primary_help?: AuthnPolicyHelpLink | undefined, secondary_help?: AuthnPolicyHelpLink | undefined}; export type AuthnPolicyEffect = {action: 'allow' | 'deny' | 'unknown', obligations?: AuthnPolicyObligations | undefined, custom_error_message?: AuthnPolicyCustomErrorMessage | undefined}; export type AuthnPolicyResourceTarget = {type: 'admin_portal' | 'user_portal' | 'application' | 'ldap', id?: string | undefined}; export type AnyValue = unknown; export type AuthnPolicyUserAttributeFilter = {field?: string | undefined, operator?: 'EQ' | undefined, value?: AnyValue | undefined}; export type AuthnPolicyUserAttributeTarget = {exclusions?: Array | undefined, inclusions?: Array | undefined}; export type AuthnPolicyUserGroupTarget = {inclusions?: Array | undefined, exclusions?: Array | undefined}; export type AuthnPolicyUserTarget = {inclusions?: Array | undefined}; export type AuthnPolicyTargets = {excludedApplications?: Array | undefined, resources?: Array | undefined, userAttributes?: AuthnPolicyUserAttributeTarget | undefined, userGroups?: AuthnPolicyUserGroupTarget | undefined, users?: AuthnPolicyUserTarget | undefined}; export type AuthnPolicyType = 'user_portal' | 'application' | 'ldap' | 'admin_portal'; export type AuthnPolicy = {id?: string | undefined, name?: string | undefined, description?: string | undefined, disabled?: boolean | undefined, monitorOnly?: boolean | undefined, managedBy?: ('ORGANIZATION' | 'ENTERPRISE' | 'UNKNOWN') | undefined, orgId?: string | undefined, orgName?: string | undefined, effect?: AuthnPolicyEffect | undefined, targets?: AuthnPolicyTargets | undefined, type?: AuthnPolicyType | undefined, conditions?: Record | undefined}; export type PatchApiApiV2AuthnPoliciesIdArgs = {'x-org-id'?: string | undefined, body: AuthnPolicy}; export type PatchApiApiV2AuthnPoliciesIdOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const authnPolicyObligations = z.object({mfa: z.object({required: z.boolean().optional(), interval: z.object({preset: z.string().optional(), custom: z.object({value: z.number().int().optional(), unit: z.string().optional()}).optional()}).optional()}).optional(), userVerification: z.object({requirement: z.enum(["none", "optional", "required"]).optional()}).optional(), mfaFactors: z.array(z.object({type: z.enum(["DURT", "WEBAUTHN", "PUSH", "DUO", "TOTP", "SMS_OTP"]).optional()})).optional()}); export const authnPolicyHelpLink = z.object({text: z.string().optional(), url: z.string().optional()}); export const authnPolicyCustomErrorMessage = z.object({message: z.string(), enabled: z.boolean(), primary_help: authnPolicyHelpLink.optional(), secondary_help: authnPolicyHelpLink.optional()}); export const authnPolicyEffect = z.object({action: z.enum(["allow", "deny", "unknown"]), obligations: authnPolicyObligations.optional(), custom_error_message: authnPolicyCustomErrorMessage.optional()}); export const authnPolicyResourceTarget = z.object({type: z.enum(["admin_portal", "user_portal", "application", "ldap"]), id: z.string().optional()}); export const anyValue = z.unknown(); export const authnPolicyUserAttributeFilter = z.object({field: z.string().optional(), operator: z.literal("EQ").optional(), value: anyValue.optional()}); export const authnPolicyUserAttributeTarget = z.object({exclusions: z.array(authnPolicyUserAttributeFilter).optional(), inclusions: z.array(authnPolicyUserAttributeFilter).optional()}); export const authnPolicyUserGroupTarget = z.object({inclusions: z.array(z.string()).optional(), exclusions: z.array(z.string()).optional()}); export const authnPolicyUserTarget = z.object({inclusions: z.array(z.string()).optional()}); export const authnPolicyTargets = z.object({excludedApplications: z.array(z.string()).optional(), resources: z.array(authnPolicyResourceTarget).optional(), userAttributes: authnPolicyUserAttributeTarget.optional(), userGroups: authnPolicyUserGroupTarget.optional(), users: authnPolicyUserTarget.optional()}); export const authnPolicyType = z.enum(["user_portal", "application", "ldap", "admin_portal"]); export const authnPolicy = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), disabled: z.boolean().optional(), monitorOnly: z.boolean().optional(), managedBy: z.enum(["ORGANIZATION", "ENTERPRISE", "UNKNOWN"]).optional(), orgId: z.string().optional(), orgName: z.string().optional(), effect: authnPolicyEffect.optional(), targets: authnPolicyTargets.optional(), type: authnPolicyType.optional(), conditions: z.object({}).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 }; /** * Patch Authentication Policy * * PATCH /api/v2/authn/policies/{id} */ export const patchApiApiV2AuthnPoliciesId = async (args: PatchApiApiV2AuthnPoliciesIdArgs, options?: PatchApiApiV2AuthnPoliciesIdOptions) => { const {baseUrl = "https://console.jumpcloud.com", 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 (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/authn/policies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(authnPolicy, await res.json()) };