Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"format:check": "prettier --check ."
},
"dependencies": {
"@haverstack/adapter-sqlite": "^0.4.0",
"@haverstack/core": "^0.4.0",
"@haverstack/adapter-sqlite": "^0.5.0",
"@haverstack/core": "^0.5.0",
"@hono/node-server": "^1.13.7",
"hono": "^4.6.0",
"pino": "^9.5.0",
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export function authMiddleware(ownerToken: string, ctx: StackContext): Middlewar
if (header?.startsWith('Bearer ')) {
const token = header.slice(7);
if (safeCompare(token, ownerToken)) {
if (!ownerEntityId) {
return c.json({ error: 'Server misconfiguration: owner entity not set' }, 500);
}
c.set('auth', { entityId: ownerEntityId });
} else {
const result = await ctx.adapter.lookupToken(token);
Expand All @@ -39,12 +36,11 @@ export function requireAuth(): MiddlewareHandler<AppEnv> {
};
}

export function requireOwner(ownerEntityId: string | null): MiddlewareHandler<AppEnv> {
export function requireOwner(ownerEntityId: string): MiddlewareHandler<AppEnv> {
return async (c, next) => {
const auth = c.get('auth');
if (!auth) return c.json({ error: 'Unauthorized' }, 401);
if (!ownerEntityId || auth.entityId !== ownerEntityId)
return c.json({ error: 'Forbidden' }, 403);
if (auth.entityId !== ownerEntityId) return c.json({ error: 'Forbidden' }, 403);
await next();
};
}
5 changes: 2 additions & 3 deletions src/routes/records.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hono } from 'hono';
import type { AppEnv } from '../types.js';
import type { StackContext } from '../stack.js';
import { requireAuth, requireOwner } from '../middleware/auth.js';
import { requireAuth } from '../middleware/auth.js';
import { parseDate, serializeRecord, serializeVersion } from '../lib/serialize.js';
import { SYSTEM_TYPES } from '@haverstack/core';
import type { StackQuery, RecordFilter, Association, Permission, TypeId } from '@haverstack/core';
Expand Down Expand Up @@ -153,7 +153,6 @@ function parseQueryParams(url: URL): StackQuery {
export function recordRoutes(ctx: StackContext): Hono<AppEnv> {
const app = new Hono<AppEnv>();
const { stack } = ctx;
const { ownerEntityId } = stack;

// POST /records/query — full query with content-field filters
// Registered before /:id patterns to avoid param capture on the literal "query" segment.
Expand Down Expand Up @@ -278,7 +277,7 @@ export function recordRoutes(ctx: StackContext): Hono<AppEnv> {
return c.json({ permissions: record.permissions ?? [] });
});

app.put('/:id/permissions', requireOwner(ownerEntityId), async (c) => {
app.put('/:id/permissions', requireAuth(), async (c) => {
const id = c.req.param('id');
const auth = c.get('auth')!;
const body = await c.req.json<{ permissions: Permission[] }>();
Expand Down
Loading