import {z} from 'zod' export type WebAuthnServiceUpdateCredentialBody = {display_name?: string | undefined}; export type PatchApiApiV1WebauthnCredentialsCredentialIdArgs = {credential_id: string, body: WebAuthnServiceUpdateCredentialBody}; export type PatchApiApiV1WebauthnCredentialsCredentialIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const webAuthnCredentialAuthenticator = z.object({aaguid: z.string().optional(), attachment: z.string().optional(), icon_dark: z.string().optional(), icon_light: z.string().optional(), name: z.string().optional()}); export const webAuthnCredentialAuthenticatorFlags = z.object({backup_eligible: z.boolean().optional(), backup_state: z.boolean().optional(), user_present: z.boolean().optional(), user_verified: z.boolean().optional()}); export const webAuthnCredentialClientInfo = z.object({city: z.string().optional(), ip: z.string().optional(), region: z.string().optional(), region_subdivision: z.string().optional()}); export const webAuthnCredentialUserAgent = z.object({browser: z.string().optional(), browser_version: z.string().optional(), device_model: z.string().optional(), device_type: z.string().optional(), os: z.string().optional(), os_version: z.string().optional(), raw: z.string().optional(), url: z.string().optional()}); export const webauthnWebAuthnCredential = z.object({attestation_type: z.string().optional(), authenticator: webAuthnCredentialAuthenticator.optional(), authenticator_flags: webAuthnCredentialAuthenticatorFlags.optional(), client_info: webAuthnCredentialClientInfo.optional(), created_at: z.string().optional(), credential_id: z.string().optional(), display_name: z.string().optional(), id: z.string().optional(), transports: z.array(z.string()).optional(), updated_at: z.string().optional(), user_agent: webAuthnCredentialUserAgent.optional(), user_id: z.string().optional()}); export const webauthnUpdateCredentialResponse = z.object({credential: webauthnWebAuthnCredential.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 }; /** * Rename a passkey * * PATCH /api/v1/webauthn/credentials/{credential_id} */ export const patchApiApiV1WebauthnCredentialsCredentialId = async (args: PatchApiApiV1WebauthnCredentialsCredentialIdArgs, options?: PatchApiApiV1WebauthnCredentialsCredentialIdOptions) => { const {baseUrl = "https://$SCALEKIT_ENVIRONMENT_URL", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/webauthn/credentials/${encodeURIComponent(String(args.credential_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(webauthnUpdateCredentialResponse, await res.json()) };