UNPKG

3.54 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 {DynamicModule, Global, LoggerService, Module} from '@nestjs/common';
26import * as _ from 'lodash';
27import {APP_GUARD} from '@nestjs/core';
28import {AuthJwtGuard} from './guard/auth-jwt.guard';
29import {AuthBasicGuard} from './guard/auth-basic.guard';
30import {AuthModuleAsyncOptions, AuthModuleOptions} from './interfaces';
31import {AuthController} from './controller/auth.controller';
32import {RoleAclGuard} from './guard/role-acl.guard';
33import {AUTH_MODULE_OPTIONS, AuthActionType} from './constants';
34import {Log} from '@nest-mods/log';
35import {UserDetailService} from './service/user-detail.service';
36import {JwtStrategy} from './strategy/jwt.strategy';
37import {BasicStrategy} from './strategy/basic.strategy';
38import {AuthService} from './service/auth.service';
39
40const defaultAuthConfig: Partial<AuthModuleOptions> = {
41 useJwt: true,
42 useBasic: true,
43 useACL: true,
44 basicAuth: {
45 defaultAuthAction: AuthActionType.TRY,
46 },
47 jwtAuth: {
48 defaultAuthAction: AuthActionType.TRY,
49 queryTokenKey: 'token',
50 secret: 'DEFAULT_PASSWORD',
51 signOptions: {
52 expiresIn: '7d',
53 audience: 'demo',
54 issuer: 'demo',
55 },
56 },
57};
58
59@Global()
60@Module({
61 providers: [AuthService, JwtStrategy, BasicStrategy, {
62 provide: APP_GUARD,
63 useClass: AuthBasicGuard,
64 }, {
65 provide: APP_GUARD,
66 useClass: AuthJwtGuard,
67 }, {
68 provide: APP_GUARD,
69 useClass: RoleAclGuard,
70 }],
71 exports: [AuthService],
72})
73export class AuthModule {
74 @Log() private static logger: LoggerService;
75
76 static forRootAsync(options: AuthModuleAsyncOptions): DynamicModule {
77 return {
78 module: AuthModule,
79 imports: options.imports,
80 providers: [{
81 provide: AUTH_MODULE_OPTIONS,
82 inject: options.inject,
83 useFactory: async (...args) => {
84 const opts = await options.useFactory(...args);
85 return _.defaultsDeep(opts, defaultAuthConfig);
86 },
87 }, {
88 provide: UserDetailService,
89 useClass: options.UserDetailService,
90 }],
91 controllers: options.enabledController ? [AuthController] : [],
92 };
93 }
94}