All files / src/app/api/patient/wallet-link route.ts

96.42% Statements 27/28
85% Branches 17/20
100% Functions 3/3
96.15% Lines 25/26

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                      2x                         11x 11x 10x 10x 10x 1x   9x     9x 9x 8x   1x                 6x 6x 5x 5x 1x               4x 4x 1x   2x   1x         2x       2x 2x          
import { NextResponse } from "next/server";
import { requireAuth, isAuthError } from "@/lib/auth-guard";
import { sessionUsername } from "@/lib/patient/session-username";
import { isSandboxUsername } from "@/lib/sandbox-account";
import { AccountError } from "@/lib/app-accounts";
import {
  clearFingerprint,
  hasFingerprint,
  pidAccountsAvailable,
} from "@/lib/pid-link";
 
export const dynamic = "force-dynamic";
 
/**
 * The link between the signed-in patient's account and their EUDI Wallet's
 * PID (ADR-067). The link itself is made by presenting the PID
 * (`POST /api/auth/eudi/start` with `{ purpose: "link" }`); this route says
 * whether there is one and removes it.
 *
 * Only a sandbox account (`kb-`, ADR-054) can carry a link; a demo persona
 * signs in with a wallet by name and is answered `eligible: false`. Neither
 * answer carries the secure hash.
 */
export async function GET(): Promise<NextResponse> {
  const auth = await requireAuth(["PATIENT"]);
  if (isAuthError(auth)) return auth;
  const username = await sessionUsername();
  const available = pidAccountsAvailable();
  if (!username || !isSandboxUsername(username)) {
    return NextResponse.json({ eligible: false, available, linked: false });
  }
  Iif (!available) {
    return NextResponse.json({ eligible: true, available, linked: false });
  }
  try {
    const linked = await hasFingerprint(username);
    return NextResponse.json({ eligible: true, available, linked });
  } catch (err) {
    return failed("GET", err);
  }
}
 
/**
 * DELETE: disconnects the wallet (ADR-067 decision 7, GDPR Art. 7(3) and
 * Art. 17): the attribute goes, the account stays.
 */
export async function DELETE(): Promise<NextResponse> {
  const auth = await requireAuth(["PATIENT"]);
  if (isAuthError(auth)) return auth;
  const username = await sessionUsername();
  if (!username || !isSandboxUsername(username)) {
    return NextResponse.json(
      {
        error: "Forbidden",
        reason: "only an account the app or a wallet created carries a link",
      },
      { status: 403 },
    );
  }
  try {
    if (!(await clearFingerprint(username))) {
      return NextResponse.json({ error: "Not found" }, { status: 404 });
    }
    return NextResponse.json({ linked: false });
  } catch (err) {
    return failed("DELETE", err);
  }
}
 
function failed(method: string, err: unknown): NextResponse {
  console.error(
    `${method} /api/patient/wallet-link:`,
    err instanceof Error ? err.message : "unknown",
  );
  const status = err instanceof AccountError ? err.status : 502;
  return NextResponse.json(
    { error: "Keycloak could not be asked about the wallet link" },
    { status },
  );
}