The Entra ID rewrite
From the provider-abstraction design to the token validator and the install flow that configures it — and where the shipped code deliberately diverges from the plan.
backend/src/lib/auth/types.ts50 lines · AuthPrincipal L1–30
Outline 3 symbols
- AuthPrincipal interface export
- AuthValidationResult type export
- authorizationEmail function export
1export interface AuthPrincipal {
2 userId: string;
3 email: string;
4 /**
5 * Display name from the IdP, when available. The auth middleware uses this
6 * to back-fill `user_profiles.display_name` on a user's first authenticated
7 * request. Subsequent requests do NOT overwrite a non-null display_name —
8 * users can edit their own name on the Account page and the IdP value is
9 * only used to seed an initially-empty row.
10 *
11 * Sources by provider:
12 * - entra: `name` claim (default in v2.0 access tokens), with fallback
13 * to `given_name + family_name` or `preferred_username`.
14 * - local: not available — local tokens carry no name claim.
15 * - supabase: not extracted (legacy path; user manages their own profile).
16 */
17 displayName?: string;
18 /**
19 * False when the IdP reports that the account has NOT proved it owns
20 * `email` (Supabase: no `email_confirmed_at`). Undefined means the provider
21 * has no such notion: Entra emails are tenant-administered directory
22 * attributes (single configured tenant, `tid` checked) and local tokens are
23 * a development-only, email-derived identity.
24 */
25 emailVerified?: boolean;
26 tenantId?: string;
27 groups: string[];
28 roles: string[];
29 provider: string;
30}
31
32export type AuthValidationResult =
33 | { ok: true; principal: AuthPrincipal }
34 | { ok: false; status: 401 | 403; detail: string };
35
36/**
37 * The email `res.locals.userEmail` carries. It is an AUTHORIZATION input,
38 * not a display value: direct grants (projects, chats, reviews, workflows) and
39 * organization invitations are matched against it. An address the account has
40 * not proved it owns must therefore match nothing, otherwise signing up as
41 * someone else's address before they do inherits whatever was shared with it.
42 * Such a session still authenticates; only email-keyed matching is withheld.
43 * (Upstream d146998d did this in requireAuth with Supabase's
44 * `email_confirmed_at`; Dev's providers report it as `emailVerified`.)
45 */
46export function authorizationEmail(principal: AuthPrincipal): string {
47 if (principal.emailVerified === false) return "";
48 return principal.email.trim().toLowerCase();
49}
50