Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(tags): allow for adding/removing tags to document using api keys (#366)

Corentin Thomassetb8c2bd70e3d0
diff --git a/.changeset/tangy-poems-hug.md b/.changeset/tangy-poems-hug.md
new file mode 100644
index 00000000..0af477cf
--- /dev/null
+++ b/.changeset/tangy-poems-hug.md
@@ -0,0 +1,5 @@
+---
+"@papra/app-server": patch
+---
+
+Allow for adding/removing tags to document using api keys
diff --git a/apps/papra-server/src/modules/app/auth/auth.models.test.ts b/apps/papra-server/src/modules/app/auth/auth.models.test.ts
index 595eb029..ebae37d9 100644
--- a/apps/papra-server/src/modules/app/auth/auth.models.test.ts
+++ b/apps/papra-server/src/modules/app/auth/auth.models.test.ts
@@ -105,7 +105,7 @@ describe('auth models', () => {
});
});
- test('when the auth type is api-key, at least one permission must match', () => {
+ test('when the auth type is api-key, all permissions must match', () => {
expect(isAuthenticationValid({
authType: 'api-key',
apiKey: {
@@ -136,6 +136,22 @@ describe('auth models', () => {
permissions: ['documents:create'],
} as ApiKey,
requiredApiKeyPermissions: ['documents:create', 'documents:read'],
+ })).to.eql(false);
+
+ expect(isAuthenticationValid({
+ authType: 'api-key',
+ apiKey: {
+ permissions: ['documents:create', 'documents:read'],
+ } as ApiKey,
+ requiredApiKeyPermissions: ['documents:create', 'documents:read'],
+ })).to.eql(true);
+
+ expect(isAuthenticationValid({
+ authType: 'api-key',
+ apiKey: {
+ permissions: ['documents:create', 'documents:read', 'documents:update'],
+ } as ApiKey,
+ requiredApiKeyPermissions: ['documents:create', 'documents:read'],
})).to.eql(true);
});
diff --git a/apps/papra-server/src/modules/app/auth/auth.models.ts b/apps/papra-server/src/modules/app/auth/auth.models.ts
index 20713507..f4e2a502 100644
--- a/apps/papra-server/src/modules/app/auth/auth.models.ts
+++ b/apps/papra-server/src/modules/app/auth/auth.models.ts
@@ -71,9 +71,9 @@ export function isAuthenticationValid({
return false;
}
- const atLeastOnePermissionMatches = apiKey.permissions.some(permission => requiredApiKeyPermissions.includes(permission));
+ const allPermissionsMatch = requiredApiKeyPermissions.every(permission => apiKey.permissions.includes(permission));
- return atLeastOnePermissionMatches;
+ return allPermissionsMatch;
}
if (authType === 'session' && session) {
diff --git a/apps/papra-server/src/modules/tags/tags.routes.ts b/apps/papra-server/src/modules/tags/tags.routes.ts
index d204c63e..36fbdf1f 100644
--- a/apps/papra-server/src/modules/tags/tags.routes.ts
+++ b/apps/papra-server/src/modules/tags/tags.routes.ts
@@ -1,5 +1,6 @@
import type { RouteDefinitionContext } from '../app/server.types';
import { z } from 'zod';
+import { API_KEY_PERMISSIONS } from '../api-keys/api-keys.constants';
import { requireAuthentication } from '../app/auth/auth.middleware';
import { getUser } from '../app/auth/auth.models';
import { createDocumentActivityRepository } from '../documents/document-activity/document-activity.repository';
@@ -142,7 +143,7 @@ function setupDeleteTagRoute({ app, db }: RouteDefinitionContext) {
function setupAddTagToDocumentRoute({ app, db }: RouteDefinitionContext) {
app.post(
'/api/organizations/:organizationId/documents/:documentId/tags',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.TAGS.READ] }),
validateParams(z.object({
organizationId: organizationIdSchema,
documentId: documentIdSchema,
@@ -182,7 +183,7 @@ function setupAddTagToDocumentRoute({ app, db }: RouteDefinitionContext) {
function setupRemoveTagFromDocumentRoute({ app, db }: RouteDefinitionContext) {
app.delete(
'/api/organizations/:organizationId/documents/:documentId/tags/:tagId',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.TAGS.READ] }),
validateParams(z.object({
organizationId: organizationIdSchema,
documentId: documentIdSchema,