import {z} from 'zod' export type JWTTokenReq = {aud?: string | undefined, claims?: Record | undefined, det?: string | undefined, dst?: string | undefined, encodedToken?: string | undefined, exp?: number | undefined, iat?: number | undefined, id?: string | undefined, iss?: string | undefined, jti?: string | undefined, kid?: string | undefined, nbf?: number | undefined, noe?: number | undefined, scope?: string | undefined, status?: string | undefined, sub?: string | undefined, tokenType?: string | undefined}; export type AuthRequestReq = {fidoAuthRequest?: string | undefined, fidoAuthResponse?: string | undefined, fidoResponseMessage?: string | undefined, id?: string | undefined, status?: ('ERROR' | 'FAILED' | 'PENDING' | 'SUCCESS') | undefined, token?: JWTTokenReq | undefined}; export type ValidateAuthDTO = {authRequest?: AuthRequestReq | undefined, policyId?: string | undefined}; export type CreateApiApiPersonsPersonIdAuthenticationRequestArgs = {personId: string, body: ValidateAuthDTO}; export type CreateApiApiPersonsPersonIdAuthenticationRequestOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const jWTTokenRes = z.object({aud: z.string().optional(), claims: z.object({}).optional(), credentialTypeHref: z.string().optional(), det: z.string().optional(), dst: z.string().optional(), encodedToken: z.string().optional(), exp: z.number().int().optional(), iat: z.number().int().optional(), id: z.string().optional(), iss: z.string().optional(), jti: z.string().optional(), kid: z.string().optional(), nbf: z.number().int().optional(), noe: z.number().int().optional(), scope: z.string().optional(), scopesAsList: z.array(z.string()).optional(), scopesAsSet: z.array(z.string()).optional(), status: z.string().optional(), sub: z.string().optional(), tokenType: z.string().optional()}); export const authRequestRes = z.object({fidoAuthRequest: z.string().optional(), fidoAuthResponse: z.string().optional(), fidoResponseMessage: z.string().optional(), id: z.string().optional(), status: z.enum(["ERROR", "FAILED", "PENDING", "SUCCESS"]).optional(), token: jWTTokenRes.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 }; /** * Validate an auth request * * POST /api/persons/{personId}/authenticationRequest */ export const createApiApiPersonsPersonIdAuthenticationRequest = async (args: CreateApiApiPersonsPersonIdAuthenticationRequestArgs, options: CreateApiApiPersonsPersonIdAuthenticationRequestOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/api/persons/${encodeURIComponent(String(args.personId))}/authenticationRequest${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(authRequestRes, await res.json()) };