|
| 1 | +const CORNELL_SUFFIX = "@cornell.edu"; |
| 2 | + |
| 3 | +function normalizeEmail(email: string): string { |
| 4 | + return email.trim().toLowerCase(); |
| 5 | +} |
| 6 | + |
| 7 | +/** Full addresses from EMAIL_WHITELIST (comma-separated), lowercased. */ |
| 8 | +export function getEmailWhitelist(): Set<string> { |
| 9 | + const raw = process.env.EMAIL_WHITELIST ?? ""; |
| 10 | + const set = new Set<string>(); |
| 11 | + for (const part of raw.split(",")) { |
| 12 | + const normalized = normalizeEmail(part); |
| 13 | + if (normalized.length > 0) { |
| 14 | + set.add(normalized); |
| 15 | + } |
| 16 | + } |
| 17 | + return set; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * True if the Firebase user email may use the API: @cornell.edu or listed in EMAIL_WHITELIST. |
| 22 | + * When true, `email` is a non-empty string. |
| 23 | + */ |
| 24 | +export function isAllowedLoginEmail( |
| 25 | + email: string | undefined, |
| 26 | + whitelist: Set<string> = getEmailWhitelist(), |
| 27 | +): email is string { |
| 28 | + if (!email) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + const e = normalizeEmail(email); |
| 32 | + if (e.endsWith(CORNELL_SUFFIX)) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + return whitelist.has(e); |
| 36 | +} |
| 37 | + |
| 38 | +function isProdEnv(): boolean { |
| 39 | + return process.env.IS_PROD?.toLowerCase() === "true"; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Human-readable token email for logs / dev error text (never throws). |
| 44 | + */ |
| 45 | +export function normalizedTokenEmailForDebug( |
| 46 | + email: string | undefined, |
| 47 | +): string { |
| 48 | + if (!email || !email.trim()) { |
| 49 | + return "(no email claim on ID token)"; |
| 50 | + } |
| 51 | + return normalizeEmail(email); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * 403 message for disallowed emails. In non-prod, includes normalized token |
| 56 | + * email and parsed EMAIL_WHITELIST for debugging. Prod stays generic. |
| 57 | + * Always logs a line to stderr when called. |
| 58 | + */ |
| 59 | +export function buildForbiddenEmailMessage(email: string | undefined): string { |
| 60 | + const base = |
| 61 | + "Only Cornell email addresses or whitelisted emails are allowed."; |
| 62 | + const normalizedFromToken = normalizedTokenEmailForDebug(email); |
| 63 | + const whitelistEntries = Array.from(getEmailWhitelist()).sort(); |
| 64 | + const listText = |
| 65 | + whitelistEntries.length > 0 |
| 66 | + ? whitelistEntries.join(", ") |
| 67 | + : "(EMAIL_WHITELIST is empty)"; |
| 68 | + |
| 69 | + console.warn("[email-auth] rejected", { |
| 70 | + normalizedFromToken, |
| 71 | + whitelistEntryCount: whitelistEntries.length, |
| 72 | + whitelistEntries: isProdEnv() ? "[redacted in prod logs]" : whitelistEntries, |
| 73 | + }); |
| 74 | + |
| 75 | + if (isProdEnv()) { |
| 76 | + return base; |
| 77 | + } |
| 78 | + return `${base} Token email (normalized): "${normalizedFromToken}". Whitelist: ${listText}.`; |
| 79 | +} |
0 commit comments