import {z} from 'zod' export type CreateApiV1AuthTokenArgs = {'App-ID': string, 'Auth-Token': string, expires_in?: number | undefined}; export type CreateApiV1AuthTokenOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const multiDomainUserSystemSchema = z.object({logo_url: z.string().optional(), mfa_methods: z.array(z.enum(["TOTP", "MAIL_2SV"])).optional(), mfa_methods_configured: z.array(z.enum(["TOTP", "MAIL_2SV"])).optional(), mfa_required: z.boolean().optional(), mfa_required_configured: z.boolean().optional(), platform_url: z.string().optional(), system_domain_id: z.string(), system_domain_name: z.string().optional(), url: z.string().optional()}); export const tokenSchema = z.object({app_id: z.string().optional(), auth_system_domains: z.array(multiDomainUserSystemSchema).optional(), date_created: z.string().optional(), date_modified: z.string().optional(), expires: z.string().optional(), id: z.string().optional(), is_admin: z.boolean().optional(), is_mfa_authenticated: z.boolean().optional(), is_saml_authenticated: z.boolean().optional(), is_super_admin: z.boolean().optional(), is_super_admin_light: z.boolean().optional(), system_domain_id: z.string().optional(), system_domain_is_plg: z.boolean().optional(), system_domain_status: z.enum(["ACTIVE", "WARNING", "FROZEN", "DEACTIVATED"]).optional(), system_domain_type: z.string().optional(), system_domain_warning_message: z.string().optional(), system_domains: z.array(z.string()).optional(), token: z.string(), user_id: z.string().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 new token without invalidating the old one * * POST /v1/auth/token/ */ export const createApiV1AuthToken = async (args: CreateApiV1AuthTokenArgs, options: CreateApiV1AuthTokenOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) if (args.expires_in != null) query.append("expires_in", String(args.expires_in)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/auth/token/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tokenSchema, await res.json()) };