feat(core): users, sessions, invites, app tokens
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
d392a65aa7
commit
e1faa7433b
@@ -0,0 +1,187 @@
|
||||
import type { TokenScope } from "@sttracker/db/schema";
|
||||
import type { Db } from "@sttracker/db/db";
|
||||
import { newId, nowIso } from "@sttracker/db/ids";
|
||||
import { fromJson, toJson } from "@sttracker/db/json";
|
||||
import { AuthError } from "./errors.ts";
|
||||
|
||||
export type User = { id: string; username: string; displayName: string; role: "admin" | "member" };
|
||||
|
||||
const USERNAME_RE = /^[a-z0-9_]{2,32}$/;
|
||||
const DAY_MS = 86_400_000;
|
||||
const SESSION_DAYS = 30;
|
||||
|
||||
function sha256Hex(s: string): string {
|
||||
return new Bun.CryptoHasher("sha256").update(s).digest("hex");
|
||||
}
|
||||
|
||||
function randomToken(bytes = 32): string {
|
||||
return crypto.getRandomValues(new Uint8Array(bytes)).toBase64({ alphabet: "base64url", omitPadding: true });
|
||||
}
|
||||
|
||||
function toUser(r: { id: string; username: string; display_name: string; role: "admin" | "member" }): User {
|
||||
return { id: r.id, username: r.username, displayName: r.display_name, role: r.role };
|
||||
}
|
||||
|
||||
export async function registerUser(
|
||||
db: Db,
|
||||
input: { username: string; password: string; displayName?: string; inviteCode?: string },
|
||||
): Promise<User> {
|
||||
const username = input.username.toLowerCase();
|
||||
if (!USERNAME_RE.test(username)) throw new AuthError("invalid_username");
|
||||
const passwordHash = await Bun.password.hash(input.password, { algorithm: "argon2id" });
|
||||
|
||||
return db.k.transaction().execute(async (tx) => {
|
||||
const existing = await tx.selectFrom("users").select("id").where("username", "=", username).executeTakeFirst();
|
||||
if (existing) throw new AuthError("username_taken");
|
||||
|
||||
const anyUser = await tx.selectFrom("users").select("id").limit(1).executeTakeFirst();
|
||||
const id = newId();
|
||||
const now = nowIso();
|
||||
|
||||
if (anyUser && !input.inviteCode) throw new AuthError("invite_required");
|
||||
|
||||
const role = anyUser ? "member" : "admin";
|
||||
const display_name = input.displayName ?? username;
|
||||
await tx
|
||||
.insertInto("users")
|
||||
.values({
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash: passwordHash,
|
||||
role,
|
||||
preferences: toJson({}),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})
|
||||
.execute();
|
||||
|
||||
// used_by references users.id, so claim after the insert; the tx rolls back on failure.
|
||||
if (anyUser) {
|
||||
const claimed = await tx
|
||||
.updateTable("invites")
|
||||
.set({ used_by: id })
|
||||
.where("code_hash", "=", sha256Hex(input.inviteCode!))
|
||||
.where("used_by", "is", null)
|
||||
.where("expires_at", ">", now)
|
||||
.executeTakeFirst();
|
||||
if (Number(claimed.numUpdatedRows) !== 1) throw new AuthError("invite_required");
|
||||
}
|
||||
return { id, username, displayName: display_name, role };
|
||||
});
|
||||
}
|
||||
|
||||
export async function login(
|
||||
db: Db,
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<{ user: User; sessionToken: string }> {
|
||||
const row = await db.k
|
||||
.selectFrom("users")
|
||||
.selectAll()
|
||||
.where("username", "=", username.toLowerCase())
|
||||
.executeTakeFirst();
|
||||
if (!row || !(await Bun.password.verify(password, row.password_hash))) {
|
||||
throw new AuthError("invalid_credentials");
|
||||
}
|
||||
const sessionToken = randomToken();
|
||||
await db.k
|
||||
.insertInto("sessions")
|
||||
.values({
|
||||
id: newId(),
|
||||
user_id: row.id,
|
||||
token_hash: sha256Hex(sessionToken),
|
||||
expires_at: new Date(Date.now() + SESSION_DAYS * DAY_MS).toISOString(),
|
||||
created_at: nowIso(),
|
||||
})
|
||||
.execute();
|
||||
return { user: toUser(row), sessionToken };
|
||||
}
|
||||
|
||||
export async function getSessionUser(db: Db, token: string): Promise<User | null> {
|
||||
const row = await db.k
|
||||
.selectFrom("sessions")
|
||||
.innerJoin("users", "users.id", "sessions.user_id")
|
||||
.select(["users.id", "users.username", "users.display_name", "users.role"])
|
||||
.where("sessions.token_hash", "=", sha256Hex(token))
|
||||
.where("sessions.expires_at", ">", nowIso())
|
||||
.executeTakeFirst();
|
||||
return row ? toUser(row) : null;
|
||||
}
|
||||
|
||||
export async function logout(db: Db, token: string): Promise<void> {
|
||||
await db.k.deleteFrom("sessions").where("token_hash", "=", sha256Hex(token)).execute();
|
||||
}
|
||||
|
||||
/** Admin-only. Returns the invite code (shown once; only its hash is stored). */
|
||||
export async function createInvite(db: Db, adminId: string, ttlDays = 7): Promise<string> {
|
||||
const admin = await db.k.selectFrom("users").select("role").where("id", "=", adminId).executeTakeFirst();
|
||||
if (admin?.role !== "admin") throw new AuthError("forbidden");
|
||||
const code = randomToken(16);
|
||||
await db.k
|
||||
.insertInto("invites")
|
||||
.values({
|
||||
id: newId(),
|
||||
code_hash: sha256Hex(code),
|
||||
created_by: adminId,
|
||||
used_by: null,
|
||||
expires_at: new Date(Date.now() + ttlDays * DAY_MS).toISOString(),
|
||||
created_at: nowIso(),
|
||||
})
|
||||
.execute();
|
||||
return code;
|
||||
}
|
||||
|
||||
export async function createAppToken(
|
||||
db: Db,
|
||||
userId: string,
|
||||
name: string,
|
||||
scopes: TokenScope[],
|
||||
): Promise<{ id: string; token: string }> {
|
||||
const id = newId();
|
||||
const token = `stt_${randomToken()}`;
|
||||
await db.k
|
||||
.insertInto("app_tokens")
|
||||
.values({
|
||||
id,
|
||||
user_id: userId,
|
||||
name,
|
||||
token_hash: sha256Hex(token),
|
||||
scopes: toJson(scopes),
|
||||
last_used_at: null,
|
||||
revoked_at: null,
|
||||
created_at: nowIso(),
|
||||
})
|
||||
.execute();
|
||||
return { id, token };
|
||||
}
|
||||
|
||||
export async function verifyAppToken(db: Db, token: string, scope: TokenScope): Promise<User | null> {
|
||||
const row = await db.k
|
||||
.selectFrom("app_tokens")
|
||||
.innerJoin("users", "users.id", "app_tokens.user_id")
|
||||
.select([
|
||||
"app_tokens.id as token_id",
|
||||
"app_tokens.scopes",
|
||||
"users.id",
|
||||
"users.username",
|
||||
"users.display_name",
|
||||
"users.role",
|
||||
])
|
||||
.where("app_tokens.token_hash", "=", sha256Hex(token))
|
||||
.where("app_tokens.revoked_at", "is", null)
|
||||
.executeTakeFirst();
|
||||
if (!row || !fromJson<string[]>(row.scopes, []).includes(scope)) return null;
|
||||
await db.k.updateTable("app_tokens").set({ last_used_at: nowIso() }).where("id", "=", row.token_id).execute();
|
||||
return toUser(row);
|
||||
}
|
||||
|
||||
export async function revokeAppToken(db: Db, userId: string, id: string): Promise<void> {
|
||||
await db.k
|
||||
.updateTable("app_tokens")
|
||||
.set({ revoked_at: nowIso() })
|
||||
.where("id", "=", id)
|
||||
.where("user_id", "=", userId)
|
||||
.where("revoked_at", "is", null)
|
||||
.execute();
|
||||
}
|
||||
Reference in New Issue
Block a user