Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | 1x 1x 24x 24x 45x 45x 24x 1x 1x 12x 12x 12x 24x 12x 21x 12x 12x 12x 12x 1x 11x 11x 12x 12x 12x 1x 10x 10x 10x 10x 10x 9x 9x 9x 8x 8x 1x 7x 6x 1x 5x 4x 4x 3x 3x 1x 3x 3x 3x 2x 2x 3x 3x 7x 7x 7x 32x 9x 9x 9x 9x 8x 8x 8x 23x 23x 23x 23x 8x 8x 41x 41x 12x 12x 32x 12x 12x 23x 23x 21x 21x 12x 8x 8x 6x 6x 1x 2x 2x 1x 1x 1x 2x 1x 7x | /**
* Adapter for the EU reference OpenID4VP Verifier backend
* (eu-digital-identity-wallet/eudi-srv-verifier-endpoint), as hosted at
* https://verifier-backend.eudiw.dev (the REST backend; the verifier.eudiw.dev
* host is the frontend UI and returns 405 on the API paths).
*
* Contract pinned against the reference backend README and its source:
* - POST /ui/presentations — initialise a cross-device transaction
* body: { dcql_query, nonce, response_mode, jar_mode, request_uri_method,
* profile, intended_use_id | registration_certificate }
* resp: { transaction_id, client_id, request_uri, request_uri_method }
* - GET /ui/presentations/{txId} — the wallet's response once posted:
* { vp_token: { <credential query id>: [presentation, ...] } }
* or { error, error_description } when the wallet declined
* - POST /utilities/validations/msoMdoc/deviceResponse — validates an mdoc
* DeviceResponse and returns its attributes
*
* Implementing Regulation (EU) 2026/2099 and the OpenID4VC High Assurance
* Interoperability Profile (HAIP) 1.0 shape the defaults (#708): the PID is
* asked for as SD-JWT VC or mdoc, whichever the wallet holds, and the wallet's
* response is encrypted (`direct_post.jwt`). The hosted verifier refuses a
* request without a relying-party registration certificate; it offers a
* public test intended use, `TEST-01`. A self-hosted verifier with our own
* certificate (the German d-you path, #182) is the same request with
* `EUDI_REGISTRATION_CERTIFICATE` set.
*
* Everything is configurable via env, read at call time. See ADR-028.
* Cryptographic verification of a presentation is DELEGATED to the verifier
* backend; this module only reads the claims of a presentation the backend
* has accepted, and checks that each SD-JWT disclosure is one the issuer
* signed.
*/
import { createHash } from "crypto";
import type { VerifiedPid } from "@/lib/eudi-patient-map";
/** The DCQL credential query ids; the wallet's vp_token is keyed by them. */
export const PID_SD_JWT_QUERY = "pid_sd_jwt";
export const PID_MDOC_QUERY = "pid_mdoc";
function settings() {
const env = process.env;
return {
base: (
env.EUDI_VERIFIER_BASE_URL ?? "https://verifier-backend.eudiw.dev"
).replace(/\/+$/, ""),
/** QR / wallet deep-link scheme. The EU reference wallet reads openid4vp://; haip-vp:// and eudi-openid4vp:// are alternatives. */
scheme: env.EUDI_VERIFIER_SCHEME ?? "openid4vp://",
doctype: env.EUDI_PID_DOCTYPE ?? "eu.europa.ec.eudi.pid.1",
/**
* The SD-JWT VC types of the PID, comma-separated: `urn:eudi:pid:1` (PID
* rulebook, EU reference wallet), `urn:eudi:pid:de:1` (German PID, d-you).
* List only what the registration certificate declares.
*/
/**
* The PID formats to ask for, comma-separated: `dc+sd-jwt`, `mso_mdoc`.
* A registration certificate names the formats it covers, and a wallet
* refuses a request for one it does not: ours from the SPRIND registrar
* covers SD-JWT VC only (#708, 2026-10-28).
*/
formats: (env.EUDI_PID_FORMATS || "dc+sd-jwt,mso_mdoc")
.split(",")
.map((f) => f.trim())
.filter((f) => f === "dc+sd-jwt" || f === "mso_mdoc"),
vct: (env.EUDI_PID_VCT || "urn:eudi:pid:1")
.split(",")
.map((v) => v.trim())
.filter(Boolean),
/** "haip" needs "direct_post.jwt"; "openid4vp" also allows cleartext. */
profile: env.EUDI_VERIFIER_PROFILE ?? "haip",
/** "get" (wallet GETs the signed request — broadest compat) or "post". */
requestUriMethod: env.EUDI_REQUEST_URI_METHOD ?? "get",
/** "direct_post.jwt" (encrypted, HAIP) or "direct_post" (cleartext, compose only). */
responseMode: env.EUDI_RESPONSE_MODE ?? "direct_post.jwt",
/** Our own registration certificate (JWS), when we have one; it wins over the intended use. */
registrationCertificate: env.EUDI_REGISTRATION_CERTIFICATE || undefined,
/** An intended use configured at the verifier; TEST-01 is the hosted verifier's public one. */
intendedUseId: env.EUDI_INTENDED_USE_ID || "TEST-01",
/**
* Ask for the birth date as well, for the secure hash of ADR-067. Only a
* deployment whose registration certificate names it sets this: a relying
* party may not ask beyond its registration (eIDAS Art. 5b(3)).
*/
birthDate: env.EUDI_PID_BIRTHDATE === "true",
};
}
export interface StartedPresentation {
transactionId: string;
clientId: string;
requestUri: string;
/** the full openid4vp deep link to render as a QR / tap on the same device */
walletLink: string;
}
export type PresentationResult =
| { status: "pending" }
| { status: "completed"; pid: VerifiedPid }
/** the wallet declined, or what it sent is not a PID we can read */
| { status: "failed"; reason: string };
async function safeText(res: Response): Promise<string> {
try {
return (await res.text()).slice(0, 300);
} catch {
return "";
}
}
/**
* The DCQL query: the PID as SD-JWT VC or as mdoc, and of it the name, which
* binds the wallet to a record, and, where the deployment's registration
* certificate names it (`EUDI_PID_BIRTHDATE=true`), the birth date, which
* goes into the secure hash of ADR-067 and nowhere else. Nothing more: no
* age, which the hub does not use and which neither the reference verifier's
* registration (`TEST-01`) nor ours declares; a wallet refuses a request that
* asks for more than the registration certificate declares (over-asking,
* ETSI TS 119 472-2; eIDAS Art. 5b(3)). Nothing of it is shown or stored: a
* patient session sees the record's pseudonym.
*/
function pidQuery(
doctype: string,
vct: string[],
formats: string[],
birthDate: boolean,
) {
const sdJwt = formats.includes("dc+sd-jwt");
const mdoc = formats.includes("mso_mdoc") || !sdJwt;
const credentials = [
{
id: PID_SD_JWT_QUERY,
format: "dc+sd-jwt",
meta: { vct_values: vct },
claims: [
{ path: ["family_name"] },
{ path: ["given_name"] },
...(birthDate ? [{ path: ["birthdate"] }] : []),
],
},
{
id: PID_MDOC_QUERY,
format: "mso_mdoc",
meta: { doctype_value: doctype },
claims: [
{ path: [doctype, "family_name"] },
{ path: [doctype, "given_name"] },
...(birthDate ? [{ path: [doctype, "birth_date"] }] : []),
],
},
].filter((c) => (c.format === "dc+sd-jwt" ? sdJwt : mdoc));
return {
credentials,
credential_sets: [
{
options: credentials.map((c) => [c.id]),
purpose: "Sign in to the Health Dataspace patient portal",
},
],
};
}
/**
* Initialise an OpenID4VP cross-device presentation request for the PID and
* return the wallet deep link to encode as a QR code.
*/
export async function startPresentation(
nonce: string,
): Promise<StartedPresentation> {
const s = settings();
const body = {
dcql_query: pidQuery(s.doctype, s.vct, s.formats, s.birthDate),
nonce,
response_mode: s.responseMode,
jar_mode: "by_reference",
request_uri_method: s.requestUriMethod,
profile: s.profile,
...(s.registrationCertificate
? { registration_certificate: s.registrationCertificate }
: { intended_use_id: s.intendedUseId }),
};
const res = await fetch(`${s.base}/ui/presentations`, {
method: "POST",
headers: { "content-type": "application/json", accept: "application/json" },
body: JSON.stringify(body),
cache: "no-store",
});
if (!res.ok) {
throw new Error(
`verifier init failed: ${res.status} ${await safeText(res)}`,
);
}
const json = (await res.json()) as {
transaction_id?: string;
presentation_id?: string;
client_id?: string;
request_uri?: string;
};
const transactionId = json.transaction_id ?? json.presentation_id ?? "";
const clientId = json.client_id ?? "";
const requestUri = json.request_uri ?? "";
if (!transactionId || !requestUri) {
throw new Error("verifier init: missing transaction_id / request_uri");
}
const params = new URLSearchParams();
if (clientId) params.set("client_id", clientId);
params.set("request_uri", requestUri);
const walletLink = `${s.scheme}?${params.toString()}`;
return { transactionId, clientId, requestUri, walletLink };
}
/**
* Poll the verifier for the wallet's response. A present `vp_token` means the
* verifier backend has accepted a validated presentation for this
* transaction; the PID's claims are then read from whichever credential the
* wallet chose.
*/
export async function getPresentationResult(
transactionId: string,
): Promise<PresentationResult> {
const s = settings();
const res = await fetch(
`${s.base}/ui/presentations/${encodeURIComponent(transactionId)}`,
{
method: "GET",
headers: { accept: "application/json" },
cache: "no-store",
},
);
// Until the wallet posts, the verifier has no response yet.
if (res.status === 404 || res.status === 204) return { status: "pending" };
const json = (await res.json().catch(() => null)) as {
vp_token?: Record<string, unknown>;
error?: string;
} | null;
if (res.status === 400 && json?.error === "PresentationNotSubmitted") {
return { status: "pending" };
}
if (!res.ok) throw new Error(`verifier poll failed: ${res.status}`);
if (json?.error) {
return { status: "failed", reason: `wallet: ${json.error}` };
}
if (!json || json.vp_token == null) return { status: "pending" };
const sdJwt = firstPresentation(json.vp_token, PID_SD_JWT_QUERY);
if (sdJwt) return fromClaims(pidFromSdJwt(sdJwt));
const mdoc = firstPresentation(json.vp_token, PID_MDOC_QUERY);
if (mdoc) return fromClaims(await pidFromMdoc(s.base, s.doctype, mdoc));
return { status: "failed", reason: "no PID in the wallet's response" };
}
/**
* Whether the wallet has fetched the signed request, from the verifier's
* event log. A wallet that will not present to this relying party fetches the
* request and then sends nothing: d-you without our own registration
* certificate did exactly that (#708, 2026-10-28). The fetch is the only sign
* the page can show that the scan reached the wallet.
*/
export async function walletFetchedRequest(
transactionId: string,
): Promise<boolean> {
const s = settings();
const res = await fetch(
`${s.base}/ui/presentations/${encodeURIComponent(transactionId)}/events`,
{ headers: { accept: "application/json" }, cache: "no-store" },
);
if (!res.ok) return false;
type Event = { actor?: string };
const json = (await res.json().catch(() => null)) as
| { events?: Event[] }
| Event[]
| null;
const events = Array.isArray(json) ? json : json?.events ?? [];
return events.some((e) => e.actor === "Wallet");
}
function fromClaims(pid: VerifiedPid | null): PresentationResult {
return pid
? { status: "completed", pid }
: { status: "failed", reason: "the PID could not be read" };
}
/** OpenID4VP 1.0 sends an array per query; earlier drafts a single value. */
function firstPresentation(
vpToken: Record<string, unknown>,
queryId: string,
): string | undefined {
const v = typeof vpToken === "object" ? vpToken[queryId] : undefined;
const first = Array.isArray(v) ? v[0] : v;
return typeof first === "string" && first ? first : undefined;
}
function decodeJson(b64url: string): unknown {
return JSON.parse(Buffer.from(b64url, "base64url").toString("utf8"));
}
/**
* The claims of an SD-JWT VC presentation: `<issuer-signed JWT>~<disclosure>~
* ...~<key binding JWT>`. The verifier has checked the signatures and the key
* binding; here every disclosure must hash to a digest the issuer signed, and
* the claims are rebuilt from the payload and those disclosures. Returns null
* when anything does not fit.
*/
export function pidFromSdJwt(presentation: string): VerifiedPid | null {
try {
const [jwt, ...rest] = presentation.split("~");
const payload = decodeJson(jwt.split(".")[1]) as Record<string, unknown>;
Iif ((payload._sd_alg ?? "sha-256") !== "sha-256") return null;
// The last part is the key binding JWT, empty when there is none.
const disclosures = rest.slice(0, -1).filter(Boolean);
const disclosed = new Map<string, [string, unknown]>();
for (const d of disclosures) {
const digest = createHash("sha256").update(d).digest("base64url");
const parts = decodeJson(d) as unknown[];
// [salt, name, value] for an object property; array elements are not read.
Eif (parts.length === 3 && typeof parts[1] === "string") {
disclosed.set(digest, [parts[1], parts[2]]);
}
}
const used = new Set<string>();
const reveal = (node: unknown): unknown => {
Iif (Array.isArray(node)) return node.map(reveal);
if (!node || typeof node !== "object") return node;
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(node)) {
if (k !== "_sd" && k !== "_sd_alg") out[k] = reveal(v);
}
const digests = (node as { _sd?: unknown })._sd;
for (const digest of Array.isArray(digests) ? digests : []) {
const hit = disclosed.get(String(digest));
if (hit) {
used.add(String(digest));
out[hit[0]] = reveal(hit[1]);
}
}
return out;
};
const claims = reveal(payload) as Record<string, unknown>;
// A disclosure the issuer did not sign is a forgery, not an extra.
if (used.size !== disclosed.size) return null;
const age = claims.age_equal_or_over as Record<string, unknown> | undefined;
return pidOf(
claims.given_name,
claims.family_name,
age?.["18"] ?? claims.age_over_18,
claims.birthdate,
);
} catch {
return null;
}
}
/**
* The claims of an mdoc DeviceResponse, as the verifier's validation utility
* returns them (it decodes the CBOR and checks the issuer's signature and
* validity again). Returns null when it refuses.
*/
async function pidFromMdoc(
base: string,
doctype: string,
deviceResponse: string,
): Promise<VerifiedPid | null> {
const res = await fetch(
`${base}/utilities/validations/msoMdoc/deviceResponse`,
{
method: "POST",
headers: {
"content-type": "application/x-www-form-urlencoded",
accept: "application/json",
},
body: new URLSearchParams({ device_response: deviceResponse }).toString(),
cache: "no-store",
},
);
if (!res.ok) return null;
const docs = (await res.json().catch(() => null)) as Array<{
docType?: string;
attributes?: Record<string, Record<string, unknown>>;
}> | null;
const attrs = Array.isArray(docs)
? docs.find((d) => d.docType === doctype)?.attributes?.[doctype]
: undefined;
return attrs
? pidOf(
attrs.given_name,
attrs.family_name,
attrs.age_over_18,
attrs.birth_date,
)
: null;
}
const BIRTH_DATE = /^\d{4}-\d{2}-\d{2}$/;
function pidOf(
given: unknown,
family: unknown,
over18: unknown,
birth?: unknown,
): VerifiedPid {
return {
...(typeof given === "string" ? { givenName: given } : {}),
...(typeof family === "string" ? { familyName: family } : {}),
...(typeof over18 === "boolean" ? { ageOver18: over18 } : {}),
...(typeof birth === "string" && BIRTH_DATE.test(birth)
? { birthDate: birth }
: {}),
};
}
|