UNPKG

3.1 kBPlain TextView Raw
1/*
2 * MIT License
3 *
4 * Copyright (c) 2019 nest-mods
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25import { Log } from '@nest-mods/log';
26import { DynamicModule, Global, LoggerService, Module } from '@nestjs/common';
27import { APP_GUARD } from '@nestjs/core';
28import * as _ from 'lodash';
29import { AUTH_MODULE_OPTIONS, AuthActionType } from './constants';
30import { AuthController } from './controller/auth.controller';
31import { AuthBasicGuard } from './guard/auth-basic.guard';
32import { AuthJwtGuard } from './guard/auth-jwt.guard';
33import { RoleAclGuard } from './guard/role-acl.guard';
34import { AuthModuleAsyncOptions, AuthModuleOptions } from './interfaces';
35import { AuthService } from './service/auth.service';
36import { BasicStrategy } from './strategy/basic.strategy';
37import { JwtStrategy } from './strategy/jwt.strategy';
38
39const defaultAuthConfig: Partial<AuthModuleOptions> = {
40 useJwt: true,
41 useBasic: true,
42 useACL: true,
43 basicAuth: {
44 defaultAuthAction: AuthActionType.TRY,
45 },
46 jwtAuth: {
47 defaultAuthAction: AuthActionType.TRY,
48 queryTokenKey: 'token',
49 secret: 'DEFAULT_PASSWORD',
50 signOptions: {
51 expiresIn: '7d',
52 audience: 'demo',
53 issuer: 'demo',
54 },
55 },
56};
57
58@Global()
59@Module({
60 providers: [AuthService, JwtStrategy, BasicStrategy, {
61 provide: APP_GUARD,
62 useClass: AuthBasicGuard,
63 }, {
64 provide: APP_GUARD,
65 useClass: AuthJwtGuard,
66 }, {
67 provide: APP_GUARD,
68 useClass: RoleAclGuard,
69 }],
70 exports: [AuthService],
71})
72export class AuthModule {
73 @Log() private static logger: LoggerService;
74
75 static forRootAsync(options: AuthModuleAsyncOptions): DynamicModule {
76 return {
77 module: AuthModule,
78 imports: options.imports,
79 providers: [{
80 provide: AUTH_MODULE_OPTIONS,
81 inject: options.inject,
82 useFactory: async (...args) => {
83 const opts = await options.useFactory(...args);
84 return _.defaultsDeep(opts, defaultAuthConfig);
85 },
86 }],
87 controllers: options.enabledController ? [AuthController] : [],
88 };
89 }
90}