@lenne.tech/nest-server 3.3.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
package/src/config.env.ts CHANGED
@@ -32,8 +32,10 @@ const config: { [env: string]: IServerOptions } = {
32
32
  },
33
33
  env: 'development',
34
34
  graphQl: {
35
- debug: true,
36
- introspection: true,
35
+ driver: {
36
+ debug: true,
37
+ introspection: true,
38
+ },
37
39
  },
38
40
  jwt: {
39
41
  secret: 'SECRET_OR_PRIVATE_KEY_DEV',
@@ -79,8 +81,10 @@ const config: { [env: string]: IServerOptions } = {
79
81
  },
80
82
  env: 'productive',
81
83
  graphQl: {
82
- debug: false,
83
- introspection: true,
84
+ driver: {
85
+ debug: false,
86
+ introspection: true,
87
+ },
84
88
  },
85
89
  jwt: {
86
90
  secret: 'SECRET_OR_PRIVATE_KEY_PROD',
@@ -20,7 +20,10 @@ export interface IServerOptions {
20
20
  * see https://docs.nestjs.com/graphql/quick-start
21
21
  * and https://www.apollographql.com/docs/apollo-server/api/apollo-server/
22
22
  */
23
- graphQl?: ApolloDriverConfig;
23
+ graphQl?: {
24
+ driver?: ApolloDriverConfig;
25
+ enableSubscriptionAuth?: boolean;
26
+ };
24
27
 
25
28
  /**
26
29
  * Configuration of JavaScript Web Token (JWT) module
@@ -1,4 +1,4 @@
1
- import { DynamicModule, Module, Type } from '@nestjs/common';
1
+ import { DynamicModule, ForwardReference, Module, Provider, Type } from '@nestjs/common';
2
2
  import { APP_GUARD } from '@nestjs/core';
3
3
  import { JwtModule, JwtModuleOptions } from '@nestjs/jwt';
4
4
  import { PassportModule } from '@nestjs/passport';
@@ -20,30 +20,46 @@ export class CoreAuthModule {
20
20
  static forRoot(
21
21
  UserModule: Type<any>,
22
22
  UserService: Type<CoreAuthUserService>,
23
- options: JwtModuleOptions
23
+ options: JwtModuleOptions & {
24
+ imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference>;
25
+ providers?: Provider[];
26
+ }
24
27
  ): DynamicModule {
28
+ // Porcess imports
29
+ let imports: any[] = [UserModule, PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule.register(options)];
30
+ if (Array.isArray(options?.imports)) {
31
+ imports = imports.concat(options.imports);
32
+ }
33
+
34
+ // Process providers
35
+ let providers = [
36
+ // [Global] The GraphQLAuthGard integrates the user into context
37
+ {
38
+ provide: APP_GUARD,
39
+ useClass: RolesGuard,
40
+ },
41
+ {
42
+ provide: CoreAuthUserService,
43
+ useClass: UserService,
44
+ },
45
+ {
46
+ provide: 'PUB_SUB',
47
+ useValue: new PubSub(),
48
+ },
49
+
50
+ // Standard services
51
+ CoreAuthService,
52
+ JwtStrategy,
53
+ ];
54
+ if (Array.isArray(options?.providers)) {
55
+ providers = imports.concat(options.providers);
56
+ }
57
+
58
+ // Return CoreAuthModule
25
59
  return {
26
60
  module: CoreAuthModule,
27
- imports: [UserModule, PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule.register(options)],
28
- providers: [
29
- // [Global] The GraphQLAuthGard integrates the user into context
30
- {
31
- provide: APP_GUARD,
32
- useClass: RolesGuard,
33
- },
34
- {
35
- provide: CoreAuthUserService,
36
- useClass: UserService,
37
- },
38
- {
39
- provide: 'PUB_SUB',
40
- useValue: new PubSub(),
41
- },
42
-
43
- // Standard services
44
- CoreAuthService,
45
- JwtStrategy,
46
- ],
61
+ imports: imports,
62
+ providers: providers,
47
63
  exports: [CoreAuthService, JwtModule, JwtStrategy, PassportModule, UserModule],
48
64
  };
49
65
  }
@@ -33,4 +33,8 @@ export class CoreAuthService {
33
33
  async validateUser(payload: JwtPayload): Promise<any> {
34
34
  return await this.userService.getViaEmail(payload.email);
35
35
  }
36
+
37
+ decodeJwt(token: string): JwtPayload {
38
+ return this.jwtService.decode(token) as JwtPayload;
39
+ }
36
40
  }
@@ -1,4 +1,4 @@
1
- import { DynamicModule, Global, Module } from '@nestjs/common';
1
+ import { DynamicModule, Global, Module, UnauthorizedException } from '@nestjs/common';
2
2
  import { APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
3
3
  import { GraphQLModule } from '@nestjs/graphql';
4
4
  import { Config } from './core/common/helpers/config.helper';
@@ -34,23 +34,43 @@ export class CoreModule {
34
34
  * Dynamic module
35
35
  * see https://docs.nestjs.com/modules#dynamic-modules
36
36
  */
37
- static forRoot(options: Partial<IServerOptions>): DynamicModule {
37
+ static forRoot(AuthService: any, AuthModule: any, options: Partial<IServerOptions>): DynamicModule {
38
38
  // Process config
39
39
  const config: IServerOptions = Config.merge(
40
40
  {
41
41
  env: 'develop',
42
42
  graphQl: {
43
- autoSchemaFile: 'schema.gql',
44
- context: ({ req }) => ({ req }),
45
- installSubscriptionHandlers: true,
46
- subscriptions: {
47
- 'subscriptions-transport-ws': {
48
- onConnect: (connectionParams) => {
49
- // TODO: Handle Authorization
50
- const authToken = connectionParams.Authorization;
43
+ driver: {
44
+ imports: [AuthModule],
45
+ inject: [AuthService],
46
+ useFactory: async (authService: any) => ({
47
+ autoSchemaFile: 'schema.gql',
48
+ context: ({ req }) => ({ req }),
49
+ installSubscriptionHandlers: true,
50
+ subscriptions: {
51
+ 'subscriptions-transport-ws': {
52
+ onConnect: async (connectionParams) => {
53
+ if (config.graphQl.enableSubscriptionAuth) {
54
+ // get authToken from authorization header
55
+ const authToken: string =
56
+ 'Authorization' in connectionParams && connectionParams?.Authorization?.split(' ')[1];
57
+
58
+ if (authToken) {
59
+ // verify authToken/getJwtPayLoad
60
+ const payload = authService.decodeJwt(authToken);
61
+ const user = await authService.validateUser(payload);
62
+ // the user/jwtPayload object found will be available as context.currentUser/jwtPayload in your GraphQL resolvers
63
+ return { user: user, headers: connectionParams };
64
+ }
65
+
66
+ throw new UnauthorizedException();
67
+ }
68
+ },
69
+ },
51
70
  },
52
- },
71
+ }),
53
72
  },
73
+ enableSubscriptionAuth: true,
54
74
  },
55
75
  port: 3000,
56
76
  mongoose: {
@@ -107,7 +127,7 @@ export class CoreModule {
107
127
  module: CoreModule,
108
128
  imports: [
109
129
  MongooseModule.forRoot(config.mongoose.uri, config.mongoose.options),
110
- GraphQLModule.forRoot<ApolloDriverConfig>(Object.assign({ driver: ApolloDriver }, config.graphQl)),
130
+ GraphQLModule.forRootAsync<ApolloDriverConfig>(Object.assign({ driver: ApolloDriver }, config.graphQl.driver)),
111
131
  ],
112
132
  providers,
113
133
  exports: [ConfigService, EmailService, TemplateService, MailjetService],
@@ -4,6 +4,7 @@ import { CoreModule } from '../core.module';
4
4
  import { AuthModule } from './modules/auth/auth.module';
5
5
  import { FileController } from './modules/file/file.controller';
6
6
  import { ServerController } from './server.controller';
7
+ import { CoreAuthService } from '../core/modules/auth/services/core-auth.service';
7
8
 
8
9
  /**
9
10
  * Server module (dynamic)
@@ -15,7 +16,7 @@ import { ServerController } from './server.controller';
15
16
  // Include modules
16
17
  imports: [
17
18
  // Include CoreModule for standard processes
18
- CoreModule.forRoot(envConfig),
19
+ CoreModule.forRoot(CoreAuthService, AuthModule.forRoot(envConfig.jwt), envConfig),
19
20
 
20
21
  // Include AuthModule for authorization handling,
21
22
  // which will also include UserModule
@@ -18,7 +18,7 @@ export enum TestGraphQLType {
18
18
  * GraphQL fields
19
19
  */
20
20
  export interface TestFieldObject {
21
- [key: string]: boolean | string[] | TestFieldObject;
21
+ [key: string]: boolean | (string | TestFieldObject)[] | TestFieldObject;
22
22
  }
23
23
 
24
24
  /**