diff --git a/packages/api/src/lib/errors.ts b/packages/api/src/lib/errors.ts index df38cce..596f664 100644 --- a/packages/api/src/lib/errors.ts +++ b/packages/api/src/lib/errors.ts @@ -11,3 +11,7 @@ export function badRequest(c: Context, msg = "Bad request") { export function unauthorized(c: Context, msg = "Unauthorized") { return c.json({ error: msg, status: 401 }, 401) } + +export function serverError(c: Context, msg = "Internal server error") { + return c.json({ error: msg, status: 500 }, 500) +} diff --git a/packages/api/src/middleware/auth.ts b/packages/api/src/middleware/auth.ts index dde32d9..c30fb97 100644 --- a/packages/api/src/middleware/auth.ts +++ b/packages/api/src/middleware/auth.ts @@ -1,4 +1,7 @@ import type { MiddlewareHandler } from "hono" +import { serverError, unauthorized } from "../lib/errors" + +const BEARER_PREFIX = "Bearer " /** * Simple token-based auth middleware. @@ -7,23 +10,25 @@ import type { MiddlewareHandler } from "hono" * GET, POST → public (no token required) * PUT, DELETE, PATCH → require Bearer token * - * BUG: The allow-list check uses `'post'` (lowercase) instead of `'POST'`. - * HTTP methods are always uppercase per RFC 7231, so POST is never matched - * as a public method — POST requests incorrectly require a token. - * - * Fix: change `'post'` to `'POST'` in the public methods array. + * Fails closed: if API_TOKEN is not configured, privileged methods are refused + * rather than falling back to a default token (ISM-1685). */ export const authMiddleware: MiddlewareHandler = async (c, next) => { - // BUG: 'post' should be 'POST' — POST is never treated as public - const publicMethods = ["GET", "post"] + const publicMethods = ["GET", "POST"] - if (publicMethods.includes(c.req.method)) { + if (publicMethods.includes(c.req.method.toUpperCase())) { return next() } - const token = c.req.header("Authorization")?.replace("Bearer ", "") - if (!token || token !== (process.env.API_TOKEN ?? "test-token")) { - return c.json({ error: "Unauthorized", status: 401 }, 401) + const expected = process.env.API_TOKEN + if (!expected) { + return serverError(c, "Server misconfigured") + } + + const header = c.req.header("Authorization") ?? "" + const token = header.startsWith(BEARER_PREFIX) ? header.slice(BEARER_PREFIX.length) : null + if (!token || token !== expected) { + return unauthorized(c) } return next() diff --git a/packages/api/src/routes/users.ts b/packages/api/src/routes/users.ts index 53e605a..8056ce3 100644 --- a/packages/api/src/routes/users.ts +++ b/packages/api/src/routes/users.ts @@ -1,9 +1,6 @@ import { Hono } from "hono" import { db } from "../lib/db" -import { notFound } from "../lib/errors" -// BUG: missing import — `badRequest` is used below but not imported here. -// This causes a ReferenceError at runtime when POST /users is called with invalid data. -// Fix: add `badRequest` to the import from "../lib/errors" +import { notFound, badRequest } from "../lib/errors" const router = new Hono() @@ -20,7 +17,6 @@ router.get("/:id", (c) => { router.post("/", async (c) => { const body = await c.req.json().catch(() => null) if (!body || !body.username || !body.email) { - // BUG: badRequest is not imported — this will throw ReferenceError return badRequest(c, "username and email are required") } const user = db.users.create({ username: body.username, email: body.email }) diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index a2a1377..b6f7974 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -1,14 +1,10 @@ /** * Shared types used by both the API and any consumers. - * - * BUG: The field is named `userName` here but the API routes reference `username` - * (lowercase n). This causes a type error in routes/users.ts and a runtime - * mismatch when serialising responses. */ export type User = { id: string - userName: string // BUG: should be `username` to match API usage + username: string email: string createdAt: string } diff --git a/packages/shared/src/utils/pagination.ts b/packages/shared/src/utils/pagination.ts index 12f8062..bc5f163 100644 --- a/packages/shared/src/utils/pagination.ts +++ b/packages/shared/src/utils/pagination.ts @@ -6,10 +6,19 @@ import type { PaginatedResponse } from "../types" * @param items Full array of items * @param page 1-indexed page number * @param size Number of items per page - * - * TODO: implement this function — it is currently a stub. - * The test in packages/shared/test/pagination.test.ts exercises the full contract. */ export function paginate(items: T[], page: number, size: number): PaginatedResponse { - throw new Error("not implemented") + const pageSize = Math.max(1, Math.trunc(size)) + const currentPage = Math.max(1, Math.trunc(page)) + const total = items.length + const totalPages = Math.ceil(total / pageSize) + const start = (currentPage - 1) * pageSize + + return { + data: items.slice(start, start + pageSize), + page: currentPage, + pageSize, + total, + totalPages, + } } diff --git a/tsconfig.json b/tsconfig.json index 53de6fd..b4bf326 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "moduleResolution": "bundler", "strict": true, "skipLibCheck": true, + "types": ["bun-types"], "paths": { "@e2e/shared": ["./packages/shared/src/index.ts"] }