Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
perf(backend:auth): lazy-load the configured authentication provider
johaven049ae917cbb7
diff --git a/backend/src/app.module.ts b/backend/src/app.module.tsindex 2bd001b3..1fbf7acd 100755--- a/backend/src/app.module.ts+++ b/backend/src/app.module.ts@@ -25,7 +25,7 @@ import { SchedulerModule } from './infrastructure/scheduler/scheduler.module'})}),AvailabilityModule,- AuthModule,+ AuthModule.register(configuration.auth.provider),DatabaseModule,CacheModule,MailerModule,diff --git a/backend/src/authentication/auth.module.spec.ts b/backend/src/authentication/auth.module.spec.tsnew file mode 100644index 00000000..e77c9091--- /dev/null+++ b/backend/src/authentication/auth.module.spec.ts@@ -0,0 +1,69 @@+import { AUTH_PROVIDER } from './providers/auth-providers.constants'++const lazyLoads = {+ mysql: vi.fn(),+ ldap: vi.fn(),+ oidc: vi.fn(),+ oidcController: vi.fn()+}++describe('AuthModule', () => {+ beforeEach(() => {+ vi.resetModules()+ vi.clearAllMocks()+ vi.doMock('./providers/mysql/auth-provider-mysql.service', () => {+ lazyLoads.mysql()+ return { AuthProviderMySQL: class AuthProviderMySQL {} }+ })+ vi.doMock('./providers/ldap/auth-provider-ldap.service', () => {+ lazyLoads.ldap()+ return { AuthProviderLDAP: class AuthProviderLDAP {} }+ })+ vi.doMock('./providers/oidc/auth-provider-oidc.service', () => {+ lazyLoads.oidc()+ return { AuthProviderOIDC: class AuthProviderOIDC {} }+ })+ vi.doMock('./providers/oidc/auth-oidc.controller', () => {+ lazyLoads.oidcController()+ return { AuthOIDCController: class AuthOIDCController {} }+ })+ })++ afterEach(() => {+ vi.doUnmock('./providers/mysql/auth-provider-mysql.service')+ vi.doUnmock('./providers/ldap/auth-provider-ldap.service')+ vi.doUnmock('./providers/oidc/auth-provider-oidc.service')+ vi.doUnmock('./providers/oidc/auth-oidc.controller')+ })++ it.each([+ { provider: AUTH_PROVIDER.MYSQL, providerName: 'AuthProviderMySQL', hasOIDCController: false },+ { provider: AUTH_PROVIDER.LDAP, providerName: 'AuthProviderLDAP', hasOIDCController: false },+ { provider: AUTH_PROVIDER.OIDC, providerName: 'AuthProviderOIDC', hasOIDCController: true }+ ])('should only load the $provider provider', async ({ provider, providerName, hasOIDCController }) => {+ const { AuthModule } = await import('./auth.module')+ const { AuthProvider } = await import('./providers/auth-providers.models')+ const definition = await AuthModule.register(provider)+ const selectedProvider = definition.providers?.find((candidate) => typeof candidate === 'function' && candidate.name === providerName)+ const controllerNames = definition.controllers?.map((controller) => controller.name) ?? []++ expect(selectedProvider).toBeDefined()+ expect(definition.providers).toContainEqual({ provide: AuthProvider, useExisting: selectedProvider })+ expect(controllerNames.includes('AuthOIDCController')).toBe(hasOIDCController)+ expect(lazyLoads.mysql).toHaveBeenCalledTimes(provider === AUTH_PROVIDER.MYSQL ? 1 : 0)+ expect(lazyLoads.ldap).toHaveBeenCalledTimes(provider === AUTH_PROVIDER.LDAP ? 1 : 0)+ expect(lazyLoads.oidc).toHaveBeenCalledTimes(provider === AUTH_PROVIDER.OIDC ? 1 : 0)+ expect(lazyLoads.oidcController).toHaveBeenCalledTimes(hasOIDCController ? 1 : 0)+ })++ it('should fall back to MySQL for an unexpected provider', async () => {+ const { AuthModule } = await import('./auth.module')+ const definition = await AuthModule.register('unexpected' as AUTH_PROVIDER)++ expect(definition.providers?.some((candidate) => typeof candidate === 'function' && candidate.name === 'AuthProviderMySQL')).toBe(true)+ expect(lazyLoads.mysql).toHaveBeenCalledOnce()+ expect(lazyLoads.ldap).not.toHaveBeenCalled()+ expect(lazyLoads.oidc).not.toHaveBeenCalled()+ expect(lazyLoads.oidcController).not.toHaveBeenCalled()+ })+})diff --git a/backend/src/authentication/auth.module.ts b/backend/src/authentication/auth.module.tsindex 7d95fe6d..5a73ddb1 100755--- a/backend/src/authentication/auth.module.ts+++ b/backend/src/authentication/auth.module.ts@@ -1,10 +1,9 @@-import { Global, Module } from '@nestjs/common'+import { type DynamicModule, Global, Module, type Provider } from '@nestjs/common'import { APP_GUARD } from '@nestjs/core'import { JwtModule } from '@nestjs/jwt'import { PassportModule } from '@nestjs/passport'import { ThrottlerModule } from '@nestjs/throttler'import { UsersModule } from '../applications/users/users.module'-import { configuration } from '../configuration/config.environment'import { CacheModule } from '../infrastructure/cache/cache.module'import { Cache } from '../infrastructure/cache/cache.service'import { AuthRateLimitStorage } from './adapters/auth-rate-limit-storage.adapter'@@ -22,52 +21,60 @@ import { AuthTokenAccessGuard } from './guards/auth-token-access.guard'import { AuthTokenAccessStrategy } from './guards/auth-token-access.strategy'import { AuthTokenRefreshGuard } from './guards/auth-token-refresh.guard'import { AuthTokenRefreshStrategy } from './guards/auth-token-refresh.strategy'-import { AUTH_PROVIDER } from './providers/auth-providers.constants'+import type { AUTH_PROVIDER } from './providers/auth-providers.constants'import { AuthProvider } from './providers/auth-providers.models'-import { selectAuthProvider } from './providers/auth-providers'-import { AuthProviderOIDCModule } from './providers/oidc/auth-provider-oidc.module'+import { loadAuthProviderDefinition } from './providers/auth-providers'import { AuthProvider2FA } from './providers/two-fa/auth-provider-two-fa.service'import { AuthTokenTwoFaGuard } from './providers/two-fa/guards/auth-token-two-fa.guard'import { AuthTokenTwoFaStrategy } from './providers/two-fa/guards/auth-token-two-fa.strategy'@Global()-@Module({- imports: [- JwtModule.register({ global: true }),- ThrottlerModule.forRootAsync({- imports: [CacheModule],- inject: [Cache],- useFactory: (cache: Cache) => ({- throttlers: [AUTH_RATE_LIMIT_OPTIONS],- storage: new AuthRateLimitStorage(cache)- })- }),- UsersModule,- PassportModule,- ...(configuration.auth.provider === AUTH_PROVIDER.OIDC ? [AuthProviderOIDCModule] : [])- ],- controllers: [AuthController],- providers: [- {- provide: APP_GUARD,- useClass: AuthTokenAccessGuard- },- AuthRateLimitGuard,- AuthTokenRefreshGuard,- AuthTokenTwoFaGuard,- AuthLocalGuard,- AuthBasicGuard,- AuthAnonymousGuard,- AuthLocalStrategy,- AuthTokenAccessStrategy,- AuthTokenRefreshStrategy,- AuthTokenTwoFaStrategy,- AuthBasicStrategy,- AuthAnonymousStrategy,- AuthManager,- AuthProvider2FA,- selectAuthProvider(configuration.auth.provider)- ],- exports: [AuthManager, AuthProvider, AuthProvider2FA, AuthRateLimitGuard]-})-export class AuthModule {}+@Module({})+export class AuthModule {+ static async register(provider: AUTH_PROVIDER): Promise<DynamicModule> {+ const { provider: authProvider, controllers: providerControllers } = await loadAuthProviderDefinition(provider)+ const providers: Provider[] = [+ {+ provide: APP_GUARD,+ useClass: AuthTokenAccessGuard+ },+ AuthRateLimitGuard,+ AuthTokenRefreshGuard,+ AuthTokenTwoFaGuard,+ AuthLocalGuard,+ AuthBasicGuard,+ AuthAnonymousGuard,+ AuthLocalStrategy,+ AuthTokenAccessStrategy,+ AuthTokenRefreshStrategy,+ AuthTokenTwoFaStrategy,+ AuthBasicStrategy,+ AuthAnonymousStrategy,+ AuthManager,+ AuthProvider2FA,+ // AuthOIDCController requires the concrete token; generic consumers reuse the same instance through AuthProvider.+ authProvider,+ { provide: AuthProvider, useExisting: authProvider }+ ]++ return {+ module: AuthModule,+ imports: [+ JwtModule.register({ global: true }),+ ThrottlerModule.forRootAsync({+ imports: [CacheModule],+ inject: [Cache],+ useFactory: (cache: Cache) => ({+ throttlers: [AUTH_RATE_LIMIT_OPTIONS],+ storage: new AuthRateLimitStorage(cache)+ })+ }),+ UsersModule,+ PassportModule+ ],+ controllers: [AuthController, ...providerControllers],+ providers,+ exports: [AuthManager, AuthProvider, AuthProvider2FA, AuthRateLimitGuard]+ }+ }+}diff --git a/backend/src/authentication/interfaces/auth-provider-definition.interface.ts b/backend/src/authentication/interfaces/auth-provider-definition.interface.tsnew file mode 100644index 00000000..858b8624--- /dev/null+++ b/backend/src/authentication/interfaces/auth-provider-definition.interface.ts@@ -0,0 +1,7 @@+import type { Type } from '@nestjs/common'+import type { AuthProvider } from '../providers/auth-providers.models'++export interface AuthProviderDefinition {+ provider: Type<AuthProvider>+ controllers: Type[]+}diff --git a/backend/src/authentication/providers/auth-providers.ts b/backend/src/authentication/providers/auth-providers.tsindex c8fab83b..ae9669e7 100644--- a/backend/src/authentication/providers/auth-providers.ts+++ b/backend/src/authentication/providers/auth-providers.ts@@ -1,23 +1,31 @@-import { Provider } from '@nestjs/common'+import type { AuthProviderDefinition } from '../interfaces/auth-provider-definition.interface'import { AUTH_PROVIDER } from './auth-providers.constants'-import { AuthProvider } from './auth-providers.models'-import { AuthProviderLDAP } from './ldap/auth-provider-ldap.service'-import { AuthProviderMySQL } from './mysql/auth-provider-mysql.service'-import { AuthProviderOIDC } from './oidc/auth-provider-oidc.service'-export function selectAuthProvider(provider: AUTH_PROVIDER): Provider {+export async function loadAuthProviderDefinition(provider: AUTH_PROVIDER): Promise<AuthProviderDefinition> {switch (provider) {- case AUTH_PROVIDER.OIDC:- // `AuthProviderOIDC` is already provided by `AuthProviderOIDCModule`- return { provide: AuthProvider, useExisting: AuthProviderOIDC }-case AUTH_PROVIDER.LDAP:- return { provide: AuthProvider, useClass: AuthProviderLDAP }+ return {+ provider: (await import('./ldap/auth-provider-ldap.service')).AuthProviderLDAP,+ controllers: []+ }- case AUTH_PROVIDER.MYSQL:- return { provide: AuthProvider, useClass: AuthProviderMySQL }+ case AUTH_PROVIDER.OIDC: {+ const [{ AuthProviderOIDC }, { AuthOIDCController }] = await Promise.all([+ import('./oidc/auth-provider-oidc.service'),+ import('./oidc/auth-oidc.controller')+ ])+ return {+ provider: AuthProviderOIDC,+ controllers: [AuthOIDCController]+ }+ }++ case AUTH_PROVIDER.MYSQL:default:- return { provide: AuthProvider, useClass: AuthProviderMySQL }+ return {+ provider: (await import('./mysql/auth-provider-mysql.service')).AuthProviderMySQL,+ controllers: []+ }}}diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.module.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.module.tsdeleted file mode 100644index c971f5c2..00000000--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.module.ts+++ /dev/null@@ -1,10 +0,0 @@-import { Module } from '@nestjs/common'-import { AuthOIDCController } from './auth-oidc.controller'-import { AuthProviderOIDC } from './auth-provider-oidc.service'--@Module({- controllers: [AuthOIDCController],- providers: [AuthProviderOIDC],- exports: [AuthProviderOIDC]-})-export class AuthProviderOIDCModule {}