All files / src/lib odrl-engine.ts

98.3% Statements 58/59
82.85% Branches 29/35
100% Functions 11/11
97.95% Lines 48/49

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                                                                                            26x                                                       140x                 26x                                                                     3x 3x         11x 5x   4x 4x   3x                     3x                                                                 12x       12x   12x                                                     11x   11x 2x       9x 9x 9x 9x 9x 9x 9x 9x   9x 11x 11x 11x 11x 11x 11x 11x 11x 2x             9x 2x 2x 3x 3x   2x 3x 3x   2x     9x                     3x     1x 1x      
import { runQuery } from "@/lib/neo4j";
import { findPermits, type PermitRow } from "@/lib/permit-gate";
import { parseGraphTime } from "@/lib/permits";
import { IS_STATIC } from "@/lib/static-export";
import { testIdentityDid } from "@/lib/test-identity";
 
/* ── Types ─────────────────────────────────────────────────────── */
 
/** A data permit the scope was derived from (Art. 68); only valid ones. */
export interface ScopePermit {
  permitId: string;
  datasetId: string | null;
  datasetTitle: string | null;
  purpose: string | null;
  validUntil: string | null;
  /** The application invoked the Art. 71(4) exception and was approved. */
  art71Exception?: boolean;
}
 
export interface OdrlScope {
  participantId: string;
  participantName: string;
  permissions: string[];
  prohibitions: string[];
  accessibleDatasets: string[];
  temporalLimit: string | null;
  policyIds: string[];
  hasActiveContract: boolean;
  hdabApproved: boolean;
  /**
   * The approved, unexpired data permits of the participant. Their datasets
   * are in accessibleDatasets, their purposes in permissions, the latest
   * validity end in temporalLimit (issue #206, M3). Empty for a participant
   * that never applied, for instance a data holder querying its own data.
   */
  permits: ScopePermit[];
  /**
   * A valid permit was granted on an application that invoked the Art. 71(4)
   * exception: only then may the persons who opted out be included (ADR-062).
   * The proxy reads it from the forwarded scope; absent means no exception.
   */
  art71Exception?: boolean;
}
 
/* ── Participant mapping ───────────────────────────────────────── */
 
const USERNAME_TO_DID: Record<string, string> = {
  "admin@edc.demo": "did:web:alpha-klinik.de:participant",
  "dr.schmidt@alphaklinik.de": "did:web:alpha-klinik.de:participant",
  "nurse.weber@alphaklinik.de": "did:web:alpha-klinik.de:participant",
  "researcher@pharmaco.de": "did:web:pharmaco.de:research",
  "hdab.officer@medreg.de": "did:web:medreg.de:hdab",
  "dr.janssen@lmc.nl": "did:web:lmc.nl:clinic",
  "patient.mueller@demo.ehds": "did:web:alpha-klinik.de:participant",
  "tc.operator@medreg.de": "did:web:medreg.de:hdab",
  // The realm the deployments import (jad/keycloak-realm.json). Without these
  // the access log named the HDAB "did:web:unknown:regulator@..." (issue #205).
  "admin@health-dataspace.local": "did:web:alpha-klinik.de:participant",
  "clinic@health-dataspace.local": "did:web:alpha-klinik.de:participant",
  "lmc@limburg-mc.nl": "did:web:lmc.nl:clinic",
  "regulator@health-dataspace.local": "did:web:medreg.de:hdab",
  "regulator-es@health-dataspace.local": "did:web:medreg.es:hdab",
};
 
/**
 * Map a session username + roles to the corresponding participant DID.
 * A forged test session (`<persona>@ci.test`) maps to
 * `did:web:ci.test:<persona>`, so its accesses say they were a test.
 * Falls back to a generic participant DID if no mapping exists.
 */
export function userToParticipantId(
  username: string,
  _roles: string[],
): string {
  return (
    USERNAME_TO_DID[username] ??
    testIdentityDid(username) ??
    `did:web:unknown:${username}`
  );
}
 
/* ── Static fallback ───────────────────────────────────────────── */
 
const STATIC_SCOPE: OdrlScope = {
  participantId: "did:web:pharmaco.de:research",
  participantName: "PharmaCo Research AG",
  permissions: [
    "scientific_research",
    "statistics",
    "policy_support",
    "education",
    "ai_training",
  ],
  prohibitions: [
    "re_identification",
    "commercial_exploitation_without_approval",
  ],
  accessibleDatasets: [
    "dataset-synthea-fhir-r4-2026",
    "dataset-synthea-omop-2026",
  ],
  temporalLimit: "2027-12-31T23:59:59",
  policyIds: ["policy-ehds-art53-synthetic-2026"],
  hasActiveContract: true,
  hdabApproved: true,
  permits: [
    {
      permitId: "hdab-decision-medreg-2025-001",
      datasetId: "dataset:synthea-fhir-r4-mvd",
      datasetTitle: "Synthea FHIR R4 cohort",
      purpose: "SCIENTIFIC_RESEARCH",
      validUntil: "2027-12-31T23:59:59",
    },
  ],
};
 
/** Neo4j's toString(datetime) has nine fractional digits; keep an ISO value. */
function isoOrNull(value: string | null | undefined): string | null {
  const t = parseGraphTime(value);
  return t === null ? null : new Date(t).toISOString();
}
 
/** The permits that still count: status APPROVED and not past validUntil. */
function validPermits(rows: PermitRow[], now: number): ScopePermit[] {
  return rows
    .filter((r) => r.status === "APPROVED")
    .filter((r) => {
      const t = parseGraphTime(r.validUntil);
      return t === null || t >= now;
    })
    .map((r) => ({
      permitId: r.permitId,
      datasetId: r.datasetId,
      datasetTitle: r.datasetTitle,
      purpose: r.purpose,
      validUntil: isoOrNull(r.validUntil),
      art71Exception: r.art71Exception === true,
    }));
}
 
function emptyScope(participantId: string): OdrlScope {
  return {
    participantId,
    participantName: participantId,
    permissions: [],
    prohibitions: [],
    accessibleDatasets: [],
    temporalLimit: null,
    policyIds: [],
    hasActiveContract: false,
    hdabApproved: false,
    permits: [],
  };
}
 
/* ── Core resolver ─────────────────────────────────────────────── */
 
/**
 * Resolve the effective ODRL scope for a participant.
 *
 * Walks the Neo4j graph:
 *   Participant → Contract → DataProduct → GOVERNED_BY → OdrlPolicy
 *   HDABApproval → GRANTS_ACCESS_TO → HealthDataset
 * and, since issue #206, the participant's own data permits:
 *   Participant → SUBMITTED → AccessApplication ← APPROVES ← HDABApproval
 *
 * Returns the union of all permissions, prohibitions, and accessible
 * datasets for the caller's active contracts and valid permits. A permit
 * the access body revoked or that has run out contributes nothing, so the
 * scope shrinks the moment the body acts (Art. 63(3), Art. 68(3)).
 */
export async function resolveOdrlScope(
  participantId: string,
): Promise<OdrlScope> {
  Iif (IS_STATIC) {
    return STATIC_SCOPE;
  }
 
  try {
    // Query the ODRL policy chain for this participant
    const rows = await runQuery<{
      participantName: string;
      policyId: string;
      permissions: string[];
      prohibitions: string[];
      temporalLimit: string | null;
      datasetId: string | null;
      contractStatus: string | null;
      approvalStatus: string | null;
    }>(
      `MATCH (p:Participant)
       WHERE p.participantId = $participantId
          OR p.did = $participantId
       OPTIONAL MATCH (p)-[:OFFERS|CONSUMES]->(:DataProduct)-[:GOVERNED_BY]->(pol:OdrlPolicy)
       OPTIONAL MATCH (p)-[:HAS_CONTRACT]->(c:Contract)-[:COVERS|GOVERNS]->(:DataProduct)-[:DESCRIBED_BY]->(ds:HealthDataset)
       OPTIONAL MATCH (approval:HDABApproval)-[:GRANTS_ACCESS_TO]->(ds)
       RETURN p.name AS participantName,
              pol.policyId AS policyId,
              coalesce(pol.ehdsPermissions, []) AS permissions,
              coalesce(pol.ehdsProhibitions, []) AS prohibitions,
              toString(pol.temporalLimit) AS temporalLimit,
              coalesce(ds.datasetId, ds.id) AS datasetId,
              c.status AS contractStatus,
              approval.status AS approvalStatus`,
      { participantId },
    );
 
    const permits = validPermits(await findPermits(participantId), Date.now());
 
    if (rows.length === 0 && permits.length === 0) {
      return emptyScope(participantId);
    }
 
    // Aggregate across all matching rows
    const permissionSet = new Set<string>();
    const prohibitionSet = new Set<string>();
    const datasetSet = new Set<string>();
    const policySet = new Set<string>();
    let temporalLimit: string | null = null;
    let hasActiveContract = false;
    let hdabApproved = false;
    let participantName = participantId;
 
    for (const row of rows) {
      Eif (row.participantName) participantName = row.participantName;
      if (row.policyId) policySet.add(row.policyId);
      for (const p of row.permissions) permissionSet.add(p);
      for (const p of row.prohibitions) prohibitionSet.add(p);
      if (row.datasetId) datasetSet.add(row.datasetId);
      if (row.temporalLimit) temporalLimit = row.temporalLimit;
      if (row.contractStatus === "ACTIVE") hasActiveContract = true;
      if ((row.approvalStatus ?? "").toUpperCase() === "APPROVED") {
        hdabApproved = true;
      }
    }
 
    // The permits decide what the data user may touch and until when. The
    // latest validity end of a valid permit is the period; a permit without
    // an end date leaves the policy's limit in place.
    if (permits.length > 0) {
      hdabApproved = true;
      for (const permit of permits) {
        Eif (permit.datasetId) datasetSet.add(permit.datasetId);
        Eif (permit.purpose) permissionSet.add(permit.purpose.toLowerCase());
      }
      const ends = permits
        .map((p) => p.validUntil)
        .filter((v): v is string => Boolean(v))
        .sort();
      Eif (ends.length > 0) temporalLimit = ends[ends.length - 1];
    }
 
    return {
      participantId,
      participantName,
      permissions: [...permissionSet],
      prohibitions: [...prohibitionSet],
      accessibleDatasets: [...datasetSet],
      temporalLimit,
      policyIds: [...policySet],
      hasActiveContract,
      hdabApproved,
      permits,
      art71Exception: permits.some((p) => p.art71Exception === true),
    };
  } catch (err) {
    console.error("resolveOdrlScope error:", err);
    return emptyScope(participantId);
  }
}