import {z} from 'zod' export type Header = {service: string, issuerCode: string, subIssuerCode: string, requestId: string, keyTag?: string | undefined, iv?: string | undefined}; export type Principal = {type: string, value: string}; export type Expiry = {type: string, value: string}; export type SessionId = string; export type TransactionId = string; export type FreeContext = {[key: string]: string}; export type ValidateRequest = {principal: Principal, expiry?: Expiry | undefined, sessionId: SessionId, transactionId?: TransactionId | undefined, userInputs?: {type: string, value: string} | undefined, freeContext?: FreeContext | undefined}; export type Signature = string; export type ValidateRequestMessage = {header: Header, body: ValidateRequest, signature?: Signature | undefined}; export type CreateApiValidateAuthenticationArgs = {Authorization?: string | undefined, Date?: unknown | undefined, Digest?: string | undefined, Signature?: string | undefined, 'x-jws-signature'?: string | undefined, body: ValidateRequestMessage}; export type CreateApiValidateAuthenticationOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const header = z.object({service: z.string().min(1).max(255), issuerCode: z.string().min(5).max(5), subIssuerCode: z.string().min(5).max(5), requestId: z.string(), keyTag: z.string().min(2).max(2).optional(), iv: z.string().min(24).max(32).optional()}); export const result = z.object({resultCode: z.enum(["SUCCESS", "FAILURE"]), trialLeft: z.number().int().optional()}); export const freeContext = z.record(z.string(), z.string().min(1).max(255)); export const validateResponse = z.object({result: result.optional(), authenticationMethod: z.string().min(2).max(2).optional(), freeContext: freeContext.optional()}); export const signature = z.string(); export const validateResponseMessage = z.object({header: header, body: validateResponse, signature: signature.optional()}); /** * Validate the authentication * * POST /validateAuthentication */ export const createApiValidateAuthentication = async (args: CreateApiValidateAuthenticationArgs, options: CreateApiValidateAuthenticationOptions) => { const {baseUrl, 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.Date != null) headers.set("Date", String(args.Date)) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Signature != null) headers.set("Signature", String(args.Signature)) if (args['x-jws-signature'] != null) headers.set("x-jws-signature", String(args['x-jws-signature'])) const url = `${baseUrl.replace(/\/+$/, '')}/validateAuthentication${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(validateResponseMessage, await res.json()) };