All files / src/lib api-sign-in.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 3/3
100% Lines 5/5

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                                                        4x                                                                                                                                                                                                 127x 1016x 168x 127x    
/**
 * Which role to sign in with for which group of API operations.
 *
 * Shown above both API explorers (components/docs/ApiSignInGuide.tsx) and
 * repeated, in English, in the description of ui/public/openapi.yaml. The
 * groups are the spec's tags. Every route needs a session (ADR-044), and the
 * operator is not a master key: the access body's decisions answer only
 * HDAB_AUTHORITY and a patient's phone and wallet only the PATIENT, so one
 * "sign in as the admin" line would send the reader into a 403.
 *
 * __tests__/unit/lib/api-sign-in.test.ts holds this to the code: each claim
 * below names operations, and the test reads their gate from the route file.
 */
import { DEMO_PERSONAS, type Role } from "@/lib/auth";
 
export interface ApiSignInRow {
  role: Role;
  /** What the role opens, in English: the key in messages/<lang>/docsDeveloper.json. */
  opens: string;
  /**
   * Operations the row promises, as "METHOD /api/path": each must admit the
   * role. The test fails when a gate moves away from under the sentence.
   */
  admits: string[];
  /** Operations that must refuse EDC_ADMIN, for the rows that say so. */
  refusesOperator?: string[];
}
 
export const API_SIGN_IN_ROWS: ApiSignInRow[] = [
  {
    role: "EDC_ADMIN",
    opens:
      "Start here. Admin, Participants, Assets, Catalog, Negotiations, Transfers, Trust Center, and every read but a patient's phone and wallet.",
    admits: [
      "GET /api/admin/components",
      "GET /api/admin/tenants",
      "DELETE /api/participants/{id}",
      "POST /api/catalog",
      "GET /api/trust-center",
      "POST /api/compliance/applications",
      "GET /api/patient/profile",
    ],
  },
  {
    role: "HDAB_AUTHORITY",
    opens:
      "Access Body: stop the clock, issue and revoke a permit, open and close a finding, ask for information. Admin: the audit trail, its retention, the policies, the secure processing environments.",
    admits: [
      "POST /api/compliance/permits",
      "POST /api/compliance/permits/revoke",
      "POST /api/compliance/applications/clock",
      "POST /api/compliance/findings",
      "POST /api/compliance/findings/close",
      "POST /api/compliance/information-requests",
      "POST /api/compliance/requests/decide",
      "GET /api/admin/audit",
      "GET /api/admin/audit/retention",
      "GET /api/admin/policies",
      "GET /api/admin/spe",
    ],
    refusesOperator: [
      "POST /api/compliance/permits",
      "POST /api/compliance/permits/revoke",
      "POST /api/compliance/applications/clock",
      "POST /api/compliance/findings",
      "POST /api/compliance/findings/close",
      "POST /api/compliance/information-requests",
      "POST /api/compliance/requests/decide",
    ],
  },
  {
    role: "DATA_USER",
    opens:
      "Access Body: file an application or a data request, publish results. Negotiations, Transfers, Analytics and NLQ as a data user sees them: pseudonyms, and nobody who opted out.",
    admits: [
      "POST /api/compliance/applications",
      "POST /api/compliance/applications/complete",
      "POST /api/compliance/requests",
      "POST /api/compliance/results",
    ],
  },
  {
    role: "DATA_HOLDER",
    opens:
      "Catalog: publish and withdraw a dataset. Access Body: decide a data request.",
    admits: [
      "POST /api/catalog",
      "DELETE /api/catalog",
      "POST /api/compliance/requests/decide",
    ],
  },
  {
    role: "PATIENT",
    opens:
      "Patient: the own record and no other, the opt-out, a consent per programme, the app pairing, the wallet link.",
    admits: [
      "GET /api/patient/profile",
      "GET /api/patient/insights",
      "POST /api/patient/opt-out",
      "POST /api/patient/programmes/consent",
      "POST /api/patient/app-pairing",
      "GET /api/patient/app-devices",
      "GET /api/patient/wallet-link",
    ],
    refusesOperator: [
      "POST /api/patient/app-pairing",
      "GET /api/patient/app-devices",
      "GET /api/patient/wallet-link",
      "PUT /api/patient/profile/interests",
    ],
  },
  {
    role: "TRUST_CENTER_OPERATOR",
    opens:
      "Trust Center. No demo user holds this role: sign in as edcadmin, who may read it.",
    admits: ["GET /api/trust-center"],
  },
];
 
/**
 * The demo users that hold a role, as the sign-in page lists them. For a role
 * no demo user holds, the operator, who is admitted wherever the table sends
 * the reader for it.
 */
export function demoUsersFor(role: Role): string[] {
  const users = DEMO_PERSONAS.filter((p) =>
    (p.roles as readonly string[]).includes(role),
  ).map((p) => p.username);
  return users.length > 0 ? users : ["edcadmin"];
}