All files / src/lib eudi-patient-map.ts

100% Statements 11/11
100% Branches 8/8
100% Functions 5/5
100% Lines 10/10

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                                                                            7x                                 309x                       14x                         50x 48x 48x 48x   90x   48x                 3x    
/**
 * Binds a verified EUDI Wallet PID to the one record it belongs to (#708).
 *
 * Until 2026-10-27 every verified wallet signed in as `patient1`. Under
 * Implementing Regulation (EU) 2026/2099 a PID identifies a person, so it may
 * open only that person's record, and a PID that belongs to nobody here is
 * refused rather than handed a demo patient.
 *
 * The hub holds no real person's data, so the only people a PID can match are
 * the fictional patient personas of the Keycloak realm (`jad/keycloak-realm.json`,
 * pinned by a unit test). A tester issues a test PID with one of their names
 * at the EU reference issuer and signs in as that patient. Matching ignores
 * case, accents and extra spaces; nothing else of the PID is stored.
 *
 * The verified name is never shown: the session carries the record's
 * pseudonym (#475, 2026-10-07).
 */
import type { EudiVerifiedPatient } from "@/lib/eudi-store";
import { patientPseudonym } from "@/lib/patient-identity";
 
/**
 * Normalised PID claims (mdoc eu.europa.ec.eudi.pid.1 / SD-JWT VC
 * urn:eudi:pid:1 or urn:eudi:pid:de:1). The German PID writes names in
 * UPPERCASE (ICAO Doc 9303); matching ignores case.
 */
export interface VerifiedPid {
  familyName?: string;
  givenName?: string;
  /** read when a wallet discloses it unasked; the hub requests only the name */
  ageOver18?: boolean;
  /**
   * `YYYY-MM-DD`; the German rulebook allows `00` for an unknown month or
   * day. Read only to compute the secure hash of ADR-067, never stored.
   */
  birthDate?: string;
}
 
/** The persons a PID can belong to: the realm's patient personas and their records. */
export const PID_BINDINGS = [
  {
    givenName: "Maria",
    familyName: "Schmidt",
    username: "patient1",
    record: "P1",
  },
  {
    givenName: "Jan",
    familyName: "de Vries",
    username: "patient2",
    record: "P2",
  },
] as const;
 
/** Case, accents and extra spaces do not count (the German PID writes UPPERCASE). */
export function normalise(s: string): string {
  return s
    .normalize("NFKD")
    .replace(/\p{M}/gu, "")
    .toLowerCase()
    .replace(/\s+/g, " ")
    .trim();
}
 
function patientOf(
  b: (typeof PID_BINDINGS)[number],
  assurance: EudiVerifiedPatient["assurance"],
): EudiVerifiedPatient {
  return {
    username: b.username,
    displayName: patientPseudonym(b.record),
    roles: ["PATIENT"],
    assurance,
  };
}
 
/**
 * The patient this PID belongs to, or null when it belongs to nobody on this
 * hub. A PID sign-in is at assurance level high (the EUDI Wallet's).
 */
export function mapPidToPatient(pid: VerifiedPid): EudiVerifiedPatient | null {
  if (!pid.givenName || !pid.familyName) return null;
  const given = normalise(pid.givenName);
  const family = normalise(pid.familyName);
  const b = PID_BINDINGS.find(
    (x) =>
      normalise(x.givenName) === given && normalise(x.familyName) === family,
  );
  return b ? patientOf(b, "high") : null;
}
 
/**
 * The patient a *simulated* wallet approval signs in as, when the deployment
 * allows it (`EUDI_DEMO_WALLET=true`, see the start route). No PID is
 * presented, so the sign-in proves nothing about the person: level low.
 */
export function demoPatient(): EudiVerifiedPatient {
  return patientOf(PID_BINDINGS[0], "low");
}