Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
magic-link.ts12.7 KB · 331 lines
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
/**
 * Block Q2 — Magic-link sign-in.
 *
 * Public surface:
 *   - generateMagicLinkToken()
 *   - startMagicLinkSignIn()  → always { ok: true } (no enumeration)
 *   - consumeMagicLinkToken()
 *
 * Structurally identical to P1 (`password-reset.ts`) and P2
 * (`email-verification.ts`): short random token, sha256-hashed at rest,
 * single-use, time-limited. The only meaningful differences are:
 *
 *   - 15-minute TTL (vs P1's 1h reset) — magic-link is a session-issuer,
 *     not a one-shot password rotation, so the blast radius of a stolen
 *     link is higher and we want the window tight.
 *   - `user_id` is nullable. When a not-yet-registered email is entered,
 *     we still mint a token row; consume creates the account on click
 *     (autoCreate=true). The click itself is proof the recipient owns
 *     the address — same trust model as a verification link.
 *
 * Security:
 *   - Plaintext token NEVER persists; we store only SHA-256(token).
 *   - startMagicLinkSignIn never reveals whether the email exists.
 *   - consumeMagicLinkToken invalidates every other unused magic-link
 *     for the same email on success (prevents multi-link abuse).
 *   - Per-email rate limit: at most 3 token mints per hour. The HTTP
 *     surface adds a per-IP rate limit on top of that.
 */

import { eq } from "drizzle-orm";
import { db } from "../db";
import { users, magicLinkTokens } from "../db/schema";
import { hashPassword } from "./auth";
import { sendEmail, absoluteUrl, type EmailMessage } from "./email";

const MAGIC_LINK_TTL_MS = 15 * 60 * 1000; // 15 minutes
const MAX_TOKENS_PER_EMAIL_PER_HOUR = 3;

// ---------------------------------------------------------------------------
// Test seam — swap the email sender without `mock.module`. Mirrors P1.
// ---------------------------------------------------------------------------
type EmailSender = (msg: EmailMessage) => Promise<unknown> | unknown;
let _emailSender: EmailSender = sendEmail;
export function __setEmailForTests(fn: EmailSender | null): void {
  _emailSender = fn ?? sendEmail;
}

// ---------------------------------------------------------------------------
// Token primitives.
// ---------------------------------------------------------------------------

function toHex(bytes: Uint8Array): string {
  let out = "";
  for (let i = 0; i < bytes.length; i++)
    out += bytes[i]!.toString(16).padStart(2, "0");
  return out;
}

async function sha256Hex(input: string): Promise<string> {
  const buf = new TextEncoder().encode(input);
  const digest = await crypto.subtle.digest("SHA-256", buf);
  return toHex(new Uint8Array(digest));
}

export function generateMagicLinkToken(): { plaintext: string; hash: string } {
  const bytes = crypto.getRandomValues(new Uint8Array(32));
  const plaintext = toHex(bytes);
  const hasher = new Bun.CryptoHasher("sha256");
  hasher.update(plaintext);
  const hash = hasher.digest("hex");
  return { plaintext, hash };
}

// ---------------------------------------------------------------------------
// Email template — reuses the same dark-theme gradient shell as P1.
// ---------------------------------------------------------------------------

function escapeHtml(s: string): string {
  return s
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#39;");
}

function buildMagicLinkEmail(opts: { signInUrl: string }) {
  const subject = "Your Gluecron sign-in link";
  const text = [
    "Hi,",
    "",
    "Click the link below to sign in to Gluecron. This link expires in 15 minutes.",
    "",
    `Sign in: ${opts.signInUrl}`,
    "",
    "If you didn't request this, ignore this email — no one can sign in",
    "without clicking the link.",
    "",
    "— gluecron",
  ].join("\n");

  const safeUrl = escapeHtml(opts.signInUrl);
  const html = `<!doctype html>
<html><head><meta charset="utf-8"><title>${escapeHtml(subject)}</title></head>
<body style="margin:0;padding:0;background:#0d1117;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,sans-serif;color:#c9d1d9">
  <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#0d1117">
    <tr><td align="center" style="padding:32px 16px">
      <table role="presentation" width="600" cellpadding="0" cellspacing="0" style="max-width:600px;background:#161b22;border:1px solid #30363d;border-radius:12px;overflow:hidden">
        <tr><td style="background:linear-gradient(135deg,#5b6ee8 0%,#5f8fa0 100%);padding:24px 28px">
          <div style="font-size:20px;font-weight:700;color:#fff;letter-spacing:-0.01em">gluecron</div>
          <div style="font-size:13px;color:rgba(255,255,255,0.85);margin-top:2px">Magic sign-in link</div>
        </td></tr>
        <tr><td style="padding:28px">
          <p style="margin:0 0 12px;font-size:15px;color:#e6edf3">Hi,</p>
          <p style="margin:0 0 16px;font-size:14px;line-height:1.55;color:#c9d1d9">Click the button below to sign in to Gluecron. This link expires in 15 minutes.</p>
          <p style="margin:0 0 24px"><a href="${safeUrl}" style="display:inline-block;background:linear-gradient(135deg,#5b6ee8 0%,#5f8fa0 100%);color:#fff;text-decoration:none;font-weight:600;font-size:14px;padding:11px 22px;border-radius:9999px">Sign in</a></p>
          <p style="margin:0 0 8px;font-size:13px;color:#8b949e">Or copy this link into your browser:</p>
          <p style="margin:0 0 24px;font-size:12px;color:#8b949e;word-break:break-all"><a href="${safeUrl}" style="color:#58a6ff;text-decoration:none">${safeUrl}</a></p>
          <p style="margin:0;font-size:12px;color:#8b949e;line-height:1.55">If you didn't request this, ignore this email — no one can sign in without clicking the link.</p>
        </td></tr>
        <tr><td style="padding:16px 28px;border-top:1px solid #30363d;font-size:11px;color:#6e7681">gluecron — AI-native code intelligence</td></tr>
      </table>
    </td></tr>
  </table>
</body></html>`;

  return { subject, text, html };
}

export function buildMagicLinkUrl(plaintextToken: string): string {
  const path = `/login/magic/callback?token=${encodeURIComponent(plaintextToken)}`;
  return absoluteUrl(path);
}

// ---------------------------------------------------------------------------
// startMagicLinkSignIn — always returns ok, never reveals enumeration.
// ---------------------------------------------------------------------------

export async function startMagicLinkSignIn(
  email: string,
  opts: { requestIp?: string; autoCreate?: boolean } = {}
): Promise<{ ok: boolean }> {
  const autoCreate = opts.autoCreate !== false; // default true
  const normalized = String(email || "").trim().toLowerCase();
  if (!normalized || !normalized.includes("@")) return { ok: true };

  try {
    // Per-email throttle — prevents enumeration via timing AND volume.
    const recent = await db
      .select({
        id: magicLinkTokens.id,
        createdAt: magicLinkTokens.createdAt,
      })
      .from(magicLinkTokens)
      .where(eq(magicLinkTokens.email, normalized))
      .limit(100);
    const cutoff = Date.now() - 60 * 60 * 1000;
    const recentCount = recent.filter(
      (r) => new Date(r.createdAt).getTime() > cutoff
    ).length;
    if (recentCount >= MAX_TOKENS_PER_EMAIL_PER_HOUR) {
      console.error(
        `[magic-link] per-email rate limit hit for ${JSON.stringify(normalized)} ip=${opts.requestIp || "?"} — generic success returned`
      );
      return { ok: true };
    }

    const [user] = await db
      .select({ id: users.id, email: users.email })
      .from(users)
      .where(eq(users.email, normalized))
      .limit(1);

    if (!user && !autoCreate) {
      console.error(
        `[magic-link] no user for email=${JSON.stringify(normalized)} ip=${opts.requestIp || "?"} autoCreate=false — generic success returned`
      );
      return { ok: true };
    }

    const { plaintext, hash } = generateMagicLinkToken();
    const expiresAt = new Date(Date.now() + MAGIC_LINK_TTL_MS);

    await db.insert(magicLinkTokens).values({
      email: normalized,
      userId: user?.id ?? null,
      tokenHash: hash,
      expiresAt,
      requestIp: opts.requestIp || null,
    });

    const signInUrl = buildMagicLinkUrl(plaintext);
    const msg = buildMagicLinkEmail({ signInUrl });

    // Fire-and-forget — never block the response on email send.
    Promise.resolve()
      .then(() =>
        _emailSender({
          to: normalized,
          subject: msg.subject,
          text: msg.text,
          html: msg.html,
        })
      )
      .catch((err) =>
        console.error("[magic-link] email send error:", err)
      );

    return { ok: true };
  } catch (err) {
    console.error("[magic-link] startMagicLinkSignIn error:", err);
    return { ok: true };
  }
}

// ---------------------------------------------------------------------------
// consumeMagicLinkToken — happy path returns a userId + createdAccount flag.
// ---------------------------------------------------------------------------

export async function consumeMagicLinkToken(
  token: string,
  opts: { autoCreate?: boolean; requestIp?: string } = {}
): Promise<{
  ok: boolean;
  userId?: string;
  createdAccount?: boolean;
  reason?: string;
}> {
  const autoCreate = opts.autoCreate !== false; // default true
  const plaintext = String(token || "").trim();
  if (!plaintext) return { ok: false, reason: "invalid" };

  try {
    const hash = await sha256Hex(plaintext);
    const [row] = await db
      .select()
      .from(magicLinkTokens)
      .where(eq(magicLinkTokens.tokenHash, hash))
      .limit(1);

    if (!row) return { ok: false, reason: "invalid" };
    if (row.usedAt) return { ok: false, reason: "used" };
    if (new Date(row.expiresAt).getTime() < Date.now())
      return { ok: false, reason: "expired" };

    let userId = row.userId ?? undefined;
    let createdAccount = false;

    if (!userId) {
      if (!autoCreate) return { ok: false, reason: "no-account" };
      // Mint a fresh account. The click is proof of email ownership, so
      // we set emailVerifiedAt immediately. The password hash is a non-
      // matchable placeholder — the user can set a real password later
      // via /settings, or just keep using magic links forever.
      // We mint the UUID client-side so we don't need .returning() on the
      // insert (keeps the surface minimal + cheap to stub in tests).
      const username = await pickFreshUsername(row.email);
      const placeholderPw = await hashPassword(generateRandomString(32));
      const newUserId = crypto.randomUUID();
      await db.insert(users).values({
        id: newUserId,
        username,
        email: row.email,
        passwordHash: placeholderPw,
        emailVerifiedAt: new Date(),
      });
      userId = newUserId;
      createdAccount = true;
    }

    const now = new Date();

    // Mark the current token used.
    await db
      .update(magicLinkTokens)
      .set({ usedAt: now })
      .where(eq(magicLinkTokens.id, row.id));

    // Invalidate every other unused magic-link for this email. We use a
    // broad eq(email) — already-used rows already have usedAt set, so
    // this is a no-op for them; what matters is unused rows being
    // burned so a second link mailed within the 15-min window can't be
    // replayed.
    await db
      .update(magicLinkTokens)
      .set({ usedAt: now })
      .where(eq(magicLinkTokens.email, row.email));

    return { ok: true, userId, createdAccount };
  } catch (err) {
    console.error("[magic-link] consumeMagicLinkToken error:", err);
    return { ok: false, reason: "invalid" };
  }
}

// ---------------------------------------------------------------------------
// Auto-create helpers.
// ---------------------------------------------------------------------------

function generateRandomString(n: number): string {
  const bytes = crypto.getRandomValues(new Uint8Array(n));
  return toHex(bytes);
}

/** 8 url-safe lowercase chars derived from random bytes. */
function shortSuffix(): string {
  const bytes = crypto.getRandomValues(new Uint8Array(8));
  const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
  let out = "";
  for (let i = 0; i < 8; i++) out += alphabet[bytes[i]! % alphabet.length];
  return out;
}

/**
 * Pick a free `user-XXXXXXXX` username. Retries a handful of times on
 * collision. Pure name minting — no DB writes here.
 */
async function pickFreshUsername(_emailHint: string): Promise<string> {
  for (let attempt = 0; attempt < 8; attempt++) {
    const candidate = `user-${shortSuffix()}`;
    const [clash] = await db
      .select({ id: users.id })
      .from(users)
      .where(eq(users.username, candidate))
      .limit(1);
    if (!clash) return candidate;
  }
  // 8 random 8-char rolls all colliding is astronomically unlikely; if it
  // happens we fall back to a longer suffix that's effectively unique.
  return `user-${shortSuffix()}${shortSuffix()}`;
}