Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(api-keys): add custom-properties permissions (#1135)

Corentin Thomassetd73933a8ee14
diff --git a/.changeset/late-mugs-flash.md b/.changeset/late-mugs-flash.md
new file mode 100644
index 00000000..d554ce25
--- /dev/null
+++ b/.changeset/late-mugs-flash.md
@@ -0,0 +1,5 @@
+---
+"@papra/app": patch
+---
+
+Added api-key permissions for managing custom properties
diff --git a/apps/papra-client/src/locales/en.dictionary.ts b/apps/papra-client/src/locales/en.dictionary.ts
index 71d3e699..8787f3d3 100644
--- a/apps/papra-client/src/locales/en.dictionary.ts
+++ b/apps/papra-client/src/locales/en.dictionary.ts
@@ -823,6 +823,11 @@ export const translations = {
'api-keys.permissions.tags.tags:read': 'Read tags',
'api-keys.permissions.tags.tags:update': 'Update tags',
'api-keys.permissions.tags.tags:delete': 'Delete tags',
+ 'api-keys.permissions.custom-properties.title': 'Custom properties',
+ 'api-keys.permissions.custom-properties.custom-properties:create': 'Create custom properties',
+ 'api-keys.permissions.custom-properties.custom-properties:read': 'Read custom properties',
+ 'api-keys.permissions.custom-properties.custom-properties:update': 'Update custom properties',
+ 'api-keys.permissions.custom-properties.custom-properties:delete': 'Delete custom properties',
'api-keys.create.title': 'Create API key',
'api-keys.create.description': 'Create a new API key to access the Papra API.',
'api-keys.create.success': 'The API key has been created successfully.',
diff --git a/apps/papra-client/src/modules/api-keys/api-keys.constants.ts b/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
index a64961e3..7243e879 100644
--- a/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
+++ b/apps/papra-client/src/modules/api-keys/api-keys.constants.ts
@@ -32,6 +32,15 @@ export const API_KEY_PERMISSIONS = [
'tags:delete',
],
},
+ {
+ section: 'custom-properties',
+ permissions: [
+ 'custom-properties:create',
+ 'custom-properties:read',
+ 'custom-properties:update',
+ 'custom-properties:delete',
+ ],
+ },
] as const;
export const API_KEY_PERMISSIONS_LIST = API_KEY_PERMISSIONS.flatMap(permission => permission.permissions);
diff --git a/apps/papra-client/src/modules/i18n/locales.test.ts b/apps/papra-client/src/modules/i18n/locales.test.ts
index 456d2e93..4061c39c 100644
--- a/apps/papra-client/src/modules/i18n/locales.test.ts
+++ b/apps/papra-client/src/modules/i18n/locales.test.ts
@@ -54,7 +54,7 @@ describe('locales', () => {
/^auth\.register\.providers\.[a-z0-9:]+$/, // auth.register.providers.google
/^webhooks\.events\.[a-z0-9]+\.[a-z0-9:]+.description$/, // webhooks.events.documents.document:created.description
/^webhooks\.events\.[a-z0-9]+\.title$/, // webhooks.events.documents.title
- /^api-keys\.permissions\.[a-z0-9:]+\.[a-z0-9:]+$/, // api-keys.permissions.documents.documents:delete
+ /^api-keys\.permissions\.[a-z0-9:-]+\.[a-z0-9:-]+$/, // api-keys.permissions.documents.documents:delete
/^organizations\.members\.roles\.[a-z0-9]+$/, // organizations.members.roles.admin
/^activity\.document\.[a-z0-9:]+$/, // activity.document.created
/^organizations\.invitations\.status\.[a-z0-9:]+$/, // organizations.invitations.status.pending
diff --git a/apps/papra-server/src/modules/api-keys/api-keys.constants.ts b/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
index 824a5508..720943e1 100644
--- a/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
+++ b/apps/papra-server/src/modules/api-keys/api-keys.constants.ts
@@ -26,6 +26,12 @@ export const API_KEY_PERMISSIONS = {
UPDATE: 'tags:update',
DELETE: 'tags:delete',
},
+ CUSTOM_PROPERTIES: {
+ CREATE: 'custom-properties:create',
+ READ: 'custom-properties:read',
+ UPDATE: 'custom-properties:update',
+ DELETE: 'custom-properties:delete',
+ },
} as const;
export const API_KEY_PERMISSIONS_VALUES = Object.values(API_KEY_PERMISSIONS).flatMap(permissions => Object.values(permissions));
diff --git a/apps/papra-server/src/modules/api-keys/e2e/create-custom-property-with-api-key.e2e.test.ts b/apps/papra-server/src/modules/api-keys/e2e/create-custom-property-with-api-key.e2e.test.ts
new file mode 100644
index 00000000..58727b65
--- /dev/null
+++ b/apps/papra-server/src/modules/api-keys/e2e/create-custom-property-with-api-key.e2e.test.ts
@@ -0,0 +1,114 @@
+import type { CustomPropertyDefinition } from '../../custom-properties/custom-properties.types';
+import { describe, expect, test } from 'vitest';
+import { createInMemoryDatabase } from '../../app/database/database.test-utils';
+import { createServer } from '../../app/server';
+import { createTestServerDependencies } from '../../app/server.test-utils';
+import { overrideConfig } from '../../config/config.test-utils';
+import { ORGANIZATION_ROLES } from '../../organizations/organizations.constants';
+
+const USER_ID = 'usr_111111111111111111111111';
+const ORG_ID = 'org_222222222222222222222222';
+
+describe('custom-property api-key e2e', () => {
+ test('one can create a custom property definition using an api key with the custom-properties:create permission', async () => {
+ const { db } = await createInMemoryDatabase({
+ users: [{ id: USER_ID, email: '[email protected]' }],
+ organizations: [{ id: ORG_ID, name: 'Org 1' }],
+ organizationMembers: [{ organizationId: ORG_ID, userId: USER_ID, role: ORGANIZATION_ROLES.OWNER }],
+ });
+
+ const { app } = createServer(createTestServerDependencies({ db, config: overrideConfig({ env: 'test' }) }));
+
+ const createApiKeyResponse = await app.request(
+ '/api/api-keys',
+ {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ name: 'Test API Key',
+ permissions: ['custom-properties:create'],
+ }),
+ },
+ { loggedInUserId: USER_ID },
+ );
+
+ expect(createApiKeyResponse.status).toBe(200);
+ const { token } = await createApiKeyResponse.json() as { token: string };
+
+ const createResponse = await app.request(`/api/organizations/${ORG_ID}/custom-properties`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`,
+ },
+ body: JSON.stringify({ name: 'Invoice Number', type: 'text' }),
+ });
+
+ expect(createResponse.status).toBe(200);
+ const { propertyDefinition } = await createResponse.json() as { propertyDefinition: CustomPropertyDefinition };
+
+ expect(propertyDefinition).to.include({
+ organizationId: ORG_ID,
+ name: 'Invoice Number',
+ key: 'invoicenumber',
+ type: 'text',
+ });
+
+ // The api key is not authorized to read definitions, only custom-properties:create is granted
+ const listResponse = await app.request(`/api/organizations/${ORG_ID}/custom-properties`, {
+ method: 'GET',
+ headers: { Authorization: `Bearer ${token}` },
+ });
+
+ expect(listResponse.status).toBe(401);
+ expect(await listResponse.json()).to.eql({
+ error: {
+ code: 'auth.unauthorized',
+ message: 'Unauthorized',
+ },
+ });
+ });
+
+ test('without the custom-properties:create permission, creating a custom property definition with an api key should fail', async () => {
+ const { db } = await createInMemoryDatabase({
+ users: [{ id: USER_ID, email: '[email protected]' }],
+ organizations: [{ id: ORG_ID, name: 'Org 1' }],
+ organizationMembers: [{ organizationId: ORG_ID, userId: USER_ID, role: ORGANIZATION_ROLES.OWNER }],
+ });
+
+ const { app } = createServer(createTestServerDependencies({ db, config: overrideConfig({ env: 'test' }) }));
+
+ const createApiKeyResponse = await app.request(
+ '/api/api-keys',
+ {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({
+ name: 'Test API Key',
+ permissions: ['documents:create'], // at least one permission is required
+ }),
+ },
+ { loggedInUserId: USER_ID },
+ );
+
+ expect(createApiKeyResponse.status).toBe(200);
+ const { token } = await createApiKeyResponse.json() as { token: string };
+
+ const createResponse = await app.request(`/api/organizations/${ORG_ID}/custom-properties`, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`,
+ },
+ body: JSON.stringify({ name: 'Invoice Number', type: 'text' }),
+ });
+
+ expect(createResponse.status).toBe(401);
+ expect(await createResponse.json()).to.eql({
+ error: {
+ code: 'auth.unauthorized',
+ message: 'Unauthorized',
+ },
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/custom-properties/custom-properties.routes.ts b/apps/papra-server/src/modules/custom-properties/custom-properties.routes.ts
index 8155b5b3..478b0a54 100644
--- a/apps/papra-server/src/modules/custom-properties/custom-properties.routes.ts
+++ b/apps/papra-server/src/modules/custom-properties/custom-properties.routes.ts
@@ -32,7 +32,7 @@ export function registerCustomPropertiesRoutes(context: RouteDefinitionContext)
function setupCreatePropertyDefinitionRoute({ app, db, config }: RouteDefinitionContext) {
app.post(
'/api/organizations/:organizationId/custom-properties',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.CREATE] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
})),
@@ -64,7 +64,7 @@ function setupCreatePropertyDefinitionRoute({ app, db, config }: RouteDefinition
function setupGetOrganizationPropertyDefinitionsRoute({ app, db }: RouteDefinitionContext) {
app.get(
'/api/organizations/:organizationId/custom-properties',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.READ] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
})),
@@ -87,7 +87,7 @@ function setupGetOrganizationPropertyDefinitionsRoute({ app, db }: RouteDefiniti
function setupGetPropertyDefinitionRoute({ app, db }: RouteDefinitionContext) {
app.get(
'/api/organizations/:organizationId/custom-properties/:propertyDefinitionId',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.READ] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
propertyDefinitionId: customPropertyDefinitionIdSchema,
@@ -115,7 +115,7 @@ function setupGetPropertyDefinitionRoute({ app, db }: RouteDefinitionContext) {
function setupUpdatePropertyDefinitionRoute({ app, db }: RouteDefinitionContext) {
app.put(
'/api/organizations/:organizationId/custom-properties/:propertyDefinitionId',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.UPDATE] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
propertyDefinitionId: customPropertyDefinitionIdSchema,
@@ -147,7 +147,7 @@ function setupUpdatePropertyDefinitionRoute({ app, db }: RouteDefinitionContext)
function setupDeletePropertyDefinitionRoute({ app, db }: RouteDefinitionContext) {
app.delete(
'/api/organizations/:organizationId/custom-properties/:propertyDefinitionId',
- requireAuthentication(),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.DELETE] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
propertyDefinitionId: customPropertyDefinitionIdSchema,
@@ -175,7 +175,7 @@ function setupDeletePropertyDefinitionRoute({ app, db }: RouteDefinitionContext)
function setupSetDocumentCustomPropertyValueRoute({ app, db }: RouteDefinitionContext) {
app.put(
'/api/organizations/:organizationId/documents/:documentId/custom-properties/:propertyDefinitionId',
- requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE] }),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.READ] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
documentId: documentIdSchema,
@@ -221,7 +221,7 @@ function setupSetDocumentCustomPropertyValueRoute({ app, db }: RouteDefinitionCo
function setupDeleteDocumentCustomPropertyValueRoute({ app, db }: RouteDefinitionContext) {
app.delete(
'/api/organizations/:organizationId/documents/:documentId/custom-properties/:propertyDefinitionId',
- requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE] }),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.UPDATE, API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.READ] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
documentId: documentIdSchema,
@@ -258,7 +258,7 @@ function setupDeleteDocumentCustomPropertyValueRoute({ app, db }: RouteDefinitio
function setupGetDocumentCustomPropertyValuesRoute({ app, db }: RouteDefinitionContext) {
app.get(
'/api/organizations/:organizationId/documents/:documentId/custom-properties',
- requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.READ] }),
+ requireAuthentication({ apiKeyPermissions: [API_KEY_PERMISSIONS.DOCUMENTS.READ, API_KEY_PERMISSIONS.CUSTOM_PROPERTIES.READ] }),
validateParams(v.strictObject({
organizationId: organizationIdSchema,
documentId: documentIdSchema,