Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(api-keys): add expiration check to getApiKeyByHash function (#994)

Corentin Thomasset9ee776f1c516
diff --git a/apps/papra-server/src/modules/api-keys/api-keys.repository.test.ts b/apps/papra-server/src/modules/api-keys/api-keys.repository.test.ts
index 7b45c149..0a2d3795 100644
--- a/apps/papra-server/src/modules/api-keys/api-keys.repository.test.ts
+++ b/apps/papra-server/src/modules/api-keys/api-keys.repository.test.ts
@@ -55,4 +55,32 @@ describe('api-keys repository', () => {
]);
});
});
+
+ describe('getApiKeyByHash', () => {
+ test('when retrieving an api key by hash, it only returns the api key if it is not expired', async () => {
+ const { db } = await createInMemoryDatabase({
+ users: [
+ { id: 'user-1', email: '[email protected]' },
+ ],
+ apiKeys: [
+ { id: 'api-key-1', userId: 'user-1', name: 'API Key 1', prefix: 'ak', keyHash: 'hash1', createdAt: new Date('2021-03-01'), updatedAt: new Date('2021-03-02'), expiresAt: new Date('2022-03-01') },
+ { id: 'api-key-2', userId: 'user-1', name: 'API Key 2', prefix: 'ak', keyHash: 'hash2', createdAt: new Date('2021-03-01'), updatedAt: new Date('2021-03-02'), expiresAt: new Date('2020-03-01') },
+ { id: 'api-key-3', userId: 'user-1', name: 'API Key 3', prefix: 'ak', keyHash: 'hash3', createdAt: new Date('2021-03-01'), updatedAt: new Date('2021-03-02'), expiresAt: null },
+ ],
+ });
+
+ const now = new Date('2021-06-01');
+
+ const repository = createApiKeysRepository({ db });
+
+ const { apiKey } = await repository.getApiKeyByHash({ keyHash: 'hash1', now });
+ expect(apiKey?.id).to.eql('api-key-1');
+
+ const { apiKey: expiredApiKey } = await repository.getApiKeyByHash({ keyHash: 'hash2', now });
+ expect(expiredApiKey).to.eql(undefined);
+
+ const { apiKey: apiKeyWithoutExpiry } = await repository.getApiKeyByHash({ keyHash: 'hash3', now });
+ expect(apiKeyWithoutExpiry?.id).to.eql('api-key-3');
+ });
+ });
});
diff --git a/apps/papra-server/src/modules/api-keys/api-keys.repository.ts b/apps/papra-server/src/modules/api-keys/api-keys.repository.ts
index 53205fc2..26132d17 100644
--- a/apps/papra-server/src/modules/api-keys/api-keys.repository.ts
+++ b/apps/papra-server/src/modules/api-keys/api-keys.repository.ts
@@ -2,7 +2,7 @@ import type { Database } from '../app/database/database.types';
import type { Logger } from '../shared/logger/logger';
import type { ApiKeyPermissions } from './api-keys.types';
import { injectArguments } from '@corentinth/chisels';
-import { and, eq, getTableColumns, inArray } from 'drizzle-orm';
+import { and, eq, getTableColumns, gt, inArray, isNull, or } from 'drizzle-orm';
import { omit, pick } from 'lodash-es';
import { organizationMembersTable, organizationsTable } from '../organizations/organizations.table';
import { createError } from '../shared/errors/errors';
@@ -148,12 +148,18 @@ async function deleteUserApiKey({ apiKeyId, userId, db }: { apiKeyId: string; us
);
}
-async function getApiKeyByHash({ keyHash, db }: { keyHash: string; db: Database }) {
+async function getApiKeyByHash({ keyHash, db, now = new Date() }: { keyHash: string; db: Database; now?: Date }) {
const [apiKey] = await db
.select()
.from(apiKeysTable)
.where(
- eq(apiKeysTable.keyHash, keyHash),
+ and(
+ eq(apiKeysTable.keyHash, keyHash),
+ or(
+ gt(apiKeysTable.expiresAt, now),
+ isNull(apiKeysTable.expiresAt),
+ ),
+ ),
);
return { apiKey };