import {z} from 'zod' export type AuthenticationPolicyRequest = {login: string, originIpAddress?: string | undefined}; export type CreateApiV1AccountsAccountIdResourcesResourceIdAuthenticationpolicyArgs = {accountId: string, resourceId: number, body: AuthenticationPolicyRequest}; export type CreateApiV1AccountsAccountIdResourcesResourceIdAuthenticationpolicyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const policyResponse = z.object({password: z.boolean().optional(), otp: z.boolean().optional(), qrCode: z.boolean().optional(), push: z.boolean().optional()}); export const authenticationPolicyResponse = z.object({username: z.string().optional(), email: z.string().optional(), hasPolicy: z.boolean().optional(), isAllowedToAuthenticate: z.boolean().optional(), isInSafeLocation: z.boolean().optional(), isInForgotToken: z.boolean().optional(), isBlocked: z.boolean().optional(), isOverallocated: z.boolean().optional(), isInQuarantine: z.boolean().optional(), policyResponse: policyResponse.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 }; /** * Returns whether the user can authenticate and which authentication methods they can use (Push, QR code, and OTP). * * POST /v1/accounts/{accountId}/resources/{resourceId}/authenticationpolicy */ export const createApiV1AccountsAccountIdResourcesResourceIdAuthenticationpolicy = async (args: CreateApiV1AccountsAccountIdResourcesResourceIdAuthenticationpolicyArgs, options?: CreateApiV1AccountsAccountIdResourcesResourceIdAuthenticationpolicyOptions) => { const {baseUrl = "https://api.usa.cloud.watchguard.com/rest/authpoint/authentication", fetch: fetchFn = fetch, ApiKeyAuth, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("WatchGuard-API-Key", ApiKeyAuth) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.accountId))}/resources/${encodeURIComponent(String(args.resourceId))}/authenticationpolicy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(authenticationPolicyResponse, await res.json()) };