From 5eab8128bd4a173d37b2b1a25c547585e368869b Mon Sep 17 00:00:00 2001 From: Maurice <61554723+mauriceboe@users.noreply.github.com> Date: Fri, 7 Aug 2026 22:18:14 +0200 Subject: [PATCH] refactor(server): fold the ephemeral-token store and the in-app action registry (#1836) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two small leftovers, moved to the domains that own them. ephemeralTokens holds the short-lived tokens the WebSocket handshake and the file-download links use. It already had startTokenCleanup/stopTokenCleanup with an unref-ed handle — the right shape. What was missing is anyone calling stop: index.ts kicked the sweep off with a bare require() after listen() and nothing ever ended it. AuthService owns that lifecycle now, through onModuleInit/onModuleDestroy, which is where it belongs — it is the service that issues the tokens. The store itself stays module state. websocket.ts consumes tokens from outside the container, and a token store is only useful if the issuer and the consumer share one. Same reasoning as the weather cache in #1835. inAppNotificationActions is a 22-line handler registry with exactly one consumer; it moves next to it as notifications/in-app-actions.ts. services/ is down to 9 files, from 44. --- server/src/index.ts | 2 -- server/src/nest/auth/auth.service.ts | 19 ++++++++++++++++--- .../auth/ephemeral-tokens.ts} | 0 server/src/nest/files/files.service.ts | 2 +- .../notifications/in-app-actions.ts} | 0 .../notifications/notifications.service.ts | 2 +- server/src/websocket.ts | 2 +- server/tests/unit/nest/auth.service.test.ts | 4 ++-- .../auth/ephemeral-tokens.test.ts} | 2 +- server/tests/unit/nest/files.service.test.ts | 2 +- .../nest/notifications.inapp-prefs.test.ts | 2 +- .../notifications/in-app-actions.test.ts} | 2 +- server/tests/unit/nest/oidc.service.test.ts | 2 +- .../tests/unit/nest/passkey.service.test.ts | 2 +- server/tests/websocket/connection.test.ts | 2 +- server/tests/websocket/wire-envelope.test.ts | 2 +- 16 files changed, 29 insertions(+), 18 deletions(-) rename server/src/{services/ephemeralTokens.ts => nest/auth/ephemeral-tokens.ts} (100%) rename server/src/{services/inAppNotificationActions.ts => nest/notifications/in-app-actions.ts} (100%) rename server/tests/unit/{services/ephemeralTokens.test.ts => nest/auth/ephemeral-tokens.test.ts} (98%) rename server/tests/unit/{services/inAppNotificationActions.test.ts => nest/notifications/in-app-actions.test.ts} (89%) diff --git a/server/src/index.ts b/server/src/index.ts index bca2c2284..117a75510 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -107,8 +107,6 @@ const onListen = () => { scheduler.startTrekPhotoCacheCleanup(); scheduler.startPlacePhotoCacheCleanup(); scheduler.startAirTrailSync(); - const { startTokenCleanup } = require('./services/ephemeralTokens'); - startTokenCleanup(); import('./websocket').then(({ setupWebSocket }) => { setupWebSocket(server); }); diff --git a/server/src/nest/auth/auth.service.ts b/server/src/nest/auth/auth.service.ts index d2e47795d..c19657119 100644 --- a/server/src/nest/auth/auth.service.ts +++ b/server/src/nest/auth/auth.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'; import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; import path from 'path'; @@ -16,7 +16,7 @@ import { getCountryFromCoords } from '../atlas/atlas-geo'; import { validatePassword } from '../common/passwordPolicy'; import { encryptMfaSecret, decryptMfaSecret } from '../common/crypto/mfaCrypto'; import { decrypt_api_key, maybe_encrypt_api_key, encrypt_api_key } from '../common/crypto/apiKeyCrypto'; -import { createEphemeralToken } from '../../services/ephemeralTokens'; +import { createEphemeralToken, startTokenCleanup, stopTokenCleanup } from './ephemeral-tokens'; // Import from sessionManager directly, NOT the ../../mcp barrel: the barrel pulls // the whole tools fan-out (and via the domain bridges, the Nest services) into // every consumer of this module — a nest→mcp→nest module cycle. @@ -119,7 +119,20 @@ export interface ResetPasswordOutcome { * passkeyService) go through auth.bridge.ts. */ @Injectable() -export class AuthService { +export class AuthService implements OnModuleInit, OnModuleDestroy { + /** + * The ephemeral-token store sweeps itself every minute. index.ts used to kick + * that off with a bare require() after listen(); it belongs to the domain that + * issues the tokens. + */ + onModuleInit(): void { + startTokenCleanup(); + } + + onModuleDestroy(): void { + stopTokenCleanup(); + } + constructor( private readonly db: DatabaseService, private readonly permissions: PermissionsService, diff --git a/server/src/services/ephemeralTokens.ts b/server/src/nest/auth/ephemeral-tokens.ts similarity index 100% rename from server/src/services/ephemeralTokens.ts rename to server/src/nest/auth/ephemeral-tokens.ts diff --git a/server/src/nest/files/files.service.ts b/server/src/nest/files/files.service.ts index 32fd14e88..5f37cb73f 100644 --- a/server/src/nest/files/files.service.ts +++ b/server/src/nest/files/files.service.ts @@ -6,7 +6,7 @@ import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared'; import { RealtimeService } from '../realtime/realtime.service'; import { PermissionsService } from '../permissions/permissions.service'; import { avatarUrl } from '../common/avatarUrl'; -import { consumeEphemeralToken } from '../../services/ephemeralTokens'; +import { consumeEphemeralToken } from '../auth/ephemeral-tokens'; import { verifyJwtAndLoadUser } from '../auth/jwt-verify'; import type { User, TripFile } from '../../types'; import { DatabaseService, type TripAccess } from '../database/database.service'; diff --git a/server/src/services/inAppNotificationActions.ts b/server/src/nest/notifications/in-app-actions.ts similarity index 100% rename from server/src/services/inAppNotificationActions.ts rename to server/src/nest/notifications/in-app-actions.ts diff --git a/server/src/nest/notifications/notifications.service.ts b/server/src/nest/notifications/notifications.service.ts index 99542981c..b9058b707 100644 --- a/server/src/nest/notifications/notifications.service.ts +++ b/server/src/nest/notifications/notifications.service.ts @@ -24,7 +24,7 @@ import { type NotifEventType, } from '../../services/notificationPreferencesService'; import { getChannel, listChannels, type ChannelMessage, type ExternalChannel } from '../../services/notifications/channelRegistry'; -import { getAction } from '../../services/inAppNotificationActions'; +import { getAction } from './in-app-actions'; import { avatarUrl } from '../common/avatarUrl'; import { DatabaseService } from '../database/database.service'; import { RealtimeService } from '../realtime/realtime.service'; diff --git a/server/src/websocket.ts b/server/src/websocket.ts index 0e500b1a4..392942270 100644 --- a/server/src/websocket.ts +++ b/server/src/websocket.ts @@ -1,7 +1,7 @@ import { WebSocketServer, WebSocket } from 'ws'; import { readEnv } from './app-config'; import { db, canAccessTrip } from './db/database'; -import { consumeEphemeralTokenWithMeta } from './services/ephemeralTokens'; +import { consumeEphemeralTokenWithMeta } from './nest/auth/ephemeral-tokens'; import { emitPluginEvent, pluginEventMeta } from './plugin-event-sink'; import { User } from './types'; import http from 'node:http'; diff --git a/server/tests/unit/nest/auth.service.test.ts b/server/tests/unit/nest/auth.service.test.ts index 258a5419c..5f0706410 100644 --- a/server/tests/unit/nest/auth.service.test.ts +++ b/server/tests/unit/nest/auth.service.test.ts @@ -53,7 +53,7 @@ vi.mock('../../../src/nest/common/crypto/apiKeyCrypto', () => ({ mask_stored_api_key: vi.fn((v: string | null | undefined) => (v ? '••••••••' : null)), encrypt_api_key: vi.fn((v) => v), })); -vi.mock('../../../src/services/ephemeralTokens', () => ({ createEphemeralToken: vi.fn() })); +vi.mock('../../../src/nest/auth/ephemeral-tokens', () => ({ createEphemeralToken: vi.fn() })); vi.mock('../../../src/services/notifications', () => ({ sendPasswordResetEmail: vi.fn() })); vi.mock('../../../src/mcp/sessionManager', () => ({ revokeUserSessions: vi.fn() })); vi.mock('../../../src/scheduler', () => ({ @@ -80,7 +80,7 @@ import { DatabaseService } from '../../../src/nest/database/database.service'; import { verifyJwtAndLoadUser } from '../../../src/nest/auth/jwt-verify'; import { authenticator } from 'otplib'; import { hashBackupCode } from '../../../src/nest/auth/auth.helpers'; -import { createEphemeralToken } from '../../../src/services/ephemeralTokens'; +import { createEphemeralToken } from '../../../src/nest/auth/ephemeral-tokens'; import { TripMembershipService } from '../../../src/nest/trip-membership/trip-membership.service'; import { revokeUserSessions } from '../../../src/mcp/sessionManager'; diff --git a/server/tests/unit/services/ephemeralTokens.test.ts b/server/tests/unit/nest/auth/ephemeral-tokens.test.ts similarity index 98% rename from server/tests/unit/services/ephemeralTokens.test.ts rename to server/tests/unit/nest/auth/ephemeral-tokens.test.ts index 0147a179d..d92e477b8 100644 --- a/server/tests/unit/services/ephemeralTokens.test.ts +++ b/server/tests/unit/nest/auth/ephemeral-tokens.test.ts @@ -7,7 +7,7 @@ beforeEach(() => { describe('ephemeralTokens', () => { async function getModule() { - return import('../../../src/services/ephemeralTokens'); + return import('../../../../src/nest/auth/ephemeral-tokens'); } // AUTH-030 — Resource token creation (single-use) diff --git a/server/tests/unit/nest/files.service.test.ts b/server/tests/unit/nest/files.service.test.ts index 8eb85ec03..ede131512 100644 --- a/server/tests/unit/nest/files.service.test.ts +++ b/server/tests/unit/nest/files.service.test.ts @@ -52,7 +52,7 @@ const { verifyJwtAndLoadUser } = vi.hoisted(() => ({ verifyJwtAndLoadUser: vi.fn vi.mock('../../../src/nest/auth/jwt-verify', () => ({ verifyJwtAndLoadUser })); const { consumeEphemeralToken } = vi.hoisted(() => ({ consumeEphemeralToken: vi.fn() })); -vi.mock('../../../src/services/ephemeralTokens', () => ({ consumeEphemeralToken })); +vi.mock('../../../src/nest/auth/ephemeral-tokens', () => ({ consumeEphemeralToken })); import type { Request } from 'express'; import { createTables } from '../../../src/db/schema'; diff --git a/server/tests/unit/nest/notifications.inapp-prefs.test.ts b/server/tests/unit/nest/notifications.inapp-prefs.test.ts index 1c0764d4d..4775aa420 100644 --- a/server/tests/unit/nest/notifications.inapp-prefs.test.ts +++ b/server/tests/unit/nest/notifications.inapp-prefs.test.ts @@ -41,7 +41,7 @@ import { createTables } from '../../../src/db/schema'; import { runMigrations } from '../../../src/db/migrations'; import { resetTestDb } from '../../helpers/test-db'; import { createUser, createAdmin, disableNotificationPref } from '../../helpers/factories'; -import { registerAction } from '../../../src/services/inAppNotificationActions'; +import { registerAction } from '../../../src/nest/notifications/in-app-actions'; import { DatabaseService } from '../../../src/nest/database/database.service'; import { RealtimeService } from '../../../src/nest/realtime/realtime.service'; import { NotificationsService } from '../../../src/nest/notifications/notifications.service'; diff --git a/server/tests/unit/services/inAppNotificationActions.test.ts b/server/tests/unit/nest/notifications/in-app-actions.test.ts similarity index 89% rename from server/tests/unit/services/inAppNotificationActions.test.ts rename to server/tests/unit/nest/notifications/in-app-actions.test.ts index 6fbbae2c6..f9ba9bc76 100644 --- a/server/tests/unit/services/inAppNotificationActions.test.ts +++ b/server/tests/unit/nest/notifications/in-app-actions.test.ts @@ -3,7 +3,7 @@ * Pure Map registry — no DB or external dependencies. */ import { describe, it, expect } from 'vitest'; -import { getAction } from '../../../src/services/inAppNotificationActions'; +import { getAction } from '../../../../src/nest/notifications/in-app-actions'; describe('getAction — built-in registrations', () => { it('NOTIF-ACT-001 — test_approve is pre-registered', () => { diff --git a/server/tests/unit/nest/oidc.service.test.ts b/server/tests/unit/nest/oidc.service.test.ts index 33df092e0..e5c2f7786 100644 --- a/server/tests/unit/nest/oidc.service.test.ts +++ b/server/tests/unit/nest/oidc.service.test.ts @@ -61,7 +61,7 @@ vi.mock('../../../src/nest/common/crypto/apiKeyCrypto', () => ({ mask_stored_api_key: vi.fn((v: string | null | undefined) => (v ? '••••••••' : null)), encrypt_api_key: vi.fn((v) => v), })); -vi.mock('../../../src/services/ephemeralTokens', () => ({ createEphemeralToken: vi.fn() })); +vi.mock('../../../src/nest/auth/ephemeral-tokens', () => ({ createEphemeralToken: vi.fn() })); vi.mock('../../../src/services/notifications', () => ({ sendPasswordResetEmail: vi.fn() })); vi.mock('../../../src/mcp/sessionManager', () => ({ revokeUserSessions: vi.fn() })); vi.mock('../../../src/scheduler', () => ({ diff --git a/server/tests/unit/nest/passkey.service.test.ts b/server/tests/unit/nest/passkey.service.test.ts index 8630480b1..0c9df4916 100644 --- a/server/tests/unit/nest/passkey.service.test.ts +++ b/server/tests/unit/nest/passkey.service.test.ts @@ -53,7 +53,7 @@ vi.mock('../../../src/nest/common/crypto/apiKeyCrypto', () => ({ mask_stored_api_key: vi.fn((v: string | null | undefined) => (v ? '••••••••' : null)), encrypt_api_key: vi.fn((v) => v), })); -vi.mock('../../../src/services/ephemeralTokens', () => ({ createEphemeralToken: vi.fn() })); +vi.mock('../../../src/nest/auth/ephemeral-tokens', () => ({ createEphemeralToken: vi.fn() })); vi.mock('../../../src/services/notifications', () => ({ sendPasswordResetEmail: vi.fn() })); vi.mock('../../../src/mcp/sessionManager', () => ({ revokeUserSessions: vi.fn() })); vi.mock('../../../src/scheduler', () => ({ diff --git a/server/tests/websocket/connection.test.ts b/server/tests/websocket/connection.test.ts index 7cf7bb64b..2721ba084 100644 --- a/server/tests/websocket/connection.test.ts +++ b/server/tests/websocket/connection.test.ts @@ -55,7 +55,7 @@ import { resetTestDb, resetRateLimits } from '../helpers/test-db'; import { createUser, createTrip } from '../helpers/factories'; import { authCookie } from '../helpers/auth'; import { setupWebSocket } from '../../src/websocket'; -import { createEphemeralToken } from '../../src/services/ephemeralTokens'; +import { createEphemeralToken } from '../../src/nest/auth/ephemeral-tokens'; import { createWsToken } from '../../src/nest/auth/auth.bridge'; let server: http.Server; diff --git a/server/tests/websocket/wire-envelope.test.ts b/server/tests/websocket/wire-envelope.test.ts index 09711dfbe..c4c8fe5c9 100644 --- a/server/tests/websocket/wire-envelope.test.ts +++ b/server/tests/websocket/wire-envelope.test.ts @@ -56,7 +56,7 @@ import { runMigrations } from '../../src/db/migrations'; import { resetTestDb } from '../helpers/test-db'; import { createUser, createTrip } from '../helpers/factories'; import { setupWebSocket, broadcast, broadcastToUser } from '../../src/websocket'; -import { createEphemeralToken } from '../../src/services/ephemeralTokens'; +import { createEphemeralToken } from '../../src/nest/auth/ephemeral-tokens'; let server: http.Server; let wsUrl: string;