import {z} from 'zod' export type EnvInfo = {arch?: string | undefined, domain?: string | undefined, hostname?: string | undefined, os?: string | undefined, osRelease?: string | undefined, osVersion?: string | undefined}; export type SdkInfo = {appId?: string | undefined, appVersion?: string | undefined, branch?: string | undefined, revision?: string | undefined, type?: string | undefined, version?: string | undefined}; export type Authenticate = {configTypes?: Array | undefined, envInfo?: EnvInfo | undefined, password?: string | undefined, sdkInfo?: SdkInfo | undefined, username?: string | undefined}; export type CreateApiAuthenticateArgs = {method: 'password' | 'cert' | 'ext-jwt', body: Authenticate}; export type CreateApiAuthenticateOptions = {baseUrl?: 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 mfaFormats = z.enum(["numeric", "alpha", "alphaNumeric"]); export const mfaProviders = z.enum(["ziti", "url"]); export const authQueryType = z.enum(["MFA", "TOTP", "EXT-JWT"]); export const authQueryDetail = z.object({clientId: z.string().optional(), format: mfaFormats.optional(), httpMethod: z.string().optional(), httpUrl: z.string().optional(), id: z.string().optional(), isTotpEnrolled: z.boolean().optional(), maxLength: z.number().int().optional(), minLength: z.number().int().optional(), provider: mfaProviders, scopes: z.array(z.string()).optional(), typeId: authQueryType.optional()}); export const authQueryList = z.array(authQueryDetail); export const entityRef = z.object({_links: z.record(z.string(), link).optional(), entity: z.string().optional(), id: z.string().optional(), name: z.string().optional()}); export const currentApiSessionDetail = z.object({_links: z.record(z.string(), link), createdAt: z.string(), id: z.string(), tags: tags.optional(), updatedAt: z.string(), authQueries: authQueryList, authenticatorId: z.string(), cachedLastActivityAt: z.string().optional(), configTypes: z.array(z.string()), identity: entityRef, identityId: z.string(), improperClientCertChain: z.boolean().optional(), ipAddress: z.string(), isCertExtendRequested: z.boolean().optional(), isCertExtendable: z.boolean(), isCertKeyRollRequested: z.boolean().optional(), isMfaComplete: z.boolean(), isMfaRequired: z.boolean(), lastActivityAt: z.string().optional(), token: z.string(), expirationSeconds: z.number().int(), expiresAt: z.string()}); 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 currentApiSessionDetailEnvelope = z.object({data: currentApiSessionDetail, 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 }; /** * Authenticate via a method supplied via a query string parameter * * POST /authenticate */ export const createApiAuthenticate = async (args: CreateApiAuthenticateArgs, options?: CreateApiAuthenticateOptions) => { const {baseUrl = "https://demo.ziti.dev/edge/client/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.method != null) query.append("method", String(args.method)) const url = `${baseUrl.replace(/\/+$/, '')}/authenticate${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(currentApiSessionDetailEnvelope, await res.json()) };