Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import { 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, '<name>')// 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<void> { const userId = await this.auth.resetPasswordFromEnv(); if (userId != null) await this.sessions.destroyAllForUser(userId); }}