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.

fix(api,test): preserve bearer sessions on auth routes

Tinc398a6444e1c
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index ba29a9624..ac1e9e888 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -384,17 +384,25 @@ export function createApp() {
},
);
- api.on(["POST", "GET", "PUT", "DELETE"], "/auth/*", (c) => {
+ api.on(["POST", "GET", "PUT", "DELETE"], "/auth/*", async (c) => {
const authHeader = c.req.header("Authorization");
+ const apiKeyHeader = c.req.header("x-api-key");
+ const bearerMatch = authHeader?.match(/^Bearer\s+(\S+)$/i);
+
+ if (bearerMatch && !apiKeyHeader) {
+ const session = await auth.api.getSession({
+ headers: c.req.raw.headers,
+ });
+
+ // Preserve Better Auth bearer session tokens on auth routes.
+ if (session?.session && session.user) {
+ return auth.handler(c.req.raw);
+ }
- if (authHeader?.startsWith("Bearer ")) {
- const apiKey = authHeader.substring(7).replace(/\s+/g, "").trim();
const headers = new Headers(c.req.raw.headers);
// Better Auth API key plugin validates from x-api-key by default.
- if (!headers.get("x-api-key")) {
- headers.set("x-api-key", apiKey);
- }
+ headers.set("x-api-key", bearerMatch[1]);
return auth.handler(
new Request(c.req.raw, {
diff --git a/tests/api-integration/device-authorization.test.ts b/tests/api-integration/device-authorization.test.ts
index 978016d1f..8077d2a5e 100644
--- a/tests/api-integration/device-authorization.test.ts
+++ b/tests/api-integration/device-authorization.test.ts
@@ -236,6 +236,15 @@ describe("API integration: device authorization (RFC 8628)", () => {
expect(accessToken).toBeTruthy();
+ const organizationsRes = await app.request("/api/auth/organization/list", {
+ headers: {
+ Authorization: `Bearer ${accessToken}`,
+ },
+ });
+ expect(organizationsRes.status).toBe(200);
+ const organizations = (await organizationsRes.json()) as unknown[];
+ expect(Array.isArray(organizations)).toBe(true);
+
const projectsRes = await app.request(
`/api/project?workspaceId=${encodeURIComponent(workspaceId)}`,
{
@@ -279,6 +288,7 @@ describe("API integration: device authorization (RFC 8628)", () => {
);
expect(res.status).toBe(200);
+
const rows = await db
.select()
.from(schema.apikeyTable)
@@ -286,6 +296,34 @@ describe("API integration: device authorization (RFC 8628)", () => {
expect(rows.length).toBe(1);
});
+ it("accepts a created API key Bearer on auth routes", async () => {
+ const member = await createWorkspaceMember();
+ const rawKey =
+ randomUUID().replace(/-/g, "") + randomUUID().replace(/-/g, "");
+ const hashed = await hashApiKeyForTest(rawKey);
+ const now = new Date();
+
+ await db.insert(schema.apikeyTable).values({
+ referenceId: member.user.id,
+ userId: member.user.id,
+ key: hashed,
+ name: "auth route api key",
+ start: rawKey.slice(0, 12),
+ createdAt: now,
+ updatedAt: now,
+ });
+
+ const { app } = createApp();
+
+ const authRouteRes = await app.request("/api/auth/organization/list", {
+ headers: {
+ Authorization: `Bearer ${rawKey}`,
+ },
+ });
+
+ expect(authRouteRes.status).toBe(200);
+ });
+
it("rejects an invalid Bearer token even when a valid session cookie is present", async () => {
const email = `device-bearer-${randomUUID()}@example.com`;
const password = "device-flow-password-12345";