nestjs-authentication 0.4.0 → 0.4.3
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/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +2 -1
- package/dist/guard.js.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +29 -30
- package/test/emailpassword.controller.ts +0 -69
- package/test/encryptor.spec.ts +0 -20
- package/test/jwt.controller.ts +0 -38
- package/test/mixed.controller.ts +0 -41
- package/test/module.spec.ts +0 -535
package/dist/guard.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAA2B,MAAM,gBAAgB,CAAC;AACxF,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAiB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEzD,MAAM,WAAW,gBAAgB;IAC7B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE;QAAE,CAAC,YAAY,EAAE,MAAM,GAAG,mBAAmB,CAAA;KAAE,CAAC;CAC/D;AAED,qBAAa,SAAU,YAAW,WAAW;IAkB7B,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,gBAAgB;IAdxD,MAAM,CAAC,MAAM,gBAAa;IAC1B,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,CAAC,OAAO,CAAC;gBAa5B,OAAO,EAAE,gBAAgB;
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAA2B,MAAM,gBAAgB,CAAC;AACxF,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAiB,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEzD,MAAM,WAAW,gBAAgB;IAC7B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE;QAAE,CAAC,YAAY,EAAE,MAAM,GAAG,mBAAmB,CAAA;KAAE,CAAC;CAC/D;AAED,qBAAa,SAAU,YAAW,WAAW;IAkB7B,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,gBAAgB;IAdxD,MAAM,CAAC,MAAM,gBAAa;IAC1B,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mBAAmB,CAAC,OAAO,CAAC;gBAa5B,OAAO,EAAE,gBAAgB;IAaxD,iBAAiB,CAAC,OAAO,EAAE,gBAAgB;;;;;IAqBrC,uBAAuB,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC;IAW/E,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IA4CxD,KAAK,CACP,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,OAAO,GACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAUtC"}
|
package/dist/guard.js
CHANGED
|
@@ -17,11 +17,12 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const passport_1 = __importDefault(require("passport"));
|
|
18
18
|
class AuthGuard {
|
|
19
19
|
constructor(options) {
|
|
20
|
-
var _a;
|
|
20
|
+
var _a, _b;
|
|
21
21
|
this.options = options;
|
|
22
22
|
for (const strategy in this.options.strategies) {
|
|
23
23
|
const options = this.options.strategies[strategy];
|
|
24
24
|
options.authInfo = (_a = options === null || options === void 0 ? void 0 : options.authInfo) !== null && _a !== void 0 ? _a : true;
|
|
25
|
+
options.session = (_b = options === null || options === void 0 ? void 0 : options.session) !== null && _b !== void 0 ? _b : false;
|
|
25
26
|
AuthGuard.registerScopes(options === null || options === void 0 ? void 0 : options.scope);
|
|
26
27
|
}
|
|
27
28
|
}
|
package/dist/guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwF;AAExF,wDAAyD;AAQzD,MAAa,SAAS;IAkBlB,YAA+B,OAAyB;;QAAzB,YAAO,GAAP,OAAO,CAAkB;QACpD,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE;YAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;YAClD,OAAO,CAAC,QAAQ,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,IAAI,CAAC;YAC7C,SAAS,CAAC,cAAc,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,CAAC,CAAC;SAC5C;IACL,CAAC;
|
|
1
|
+
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAwF;AAExF,wDAAyD;AAQzD,MAAa,SAAS;IAkBlB,YAA+B,OAAyB;;QAAzB,YAAO,GAAP,OAAO,CAAkB;QACpD,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE;YAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;YAClD,OAAO,CAAC,QAAQ,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,IAAI,CAAC;YAC7C,OAAO,CAAC,OAAO,GAAG,MAAA,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AC/B,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;qBACpB;oBACD,OAAO,CAAC,IAAI,CAAC,CAAC;gBAClB,CAAC,CAAC,CAAC;YACP,CAAC,CAAC,CAAC;QACP,CAAC;KAAA;;AAxHL,8BAyHC;AArHU,gBAAM,GAAG,IAAI,GAAG,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wDAAsC;AACtC,8CAA4B;AAC5B,8CAA4B;AAC5B,0CAAwB;AACxB,yDAAuC;AACvC,uDAAqC;AACrC,uDAAqC;AACrC,wDAAsC;AACtC,2CAAyB;AACzB,wEAAsD;AACtD,4DAA0C;AAC1C,mEAAiD;AACjD,4CAA0B;AAC1B,6DAA2C;AAC3C,mDAAiC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-authentication",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
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",
|
|
@@ -21,48 +21,46 @@
|
|
|
21
21
|
"@nestjs/common": "^8",
|
|
22
22
|
"@nestjs/core": "^8",
|
|
23
23
|
"@nestjs/passport": "^8",
|
|
24
|
-
"nestjs-console": "^7.0.1",
|
|
25
24
|
"passport": "^0.5.2"
|
|
26
25
|
},
|
|
27
26
|
"dependencies": {
|
|
28
27
|
"debug": "^4.3.3",
|
|
29
28
|
"jsonwebtoken": "^8.5.1",
|
|
30
29
|
"passport-jwt": "^4.0.0",
|
|
31
|
-
"passport-local": "^1.0.0"
|
|
32
|
-
"remove": "^0.1.5"
|
|
30
|
+
"passport-local": "^1.0.0"
|
|
33
31
|
},
|
|
34
32
|
"devDependencies": {
|
|
35
|
-
"@nestjs/common": "8.
|
|
36
|
-
"@nestjs/core": "8.
|
|
37
|
-
"@nestjs/passport": "8.1
|
|
38
|
-
"@nestjs/platform-express": "^8.
|
|
39
|
-
"@nestjs/testing": "8.
|
|
33
|
+
"@nestjs/common": "^8.4.4",
|
|
34
|
+
"@nestjs/core": "^8.4.4",
|
|
35
|
+
"@nestjs/passport": "^8.2.1",
|
|
36
|
+
"@nestjs/platform-express": "^8.4.4",
|
|
37
|
+
"@nestjs/testing": "^8.4.4",
|
|
40
38
|
"@types/debug": "^4.1.7",
|
|
41
|
-
"@types/express": "4.17.13",
|
|
39
|
+
"@types/express": "^4.17.13",
|
|
42
40
|
"@types/express-session": "^1.17.4",
|
|
43
|
-
"@types/jest": "27.4.
|
|
44
|
-
"@types/node": "17.0.
|
|
45
|
-
"@types/passport": "1.0.7",
|
|
41
|
+
"@types/jest": "^27.4.1",
|
|
42
|
+
"@types/node": "^17.0.27",
|
|
43
|
+
"@types/passport": "^1.0.7",
|
|
46
44
|
"@types/passport-jwt": "^3.0.6",
|
|
47
45
|
"@types/passport-local": "^1.0.34",
|
|
48
|
-
"@types/supertest": "^2.0.
|
|
49
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
50
|
-
"@typescript-eslint/parser": "5.
|
|
51
|
-
"
|
|
52
|
-
"eslint": "8.
|
|
53
|
-
"eslint-config-prettier": "8.3.0",
|
|
46
|
+
"@types/supertest": "^2.0.12",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^5.21.0",
|
|
48
|
+
"@typescript-eslint/parser": "^5.21.0",
|
|
49
|
+
"eslint": "^8.14.0",
|
|
50
|
+
"eslint-config-prettier": "^8.5.0",
|
|
54
51
|
"express-session": "^1.17.2",
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
52
|
+
"i18next": "^21.6.16",
|
|
53
|
+
"jest": "^27.5.1",
|
|
54
|
+
"passport": "^0.5.2",
|
|
55
|
+
"prettier": "^2.6.2",
|
|
56
|
+
"reflect-metadata": "^0.1.13",
|
|
57
|
+
"rxjs": "^7.5.5",
|
|
60
58
|
"supertest": "^6.2.2",
|
|
61
|
-
"ts-jest": "27.1.
|
|
62
|
-
"ts-node": "10.
|
|
63
|
-
"tsconfig-paths": "3.
|
|
64
|
-
"typedoc": "0.22.
|
|
65
|
-
"typescript": "4.
|
|
59
|
+
"ts-jest": "^27.1.4",
|
|
60
|
+
"ts-node": "^10.7.0",
|
|
61
|
+
"tsconfig-paths": "^3.14.1",
|
|
62
|
+
"typedoc": "^0.22.15",
|
|
63
|
+
"typescript": "^4.6.3"
|
|
66
64
|
},
|
|
67
65
|
"jest": {
|
|
68
66
|
"moduleFileExtensions": [
|
|
@@ -81,5 +79,6 @@
|
|
|
81
79
|
"!**/test/**"
|
|
82
80
|
],
|
|
83
81
|
"coverageDirectory": "./coverage"
|
|
84
|
-
}
|
|
82
|
+
},
|
|
83
|
+
"packageManager": "yarn@3.2.0"
|
|
85
84
|
}
|
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
}
|
package/test/encryptor.spec.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
});
|
package/test/jwt.controller.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
}
|
package/test/mixed.controller.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
}
|
package/test/module.spec.ts
DELETED
|
@@ -1,535 +0,0 @@
|
|
|
1
|
-
import { NestApplication } from '@nestjs/core';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import { randomBytes } from 'crypto';
|
|
4
|
-
import session from 'express-session';
|
|
5
|
-
import { ExtractJwt } from 'passport-jwt';
|
|
6
|
-
import request from 'supertest';
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
AuthMemoryUserProvider,
|
|
10
|
-
AuthModule,
|
|
11
|
-
AuthService,
|
|
12
|
-
EmailPasswordStrategy,
|
|
13
|
-
Encryptor,
|
|
14
|
-
JwtStrategy,
|
|
15
|
-
MemoryUser,
|
|
16
|
-
MemoryUserProvider,
|
|
17
|
-
MODULE_OPTIONS_TOKEN
|
|
18
|
-
} from '../src';
|
|
19
|
-
import { EmailPaswordController } from './emailpassword.controller';
|
|
20
|
-
import { JwtController } from './jwt.controller';
|
|
21
|
-
import { MixedController } from './mixed.controller';
|
|
22
|
-
|
|
23
|
-
class User implements MemoryUser {
|
|
24
|
-
_id: string;
|
|
25
|
-
email: string;
|
|
26
|
-
passwordEncrypted: string;
|
|
27
|
-
namespace?: string;
|
|
28
|
-
authSerialize() {
|
|
29
|
-
return { _id: this._id, namespace: this.namespace ?? 'default' };
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const key = randomBytes(32).toString('hex');
|
|
34
|
-
const iv = randomBytes(16).toString('hex');
|
|
35
|
-
|
|
36
|
-
const userTest = new User();
|
|
37
|
-
userTest._id = '1';
|
|
38
|
-
userTest.email = 'userTest@nestjs-authentication.com';
|
|
39
|
-
const userTestPassword = 'password';
|
|
40
|
-
|
|
41
|
-
const userTest2 = new User();
|
|
42
|
-
userTest2._id = '1';
|
|
43
|
-
userTest2.email = 'userTest2@nestjs-authentication.com';
|
|
44
|
-
userTest2.namespace = 'user';
|
|
45
|
-
const userTest2Password = 'password2';
|
|
46
|
-
|
|
47
|
-
const jwtSecret = 'nestjs-authentication';
|
|
48
|
-
|
|
49
|
-
let app: NestApplication;
|
|
50
|
-
|
|
51
|
-
beforeAll(async () => {
|
|
52
|
-
const builder = await Test.createTestingModule({
|
|
53
|
-
imports: [
|
|
54
|
-
AuthModule.forRoot({
|
|
55
|
-
encrypt: { key, iv },
|
|
56
|
-
jwt: { secretOrKey: jwtSecret, jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken() }
|
|
57
|
-
}),
|
|
58
|
-
AuthModule.registerAuthProviders([
|
|
59
|
-
{
|
|
60
|
-
provide: 'AuthMemoryUserProvider_default',
|
|
61
|
-
inject: [Encryptor, AuthService],
|
|
62
|
-
useFactory: (encryptor: Encryptor) => {
|
|
63
|
-
const memoryUserProvider = new MemoryUserProvider<User>();
|
|
64
|
-
memoryUserProvider.registerUser(userTest);
|
|
65
|
-
return new AuthMemoryUserProvider<User>('default', memoryUserProvider, encryptor);
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
provide: 'AuthMemoryUserProvider_user',
|
|
70
|
-
inject: [Encryptor, AuthService],
|
|
71
|
-
useFactory: (encryptor: Encryptor) => {
|
|
72
|
-
const memoryUserProvider = new MemoryUserProvider<User>();
|
|
73
|
-
memoryUserProvider.registerUser(userTest2);
|
|
74
|
-
return new AuthMemoryUserProvider<User>('user', memoryUserProvider, encryptor);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
])
|
|
78
|
-
],
|
|
79
|
-
controllers: [EmailPaswordController, JwtController, MixedController]
|
|
80
|
-
}).compile();
|
|
81
|
-
|
|
82
|
-
app = builder.createNestApplication();
|
|
83
|
-
|
|
84
|
-
// init the session
|
|
85
|
-
app.use(
|
|
86
|
-
session({
|
|
87
|
-
secret: 'secret',
|
|
88
|
-
resave: false,
|
|
89
|
-
saveUninitialized: false,
|
|
90
|
-
cookie: {
|
|
91
|
-
maxAge: 100000,
|
|
92
|
-
httpOnly: false
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
await app.init();
|
|
98
|
-
|
|
99
|
-
userTest.passwordEncrypted = app.get(Encryptor).encrypt(userTestPassword);
|
|
100
|
-
userTest2.passwordEncrypted = app.get(Encryptor).encrypt(userTest2Password);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe('AuthModule', () => {
|
|
104
|
-
test('The AuthModule must create the options provider', async () => {
|
|
105
|
-
const options = app.get(MODULE_OPTIONS_TOKEN);
|
|
106
|
-
expect(options).toHaveProperty('encrypt.key');
|
|
107
|
-
expect(options).toHaveProperty('encrypt.iv');
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('The AuthModule must create the Encryptor', async () => {
|
|
111
|
-
const encryptor = app.get(Encryptor);
|
|
112
|
-
expect(encryptor).toBeInstanceOf(Encryptor);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('The AuthModule must create the AuthService', async () => {
|
|
116
|
-
const authService = app.get(AuthService);
|
|
117
|
-
expect(authService).toBeInstanceOf(AuthService);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test('The AuthModule must create the AuthMemoryUserProvider_default', async () => {
|
|
121
|
-
const memoryProvider = app.get('AuthMemoryUserProvider_default');
|
|
122
|
-
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('The AuthModule must create the AuthMemoryUserProvider_user', async () => {
|
|
126
|
-
const memoryProvider = app.get('AuthMemoryUserProvider_user');
|
|
127
|
-
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test('The AuthModule must create the EmailPasswordStrategy', async () => {
|
|
131
|
-
const strategy = app.get(EmailPasswordStrategy);
|
|
132
|
-
expect(strategy).toBeInstanceOf(EmailPasswordStrategy);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test('The AuthModule must create the JwtStrategy', async () => {
|
|
136
|
-
const strategy = app.get(JwtStrategy);
|
|
137
|
-
expect(strategy).toBeInstanceOf(JwtStrategy);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
describe('AuthService', () => {
|
|
142
|
-
test('The AuthService must have the providers registered', async () => {
|
|
143
|
-
const authService = app.get(AuthService);
|
|
144
|
-
|
|
145
|
-
const memoryProvider = authService.getAuthProvider('default');
|
|
146
|
-
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
147
|
-
|
|
148
|
-
const memoryProvider2 = authService.getAuthProvider('user');
|
|
149
|
-
expect(memoryProvider2).toBeInstanceOf(AuthMemoryUserProvider);
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe('E2E EmailPasswordStrategy', () => {
|
|
154
|
-
describe('Authenticated', () => {
|
|
155
|
-
test(`GET /emailpassword/authenticated return the user if cookie set, empty if not`, async () => {
|
|
156
|
-
const user1 = request.agent(app.getHttpServer());
|
|
157
|
-
await user1
|
|
158
|
-
.post('/emailpassword/login_check')
|
|
159
|
-
.set('accept', 'application/json')
|
|
160
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
161
|
-
.expect({ success: true });
|
|
162
|
-
await user1
|
|
163
|
-
.get('/emailpassword/authenticated')
|
|
164
|
-
.expect(200)
|
|
165
|
-
.expect((res) => {
|
|
166
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
167
|
-
});
|
|
168
|
-
await request(app.getHttpServer()).get('/emailpassword/authenticated').expect(200);
|
|
169
|
-
});
|
|
170
|
-
test(`GET /emailpassword/authenticated return the good user`, async () => {
|
|
171
|
-
const user1 = request.agent(app.getHttpServer());
|
|
172
|
-
await user1
|
|
173
|
-
.post('/emailpassword/login_check')
|
|
174
|
-
.set('accept', 'application/json')
|
|
175
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
176
|
-
.expect({ success: true });
|
|
177
|
-
await user1
|
|
178
|
-
.get('/emailpassword/authenticated')
|
|
179
|
-
.expect(200)
|
|
180
|
-
.expect((res) => {
|
|
181
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
const user2 = request.agent(app.getHttpServer());
|
|
185
|
-
await user2
|
|
186
|
-
.post('/emailpassword/login_check')
|
|
187
|
-
.set('accept', 'application/json')
|
|
188
|
-
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
189
|
-
.expect({ success: true });
|
|
190
|
-
await user2
|
|
191
|
-
.get('/emailpassword/authenticated')
|
|
192
|
-
.expect(200)
|
|
193
|
-
.expect((res) => {
|
|
194
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
195
|
-
});
|
|
196
|
-
await request(app.getHttpServer()).get('/emailpassword/authenticated').expect(200);
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
describe('Protected', () => {
|
|
201
|
-
test(`GET /emailpassword/protected return the user if cookie set, throw 403 if not`, async () => {
|
|
202
|
-
const user1 = request.agent(app.getHttpServer());
|
|
203
|
-
await user1
|
|
204
|
-
.post('/emailpassword/login_check')
|
|
205
|
-
.set('accept', 'application/json')
|
|
206
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
207
|
-
.expect({ success: true });
|
|
208
|
-
await user1
|
|
209
|
-
.get('/emailpassword/protected')
|
|
210
|
-
.expect(200)
|
|
211
|
-
.expect((res) => {
|
|
212
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
213
|
-
});
|
|
214
|
-
await request(app.getHttpServer()).get('/emailpassword/protected').expect(403);
|
|
215
|
-
});
|
|
216
|
-
test(`GET /emailpassword/protected return the good user`, async () => {
|
|
217
|
-
const user1 = request.agent(app.getHttpServer());
|
|
218
|
-
await user1
|
|
219
|
-
.post('/emailpassword/login_check')
|
|
220
|
-
.set('accept', 'application/json')
|
|
221
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
222
|
-
.expect({ success: true });
|
|
223
|
-
await user1
|
|
224
|
-
.get('/emailpassword/protected')
|
|
225
|
-
.expect(200)
|
|
226
|
-
.expect((res) => {
|
|
227
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
228
|
-
});
|
|
229
|
-
const user2 = request.agent(app.getHttpServer());
|
|
230
|
-
await user2
|
|
231
|
-
.post('/emailpassword/login_check')
|
|
232
|
-
.set('accept', 'application/json')
|
|
233
|
-
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
234
|
-
.expect({ success: true });
|
|
235
|
-
await user2
|
|
236
|
-
.get('/emailpassword/protected')
|
|
237
|
-
.expect(200)
|
|
238
|
-
.expect((res) => {
|
|
239
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
240
|
-
});
|
|
241
|
-
await request(app.getHttpServer()).get('/emailpassword/protected').expect(403);
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
describe('Session', () => {
|
|
246
|
-
test(`GET /emailpassword/nosession return empty user if cookie set`, async () => {
|
|
247
|
-
const user1 = request.agent(app.getHttpServer());
|
|
248
|
-
await user1
|
|
249
|
-
.post('/emailpassword/login_check')
|
|
250
|
-
.set('accept', 'application/json')
|
|
251
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
252
|
-
.expect({ success: true });
|
|
253
|
-
|
|
254
|
-
await user1
|
|
255
|
-
.get('/emailpassword/session')
|
|
256
|
-
.expect(200)
|
|
257
|
-
.expect((res) => {
|
|
258
|
-
expect(res.body).not.toHaveProperty('user');
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
await request(app.getHttpServer())
|
|
262
|
-
.get('/emailpassword/session')
|
|
263
|
-
.expect((res) => expect(res.body).not.toHaveProperty('user'));
|
|
264
|
-
});
|
|
265
|
-
test(`GET /emailpassword/protected return the good user`, async () => {
|
|
266
|
-
const user1 = request.agent(app.getHttpServer());
|
|
267
|
-
await user1
|
|
268
|
-
.post('/emailpassword/login_check')
|
|
269
|
-
.set('accept', 'application/json')
|
|
270
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
271
|
-
.expect({ success: true });
|
|
272
|
-
await user1
|
|
273
|
-
.get('/emailpassword/session')
|
|
274
|
-
.expect(200)
|
|
275
|
-
.expect((res) => {
|
|
276
|
-
expect(res.body).not.toHaveProperty('user');
|
|
277
|
-
});
|
|
278
|
-
const user2 = request.agent(app.getHttpServer());
|
|
279
|
-
await user2
|
|
280
|
-
.post('/emailpassword/login_check')
|
|
281
|
-
.set('accept', 'application/json')
|
|
282
|
-
.send({ email: userTest2.email, password: userTest2Password, namespace: 'user' })
|
|
283
|
-
.expect({ success: true });
|
|
284
|
-
await user2
|
|
285
|
-
.get('/emailpassword/session')
|
|
286
|
-
.expect(200)
|
|
287
|
-
.expect((res) => {
|
|
288
|
-
expect(res.body).not.toHaveProperty('user');
|
|
289
|
-
});
|
|
290
|
-
await request(app.getHttpServer()).get('/emailpassword/session').expect(200);
|
|
291
|
-
});
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
describe('Logout', () => {
|
|
295
|
-
test(`GET /emailpassword/logout and logout the user`, async () => {
|
|
296
|
-
const user1 = request.agent(app.getHttpServer());
|
|
297
|
-
await user1
|
|
298
|
-
.post('/emailpassword/login_check')
|
|
299
|
-
.set('accept', 'application/json')
|
|
300
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
301
|
-
.expect({ success: true });
|
|
302
|
-
await user1
|
|
303
|
-
.get('/emailpassword/authenticated')
|
|
304
|
-
.expect(200)
|
|
305
|
-
.expect((res) => {
|
|
306
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
307
|
-
});
|
|
308
|
-
await user1.post('/emailpassword/logout').expect(200);
|
|
309
|
-
|
|
310
|
-
await user1.post('/emailpassword/authenticated').expect((res) => {
|
|
311
|
-
expect(res.body).not.toHaveProperty('user');
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
describe('E2E JWTStrategy', () => {
|
|
318
|
-
test(`Generate a jwt`, () => {
|
|
319
|
-
const strategy = app.get(JwtStrategy);
|
|
320
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
321
|
-
expect(typeof token).toBe('string');
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
describe('Authenticated', () => {
|
|
325
|
-
test(`GET /jwt/authenticated return the user if token set, empty if not`, async () => {
|
|
326
|
-
const strategy = app.get(JwtStrategy);
|
|
327
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
328
|
-
const user1 = request.agent(app.getHttpServer());
|
|
329
|
-
await user1
|
|
330
|
-
.get('/jwt/authenticated')
|
|
331
|
-
.set('Authorization', `Bearer ${token}`)
|
|
332
|
-
.expect(200)
|
|
333
|
-
.expect((res) => {
|
|
334
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
335
|
-
});
|
|
336
|
-
await user1
|
|
337
|
-
.get('/jwt/authenticated')
|
|
338
|
-
.expect(200)
|
|
339
|
-
.expect((res) => {
|
|
340
|
-
expect(res.body).not.toHaveProperty('user');
|
|
341
|
-
});
|
|
342
|
-
await user1.get('/jwt/authenticated').expect(200);
|
|
343
|
-
await request(app.getHttpServer()).get('/jwt/authenticated').expect(200);
|
|
344
|
-
});
|
|
345
|
-
test(`GET /jwt/authenticated return the good user`, async () => {
|
|
346
|
-
const strategy = app.get(JwtStrategy);
|
|
347
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
348
|
-
const user1 = request.agent(app.getHttpServer());
|
|
349
|
-
await user1
|
|
350
|
-
.get('/jwt/authenticated')
|
|
351
|
-
.set('Authorization', `Bearer ${token}`)
|
|
352
|
-
.expect(200)
|
|
353
|
-
.expect((res) => {
|
|
354
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
const token2 = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
358
|
-
await user1
|
|
359
|
-
.get('/jwt/authenticated')
|
|
360
|
-
.set('Authorization', `Bearer ${token2}`)
|
|
361
|
-
.expect(200)
|
|
362
|
-
.expect((res) => {
|
|
363
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
364
|
-
});
|
|
365
|
-
await user1.get('/jwt/authenticated').expect(200);
|
|
366
|
-
});
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
describe('Protected', () => {
|
|
370
|
-
test(`GET /jwt/protected return the user if token set, throw if not`, async () => {
|
|
371
|
-
const strategy = app.get(JwtStrategy);
|
|
372
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
373
|
-
const user1 = request.agent(app.getHttpServer());
|
|
374
|
-
await user1
|
|
375
|
-
.get('/jwt/protected')
|
|
376
|
-
.set('Authorization', `Bearer ${token}`)
|
|
377
|
-
.expect(200)
|
|
378
|
-
.expect((res) => {
|
|
379
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
380
|
-
});
|
|
381
|
-
await user1.get('/jwt/protected').expect(403);
|
|
382
|
-
await request(app.getHttpServer()).get('/jwt/protected').expect(403);
|
|
383
|
-
});
|
|
384
|
-
test(`GET /jwt/protected return the good user`, async () => {
|
|
385
|
-
const strategy = app.get(JwtStrategy);
|
|
386
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
387
|
-
const user1 = request.agent(app.getHttpServer());
|
|
388
|
-
await user1
|
|
389
|
-
.get('/jwt/protected')
|
|
390
|
-
.set('Authorization', `Bearer ${token}`)
|
|
391
|
-
.expect(200)
|
|
392
|
-
.expect((res) => {
|
|
393
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
394
|
-
});
|
|
395
|
-
const token2 = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
396
|
-
await user1
|
|
397
|
-
.get('/jwt/protected')
|
|
398
|
-
.set('Authorization', `Bearer ${token2}`)
|
|
399
|
-
.expect(200)
|
|
400
|
-
.expect((res) => {
|
|
401
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
402
|
-
});
|
|
403
|
-
await user1.get('/jwt/protected').expect(403);
|
|
404
|
-
});
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
describe('Session', () => {
|
|
408
|
-
test(`GET /jwt/session return the user if token set, throw if not`, async () => {
|
|
409
|
-
const strategy = app.get(JwtStrategy);
|
|
410
|
-
const token = strategy.sign({ _id: userTest._id }, jwtSecret);
|
|
411
|
-
|
|
412
|
-
// this request must login the user and persist it in the session for next call
|
|
413
|
-
const user1 = request.agent(app.getHttpServer());
|
|
414
|
-
await user1
|
|
415
|
-
.get('/jwt/protected')
|
|
416
|
-
.set('Authorization', `Bearer ${token}`)
|
|
417
|
-
.expect(200)
|
|
418
|
-
.expect((res) => {
|
|
419
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
// this request must get the user from session, even if no token is provided
|
|
423
|
-
await user1
|
|
424
|
-
.get('/jwt/session')
|
|
425
|
-
.expect(200)
|
|
426
|
-
.expect((res) => {
|
|
427
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
await request(app.getHttpServer()).get('/jwt/session').expect(403);
|
|
431
|
-
});
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
describe('E2E MixedStrategy (emailpassword and jwt)', () => {
|
|
436
|
-
let user1: request.SuperAgentTest;
|
|
437
|
-
beforeAll(async () => {
|
|
438
|
-
user1 = request.agent(app.getHttpServer());
|
|
439
|
-
await user1
|
|
440
|
-
.post('/emailpassword/login_check')
|
|
441
|
-
.set('accept', 'application/json')
|
|
442
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
443
|
-
.expect({ success: true });
|
|
444
|
-
});
|
|
445
|
-
|
|
446
|
-
describe('Authenticated', () => {
|
|
447
|
-
test(`GET /mixed/authenticated return the user from session`, async () => {
|
|
448
|
-
await user1
|
|
449
|
-
.get('/mixed/authenticated')
|
|
450
|
-
.expect(200)
|
|
451
|
-
.expect((res) => {
|
|
452
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
test(`GET /mixed/authenticated return the user from jwt token`, async () => {
|
|
456
|
-
const strategy = app.get(JwtStrategy);
|
|
457
|
-
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
458
|
-
await request
|
|
459
|
-
.agent(app.getHttpServer())
|
|
460
|
-
.get('/mixed/authenticated')
|
|
461
|
-
.set('Authorization', `Bearer ${token}`)
|
|
462
|
-
.expect(200)
|
|
463
|
-
.expect((res) => {
|
|
464
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
465
|
-
});
|
|
466
|
-
});
|
|
467
|
-
test(`GET /mixed/authenticated do not return the user (no cookie, no token)`, async () => {
|
|
468
|
-
await request
|
|
469
|
-
.agent(app.getHttpServer())
|
|
470
|
-
.get('/mixed/authenticated')
|
|
471
|
-
.expect(200)
|
|
472
|
-
.expect((res) => {
|
|
473
|
-
expect(res.body).not.toHaveProperty('user');
|
|
474
|
-
});
|
|
475
|
-
});
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
describe('Protected', () => {
|
|
479
|
-
test(`GET /mixed/protected return the user from session`, async () => {
|
|
480
|
-
await user1
|
|
481
|
-
.get('/mixed/protected')
|
|
482
|
-
.expect(200)
|
|
483
|
-
.expect((res) => {
|
|
484
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
485
|
-
});
|
|
486
|
-
});
|
|
487
|
-
test(`GET /mixed/protected return the user from jwt token`, async () => {
|
|
488
|
-
const strategy = app.get(JwtStrategy);
|
|
489
|
-
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
490
|
-
await request
|
|
491
|
-
.agent(app.getHttpServer())
|
|
492
|
-
.get('/mixed/authenticated')
|
|
493
|
-
.set('Authorization', `Bearer ${token}`)
|
|
494
|
-
.expect(200)
|
|
495
|
-
.expect((res) => {
|
|
496
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
test(`GET /mixed/protected throw (no cookie, no token)`, async () => {
|
|
500
|
-
await request.agent(app.getHttpServer()).get('/mixed/protected').expect(403);
|
|
501
|
-
});
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
describe('Session', () => {
|
|
505
|
-
test(`GET /mixed/session do not return the user from session (user is logged in)`, async () => {
|
|
506
|
-
await user1
|
|
507
|
-
.get('/mixed/session')
|
|
508
|
-
.expect(200)
|
|
509
|
-
.expect((res) => {
|
|
510
|
-
expect(res.body).not.toHaveProperty('user');
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
test(`GET /mixed/session return the user from jwt token`, async () => {
|
|
514
|
-
const strategy = app.get(JwtStrategy);
|
|
515
|
-
const token = strategy.sign({ _id: userTest2._id, namespace: 'user' }, jwtSecret);
|
|
516
|
-
await request
|
|
517
|
-
.agent(app.getHttpServer())
|
|
518
|
-
.get('/mixed/session')
|
|
519
|
-
.set('Authorization', `Bearer ${token}`)
|
|
520
|
-
.expect(200)
|
|
521
|
-
.expect((res) => {
|
|
522
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
test(`GET /mixed/session return empty`, async () => {
|
|
526
|
-
await request
|
|
527
|
-
.agent(app.getHttpServer())
|
|
528
|
-
.get('/mixed/session')
|
|
529
|
-
.expect(200)
|
|
530
|
-
.expect((res) => {
|
|
531
|
-
expect(res.body).not.toHaveProperty('user');
|
|
532
|
-
});
|
|
533
|
-
});
|
|
534
|
-
});
|
|
535
|
-
});
|