summaryrefslogtreecommitdiff
path: root/app/lib/auth.server.ts
blob: e0e93b811d2c450641cae8b33e10b0c7624c77ee (plain)
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
/**
 * Single-user passkey auth + favorites.
 *
 * - どのパスキーでログインしても同じ「オーナー」としてログインする(ユーザーは1人)
 * - 未ログイン時のパスキー登録は、DATA_DIR/registration-token に書かれたトークンが必要
 *   - パスキーが1つも無ければ起動後の初回アクセス時に自動生成される
 *   - 全デバイスを失った場合は、このファイルを手で作れば再登録できる
 * - ログイン済みなら追加のパスキー(別デバイス)をトークン無しで登録できる
 */
import crypto from "crypto";
import fs from "fs";
import path from "path";
import {
  generateAuthenticationOptions,
  generateRegistrationOptions,
  verifyAuthenticationResponse,
  verifyRegistrationResponse,
  type AuthenticationResponseJSON,
  type RegistrationResponseJSON,
} from "@simplewebauthn/server";
import { DATA_DIR, getDb, normalizeTerm } from "./db.server";

const SESSION_COOKIE = "sid";
const CHALLENGE_COOKIE = "wa_chal";
const SESSION_DAYS = 90;
const CHALLENGE_MINUTES = 5;
const RP_NAME = "ライブに行くしかない";
const TOKEN_PATH = path.join(DATA_DIR, "registration-token");

// ---------- Request helpers ----------

/** Origin/RP ID as seen by the browser. Behind Caddy, the app itself receives plain http. */
function relyingParty(request: Request): { origin: string; rpID: string } {
  const url = new URL(request.url);
  const host = request.headers.get("x-forwarded-host") ?? request.headers.get("host") ?? url.host;
  const proto = request.headers.get("x-forwarded-proto") ?? url.protocol.replace(/:$/, "");
  return { origin: `${proto}://${host}`, rpID: host.replace(/:\d+$/, "") };
}

function readCookie(request: Request, name: string): string | null {
  const header = request.headers.get("cookie");
  if (!header) return null;
  for (const part of header.split(";")) {
    const [k, ...v] = part.trim().split("=");
    if (k === name) return decodeURIComponent(v.join("="));
  }
  return null;
}

function cookie(request: Request, name: string, value: string, maxAgeSec: number): string {
  const secure = relyingParty(request).origin.startsWith("https:") ? "; Secure" : "";
  return `${name}=${encodeURIComponent(value)}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${maxAgeSec}${secure}`;
}

function sqlTimeIn(seconds: number): string {
  return new Date(Date.now() + seconds * 1000).toISOString().replace("T", " ").slice(0, 19);
}

// ---------- Sessions ----------

export function isLoggedIn(request: Request): boolean {
  const sid = readCookie(request, SESSION_COOKIE);
  if (!sid) return false;
  const row = getDb()
    .prepare("SELECT 1 FROM sessions WHERE id = ? AND expires_at > datetime('now')")
    .get(sid);
  return !!row;
}

export function requireLogin(request: Request): void {
  if (!isLoggedIn(request)) {
    const url = new URL(request.url);
    throw new Response(null, {
      status: 302,
      headers: { Location: `/login?next=${encodeURIComponent(url.pathname + url.search)}` },
    });
  }
}

function createSession(request: Request): string {
  const db = getDb();
  db.prepare("DELETE FROM sessions WHERE expires_at <= datetime('now')").run();
  const sid = crypto.randomBytes(32).toString("base64url");
  const maxAge = SESSION_DAYS * 86400;
  db.prepare("INSERT INTO sessions (id, expires_at) VALUES (?, ?)").run(sid, sqlTimeIn(maxAge));
  return cookie(request, SESSION_COOKIE, sid, maxAge);
}

export function logout(request: Request): string {
  const sid = readCookie(request, SESSION_COOKIE);
  if (sid) getDb().prepare("DELETE FROM sessions WHERE id = ?").run(sid);
  return cookie(request, SESSION_COOKIE, "", 0);
}

// ---------- Challenges (one-time, short-lived) ----------

function storeChallenge(request: Request, challenge: string): string {
  const db = getDb();
  db.prepare("DELETE FROM auth_challenges WHERE expires_at <= datetime('now')").run();
  const id = crypto.randomBytes(16).toString("base64url");
  const maxAge = CHALLENGE_MINUTES * 60;
  db.prepare("INSERT INTO auth_challenges (id, challenge, expires_at) VALUES (?, ?, ?)").run(
    id, challenge, sqlTimeIn(maxAge)
  );
  return cookie(request, CHALLENGE_COOKIE, id, maxAge);
}

function consumeChallenge(request: Request): string | null {
  const id = readCookie(request, CHALLENGE_COOKIE);
  if (!id) return null;
  const db = getDb();
  const row = db
    .prepare("SELECT challenge FROM auth_challenges WHERE id = ? AND expires_at > datetime('now')")
    .get(id) as { challenge: string } | undefined;
  db.prepare("DELETE FROM auth_challenges WHERE id = ?").run(id);
  return row?.challenge ?? null;
}

// ---------- Registration token ----------

function passkeyCount(): number {
  return (getDb().prepare("SELECT COUNT(*) AS n FROM passkeys").get() as { n: number }).n;
}

/** Ensure a registration token exists while no passkey is registered (first-time setup). */
export function ensureSetupToken(): void {
  if (passkeyCount() > 0 || fs.existsSync(TOKEN_PATH)) return;
  fs.writeFileSync(TOKEN_PATH, crypto.randomBytes(24).toString("base64url") + "\n", { mode: 0o600 });
  console.log(`[auth] No passkey registered. Registration token written to ${TOKEN_PATH}`);
}

function checkToken(token: string | null): boolean {
  if (!token || !fs.existsSync(TOKEN_PATH)) return false;
  const expected = Buffer.from(fs.readFileSync(TOKEN_PATH, "utf8").trim());
  const given = Buffer.from(token.trim());
  return expected.length === given.length && crypto.timingSafeEqual(expected, given);
}

export function canRegister(request: Request, token: string | null): boolean {
  return isLoggedIn(request) || checkToken(token);
}

// ---------- WebAuthn ceremonies ----------

interface PasskeyRow {
  id: string;
  public_key: Buffer;
  counter: number;
  transports: string | null;
}

const OWNER_ID = new TextEncoder().encode("owner");

export async function registrationOptions(request: Request) {
  const { rpID } = relyingParty(request);
  const existing = getDb().prepare("SELECT id, transports FROM passkeys").all() as PasskeyRow[];
  const options = await generateRegistrationOptions({
    rpName: RP_NAME,
    rpID,
    userID: OWNER_ID,
    userName: "owner",
    userDisplayName: "オーナー",
    attestationType: "none",
    excludeCredentials: existing.map((p) => ({
      id: p.id,
      transports: p.transports ? JSON.parse(p.transports) : undefined,
    })),
    authenticatorSelection: { residentKey: "required", userVerification: "preferred" },
  });
  return { options, setCookie: storeChallenge(request, options.challenge) };
}

export async function verifyRegistration(
  request: Request,
  response: RegistrationResponseJSON,
  label: string | null,
  usedToken: boolean
): Promise<string> {
  const expectedChallenge = consumeChallenge(request);
  if (!expectedChallenge) throw new Error("challenge expired");
  const { origin, rpID } = relyingParty(request);
  const result = await verifyRegistrationResponse({
    response,
    expectedChallenge,
    expectedOrigin: origin,
    expectedRPID: rpID,
    requireUserVerification: false,
  });
  if (!result.verified) throw new Error("registration not verified");

  const { credential } = result.registrationInfo;
  getDb()
    .prepare("INSERT INTO passkeys (id, public_key, counter, transports, label) VALUES (?, ?, ?, ?, ?)")
    .run(
      credential.id,
      Buffer.from(credential.publicKey),
      credential.counter,
      credential.transports ? JSON.stringify(credential.transports) : null,
      label?.slice(0, 60) || null
    );

  // Token is single-use
  if (usedToken) fs.rmSync(TOKEN_PATH, { force: true });

  return createSession(request);
}

export async function authenticationOptions(request: Request) {
  const { rpID } = relyingParty(request);
  const options = await generateAuthenticationOptions({ rpID, userVerification: "preferred" });
  return { options, setCookie: storeChallenge(request, options.challenge) };
}

export async function verifyAuthentication(
  request: Request,
  response: AuthenticationResponseJSON
): Promise<string> {
  const expectedChallenge = consumeChallenge(request);
  if (!expectedChallenge) throw new Error("challenge expired");
  const db = getDb();
  const passkey = db.prepare("SELECT * FROM passkeys WHERE id = ?").get(response.id) as
    | PasskeyRow
    | undefined;
  if (!passkey) throw new Error("unknown passkey");

  const { origin, rpID } = relyingParty(request);
  const result = await verifyAuthenticationResponse({
    response,
    expectedChallenge,
    expectedOrigin: origin,
    expectedRPID: rpID,
    credential: {
      id: passkey.id,
      publicKey: new Uint8Array(passkey.public_key),
      counter: passkey.counter,
      transports: passkey.transports ? JSON.parse(passkey.transports) : undefined,
    },
    requireUserVerification: false,
  });
  if (!result.verified) throw new Error("authentication not verified");

  db.prepare("UPDATE passkeys SET counter = ?, last_used_at = datetime('now') WHERE id = ?").run(
    result.authenticationInfo.newCounter,
    passkey.id
  );
  return createSession(request);
}

// ---------- Passkey management ----------

export interface PasskeyInfo {
  id: string;
  label: string | null;
  created_at: string;
  last_used_at: string | null;
}

export function listPasskeys(): PasskeyInfo[] {
  return getDb()
    .prepare("SELECT id, label, created_at, last_used_at FROM passkeys ORDER BY created_at")
    .all() as PasskeyInfo[];
}

/** Refuses to delete the last passkey so the owner can't lock themselves out. */
export function deletePasskey(id: string): boolean {
  if (passkeyCount() <= 1) return false;
  return getDb().prepare("DELETE FROM passkeys WHERE id = ?").run(id).changes > 0;
}

// ---------- Favorites ----------

export interface Favorite {
  id: number;
  term: string;
  upcoming_count: number;
}

export function listFavorites(): Favorite[] {
  return getDb()
    .prepare(
      `SELECT f.id, f.term,
              (SELECT COUNT(*) FROM events e
               WHERE e.date >= date('now', 'localtime')
                 AND instr(nfkc_lower(e.title || ' ' || COALESCE(e.artist, '')), f.term_norm) > 0
              ) AS upcoming_count
       FROM favorites f ORDER BY f.term COLLATE NOCASE`
    )
    .all() as Favorite[];
}

export function addFavorite(term: string): boolean {
  const trimmed = term.trim().slice(0, 100);
  const norm = normalizeTerm(trimmed);
  if (!norm) return false;
  return (
    getDb()
      .prepare("INSERT OR IGNORE INTO favorites (term, term_norm) VALUES (?, ?)")
      .run(trimmed, norm).changes > 0
  );
}

export function deleteFavorite(id: number): void {
  getDb().prepare("DELETE FROM favorites WHERE id = ?").run(id);
}