import { Module, OnApplicationBootstrap, Provider } from '@nestjs/common'; import { APP_GUARD } from '@nestjs/core'; import { PassportModule } from '@nestjs/passport'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { SessionService } from './session.service'; import { LocalStrategy } from './local.strategy'; import { OidcService } from './oidc.service'; import { SessionAuthGuard } from './session.guard'; import { AUTH_PROVIDERS } from './auth.constants'; import type { AuthProviderInfo } from './auth-provider'; import { OWNERSHIP_MIGRATION, SESSION_REPOSITORY, SETTINGS_REPOSITORY, USER_REPOSITORY, } from '../domain/ports'; import { DrizzleOwnershipMigration } from '../infrastructure/persistence/ownership-migration.repository'; import { DrizzleUserRepository } from '../infrastructure/persistence/user.repository'; import { DrizzleSessionRepository } from '../infrastructure/persistence/session.repository'; import { DrizzleSettingsRepository } from '../infrastructure/persistence/settings.repository'; // The built-in local (username/password) provider. Always present. const LOCAL_PROVIDER: AuthProviderInfo = { name: 'local', label: 'Password', kind: 'credentials' }; // ------------------------------------------------------------------------- // Adding another SSO protocol // // OIDC ships (configured at runtime). To add a further one (SAML, OAuth2, ...): // 1. Write a Passport strategy with PassportStrategy(Strategy, '') // whose validate() resolves-or-creates a User (findByProvider / create on // UserRepository) and returns SessionService.toPublicUser(user). // 2. Add the strategy class to STRATEGIES below and its descriptor to // PROVIDERS. Add a login route (or reuse the redirect flow) keyed by the // same name. // No other file needs to change - the guard, sessions and cookie are shared. // See docs/auth.md for a worked OIDC example. // ------------------------------------------------------------------------- const STRATEGIES: Provider[] = [LocalStrategy]; const PROVIDERS: AuthProviderInfo[] = [LOCAL_PROVIDER]; @Module({ imports: [PassportModule], controllers: [AuthController], providers: [ AuthService, SessionService, OidcService, // The user/session repositories depend only on the global DRIZZLE tokens, // so the auth module owns them directly (they aren't needed elsewhere). { provide: USER_REPOSITORY, useClass: DrizzleUserRepository }, { provide: OWNERSHIP_MIGRATION, useClass: DrizzleOwnershipMigration }, { provide: SESSION_REPOSITORY, useClass: DrizzleSessionRepository }, // The SSO configuration lives in app_settings, same as the AI settings. { provide: SETTINGS_REPOSITORY, useClass: DrizzleSettingsRepository }, ...STRATEGIES, { provide: AUTH_PROVIDERS, useValue: PROVIDERS }, // App-wide gate: protects every route not marked @Public(). { provide: APP_GUARD, useClass: SessionAuthGuard }, ], exports: [AuthService, SessionService, OidcService], }) export class AuthModule implements OnApplicationBootstrap { constructor( private readonly auth: AuthService, private readonly sessions: SessionService, ) {} // Operator recovery: AUTH_RESET_PASSWORD="username:new-password" resets // that local account at boot (forgotten password - there is no email // infrastructure to send reset links). The user's sessions are revoked; // the variable should be removed right after signing back in. async onApplicationBootstrap(): Promise { const userId = await this.auth.resetPasswordFromEnv(); if (userId != null) await this.sessions.destroyAllForUser(userId); } }