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 | 81x 81x 81x 172x 172x 1497x 58x 54x 56x 56x 56x 56x 116x 116x 88x 88x 88x 88x 88x 1x | /**
* Short-TTL, in-memory store for in-flight EUDI Wallet OpenID4VP login
* transactions, keyed by an opaque server-issued session id (`sid`).
*
* The `sid` is the only handle the browser ever sees — the verifier-side
* transaction id, the nonce, and (once verified) the resolved patient identity
* never leave the server. The browser polls `GET /api/auth/eudi/status?sid=…`
* and, on completion, calls `signIn("eudi-wallet", { sid })`; the Credentials
* provider re-reads the pinned, server-verified result for that `sid`.
*
* Persistence: an in-memory Map, one per replica. The Azure UI runs up to six
* replicas (ADR-061), so its ingress has sticky sessions on
* (scripts/azure/size-ui.sh) and every poll from a browser reaches the replica
* that started its sign-in; without them the first poll answered 404 (#708).
* It does NOT survive a revision rollover/restart — in-flight logins would
* drop. A durable store (Neo4j / Vault / Postgres) is the production path.
* Flagged in ADR-028.
*/
import { randomUUID, randomBytes } from "crypto";
import type { AssuranceLevel } from "@/lib/assurance";
export type EudiTxStatus = "pending" | "completed" | "error";
export interface EudiVerifiedPatient {
/** username key matching DEMO_PERSONAS / PATIENT_RESOURCE_MAP (e.g. "patient1") */
username: string;
/** display name — the verified wallet holder's name when available */
displayName: string;
roles: string[];
/** the sign-in's eIDAS level: high for a PID, low for the simulated wallet (#708) */
assurance: AssuranceLevel;
}
/**
* What a presentation is for (ADR-067): to sign in, or to link the PID to the
* signed-in sandbox account.
*/
export type EudiPurpose = "signin" | "link";
export interface EudiTransaction {
sid: string;
/** "signin" unless the start route was asked to link */
purpose: EudiPurpose;
/** the signed-in account a link is for; set only when purpose is "link" */
linkUsername?: string;
/**
* The PID's secure hash (ADR-067), kept only for the register step after a
* sign-in found no account, and dropped with the transaction. Never a name
* or a birth date: those are not stored anywhere.
*/
fingerprint?: string;
/** true when a sign-in found no account and the PID may create one */
canRegister?: boolean;
/** verifier-side transaction / presentation id */
transactionId: string;
/** anti-replay nonce echoed by the wallet's presentation */
nonce: string;
status: EudiTxStatus;
createdAt: number;
/** pinned only after the verifier confirms a valid presentation */
verifiedPatient?: EudiVerifiedPatient;
/** when the verifier first saw the wallet fetch the request (#708) */
walletOpenedAt?: number;
/** true once a session has been minted from this sid (single-use) */
consumed?: boolean;
error?: string;
}
const TTL_MS = 5 * 60 * 1000;
// One Map per process, not per route bundle: Next.js compiles each route on its
// own (and recompiles on the fly in dev), so a module-level Map would give the
// start route, the status route and the NextAuth callback three different
// stores. The usual globalThis anchor keeps it one.
const g = globalThis as unknown as {
__eudiTxStore?: Map<string, EudiTransaction>;
};
const store: Map<string, EudiTransaction> =
g.__eudiTxStore ?? (g.__eudiTxStore = new Map());
/** Remove expired transactions so the Map can't grow unbounded. */
function sweep(): void {
const cutoff = Date.now() - TTL_MS;
for (const [sid, tx] of store) {
if (tx.createdAt < cutoff) store.delete(sid);
}
}
/** Opaque browser-facing id. */
export function newSid(): string {
return randomUUID();
}
/** Anti-replay nonce for the OpenID4VP request. */
export function newNonce(): string {
return randomBytes(24).toString("base64url");
}
export function putTransaction(
tx: Omit<EudiTransaction, "status" | "createdAt" | "purpose"> &
Partial<Pick<EudiTransaction, "status" | "createdAt" | "purpose">>,
): EudiTransaction {
sweep();
const full: EudiTransaction = {
status: "pending",
createdAt: Date.now(),
purpose: "signin",
...tx,
};
store.set(full.sid, full);
return full;
}
export function getTransaction(sid: string): EudiTransaction | undefined {
sweep();
return store.get(sid);
}
export function updateTransaction(
sid: string,
patch: Partial<EudiTransaction>,
): EudiTransaction | undefined {
const tx = store.get(sid);
Iif (!tx) return undefined;
const next = { ...tx, ...patch };
store.set(sid, next);
return next;
}
export function deleteTransaction(sid: string): void {
store.delete(sid);
}
|