Skip to content

Commit

Permalink
fix: api keys authentication does not work #1511
Browse files Browse the repository at this point in the history
  • Loading branch information
Meierschlumpf committed Nov 20, 2024
1 parent ed74604 commit 4ffb40f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ApiKeysManagement = ({ apiKeys }: ApiKeysManagementProps) => {
const { mutate, isPending } = clientApi.apiKeys.create.useMutation({
async onSuccess(data) {
openModal({
apiKey: data.randomToken,
apiKey: data.apiKey,
});
await revalidatePathActionAsync("/manage/tools/api");
},
Expand Down
23 changes: 19 additions & 4 deletions apps/nextjs/src/app/api/[...trpc]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createOpenApiFetchHandler } from "trpc-swagger/build/index.mjs";

import { appRouter, createTRPCContext } from "@homarr/api";
import { hashPasswordAsync } from "@homarr/auth";
import type { Session } from "@homarr/auth";
import { createSessionAsync } from "@homarr/auth/server";
import { db, eq } from "@homarr/db";
Expand Down Expand Up @@ -28,12 +29,19 @@ const getSessionOrDefaultFromHeadersAsync = async (apiKeyHeaderValue: string | n
return null;
}

const [apiKeyId, apiKey] = apiKeyHeaderValue.split(".");

if (!apiKeyId || !apiKey) {
logger.warn("An attempt to authenticate over API has failed due to invalid API key format");
return null;
}

const apiKeyFromDb = await db.query.apiKeys.findFirst({
where: eq(apiKeys.apiKey, apiKeyHeaderValue),
where: eq(apiKeys.id, apiKeyId),
columns: {
id: true,
apiKey: false,
salt: false,
apiKey: true,
salt: true,
},
with: {
user: {
Expand All @@ -47,7 +55,14 @@ const getSessionOrDefaultFromHeadersAsync = async (apiKeyHeaderValue: string | n
},
});

if (apiKeyFromDb === undefined) {
if (!apiKeyFromDb) {
logger.warn("An attempt to authenticate over API has failed");
return null;
}

const hashedApiKey = await hashPasswordAsync(apiKey, apiKeyFromDb.salt);

if (apiKeyFromDb.apiKey !== hashedApiKey) {
logger.warn("An attempt to authenticate over API has failed");
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/router/apiKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ export const apiKeysRouter = createTRPCRouter({
const salt = await createSaltAsync();
const randomToken = generateSecureRandomToken(64);
const hashedRandomToken = await hashPasswordAsync(randomToken, salt);
const id = createId();
await db.insert(apiKeys).values({
id: createId(),
id,
apiKey: hashedRandomToken,
salt,
userId: ctx.session.user.id,
});
return {
randomToken,
apiKey: `${id}.${randomToken}`,
};
}),
});

0 comments on commit 4ffb40f

Please # to comment.