import {z} from 'zod' export type PasswordType = string; export type WebAuthnAuthenticationResponse = {id: string, rawId: string, type: 'public-key', authenticatorAttachment?: ('cross-platform' | 'platform') | undefined, clientExtensionResults: {appid?: boolean | undefined, credProps?: ({rk?: boolean | undefined} | {[key: string]: unknown}) | undefined, hmacCreateSecret?: boolean | undefined} | {[key: string]: unknown}, response: {clientDataJSON: string, authenticatorData: string, signature: string, userHandle?: string | undefined} | {[key: string]: unknown}} | {[key: string]: unknown}; export type EnableMfaTotpRequest = {secret: string, code: string, password?: PasswordType | undefined, mfa_method?: ('totp' | 'webauthn' | 'email_code') | undefined, mfa_code?: string | undefined, email_code_ticket?: string | undefined, email_code?: string | undefined, webauthn_response?: WebAuthnAuthenticationResponse | undefined, webauthn_challenge?: string | undefined}; export type CreateApiUsersMeMfaTotpEnableArgs = {body: EnableMfaTotpRequest}; export type CreateApiUsersMeMfaTotpEnableOptions = {baseUrl?: string | undefined, sessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const mfaBackupCodesResponse = z.object({backup_codes: z.array(z.object({code: z.string(), consumed: z.boolean()}))}); 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 }; /** * Enable TOTP multi-factor authentication * * POST /users/@me/mfa/totp/enable */ export const createApiUsersMeMfaTotpEnable = async (args: CreateApiUsersMeMfaTotpEnableArgs, options?: CreateApiUsersMeMfaTotpEnableOptions) => { const {baseUrl = "https://web.fluxer.app/api/v1", fetch: fetchFn = fetch, sessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sessionToken !== undefined) headers.set("Authorization", sessionToken) const url = `${baseUrl.replace(/\/+$/, '')}/users/@me/mfa/totp/enable${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(mfaBackupCodesResponse, await res.json()) };