From e4b133da7c6067cf6c7095201aa77780d75c7b30 Mon Sep 17 00:00:00 2001 From: Szymon Szafoni Date: Thu, 23 Nov 2023 22:45:32 +0100 Subject: [PATCH] Revert "move setup sessions to server config" This reverts commit 86b176dc7ee96386b1812a94d74bc92b106fea3f. --- .../modules/server/admin-api.server.module.ts | 4 +- .../src/modules/server/server.config.ts | 45 ------------------- .../src/modules/server/server.module.ts | 44 +++++++++++++++++- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 34a1d148f7c..fc11b5b772b 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -10,8 +10,8 @@ import { FileEntity } from '@modules/files/entity'; import { FileRecord } from '@modules/files-storage/entity'; import { RedisClient } from 'redis'; import { REDIS_CLIENT, RedisModule } from '@src/infra/redis'; -import { defaultMikroOrmOptions } from './server.module'; -import { serverConfig, setupSessions } from './server.config'; +import { defaultMikroOrmOptions, setupSessions } from './server.module'; +import { serverConfig } from './server.config'; import { AdminApiServerController } from './controller'; import { DeletionApiModule } from '../deletion/deletion-api.module'; diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 052e5e77d80..09f410d4c94 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -6,11 +6,6 @@ import type { CommonCartridgeConfig } from '@modules/learnroom/common-cartridge' import type { UserConfig } from '@modules/user'; import type { CoreModuleConfig } from '@src/core'; import { MailConfig } from '@src/infra/mail/interfaces/mail-config'; -import { MiddlewareConsumer } from '@nestjs/common'; -import { RedisClient } from 'redis'; -import { LegacyLogger } from '@src/core/logger'; -import connectRedis from 'connect-redis'; -import session from 'express-session'; import { XApiKeyConfig } from '../authentication/config/x-api-key.config'; export enum NodeEnvType { @@ -57,43 +52,3 @@ const config: ServerConfig = { }; export const serverConfig = () => config; - -export const setupSessions = ( - consumer: MiddlewareConsumer, - redisClient: RedisClient | undefined, - logger: LegacyLogger -) => { - const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; - - let store: connectRedis.RedisStore | undefined; - if (redisClient) { - const RedisStore: connectRedis.RedisStore = connectRedis(session); - store = new RedisStore({ - client: redisClient, - ttl: sessionDuration, - }); - } else { - logger.warn( - 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' - ); - } - - consumer - .apply( - session({ - store, - secret: Configuration.get('SESSION__SECRET') as string, - resave: false, - saveUninitialized: false, - name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, - proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, - cookie: { - secure: Configuration.get('SESSION__SECURE') as boolean, - sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', - httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, - maxAge: sessionDuration * 1000, - }, - }) - ) - .forRoutes('*'); -}; diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index 1e34fe9bd43..030f91207a1 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -34,9 +34,11 @@ import { ALL_ENTITIES } from '@shared/domain'; import { createConfigModuleOptions, DB_PASSWORD, DB_URL, DB_USERNAME } from '@src/config'; import { CoreModule } from '@src/core'; import { LegacyLogger, LoggerModule } from '@src/core/logger'; +import connectRedis from 'connect-redis'; +import session from 'express-session'; import { RedisClient } from 'redis'; import { ServerController } from './controller/server.controller'; -import { serverConfig, setupSessions } from './server.config'; +import { serverConfig } from './server.config'; const serverModules = [ ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), @@ -83,6 +85,46 @@ export const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), }; +export const setupSessions = ( + consumer: MiddlewareConsumer, + redisClient: RedisClient | undefined, + logger: LegacyLogger +) => { + const sessionDuration: number = Configuration.get('SESSION__EXPIRES_SECONDS') as number; + + let store: connectRedis.RedisStore | undefined; + if (redisClient) { + const RedisStore: connectRedis.RedisStore = connectRedis(session); + store = new RedisStore({ + client: redisClient, + ttl: sessionDuration, + }); + } else { + logger.warn( + 'The RedisStore for sessions is not setup, since the environment variable REDIS_URI is not defined. Sessions are using the build-in MemoryStore. This should not be used in production!' + ); + } + + consumer + .apply( + session({ + store, + secret: Configuration.get('SESSION__SECRET') as string, + resave: false, + saveUninitialized: false, + name: Configuration.has('SESSION__NAME') ? (Configuration.get('SESSION__NAME') as string) : undefined, + proxy: Configuration.has('SESSION__PROXY') ? (Configuration.get('SESSION__PROXY') as boolean) : undefined, + cookie: { + secure: Configuration.get('SESSION__SECURE') as boolean, + sameSite: Configuration.get('SESSION__SAME_SITE') as boolean | 'lax' | 'strict' | 'none', + httpOnly: Configuration.get('SESSION__HTTP_ONLY') as boolean, + maxAge: sessionDuration * 1000, + }, + }) + ) + .forRoutes('*'); +}; + /** * Server Module used for production */