|
| 1 | +import { DynamicModule, INestApplication, Logger, Module } from '@nestjs/common'; |
| 2 | +import { defaultAuthOptions, type AuthOptions } from '../main'; |
| 3 | +import { auth } from 'express-openid-connect'; |
| 4 | +import { AuthController } from './controllers/auth.controller'; |
| 5 | +import { AuthService } from './providers/auth.service'; |
| 6 | +import { deepMerge } from 'packages/js-utils/dist/main'; |
| 7 | +import { AuthOptionsProvider } from './providers/auth-options.provider'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The authentication module. This module is responsible for handling authentication and |
| 11 | + * authorization. It is based on the `express-openid-connect?` library and is intended |
| 12 | + * for use with an OIDC provider. |
| 13 | + * @example |
| 14 | + * // main.ts |
| 15 | + * import { AuthModule, AuthOptions } from '@nestjs-oidc/core'; |
| 16 | + * const authConfig: AuthOptions = { |
| 17 | + * // This is the minimum set of options you need to provide |
| 18 | + * roles: { |
| 19 | + * admin: "admin", |
| 20 | + * user: "user", |
| 21 | + * // ... more roles ... |
| 22 | + * }, |
| 23 | + * oidc: { |
| 24 | + * issuerBaseURL: 'https://example.com', |
| 25 | + * baseURL: 'http://localhost:3000', |
| 26 | + * clientID: 'client-id', |
| 27 | + * clientSecret: 'client-secret', |
| 28 | + * secret: 'session-secret', |
| 29 | + * } |
| 30 | + * } |
| 31 | + * await AuthModule.bootstrap(app, authConfig); |
| 32 | + * |
| 33 | + * // app.module.ts |
| 34 | + * import { AuthModule } from '@nestjs-oidc/core'; |
| 35 | + * @Module({ |
| 36 | + * imports: [AuthModule.forRoot(authConfig)], |
| 37 | + * }) |
| 38 | + * export class AppModule {} |
| 39 | + */ |
| 40 | +@Module({}) |
| 41 | +export class AuthModule { |
| 42 | + /** |
| 43 | + * Bootstraps authentication. This must be called during application bootstrapping. |
| 44 | + * @param app The Nest application instance. |
| 45 | + * @param options The authentication options. |
| 46 | + */ |
| 47 | + static async bootstrap(app: INestApplication, options: AuthOptions) { |
| 48 | + const mergedOptions = this.mergeOptionsWithDefaultValues(options); |
| 49 | + const { disable, oidc } = mergedOptions; |
| 50 | + if (disable) { |
| 51 | + Logger.warn('Authentication is disabled. All routes will be accessible.', AuthModule.name); |
| 52 | + return; |
| 53 | + } |
| 54 | + app.use(auth(oidc)); |
| 55 | + Logger.log(`Authentication is enabled and will be handled by issuer at '${oidc.issuerBaseURL}'.`, AuthModule.name); |
| 56 | + } |
| 57 | + |
| 58 | + static forRoot(options: AuthOptions): DynamicModule { |
| 59 | + return { |
| 60 | + module: AuthModule, |
| 61 | + controllers: [AuthController], |
| 62 | + providers: [ |
| 63 | + AuthService, |
| 64 | + { |
| 65 | + provide: AuthOptionsProvider, |
| 66 | + useValue: new AuthOptionsProvider(this.mergeOptionsWithDefaultValues(options)), |
| 67 | + }, |
| 68 | + ], |
| 69 | + exports: [AuthService, AuthOptionsProvider], |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + static mergeOptionsWithDefaultValues(options: AuthOptions) { |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 75 | + const mergedOptions = deepMerge(defaultAuthOptions as any, options as any) as unknown as AuthOptions; |
| 76 | + return mergedOptions; |
| 77 | + } |
| 78 | +} |
0 commit comments