@lenne.tech/nest-server 9.2.0 → 9.2.2

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.
Files changed (73) hide show
  1. package/README.md +1 -2
  2. package/dist/config.env.js +3 -0
  3. package/dist/config.env.js.map +1 -1
  4. package/dist/core/common/helpers/context.helper.d.ts +5 -2
  5. package/dist/core/common/helpers/context.helper.js +14 -8
  6. package/dist/core/common/helpers/context.helper.js.map +1 -1
  7. package/dist/core/common/interceptors/check-security.interceptor.js +1 -2
  8. package/dist/core/common/interceptors/check-security.interceptor.js.map +1 -1
  9. package/dist/core/common/interfaces/server-options.interface.d.ts +3 -1
  10. package/dist/core/common/pipes/check-input.pipe.js +2 -2
  11. package/dist/core/common/pipes/check-input.pipe.js.map +1 -1
  12. package/dist/core/modules/auth/core-auth.module.js +5 -1
  13. package/dist/core/modules/auth/core-auth.module.js.map +1 -1
  14. package/dist/core/modules/auth/core-auth.resolver.d.ts +6 -6
  15. package/dist/core/modules/auth/core-auth.resolver.js +25 -22
  16. package/dist/core/modules/auth/core-auth.resolver.js.map +1 -1
  17. package/dist/core/modules/auth/guards/auth.guard.js +9 -2
  18. package/dist/core/modules/auth/guards/auth.guard.js.map +1 -1
  19. package/dist/core/modules/auth/inputs/core-auth-sign-in.input.d.ts +1 -0
  20. package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js +6 -1
  21. package/dist/core/modules/auth/inputs/core-auth-sign-in.input.js.map +1 -1
  22. package/dist/core/modules/auth/inputs/core-auth-sign-up.input.d.ts +2 -5
  23. package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js +2 -23
  24. package/dist/core/modules/auth/inputs/core-auth-sign-up.input.js.map +1 -1
  25. package/dist/core/modules/auth/interfaces/core-auth-user.interface.d.ts +2 -2
  26. package/dist/core/modules/auth/interfaces/core-token-data.interface.d.ts +5 -0
  27. package/dist/core/modules/auth/interfaces/core-token-data.interface.js +3 -0
  28. package/dist/core/modules/auth/interfaces/core-token-data.interface.js.map +1 -0
  29. package/dist/core/modules/auth/interfaces/jwt-payload.interface.d.ts +3 -0
  30. package/dist/core/modules/auth/services/core-auth.service.d.ts +11 -9
  31. package/dist/core/modules/auth/services/core-auth.service.js +62 -50
  32. package/dist/core/modules/auth/services/core-auth.service.js.map +1 -1
  33. package/dist/core/modules/auth/strategies/jwt-refresh.strategy.js +1 -10
  34. package/dist/core/modules/auth/strategies/jwt-refresh.strategy.js.map +1 -1
  35. package/dist/core/modules/auth/strategies/jwt.strategy.js +1 -1
  36. package/dist/core/modules/auth/strategies/jwt.strategy.js.map +1 -1
  37. package/dist/core/modules/auth/tokens.decorator.d.ts +1 -0
  38. package/dist/core/modules/auth/tokens.decorator.js +20 -0
  39. package/dist/core/modules/auth/tokens.decorator.js.map +1 -0
  40. package/dist/core/modules/file/core-file.controller.d.ts +2 -2
  41. package/dist/core/modules/file/core-file.controller.js +2 -2
  42. package/dist/core/modules/file/core-file.controller.js.map +1 -1
  43. package/dist/core/modules/user/core-user.model.d.ts +2 -2
  44. package/dist/core/modules/user/core-user.model.js +0 -6
  45. package/dist/core/modules/user/core-user.model.js.map +1 -1
  46. package/dist/core.module.js +1 -1
  47. package/dist/core.module.js.map +1 -1
  48. package/dist/index.d.ts +4 -2
  49. package/dist/index.js +4 -2
  50. package/dist/index.js.map +1 -1
  51. package/dist/tsconfig.build.tsbuildinfo +1 -1
  52. package/package.json +1 -1
  53. package/src/config.env.ts +3 -0
  54. package/src/core/common/helpers/context.helper.ts +25 -10
  55. package/src/core/common/interceptors/check-security.interceptor.ts +3 -2
  56. package/src/core/common/interfaces/server-options.interface.ts +11 -1
  57. package/src/core/common/pipes/check-input.pipe.ts +2 -2
  58. package/src/core/modules/auth/core-auth.module.ts +5 -1
  59. package/src/core/modules/auth/core-auth.resolver.ts +22 -19
  60. package/src/core/modules/auth/guards/auth.guard.ts +9 -2
  61. package/src/core/modules/auth/inputs/core-auth-sign-in.input.ts +4 -1
  62. package/src/core/modules/auth/inputs/core-auth-sign-up.input.ts +3 -16
  63. package/src/core/modules/auth/interfaces/core-auth-user.interface.ts +3 -6
  64. package/src/core/modules/auth/interfaces/core-token-data.interface.ts +19 -0
  65. package/src/core/modules/auth/interfaces/jwt-payload.interface.ts +3 -0
  66. package/src/core/modules/auth/services/core-auth.service.ts +93 -81
  67. package/src/core/modules/auth/strategies/jwt-refresh.strategy.ts +1 -11
  68. package/src/core/modules/auth/strategies/jwt.strategy.ts +1 -1
  69. package/src/core/modules/auth/tokens.decorator.ts +36 -0
  70. package/src/core/modules/file/core-file.controller.ts +2 -2
  71. package/src/core/modules/user/core-user.model.ts +5 -12
  72. package/src/core.module.ts +1 -2
  73. package/src/index.ts +4 -2
@@ -0,0 +1,36 @@
1
+ import { createParamDecorator, ExecutionContext } from '@nestjs/common';
2
+ import { GqlExecutionContext } from '@nestjs/graphql';
3
+ import { getContextData } from '../../common/helpers/context.helper';
4
+
5
+ /**
6
+ * Get current tokens
7
+ */
8
+ export const Tokens = createParamDecorator(
9
+ (
10
+ tokenId: 'token' | 'refreshToken' | undefined,
11
+ ctx: ExecutionContext
12
+ ): string | { token: string; refreshToken: string } => {
13
+ // Get prepared context (REST or GraphQL)
14
+ const context = getContextData(ctx);
15
+
16
+ // Get token from cookie or authorization header
17
+ const token =
18
+ context?.request?.cookies?.['token'] ||
19
+ context?.request
20
+ ?.get('Authorization')
21
+ ?.replace(/bearer/i, '')
22
+ .trim();
23
+
24
+ // Refresh token from cookie or authorization header (the authorization header does not distinguish the tokens)
25
+ const refreshToken = context?.request?.cookies?.['refreshToken'] || token;
26
+
27
+ // Set tokens
28
+ const tokens = { token, refreshToken };
29
+
30
+ // Return tokens
31
+ if (tokenId?.length) {
32
+ return tokens[tokenId];
33
+ }
34
+ return tokens;
35
+ }
36
+ );
@@ -1,6 +1,6 @@
1
1
  import { BadRequestException, Controller, Get, NotFoundException, Param, Res } from '@nestjs/common';
2
- import { User } from '../../../server/modules/user/user.model';
3
2
  import { RESTUser } from '../../common/decorators/rest-user.decorator';
3
+ import { CoreUserModel } from '../user/core-user.model';
4
4
  import { CoreFileService } from './core-file.service';
5
5
 
6
6
  /**
@@ -17,7 +17,7 @@ export abstract class CoreFileController {
17
17
  * Download file
18
18
  */
19
19
  @Get(':filename')
20
- async getFile(@Param('filename') filename: string, @Res() res, @RESTUser() user: User) {
20
+ async getFile(@Param('filename') filename: string, @Res() res, @RESTUser() user: CoreUserModel) {
21
21
  if (!filename) {
22
22
  throw new BadRequestException('Missing filename for download');
23
23
  }
@@ -2,8 +2,8 @@ import { Field, ObjectType } from '@nestjs/graphql';
2
2
  import { Prop, raw, Schema as MongooseSchema } from '@nestjs/mongoose';
3
3
  import { IsEmail, IsOptional } from 'class-validator';
4
4
  import { Document } from 'mongoose';
5
- import { User } from '../../../server/modules/user/user.model';
6
5
  import { CorePersistenceModel } from '../../common/models/core-persistence.model';
6
+ import { CoreTokenData } from '../auth/interfaces/core-token-data.interface';
7
7
 
8
8
  export type CoreUserModelDocument = CoreUserModel & Document;
9
9
 
@@ -71,20 +71,13 @@ export abstract class CoreUserModel extends CorePersistenceModel {
71
71
  passwordResetToken: string = undefined;
72
72
 
73
73
  /**
74
- * Hashed refresh JWT
75
- */
76
- @IsOptional()
77
- @Prop()
78
- refreshToken: string = undefined;
79
-
80
- /**
81
- * Refresh tokens for devices
82
- * key: deviceID
83
- * value: hashed JWT
74
+ * Refresh tokens (for devices)
75
+ * key: Token
76
+ * value: TokenData
84
77
  */
85
78
  @IsOptional()
86
79
  @Prop(raw({}))
87
- refreshTokens: Record<string, string> = undefined;
80
+ refreshTokens: Record<string, CoreTokenData> = undefined;
88
81
 
89
82
  /**
90
83
  * Verification token of the user
@@ -68,8 +68,7 @@ export class CoreModule implements NestModule {
68
68
  onConnect: async (connectionParams) => {
69
69
  if (config.graphQl.enableSubscriptionAuth) {
70
70
  // get authToken from authorization header
71
- const authToken: string =
72
- 'Authorization' in connectionParams && connectionParams?.Authorization?.split(' ')[1];
71
+ const authToken: string = connectionParams?.Authorization?.split(' ')[1];
73
72
 
74
73
  if (authToken) {
75
74
  // verify authToken/getJwtPayLoad
package/src/index.ts CHANGED
@@ -81,14 +81,16 @@ export * from './core/modules/auth/guards/roles.guard';
81
81
  export * from './core/modules/auth/inputs/core-auth-sign-in.input';
82
82
  export * from './core/modules/auth/inputs/core-auth-sign-up.input';
83
83
  export * from './core/modules/auth/interfaces/core-auth-user.interface';
84
+ export * from './core/modules/auth/interfaces/core-token-data.interface';
84
85
  export * from './core/modules/auth/interfaces/jwt-payload.interface';
85
86
  export * from './core/modules/auth/services/core-auth.service';
86
87
  export * from './core/modules/auth/services/core-auth-user.service';
88
+ export * from './core/modules/auth/strategies/jwt.strategy';
89
+ export * from './core/modules/auth/strategies/jwt-refresh.strategy';
87
90
  export * from './core/modules/auth/core-auth.model';
88
91
  export * from './core/modules/auth/core-auth.module';
89
92
  export * from './core/modules/auth/core-auth.resolver';
90
- export * from './core/modules/auth/strategies/jwt.strategy';
91
- export * from './core/modules/auth/strategies/jwt-refresh.strategy';
93
+ export * from './core/modules/auth/tokens.decorator';
92
94
 
93
95
  // =====================================================================================================================
94
96
  // Core - Modules - File