Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(api): enhance workspace access validation with API key support

Andrej Acevskic18391a81f84
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 52d92bdf2..bfa27e7e7 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -21,11 +21,18 @@ import timeEntry from "./time-entry";
import { migrateSessionColumn } from "./utils/migrate-session-column";
import { migrateWorkspaceUserEmail } from "./utils/migrate-workspace-user-email";
+type ApiKey = {
+ id: string;
+ userId: string;
+ enabled: boolean;
+};
+
const app = new Hono<{
Variables: {
user: User | null;
session: Session | null;
userId: string;
+ apiKey?: ApiKey;
};
}>();
@@ -56,6 +63,7 @@ const api = new Hono<{
user: User | null;
session: Session | null;
userId: string;
+ apiKey?: ApiKey;
};
}>();
@@ -126,6 +134,7 @@ api.use("*", async (c, next) => {
c.set("userId", result.key.userId);
c.set("user", null);
c.set("session", null);
+ c.set("apiKey", result.key);
return next();
}
diff --git a/apps/api/src/utils/validate-workspace-access.ts b/apps/api/src/utils/validate-workspace-access.ts
index 8b0ef9325..49abee1e4 100644
--- a/apps/api/src/utils/validate-workspace-access.ts
+++ b/apps/api/src/utils/validate-workspace-access.ts
@@ -2,15 +2,10 @@ import { and, eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db, { schema } from "../database";
-/**
- * Validates that a user has access to a workspace
- * @param userId - The user's ID
- * @param workspaceId - The workspace ID to check
- * @throws HTTPException with 403 if user doesn't have access
- */
export async function validateWorkspaceAccess(
userId: string,
workspaceId: string,
+ apiKeyId?: string,
): Promise<void> {
const membership = await db
.select()
@@ -28,4 +23,24 @@ export async function validateWorkspaceAccess(
message: "You don't have access to this workspace",
});
}
+
+ if (apiKeyId) {
+ const apiKey = await db
+ .select()
+ .from(schema.apikeyTable)
+ .where(
+ and(
+ eq(schema.apikeyTable.id, apiKeyId),
+ eq(schema.apikeyTable.userId, userId),
+ eq(schema.apikeyTable.enabled, true),
+ ),
+ )
+ .limit(1);
+
+ if (apiKey.length === 0) {
+ throw new HTTPException(403, {
+ message: "Invalid API key for this workspace",
+ });
+ }
+ }
}
diff --git a/apps/api/src/utils/workspace-access-middleware.ts b/apps/api/src/utils/workspace-access-middleware.ts
index d40c193c0..3d9cdaeba 100644
--- a/apps/api/src/utils/workspace-access-middleware.ts
+++ b/apps/api/src/utils/workspace-access-middleware.ts
@@ -56,7 +56,11 @@ export function workspaceAccessMiddleware(
});
}
- await validateWorkspaceAccess(userId, workspaceId);
+ // Get API key ID if present (for API key authentication)
+ const apiKey = c.get("apiKey");
+ const apiKeyId = apiKey?.id;
+
+ await validateWorkspaceAccess(userId, workspaceId, apiKeyId);
c.set("workspaceId", workspaceId);
diff --git a/apps/docs/next-env.d.ts b/apps/docs/next-env.d.ts
index 9edff1c7c..c4b7818fb 100644
--- a/apps/docs/next-env.d.ts
+++ b/apps/docs/next-env.d.ts
@@ -1,6 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
-import "./.next/types/routes.d.ts";
+import "./.next/dev/types/routes.d.ts";
// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.