const AUTH_NS = "http://ksef.mf.gov.pl/auth/token/2.0"; export interface AuthTokenRequestXmlOptions { challenge: string; contextIdentifierType: string; contextIdentifierValue: string; subjectIdentifierType?: string; authorizationPolicyXml?: string | null; } export function buildAuthTokenRequestXml(options: AuthTokenRequestXmlOptions): string { const contextTag = normalizeContextIdentifierType(options.contextIdentifierType); const authorizationPolicy = options.authorizationPolicyXml ? `\n ${options.authorizationPolicyXml}` : ""; return ( '\n' + `\n` + ` ${options.challenge}\n` + " \n" + ` <${contextTag}>${options.contextIdentifierValue}\n` + " \n" + ` ${ options.subjectIdentifierType ?? "certificateSubject" }${authorizationPolicy}\n` + "" ); } export function normalizeContextIdentifierType(value: string): string { const key = value.trim().toLowerCase(); if (key === "nip") { return "Nip"; } if (key === "internalid") { return "InternalId"; } if (key === "nipvatue") { return "NipVatUe"; } if (key === "peppolid") { return "PeppolId"; } throw new Error("Unsupported context identifier type"); }