import {z} from 'zod' export type GetApiCurrentIdentityMfaOptions = {baseUrl?: string | undefined, ztSession?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({comment: z.string().optional(), href: z.string(), method: z.string().optional()}); export const tags = z.record(z.string(), z.object({})); export const detailMfa = z.object({_links: z.record(z.string(), link), createdAt: z.string(), id: z.string(), tags: tags.optional(), updatedAt: z.string(), isVerified: z.boolean(), provisioningUrl: z.string().optional(), recoveryCodes: z.array(z.string()).optional()}); export const pagination = z.object({limit: z.number(), offset: z.number(), totalCount: z.number()}); export const meta = z.object({apiEnrollmentVersion: z.string().optional(), apiVersion: z.string().optional(), filterableFields: z.array(z.string()).optional(), pagination: pagination.optional()}); export const detailMfaEnvelope = z.object({data: detailMfa, meta: meta}); 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 the current status of MFA enrollment * * GET /current-identity/mfa * * Also accepts auth: oauth2 */ export const getApiCurrentIdentityMfa = async (options?: GetApiCurrentIdentityMfaOptions) => { const {baseUrl = "https://demo.ziti.dev/edge/client/v1", fetch: fetchFn = fetch, ztSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ztSession !== undefined) headers.set("zt-session", ztSession) const url = `${baseUrl.replace(/\/+$/, '')}/current-identity/mfa${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(detailMfaEnvelope, await res.json()) };