import {z} from 'zod' export type CrackerBinaryCreate = {data: {type: 'crackerBinary', attributes: {sourceType?: ('inline' | 'import' | 'url' | null) | undefined, sourceData?: (string | null) | undefined, crackerBinaryTypeId: number, version: string, downloadUrl?: (string | null) | undefined, binaryName: string, accessGroupId: number}}}; export type CreateApiApiV2UiCrackersArgs = {body: CrackerBinaryCreate}; export type CreateApiApiV2UiCrackersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const crackerBinaryPostPatchResponse = z.object({jsonapi: z.object({version: z.string(), ext: z.array(z.string()).optional()}), links: z.object({self: z.string()}), data: z.object({id: z.number().int(), type: z.literal("crackerBinary"), attributes: z.object({crackerBinaryTypeId: z.number().int(), version: z.string(), downloadUrl: z.string().nullable(), binaryName: z.string(), filename: z.string().nullable(), accessGroupId: z.number().int()}), links: z.object({self: z.string()}), relationships: z.object({accessGroup: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("accessGroup"), id: z.number().int()}).nullable().optional()}), crackerBinaryType: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.object({type: z.literal("crackerBinaryType"), id: z.number().int()}).nullable().optional()}), hashtypes: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("hashType"), id: z.number().int()})).optional()}), tasks: z.object({links: z.object({self: z.string(), related: z.string()}), data: z.array(z.object({type: z.literal("task"), id: z.number().int()})).optional()})})}), included: z.array(z.discriminatedUnion("type", [z.object({id: z.number().int(), type: z.literal("crackerBinaryType"), attributes: z.object({typeName: z.string(), isChunkingAvailable: z.boolean().nullable()})}), z.object({id: z.number().int(), type: z.literal("accessGroup"), attributes: z.object({groupName: z.string()})}), z.object({id: z.number().int(), type: z.literal("task"), attributes: z.object({taskName: z.string(), attackCmd: z.string(), chunkTime: z.number().int(), statusTimer: z.number().int(), keyspace: z.number().int(), keyspaceProgress: z.number().int(), priority: z.number().int(), maxAgents: z.number().int(), color: z.string().nullable(), isSmall: z.boolean(), isCpuTask: z.boolean(), useNewBench: z.boolean(), skipKeyspace: z.number().int(), crackerBinaryId: z.number().int(), crackerBinaryTypeId: z.number().int().nullable(), taskWrapperId: z.number().int(), isArchived: z.boolean(), notes: z.string(), staticChunks: z.number().int(), chunkSize: z.number().int(), forcePipe: z.boolean(), preprocessorId: z.number().int(), preprocessorCommand: z.string()})}), z.object({id: z.number().int(), type: z.literal("hashType"), attributes: z.object({description: z.string(), isSalted: z.boolean(), isSlowHash: 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 }; /** * Create CrackerBinarys * * POST /api/v2/ui/crackers */ export const createApiApiV2UiCrackers = async (args: CreateApiApiV2UiCrackersArgs, options: CreateApiApiV2UiCrackersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/ui/crackers${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(crackerBinaryPostPatchResponse, await res.json()) };