-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic-link.ts
More file actions
359 lines (331 loc) · 12.1 KB
/
Copy pathmagic-link.ts
File metadata and controls
359 lines (331 loc) · 12.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { randomBytes } from 'node:crypto';
import { nanoid } from 'nanoid';
import { and, eq, isNull, lt } from 'drizzle-orm';
import { db } from '../db/client.js';
import { guardianLink, magicLinkToken, session, user, userRole } from '../db/schema.js';
import { sendEmail } from '../integrations/email/ses.js';
import {
renderApplicantInviteEmail,
renderGuardianInviteEmail,
} from '../integrations/email/templates.js';
import { env } from '../env.js';
import { sessionTtlSeconds } from './session.js';
export type MagicLinkPurpose = 'applicant_signin' | 'guardian_invite' | 'guardian_signin';
export type SignInRole = 'applicant' | 'guardian';
const TOKEN_TTL_SECONDS = 15 * 60;
function nowSeconds(): number {
return Math.floor(Date.now() / 1000);
}
function newToken(): string {
return randomBytes(32).toString('base64url');
}
/*
* Canonical role of an existing user, based on explicit user_role rows.
* Returns null for a "ghost" record (email exists but the user has never
* completed sign-in and has no application data). Guardian wins if both are
* present — a rare case, but the parent role is more restrictive.
*/
export function canonicalRoleForEmail(email: string): SignInRole | null {
const normalized = email.trim().toLowerCase();
const u = db.select({ id: user.id }).from(user).where(eq(user.email, normalized)).get();
if (!u) return null;
return canonicalRole(u.id);
}
function canonicalRole(userId: string): SignInRole | null {
const roles = db
.select({ role: userRole.role })
.from(userRole)
.where(eq(userRole.userId, userId))
.all()
.map((r) => r.role);
if (roles.includes('guardian')) return 'guardian';
if (roles.includes('applicant')) return 'applicant';
return null;
}
export type RequestLinkFailure = { reason: 'role_mismatch'; registeredAs: SignInRole };
export type RequestLinkResult = { ok: true } | ({ ok: false } & RequestLinkFailure);
export async function requestMagicLink(
email: string,
role: SignInRole,
requestIp?: string | undefined,
): Promise<RequestLinkResult> {
const normalized = email.trim().toLowerCase();
const userId = upsertUser(normalized);
const canonical = canonicalRole(userId);
if (canonical !== null && canonical !== role) {
return { ok: false, reason: 'role_mismatch', registeredAs: canonical };
}
const purpose: MagicLinkPurpose =
role === 'guardian' ? 'guardian_signin' : 'applicant_signin';
const token = issueToken(userId, purpose, requestIp);
const link = `${env.APP_URL}/api/auth/verify?token=${encodeURIComponent(token)}`;
const forParent = role === 'guardian';
await sendEmail({
to: normalized,
subject: forParent
? 'Your ℝℙ² parent-portal sign-in link'
: 'Your ℝℙ² sign-in link',
text: [
'Hello,',
'',
forParent
? 'Follow this link to sign in to the ℝℙ² parent portal:'
: 'Follow this link to sign in to your ℝℙ² application:',
link,
'',
'This link expires in 15 minutes and can only be used once.',
'',
"If you didn't request this, you can ignore this email.",
].join('\n'),
html: `
<p>Hello,</p>
<p>${
forParent
? 'Follow this link to sign in to the ℝℙ² parent portal:'
: 'Follow this link to sign in to your ℝℙ² application:'
}</p>
<p><a href="${link}">${link}</a></p>
<p>This link expires in 15 minutes and can only be used once.</p>
<p>If you didn't request this, you can ignore this email.</p>
`,
});
return { ok: true };
}
/*
* Sends a `guardian_invite` magic link. Called from the applications service
* when the applicant first saves guardian_email (and rate-limited resends
* afterward). The token's `purpose='guardian_invite'` causes the interstitial
* to skip DOB collection and consumeToken to grant the `guardian` role +
* flip `guardian_link.acceptedAt`.
*/
/*
* Sends an `applicant_signin` magic link to a student the parent invited from
* their portal. Parallel of requestGuardianInvite: creates the user record if
* new, but the email is the parent-invites-student template rather than the
* plain sign-in one, so the recipient knows why they're being invited.
*/
export async function requestApplicantInvite(
applicantEmail: string,
guardianName: string,
): Promise<{ applicantUserId: string }> {
const normalized = applicantEmail.trim().toLowerCase();
const applicantUserId = upsertUser(normalized);
const token = issueToken(applicantUserId, 'applicant_signin');
const link = `${env.APP_URL}/api/auth/verify?token=${encodeURIComponent(token)}`;
const rendered = renderApplicantInviteEmail({ guardianName, magicLinkUrl: link });
await sendEmail({
to: normalized,
subject: rendered.subject,
text: rendered.text,
html: rendered.html,
});
return { applicantUserId };
}
export async function requestGuardianInvite(
guardianEmail: string,
applicantName: string,
): Promise<{ guardianUserId: string }> {
const normalized = guardianEmail.trim().toLowerCase();
const guardianUserId = upsertUser(normalized);
const token = issueToken(guardianUserId, 'guardian_invite');
const link = `${env.APP_URL}/api/auth/verify?token=${encodeURIComponent(token)}`;
const rendered = renderGuardianInviteEmail({ applicantName, magicLinkUrl: link });
await sendEmail({
to: normalized,
subject: rendered.subject,
text: rendered.text,
html: rendered.html,
});
return { guardianUserId };
}
function upsertUser(normalizedEmail: string): string {
const existing = db.select().from(user).where(eq(user.email, normalizedEmail)).get();
if (existing) return existing.id;
const id = nanoid();
db.insert(user).values({ id, email: normalizedEmail }).run();
return id;
}
function issueToken(
userId: string,
purpose: MagicLinkPurpose,
requestIp?: string | undefined,
): string {
const token = newToken();
const expiresAt = nowSeconds() + TOKEN_TTL_SECONDS;
db.insert(magicLinkToken)
.values({
token,
userId,
purpose,
expiresAt,
requestIp: requestIp ?? null,
})
.run();
return token;
}
export type TokenFailure = 'invalid' | 'expired' | 'used';
export type PreviewResult =
| {
ok: true;
email: string;
expiresAt: number;
needsDob: boolean;
purpose: MagicLinkPurpose;
}
| { ok: false; reason: TokenFailure };
/*
* Read-only lookup of a token. Does NOT consume — safe to call on GETs that
* an email scanner (Microsoft ATP, Mimecast, Proofpoint, etc.) may prefetch
* before a human ever clicks. Consume the token via consumeToken() only
* after an explicit human action (POST from the interstitial page).
*/
export function previewToken(token: string): PreviewResult {
const now = nowSeconds();
const row = db
.select({
token: magicLinkToken.token,
userId: magicLinkToken.userId,
purpose: magicLinkToken.purpose,
expiresAt: magicLinkToken.expiresAt,
usedAt: magicLinkToken.usedAt,
email: user.email,
dob: user.dob,
})
.from(magicLinkToken)
.innerJoin(user, eq(user.id, magicLinkToken.userId))
.where(eq(magicLinkToken.token, token))
.get();
if (!row) return { ok: false, reason: 'invalid' };
if (row.usedAt !== null) return { ok: false, reason: 'used' };
if (row.expiresAt <= now) return { ok: false, reason: 'expired' };
// Guardians don't go through the DOB gate — parents are adults, and we
// never ask for their DOB.
const needsDob = row.purpose === 'applicant_signin' && row.dob === null;
return {
ok: true,
email: row.email,
expiresAt: row.expiresAt,
needsDob,
purpose: row.purpose,
};
}
export const MINIMUM_AGE_YEARS = 13;
export function ageInYears(dobIso: string, at: Date = new Date()): number | null {
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dobIso);
if (!m) return null;
const y = Number(m[1]);
const mo = Number(m[2]);
const d = Number(m[3]);
if (mo < 1 || mo > 12 || d < 1 || d > 31) return null;
const dob = new Date(Date.UTC(y, mo - 1, d));
if (Number.isNaN(dob.getTime())) return null;
let age = at.getUTCFullYear() - y;
const cm = at.getUTCMonth() + 1;
const cd = at.getUTCDate();
if (cm < mo || (cm === mo && cd < d)) age--;
return age;
}
export type ConsumeResult =
| { ok: true; sessionId: string; userId: string; purpose: MagicLinkPurpose }
| { ok: false; reason: TokenFailure | 'dob_required' | 'too_young' | 'invalid_dob' };
/*
* Atomically mark a token used and mint a session. Called from POST-only
* routes; a scanner following the interstitial page will not fire this.
*
* If the user's DOB is not yet set, `dob` must be provided (YYYY-MM-DD).
* Enforces MINIMUM_AGE_YEARS at sign-in time; under-age users are rejected
* and their user record is deleted to avoid retaining any personal info.
*/
export function consumeToken(
token: string,
meta: { userAgent?: string | undefined; ip?: string | undefined; dob?: string | undefined },
): ConsumeResult {
const now = nowSeconds();
const row = db
.select({
token: magicLinkToken.token,
userId: magicLinkToken.userId,
purpose: magicLinkToken.purpose,
expiresAt: magicLinkToken.expiresAt,
usedAt: magicLinkToken.usedAt,
dob: user.dob,
})
.from(magicLinkToken)
.innerJoin(user, eq(user.id, magicLinkToken.userId))
.where(eq(magicLinkToken.token, token))
.get();
if (!row) return { ok: false, reason: 'invalid' };
if (row.usedAt !== null) return { ok: false, reason: 'used' };
if (row.expiresAt <= now) return { ok: false, reason: 'expired' };
const isApplicant = row.purpose === 'applicant_signin';
let dobToStore: string | null = row.dob;
if (isApplicant && row.dob === null) {
if (!meta.dob) return { ok: false, reason: 'dob_required' };
const age = ageInYears(meta.dob);
if (age === null) return { ok: false, reason: 'invalid_dob' };
if (age < MINIMUM_AGE_YEARS) {
// Consume the token first so it can't be retried with a different DOB,
// then delete the user record entirely (cascades to token + session +
// any partial application data). We keep no personal info about the
// rejected under-13.
db.update(magicLinkToken)
.set({ usedAt: now })
.where(eq(magicLinkToken.token, token))
.run();
db.delete(user).where(eq(user.id, row.userId)).run();
return { ok: false, reason: 'too_young' };
}
dobToStore = meta.dob;
}
const updated = db
.update(magicLinkToken)
.set({ usedAt: now })
.where(and(eq(magicLinkToken.token, token), isNull(magicLinkToken.usedAt)))
.run();
if (updated.changes === 0) return { ok: false, reason: 'used' };
// Grant the appropriate explicit role. Applicant is normally implicit at
// read time, but persisting it here lets requestMagicLink detect
// cross-role conflicts on future sign-in attempts.
const grantedRole: 'applicant' | 'guardian' =
row.purpose === 'applicant_signin' ? 'applicant' : 'guardian';
db.insert(userRole)
.values({ userId: row.userId, role: grantedRole })
.onConflictDoNothing()
.run();
// Guardian-invite acceptance also flips any pending guardian_link rows.
// Idempotent — safe on re-entry via a fresh sign-in link later.
if (row.purpose === 'guardian_invite') {
db.update(guardianLink)
.set({ acceptedAt: now })
.where(
and(
eq(guardianLink.guardianUserId, row.userId),
isNull(guardianLink.acceptedAt),
),
)
.run();
}
const sessionId = nanoid(32);
db.insert(session)
.values({
id: sessionId,
userId: row.userId,
expiresAt: now + sessionTtlSeconds(),
userAgent: meta.userAgent ?? null,
ip: meta.ip ?? null,
})
.run();
db.update(user)
.set({ lastLoginAt: now, dob: dobToStore })
.where(eq(user.id, row.userId))
.run();
return { ok: true, sessionId, userId: row.userId, purpose: row.purpose };
}
export function destroySession(sessionId: string): void {
db.delete(session).where(eq(session.id, sessionId)).run();
}
export function purgeExpired(): void {
const now = nowSeconds();
db.delete(magicLinkToken).where(lt(magicLinkToken.expiresAt, now)).run();
db.delete(session).where(lt(session.expiresAt, now)).run();
}