Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/auth/access-snapshot.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ describe.skipIf(!baseURL || !databaseURL || !clientId || !audience)(
{
sub: "snapshot-integration-user",
name: "Snapshot User",
email: "snapshot@identity.invalid",
// A sign-in placeholder is never sent.
email: null,
},
]),
);
Expand Down
24 changes: 15 additions & 9 deletions src/auth/contact-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ export function directoryEmail(email: string, tokens: SignInToken[]) {
return providerEmail("pn-entra", tokens) ?? contactEmail(email, tokens);
}

/** `directoryEmail` for many people at once, keyed by user ID. Null when none is known. */
/**
* `directoryEmail` for many people at once, keyed by user ID. Null when none is known.
* `everyone` reads every account instead of naming each person, for lists of all users.
*/
export async function contactEmails(
reader: Reader,
people: { id: string; email: string }[],
scope: "listed" | "everyone" = "listed",
): Promise<Map<string, string | null>> {
const tokens = people.length
? await reader
Expand All @@ -98,21 +102,23 @@ export async function contactEmails(
)
.where(
and(
inArray(
account.userId,
people.map((person) => person.id),
),
scope === "listed"
? inArray(
account.userId,
people.map((person) => person.id),
)
: undefined,
inArray(account.providerId, EMAIL_PROVIDERS),
),
)
: [];
const byUser = new Map<string, SignInToken[]>();
for (const token of tokens)
byUser.set(token.userId, [...(byUser.get(token.userId) ?? []), token]);
return new Map(
people.map((person) => [
person.id,
directoryEmail(
person.email,
tokens.filter((token) => token.userId === person.id),
) ?? null,
directoryEmail(person.email, byUser.get(person.id) ?? []) ?? null,
]),
);
}
Expand Down
18 changes: 15 additions & 3 deletions src/routes/api/internal/esign-users.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { createFileRoute } from "@tanstack/react-router";
import { asc } from "drizzle-orm";
import { snapshotClientId } from "@/auth/access-snapshot-token";
import { contactEmails } from "@/auth/contact-email";
import { projectionForClient } from "@/auth/access-projections";
import { db } from "@/db";
import { user } from "@/db/schema";
import { env } from "@/env";

const headers = { "Cache-Control": "private, no-store", Vary: "Authorization" };

/** Registered identities for the backend's e-signing picker; access is checked by the backend's live snapshot. */
/**
* Registered identities for the backend's e-signing picker; access is checked by the backend's live snapshot.
* The email is the one administrators see (PoliNetwork first), never a sign-in placeholder.
*/
export const Route = createFileRoute("/api/internal/esign-users")({
server: {
handlers: {
Expand All @@ -20,10 +24,18 @@ export const Route = createFileRoute("/api/internal/esign-users")({
if (projectionForClient(clientId) !== "backend")
return Response.json({ error: "Forbidden" }, { status: 403, headers });
const people = await db
.select({ sub: user.id, name: user.name, email: user.email })
.select({ id: user.id, name: user.name, email: user.email })
.from(user)
.orderBy(asc(user.name), asc(user.id));
return Response.json(people, { headers });
const emails = await contactEmails(db, people, "everyone");
return Response.json(
people.map((person) => ({
sub: person.id,
name: person.name,
email: emails.get(person.id) ?? null,
})),
{ headers },
);
},
},
},
Expand Down
Loading