nestjs-authentication 0.1.8 → 0.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.
Files changed (92) hide show
  1. package/dist/classes/memory.user.d.ts +4 -5
  2. package/dist/classes/memory.user.d.ts.map +1 -1
  3. package/dist/classes/memory.user.js +0 -5
  4. package/dist/classes/memory.user.js.map +1 -1
  5. package/dist/constants.d.ts +3 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/constants.js +6 -0
  8. package/dist/constants.js.map +1 -0
  9. package/dist/encryptor.d.ts +7 -0
  10. package/dist/encryptor.d.ts.map +1 -0
  11. package/dist/encryptor.js +31 -0
  12. package/dist/encryptor.js.map +1 -0
  13. package/dist/guard.d.ts +20 -8
  14. package/dist/guard.d.ts.map +1 -1
  15. package/dist/guard.js +86 -48
  16. package/dist/guard.js.map +1 -1
  17. package/dist/index.d.ts +13 -13
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +13 -13
  20. package/dist/index.js.map +1 -1
  21. package/dist/interfaces/auth.user.d.ts +8 -0
  22. package/dist/interfaces/auth.user.d.ts.map +1 -0
  23. package/dist/interfaces/{jwt.payload.js → auth.user.js} +1 -1
  24. package/dist/interfaces/auth.user.js.map +1 -0
  25. package/dist/interfaces/encrypt.d.ts +2 -2
  26. package/dist/interfaces/encrypt.d.ts.map +1 -1
  27. package/dist/interfaces/options.d.ts +4 -2
  28. package/dist/interfaces/options.d.ts.map +1 -1
  29. package/dist/interfaces/provider.d.ts +4 -4
  30. package/dist/interfaces/provider.d.ts.map +1 -1
  31. package/dist/module.d.ts +15 -5
  32. package/dist/module.d.ts.map +1 -1
  33. package/dist/module.js +89 -29
  34. package/dist/module.js.map +1 -1
  35. package/dist/providers/auth.memory.user.provider.d.ts +15 -0
  36. package/dist/providers/auth.memory.user.provider.d.ts.map +1 -0
  37. package/dist/providers/auth.memory.user.provider.js +30 -0
  38. package/dist/providers/auth.memory.user.provider.js.map +1 -0
  39. package/dist/providers/auth.provider.d.ts +8 -11
  40. package/dist/providers/auth.provider.d.ts.map +1 -1
  41. package/dist/providers/auth.provider.js +10 -33
  42. package/dist/providers/auth.provider.js.map +1 -1
  43. package/dist/providers/{memory.provider.d.ts → memory.user.provider.d.ts} +2 -2
  44. package/dist/providers/memory.user.provider.d.ts.map +1 -0
  45. package/dist/providers/{memory.provider.js → memory.user.provider.js} +5 -5
  46. package/dist/providers/memory.user.provider.js.map +1 -0
  47. package/dist/service.d.ts +5 -10
  48. package/dist/service.d.ts.map +1 -1
  49. package/dist/service.js +2 -35
  50. package/dist/service.js.map +1 -1
  51. package/dist/strategies/emailpassword.d.ts +14 -0
  52. package/dist/strategies/emailpassword.d.ts.map +1 -0
  53. package/dist/strategies/{local.js → emailpassword.js} +18 -17
  54. package/dist/strategies/emailpassword.js.map +1 -0
  55. package/dist/strategies/jwt.d.ts +22 -0
  56. package/dist/strategies/jwt.d.ts.map +1 -0
  57. package/dist/strategies/jwt.js +40 -0
  58. package/dist/strategies/jwt.js.map +1 -0
  59. package/dist/user.serializer.d.ts +4 -6
  60. package/dist/user.serializer.d.ts.map +1 -1
  61. package/dist/user.serializer.js +5 -14
  62. package/dist/user.serializer.js.map +1 -1
  63. package/package.json +50 -47
  64. package/test/emailpassword.controller.ts +69 -0
  65. package/test/encryptor.spec.ts +20 -0
  66. package/test/jwt.controller.ts +38 -0
  67. package/test/mixed.controller.ts +41 -0
  68. package/test/module.spec.ts +535 -0
  69. package/dist/classes/user.d.ts +0 -8
  70. package/dist/classes/user.d.ts.map +0 -1
  71. package/dist/classes/user.js +0 -7
  72. package/dist/classes/user.js.map +0 -1
  73. package/dist/controller.d.ts +0 -7
  74. package/dist/controller.d.ts.map +0 -1
  75. package/dist/controller.js +0 -37
  76. package/dist/controller.js.map +0 -1
  77. package/dist/interfaces/emailpassword.d.ts +0 -5
  78. package/dist/interfaces/emailpassword.d.ts.map +0 -1
  79. package/dist/interfaces/emailpassword.js +0 -3
  80. package/dist/interfaces/emailpassword.js.map +0 -1
  81. package/dist/interfaces/jwt.payload.d.ts +0 -6
  82. package/dist/interfaces/jwt.payload.d.ts.map +0 -1
  83. package/dist/interfaces/jwt.payload.js.map +0 -1
  84. package/dist/interfaces/login.request.d.ts +0 -6
  85. package/dist/interfaces/login.request.d.ts.map +0 -1
  86. package/dist/interfaces/login.request.js +0 -3
  87. package/dist/interfaces/login.request.js.map +0 -1
  88. package/dist/providers/memory.provider.d.ts.map +0 -1
  89. package/dist/providers/memory.provider.js.map +0 -1
  90. package/dist/strategies/local.d.ts +0 -9
  91. package/dist/strategies/local.d.ts.map +0 -1
  92. package/dist/strategies/local.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwt.js","sourceRoot":"","sources":["../../src/strategies/jwt.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+CAAoD;AACpD,gEAAwD;AACxD,+CAAyD;AAIzD,MAAa,WAAwB,SAAQ,IAAA,2BAAgB,EAAC,uBAAQ,EAAE,KAAK,CAAC;IAC1E,YAA+B,WAAwB,EAAqB,OAAwB;QAChG,KAAK,CAAC,OAAO,CAAC,CAAC;QADY,gBAAW,GAAX,WAAW,CAAa;QAAqB,YAAO,GAAP,OAAO,CAAiB;IAEpG,CAAC;IAES,YAAY,CAAC,OAAyD;;QAC5E,OAAO,MAAA,OAAO,CAAC,SAAS,mCAAI,SAAS,CAAC;IAC1C,CAAC;IAEK,QAAQ,CAAC,OAAyD;;YACpE,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YACtD,OAAO,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC7D,CAAC;KAAA;IAED,IAAI,CAAC,OAAyD,EAAE,kBAA0B,EAAE,OAAqB;QAC7G,OAAO,sBAAG,CAAC,IAAI,CAAC,OAAO,EAAE,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAC1D,CAAC;CACJ;AAjBD,kCAiBC"}
@@ -1,12 +1,10 @@
1
1
  import { PassportSerializer } from '@nestjs/passport';
2
+ import { IAuthUser } from './interfaces/auth.user';
2
3
  import { AuthService } from './service';
3
- export declare class UserSerializer extends PassportSerializer {
4
+ export declare class UserSerializer<User extends IAuthUser = any> extends PassportSerializer {
4
5
  protected readonly authService: AuthService;
5
6
  constructor(authService: AuthService);
6
- serializeUser(user: any, done: (e?: Error, payload?: any) => void): void;
7
- deserializeUser(payload: {
8
- _id: string;
9
- namespace: string;
10
- }, done: (e?: Error, user?: any) => void): Promise<void>;
7
+ serializeUser(user: User, done: (e?: Error, payload?: any) => void): void;
8
+ deserializeUser(payload: Record<string, unknown>, done: (e?: Error | 'pass', user?: any) => void): Promise<void>;
11
9
  }
12
10
  //# sourceMappingURL=user.serializer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"user.serializer.d.ts","sourceRoot":"","sources":["../src/user.serializer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAKxC,qBACa,cAAe,SAAQ,kBAAkB;IACtC,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW;gBAAxB,WAAW,EAAE,WAAW;IAIvD,aAAa,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,IAAI;IAW3D,eAAe,CAAC,OAAO,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI;CAc3G"}
1
+ {"version":3,"file":"user.serializer.d.ts","sourceRoot":"","sources":["../src/user.serializer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAKxC,qBACa,cAAc,CAAC,IAAI,SAAS,SAAS,GAAG,GAAG,CAAE,SAAQ,kBAAkB;IACpE,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW;gBAAxB,WAAW,EAAE,WAAW;IAIvD,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,IAAI,GAAG,IAAI;IAOnE,eAAe,CACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,KAAK,IAAI,GAC/C,OAAO,CAAC,IAAI,CAAC;CASnB"}
@@ -31,31 +31,22 @@ let UserSerializer = class UserSerializer extends passport_1.PassportSerializer
31
31
  if (user === undefined || user === null) {
32
32
  return done();
33
33
  }
34
- done(null, {
35
- _id: user._id.toString(),
36
- namespace: user.namespace
37
- });
34
+ done(undefined, user.authSerialize());
38
35
  }
39
36
  deserializeUser(payload, done) {
40
37
  return __awaiter(this, void 0, void 0, function* () {
41
- if (payload._id === undefined || payload.namespace === undefined) {
42
- done();
43
- }
44
38
  try {
45
- const user = yield this.authService.loadUser({
46
- _id: payload._id,
47
- namespace: payload.namespace
48
- });
49
- done(null, user);
39
+ const user = yield this.authService.loadUser(payload);
40
+ done(undefined, user);
50
41
  }
51
42
  catch (e) {
52
- done(e);
43
+ done('pass');
53
44
  }
54
45
  });
55
46
  }
56
47
  };
57
48
  UserSerializer = __decorate([
58
- common_1.Injectable(),
49
+ (0, common_1.Injectable)(),
59
50
  __metadata("design:paramtypes", [service_1.AuthService])
60
51
  ], UserSerializer);
61
52
  exports.UserSerializer = UserSerializer;
@@ -1 +1 @@
1
- {"version":3,"file":"user.serializer.js","sourceRoot":"","sources":["../src/user.serializer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+CAAsD;AAEtD,uCAAwC;AAMxC,IAAa,cAAc,GAA3B,MAAa,cAAe,SAAQ,6BAAkB;IAClD,YAA+B,WAAwB;QACnD,KAAK,EAAE,CAAC;QADmB,gBAAW,GAAX,WAAW,CAAa;IAEvD,CAAC;IAED,aAAa,CAAC,IAAS,EAAE,IAAwC;QAC7D,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,EAAE;YACrC,OAAO,IAAI,EAAE,CAAC;SACjB;QACD,IAAI,CAAC,IAAI,EAAE;YAEP,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE;YACxB,SAAS,EAAE,IAAI,CAAC,SAAS;SAC5B,CAAC,CAAC;IACP,CAAC;IAEK,eAAe,CAAC,OAA2C,EAAE,IAAqC;;YACpG,IAAI,OAAO,CAAC,GAAG,KAAK,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE;gBAC9D,IAAI,EAAE,CAAC;aACV;YACD,IAAI;gBACA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC;oBACzC,GAAG,EAAE,OAAO,CAAC,GAAG;oBAChB,SAAS,EAAE,OAAO,CAAC,SAAS;iBAC/B,CAAC,CAAC;gBACH,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;aACpB;YAAC,OAAO,CAAC,EAAE;gBACR,IAAI,CAAC,CAAC,CAAC,CAAC;aACX;QACL,CAAC;KAAA;CACJ,CAAA;AA9BY,cAAc;IAD1B,mBAAU,EAAE;qCAEmC,qBAAW;GAD9C,cAAc,CA8B1B;AA9BY,wCAAc"}
1
+ {"version":3,"file":"user.serializer.js","sourceRoot":"","sources":["../src/user.serializer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+CAAsD;AAGtD,uCAAwC;AAMxC,IAAa,cAAc,GAA3B,MAAa,cAA6C,SAAQ,6BAAkB;IAChF,YAA+B,WAAwB;QACnD,KAAK,EAAE,CAAC;QADmB,gBAAW,GAAX,WAAW,CAAa;IAEvD,CAAC;IAED,aAAa,CAAC,IAAU,EAAE,IAAwC;QAC9D,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,EAAE;YACrC,OAAO,IAAI,EAAE,CAAC;SACjB;QACD,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;IAC1C,CAAC;IAEK,eAAe,CACjB,OAAgC,EAChC,IAA8C;;YAE9C,IAAI;gBACA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;gBACtD,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;aACzB;YAAC,OAAO,CAAC,EAAE;gBAER,IAAI,CAAC,MAAM,CAAC,CAAC;aAChB;QACL,CAAC;KAAA;CACJ,CAAA;AAxBY,cAAc;IAD1B,IAAA,mBAAU,GAAE;qCAEmC,qBAAW;GAD9C,cAAc,CAwB1B;AAxBY,wCAAc"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nestjs-authentication",
3
- "version": "0.1.8",
3
+ "version": "0.4.0",
4
4
  "description": "A nestjs module that support multiple authentications provider",
5
5
  "author": "Alex Hermann <contact@pop-code.com>",
6
6
  "repository": "https://github.com/Pop-Code/nestjs-authentication.git",
@@ -8,55 +8,61 @@
8
8
  "main": "dist/index.js",
9
9
  "types": "./dist/index.d.ts",
10
10
  "scripts": {
11
- "build": "rm -Rf dist && tsc -b",
12
- "doc": "rm -Rf ./docs && typedoc ./src && touch ./docs/.nojekyll",
11
+ "build": "rm -Rf dist && tsc -b tsconfig.build.json",
12
+ "format": "prettier \"**/*.ts\" --ignore-path ./.prettierignore --write && git status",
13
+ "lint": "eslint .",
14
+ "doc": "rm -Rf ./docs && typedoc && touch ./docs/.nojekyll",
13
15
  "test": "jest",
14
16
  "test:watch": "jest --watch",
15
17
  "test:cov": "jest --coverage",
16
- "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest",
17
- "lint": "eslint --ext .js,.ts,.jsx,.tsx .",
18
- "format": "prettier \"**/*.ts\" --ignore-path .prettierignore --write && git status"
18
+ "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"
19
19
  },
20
20
  "peerDependencies": {
21
- "@nestjs/common": "^7",
22
- "@nestjs/core": "^7",
23
- "@nestjs/passport": "^7",
24
- "nestjs-console": "^4.0.0",
25
- "passport": "^0.4.1"
21
+ "@nestjs/common": "^8",
22
+ "@nestjs/core": "^8",
23
+ "@nestjs/passport": "^8",
24
+ "nestjs-console": "^7.0.1",
25
+ "passport": "^0.5.2"
26
26
  },
27
27
  "dependencies": {
28
- "passport-local": "~1.0.0"
28
+ "debug": "^4.3.3",
29
+ "jsonwebtoken": "^8.5.1",
30
+ "passport-jwt": "^4.0.0",
31
+ "passport-local": "^1.0.0",
32
+ "remove": "^0.1.5"
29
33
  },
30
34
  "devDependencies": {
31
- "@nestjs/common": "7.5.5",
32
- "@nestjs/core": "7.5.5",
33
- "@nestjs/testing": "7.5.5",
34
- "@nestjs/passport": "7.1.5",
35
- "@types/jest": "26.0.15",
36
- "@types/node": "14.14.10",
37
- "@types/passport": "1.0.4",
38
- "commander": "6.2.0",
39
- "@typescript-eslint/eslint-plugin": "4.8.2",
40
- "@typescript-eslint/parser": "4.8.2",
41
- "eslint": "7.14.0",
42
- "eslint-config-prettier": "6.15.0",
43
- "eslint-config-standard-with-typescript": "19.0.1",
44
- "eslint-plugin-import": "2.22.1",
45
- "eslint-plugin-node": "11.1.0",
46
- "eslint-plugin-prefer-arrow": "1.2.2",
47
- "eslint-plugin-promise": "4.2.1",
48
- "eslint-plugin-standard": "5.0.0",
49
- "jest": "26.6.3",
50
- "nestjs-console": "4.0.0",
51
- "passport": "0.4.1",
52
- "prettier": "2.2.1",
35
+ "@nestjs/common": "8.2.6",
36
+ "@nestjs/core": "8.2.6",
37
+ "@nestjs/passport": "8.1.0",
38
+ "@nestjs/platform-express": "^8.2.6",
39
+ "@nestjs/testing": "8.2.6",
40
+ "@types/debug": "^4.1.7",
41
+ "@types/express": "4.17.13",
42
+ "@types/express-session": "^1.17.4",
43
+ "@types/jest": "27.4.0",
44
+ "@types/node": "17.0.14",
45
+ "@types/passport": "1.0.7",
46
+ "@types/passport-jwt": "^3.0.6",
47
+ "@types/passport-local": "^1.0.34",
48
+ "@types/supertest": "^2.0.11",
49
+ "@typescript-eslint/eslint-plugin": "5.10.2",
50
+ "@typescript-eslint/parser": "5.10.2",
51
+ "codecov": "^3.8.3",
52
+ "eslint": "8.8.0",
53
+ "eslint-config-prettier": "8.3.0",
54
+ "express-session": "^1.17.2",
55
+ "jest": "27.4.7",
56
+ "passport": "0.5.2",
57
+ "prettier": "2.5.1",
53
58
  "reflect-metadata": "0.1.13",
54
- "rxjs": "6.6.3",
55
- "ts-jest": "26.4.4",
56
- "ts-node": "9.0.0",
57
- "tsconfig-paths": "3.9.0",
58
- "typedoc": "0.19.2",
59
- "typescript": "4.0.5"
59
+ "rxjs": "7.5.2",
60
+ "supertest": "^6.2.2",
61
+ "ts-jest": "27.1.3",
62
+ "ts-node": "10.4.0",
63
+ "tsconfig-paths": "3.12.0",
64
+ "typedoc": "0.22.11",
65
+ "typescript": "4.5.5"
60
66
  },
61
67
  "jest": {
62
68
  "moduleFileExtensions": [
@@ -64,19 +70,16 @@
64
70
  "json",
65
71
  "ts"
66
72
  ],
67
- "rootDir": "src",
73
+ "rootDir": ".",
68
74
  "testRegex": ".spec.ts$",
69
75
  "transform": {
70
76
  "^.+\\.(t|j)s$": "ts-jest"
71
77
  },
72
78
  "collectCoverageFrom": [
73
- "**/*.{js,jsx,ts}",
74
- "!index.ts",
79
+ "src/**/*.{js,jsx,ts}",
80
+ "!src/index.ts",
75
81
  "!**/test/**"
76
82
  ],
77
- "coverageDirectory": "../coverage"
78
- },
79
- "engines": {
80
- "node": ">=0.12"
83
+ "coverageDirectory": "./coverage"
81
84
  }
82
85
  }
@@ -0,0 +1,69 @@
1
+ import { Controller, Get, HttpCode, Post, Req, UseGuards } from '@nestjs/common';
2
+ import { Request } from 'express';
3
+
4
+ import { AuthGuard } from '../src';
5
+
6
+ @Controller('emailpassword')
7
+ export class EmailPaswordController {
8
+ /**
9
+ * A method protected by emailpassword
10
+ * - required, AuthGuard will fail if request is not authenticated
11
+ * - do not get user from session
12
+ * - save the user in the session
13
+ */
14
+ @Post('login_check')
15
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: { session: true } }, required: true }))
16
+ async loginCheckAction(@Req() req: Request) {
17
+ return { success: req.user !== undefined };
18
+ }
19
+
20
+ /**
21
+ * A method protected by emailpassword
22
+ * - required, AuthGuard will fail if request is not authenticated
23
+ * - get user from session
24
+ * - logout the user
25
+ */
26
+ @Post('logout')
27
+ @HttpCode(200)
28
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {} }, fromSession: true, required: true }))
29
+ async logoutAction(@Req() req: Request) {
30
+ if (req.isAuthenticated()) {
31
+ req.logOut();
32
+ }
33
+ return { success: true };
34
+ }
35
+
36
+ /**
37
+ * A method protected by emailpassword
38
+ * - not required, AuthGuard will succeed if request is not authenticated
39
+ * - get user from session
40
+ */
41
+ @Get('authenticated')
42
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {} }, fromSession: true }))
43
+ async authenticated(@Req() req: Request) {
44
+ return { user: req.user };
45
+ }
46
+
47
+ /**
48
+ * A method protected by emailpassword
49
+ * - required, AuthGuard will fail if request is not authenticated
50
+ * - get user from session
51
+ */
52
+ @Get('protected')
53
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {} }, fromSession: true, required: true }))
54
+ async protected(@Req() req: Request) {
55
+ return { user: req.user };
56
+ }
57
+
58
+ /**
59
+ * A method protected by emailpassword
60
+ * - not required, AuthGuard will succeed if request is not authenticated
61
+ * - do not get user from session
62
+ * Warning: This method will always return an empty object ! because user is not loaded from session
63
+ */
64
+ @Get('session')
65
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {} } }))
66
+ async noSession(@Req() req: Request) {
67
+ return { user: req.user };
68
+ }
69
+ }
@@ -0,0 +1,20 @@
1
+ import { Encryptor } from '../src';
2
+
3
+ const key = '77c0c891a5a062694a5ef1fdcf3719fe460d9c6e7ad5bda8715e44fdb2c8ae85';
4
+ const iv = 'f62456b5e1604435e3a548002d8b8944';
5
+ const encryptor = new Encryptor({ key, iv });
6
+
7
+ describe('Encryptor', () => {
8
+ test('The Encryptor must encrypt a value and give same result multiple times', async () => {
9
+ const password = 'Hello, I am a strong * password * encrypted';
10
+ const encrypted = 'b28d5453627e7f65fa1e0830c5e7d81d449d7f55a2e7266330148587fdeb623e28a6f135b6d1c411f35975';
11
+ for (let i = 0; i < 1000; i++) {
12
+ expect(encryptor.encrypt(password)).toEqual(encrypted);
13
+ }
14
+ });
15
+ test('The Encryptor must encrypt two different value and give different results', async () => {
16
+ const password1 = 'firstpassword';
17
+ const password2 = 'differentpassword';
18
+ expect(encryptor.encrypt(password1)).not.toEqual(encryptor.encrypt(password2));
19
+ });
20
+ });
@@ -0,0 +1,38 @@
1
+ import { Controller, Get, Req, UseGuards } from '@nestjs/common';
2
+ import { Request } from 'express';
3
+
4
+ import { AuthGuard } from '../src';
5
+
6
+ @Controller('jwt')
7
+ export class JwtController {
8
+ /**
9
+ * A method protected by jwt
10
+ * - not required, AuthGuard will succeed if request is not authenticated
11
+ */
12
+ @Get('authenticated')
13
+ @UseGuards(new AuthGuard({ strategies: { jwt: {} } }))
14
+ async authenticated(@Req() req: Request) {
15
+ return { user: req.user };
16
+ }
17
+
18
+ /**
19
+ * A method protected by jwt
20
+ * - required, AuthGuard will fail if request is not authenticated
21
+ */
22
+ @Get('protected')
23
+ @UseGuards(new AuthGuard({ strategies: { jwt: {} }, required: true }))
24
+ async protected(@Req() req: Request) {
25
+ return { user: req.user };
26
+ }
27
+
28
+ /**
29
+ * A method protected by jwt
30
+ * - required, AuthGuard will fail if request is not authenticated
31
+ * - get the user from session
32
+ */
33
+ @Get('session')
34
+ @UseGuards(new AuthGuard({ strategies: { jwt: {} }, fromSession: true, required: true }))
35
+ async session(@Req() req: Request) {
36
+ return { user: req.user };
37
+ }
38
+ }
@@ -0,0 +1,41 @@
1
+ import { Controller, Get, Req, UseGuards } from '@nestjs/common';
2
+ import { Request } from 'express';
3
+
4
+ import { AuthGuard } from '../src';
5
+
6
+ @Controller('mixed')
7
+ export class MixedController {
8
+ /**
9
+ * A method protected by emailpassword, jwt
10
+ * - not required, AuthGuard will succeed if request is not authenticated
11
+ * - get user from session
12
+ */
13
+ @Get('authenticated')
14
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {}, jwt: {} }, fromSession: true }))
15
+ async authenticated(@Req() req: Request) {
16
+ return { user: req.user };
17
+ }
18
+
19
+ /**
20
+ * A method protected by emailpassword, jwt
21
+ * - required, AuthGuard will fail if request is not authenticated
22
+ * - get user from session
23
+ */
24
+ @Get('protected')
25
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {}, jwt: {} }, fromSession: true, required: true }))
26
+ async protected(@Req() req: Request) {
27
+ return { user: req.user };
28
+ }
29
+
30
+ /**
31
+ * A method protected by emailpassword, jwt
32
+ * - not required, AuthGuard will succeed if request is not authenticated
33
+ * - do not get user from session
34
+ * Warning: This method will always return an empty object for emailpassword. if no token provided for jwt
35
+ */
36
+ @Get('session')
37
+ @UseGuards(new AuthGuard({ strategies: { emailpassword: {}, jwt: {} } }))
38
+ async noSession(@Req() req: Request) {
39
+ return { user: req.user };
40
+ }
41
+ }