The minimalistic document archiving platform.
feat(logging): add logging context for API key and session authentication (#625)
Corentin Thomassetee9eff491428
diff --git a/.changeset/tiny-rockets-jog.md b/.changeset/tiny-rockets-jog.mdnew file mode 100644index 00000000..c31886e4--- /dev/null+++ b/.changeset/tiny-rockets-jog.md@@ -0,0 +1,5 @@+---+"@papra/docker": patch+---++Improved server authentication loggingdiff --git a/apps/papra-server/src/modules/api-keys/api-keys.middlewares.ts b/apps/papra-server/src/modules/api-keys/api-keys.middlewares.tsindex 9de91a2c..886765b0 100644--- a/apps/papra-server/src/modules/api-keys/api-keys.middlewares.ts+++ b/apps/papra-server/src/modules/api-keys/api-keys.middlewares.ts@@ -3,6 +3,7 @@ import type { Context } from '../app/server.types';import { createMiddleware } from 'hono/factory';import { createUnauthorizedError } from '../app/auth/auth.errors';import { getAuthorizationHeader } from '../shared/headers/headers.models';+import { addLogContext } from '../shared/logger/logger';import { isNil } from '../shared/utils';import { looksLikeAnApiKey } from './api-keys.models';import { createApiKeysRepository } from './api-keys.repository';@@ -39,9 +40,14 @@ export function createApiKeyMiddleware({ db }: { db: Database }) {const { apiKey } = await getApiKey({ token, apiKeyRepository });if (apiKey) {+ const userId = apiKey.userId;+ const authType = 'api-key';+context.set('apiKey', apiKey);- context.set('userId', apiKey.userId);- context.set('authType', 'api-key');+ context.set('userId', userId);+ context.set('authType', authType);++ addLogContext({ userId, authType, apiKeyId: apiKey.id });}await next();diff --git a/apps/papra-server/src/modules/app/auth/auth.routes.ts b/apps/papra-server/src/modules/app/auth/auth.routes.tsindex 216d3a0d..4fc3c8c7 100644--- a/apps/papra-server/src/modules/app/auth/auth.routes.ts+++ b/apps/papra-server/src/modules/app/auth/auth.routes.ts@@ -1,6 +1,7 @@import type { Context, RouteDefinitionContext } from '../server.types';import type { Session } from './auth.types';import { get } from 'lodash-es';+import { addLogContext } from '../../shared/logger/logger';import { isDefined, isString } from '../../shared/utils';export function registerAuthRoutes({ app, auth, config }: RouteDefinitionContext) {@@ -11,12 +12,18 @@ export function registerAuthRoutes({ app, auth, config }: RouteDefinitionContext);app.use('*', async (context: Context, next) => {- const session = await auth.api.getSession({ headers: context.req.raw.headers });+ const sessionData = await auth.api.getSession({ headers: context.req.raw.headers });- if (session) {- context.set('userId', session.user.id);- context.set('session', session.session);- context.set('authType', 'session');+ if (sessionData) {+ const { user, session } = sessionData;+ const userId = user.id;+ const authType = 'session';++ context.set('userId', userId);+ context.set('session', session);+ context.set('authType', authType);++ addLogContext({ userId, authType, sessionId: session.id });}return next();