import {z} from 'zod' export type Cursor = {cursor?: string | undefined}; export type DateTimeRFC3339 = string; export type ResetCursor = {limit?: number | undefined, start_time?: DateTimeRFC3339 | undefined, end_time?: DateTimeRFC3339 | undefined}; export type CreateApiApiV1SigninattemptsArgs = {body: Cursor | ResetCursor}; export type CreateApiApiV1SigninattemptsOptions = {baseUrl?: string | undefined, jwtsa?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string(); export const dateTimeRFC3339 = z.string(); export const details = z.object({value: z.string().optional()}); export const user = z.object({uuid: uUID.optional(), name: z.string().optional(), email: z.string().optional()}); export const client = z.object({app_name: z.string().optional(), app_version: z.string().optional(), platform_name: z.string().optional(), platform_version: z.string().optional(), os_name: z.string().optional(), os_version: z.string().optional(), ip_address: z.string().optional()}); export const location = z.object({country: z.string().optional(), region: z.string().optional(), city: z.string().optional(), longitude: z.number().optional(), latitude: z.number().optional()}); export const signInAttempt = z.object({uuid: uUID.optional(), session_uuid: uUID.optional(), timestamp: dateTimeRFC3339.optional(), category: z.enum(["success", "credentials_failed", "mfa_failed", "sso_failed", "modern_version_failed", "firewall_failed", "firewall_reported_success"]).optional(), type: z.enum(["credentials_ok", "mfa_ok", "password_secret_bad", "mfa_missing", "totp_disabled", "totp_bad", "totp_timeout", "u2f_disabled", "u2f_bad", "u2f_timout", "duo_disabled", "duo_bad", "duo_timeout", "duo_native_bad", "service_account_sso_denied", "non_sso_user", "sso_user_mismatch", "platform_secret_disabled", "platform_secret_bad", "platform_secret_proxy", "code_disabled", "code_bad", "code_timeout", "ip_blocked", "continent_blocked", "country_blocked", "anonymous_blocked", "all_blocked", "modern_version_missing", "modern_version_old"]).optional(), country: z.string().optional(), details: details.optional(), target_user: user.optional(), client: client.optional(), location: location.optional()}); export const signInAttemptItems = z.object({items: z.array(signInAttempt).optional(), cursor: z.string().optional(), has_more: z.boolean().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 }; /** * Retrieves events for both successful and failed attempts to sign into a 1Password account * * POST /api/v1/signinattempts */ export const createApiApiV1Signinattempts = async (args: CreateApiApiV1SigninattemptsArgs, options?: CreateApiApiV1SigninattemptsOptions) => { const {baseUrl = "https://events.1password.com", fetch: fetchFn = fetch, jwtsa} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (jwtsa !== undefined) headers.set('authorization', `Bearer ${jwtsa}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/signinattempts${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(signInAttemptItems, await res.json()) };