nestjs-authentication 0.3.0 → 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.
- package/dist/guard.d.ts +13 -9
- package/dist/guard.d.ts.map +1 -1
- package/dist/guard.js +27 -8
- package/dist/guard.js.map +1 -1
- package/dist/interfaces/auth.user.d.ts +3 -1
- package/dist/interfaces/auth.user.d.ts.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/dist/providers/auth.memory.user.provider.d.ts +0 -2
- package/dist/providers/auth.memory.user.provider.d.ts.map +1 -1
- package/dist/providers/auth.memory.user.provider.js +0 -18
- package/dist/providers/auth.memory.user.provider.js.map +1 -1
- package/dist/providers/auth.provider.d.ts.map +1 -1
- package/dist/providers/auth.provider.js +1 -5
- package/dist/providers/auth.provider.js.map +1 -1
- package/dist/strategies/emailpassword.d.ts.map +1 -1
- package/dist/strategies/emailpassword.js +1 -2
- package/dist/strategies/emailpassword.js.map +1 -1
- package/package.json +1 -1
- package/test/emailpassword.controller.ts +69 -0
- package/test/jwt.controller.ts +38 -0
- package/test/mixed.controller.ts +41 -0
- package/test/module.spec.ts +368 -94
- package/test/controller.ts +0 -43
package/dist/guard.d.ts
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
+
/// <reference types="qs" />
|
|
1
2
|
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { Request } from 'express';
|
|
3
|
+
import { Request, Response } from 'express';
|
|
3
4
|
import { AuthenticateOptions } from 'passport';
|
|
4
|
-
export
|
|
5
|
-
|
|
5
|
+
export interface AuthGuardOptions {
|
|
6
|
+
fromSession?: boolean;
|
|
7
|
+
required?: boolean;
|
|
8
|
+
strategies: {
|
|
6
9
|
[strategyName: string]: AuthenticateOptions;
|
|
7
10
|
};
|
|
8
|
-
|
|
11
|
+
}
|
|
12
|
+
export declare class AuthGuard implements CanActivate {
|
|
13
|
+
protected readonly options: AuthGuardOptions;
|
|
9
14
|
static scopes: Map<any, any>;
|
|
10
15
|
static registerScopes(_scopes: AuthenticateOptions['scope']): void;
|
|
11
|
-
constructor(
|
|
12
|
-
[strategyName: string]: AuthenticateOptions;
|
|
13
|
-
}, isAuthRequired?: boolean | undefined);
|
|
16
|
+
constructor(options: AuthGuardOptions);
|
|
14
17
|
getRequestReponse(context: ExecutionContext): {
|
|
15
|
-
request: any
|
|
16
|
-
response: any
|
|
18
|
+
request: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
19
|
+
response: Response<any, Record<string, any>>;
|
|
17
20
|
next: any;
|
|
18
21
|
};
|
|
22
|
+
authenticateFromSession(request: Request, response: Response): Promise<boolean>;
|
|
19
23
|
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
20
24
|
login(user: Record<string, unknown>, options: AuthenticateOptions, request: Request): Promise<Record<string, unknown>>;
|
|
21
25
|
}
|
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,
|
|
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;IAYxD,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
|
@@ -13,14 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.AuthGuard = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
16
17
|
const passport_1 = __importDefault(require("passport"));
|
|
17
18
|
class AuthGuard {
|
|
18
|
-
constructor(
|
|
19
|
+
constructor(options) {
|
|
19
20
|
var _a;
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
const options = this.strategies[strategy];
|
|
21
|
+
this.options = options;
|
|
22
|
+
for (const strategy in this.options.strategies) {
|
|
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
25
|
AuthGuard.registerScopes(options === null || options === void 0 ? void 0 : options.scope);
|
|
26
26
|
}
|
|
@@ -54,13 +54,32 @@ class AuthGuard {
|
|
|
54
54
|
throw err;
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
+
else {
|
|
58
|
+
throw new common_1.NotImplementedException(`Auth Context ${context.getType()} not implemented`);
|
|
59
|
+
}
|
|
57
60
|
return { request, response, next };
|
|
58
61
|
}
|
|
62
|
+
authenticateFromSession(request, response) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
return yield new Promise((resolve, reject) => passport_1.default.session()(request, response, () => {
|
|
65
|
+
if (request.isAuthenticated()) {
|
|
66
|
+
return resolve(true);
|
|
67
|
+
}
|
|
68
|
+
resolve(false);
|
|
69
|
+
}));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
59
72
|
canActivate(context) {
|
|
60
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
61
74
|
const { request, response, next } = this.getRequestReponse(context);
|
|
62
|
-
|
|
63
|
-
const
|
|
75
|
+
if (this.options.fromSession) {
|
|
76
|
+
const isAuthenticatedFromSession = yield this.authenticateFromSession(request, response);
|
|
77
|
+
if (isAuthenticatedFromSession) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
for (const strategy in this.options.strategies) {
|
|
82
|
+
const options = this.options.strategies[strategy];
|
|
64
83
|
const success = yield new Promise((resolve, reject) => passport_1.default.authenticate(strategy, options !== null && options !== void 0 ? options : {}, (err, user, authInfo) => __awaiter(this, void 0, void 0, function* () {
|
|
65
84
|
if (request.authInfo === undefined) {
|
|
66
85
|
request.authInfo = [];
|
|
@@ -84,7 +103,7 @@ class AuthGuard {
|
|
|
84
103
|
return success;
|
|
85
104
|
}
|
|
86
105
|
}
|
|
87
|
-
return this.
|
|
106
|
+
return this.options.required ? request.isAuthenticated() : true;
|
|
88
107
|
});
|
|
89
108
|
}
|
|
90
109
|
login(user, options, request) {
|
package/dist/guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":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OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;QACpE,CAAC;KAAA;IAEK,KAAK,CACP,IAA6B,EAC7B,OAA4B,EAC5B,OAAgB;;YAEhB,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACzC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAQ,EAAE,EAAE;oBACtC,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI,EAAE;wBAC/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;;AAvHL,8BAwHC;AApHU,gBAAM,GAAG,IAAI,GAAG,EAAE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.user.d.ts","sourceRoot":"","sources":["../../src/interfaces/auth.user.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACtB,aAAa,IAAI,
|
|
1
|
+
{"version":3,"file":"auth.user.d.ts","sourceRoot":"","sources":["../../src/interfaces/auth.user.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,SAAS;IACtB,aAAa,IAAI;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,GAAG;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACpE"}
|
package/dist/module.js
CHANGED
|
@@ -21,7 +21,7 @@ const jwt_1 = require("./strategies/jwt");
|
|
|
21
21
|
const user_serializer_1 = require("./user.serializer");
|
|
22
22
|
let AuthModule = AuthModule_1 = class AuthModule {
|
|
23
23
|
configure(consumer) {
|
|
24
|
-
consumer.apply(passport_1.default.initialize()
|
|
24
|
+
consumer.apply(passport_1.default.initialize()).forRoutes('*');
|
|
25
25
|
}
|
|
26
26
|
static createOptionsProvider(options) {
|
|
27
27
|
return {
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAyG;AACzG,wDAAgC;AAEhC,2CAA6E;AAC7E,2CAAwC;AAExC,uCAAwC;AACxC,8DAAmE;AACnE,0CAA+C;AAC/C,uDAAmD;AAInD,IAAa,UAAU,kBAAvB,MAAa,UAAU;IACnB,SAAS,CAAC,QAA4B;QAClC,QAAQ,CAAC,KAAK,CAAC,kBAAQ,CAAC,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAAyG;AACzG,wDAAgC;AAEhC,2CAA6E;AAC7E,2CAAwC;AAExC,uCAAwC;AACxC,8DAAmE;AACnE,0CAA+C;AAC/C,uDAAmD;AAInD,IAAa,UAAU,kBAAvB,MAAa,UAAU;IACnB,SAAS,CAAC,QAA4B;QAClC,QAAQ,CAAC,KAAK,CAAC,kBAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACzD,CAAC;IAES,MAAM,CAAC,qBAAqB,CAAC,OAA2B;QAC9D,OAAO;YACH,OAAO,EAAE,gCAAoB;YAC7B,QAAQ,EAAE,OAAO;SACpB,CAAC;IACN,CAAC;IACS,MAAM,CAAC,0BAA0B,CACvC,OAAgC;QAEhC,uBACI,OAAO,EAAE,gCAAoB,IAC1B,OAAO,EACZ;IACN,CAAC;IAES,MAAM,CAAC,yBAAyB;QACtC,OAAO;YACH,OAAO,EAAE,qBAAW;YACpB,QAAQ,EAAE,qBAAW;SACxB,CAAC;IACN,CAAC;IAES,MAAM,CAAC,uBAAuB;QACpC,OAAO;YACH,OAAO,EAAE,qBAAS;Y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CAAC,OAAO,CAAC,OAA2B;QACtC,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAAgC;QAChD,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,CAAC,qBAAqB,CAAC,SAAqB;QAC9C,MAAM,gBAAgB,GAAa;YAC/B,OAAO,EAAE,oCAAwB;YACjC,MAAM,EAAE;gBACJ,qBAAW;gBACX,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;oBACnB,IAAK,CAAS,CAAC,OAAO,KAAK,SAAS,EAAE;wBAClC,OAAQ,CAAS,CAAC,OAAO,CAAC;qBAC7B;yBAAM;wBACH,OAAO,CAAC,CAAC;qBACZ;gBACL,CAAC,CAAC;aACL;YACD,UAAU,EAAE,CAAC,WAAwB,EAAE,GAAG,SAAc,EAAE,EAAE;gBACxD,KAAK,MAAM,CAAC,IAAI,SAAS,EAAE;oBACvB,WAAW,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC;iBACvC;YACL,CAAC;SACJ,CAAC;QACF,OAAO;YACH,MAAM,EAAE,YAAU;YAClB,SAAS,EAAE,CAAC,GAAG,SAAS,EAAE,gBAAgB,CAAC;SAC9C,CAAC;IACN,CAAC;CACJ,CAAA;AA9GY,UAAU;IAFtB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,UAAU,CA8GtB;AA9GY,gCAAU"}
|
|
@@ -5,8 +5,6 @@ import { AuthProvider } from './auth.provider';
|
|
|
5
5
|
export declare class AuthMemoryUserProvider<User extends MemoryUser> extends AuthProvider<User> {
|
|
6
6
|
protected readonly encryptor: IEncryptor;
|
|
7
7
|
constructor(providerName: string, userProvider: IUserProvider<User>, encryptor: IEncryptor);
|
|
8
|
-
getName(): string;
|
|
9
|
-
loadUser(data: Partial<User>): Promise<User | undefined>;
|
|
10
8
|
protected userChecker(data: Partial<User> & {
|
|
11
9
|
password?: string;
|
|
12
10
|
}, user?: User): User | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.memory.user.provider.d.ts","sourceRoot":"","sources":["../../src/providers/auth.memory.user.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,qBAAa,sBAAsB,CAAC,IAAI,SAAS,UAAU,CAAE,SAAQ,YAAY,CAAC,IAAI,CAAC;IACd,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU;gBAAjG,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,CAAC,IAAI,CAAC,EAAqB,SAAS,EAAE,UAAU;IAI7G,
|
|
1
|
+
{"version":3,"file":"auth.memory.user.provider.d.ts","sourceRoot":"","sources":["../../src/providers/auth.memory.user.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,qBAAa,sBAAsB,CAAC,IAAI,SAAS,UAAU,CAAE,SAAQ,YAAY,CAAC,IAAI,CAAC;IACd,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU;gBAAjG,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,CAAC,IAAI,CAAC,EAAqB,SAAS,EAAE,UAAU;IAI7G,SAAS,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,SAAS;IAajG,SAAS,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAMlG"}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.AuthMemoryUserProvider = void 0;
|
|
13
4
|
const auth_provider_1 = require("./auth.provider");
|
|
@@ -16,15 +7,6 @@ class AuthMemoryUserProvider extends auth_provider_1.AuthProvider {
|
|
|
16
7
|
super(providerName, userProvider);
|
|
17
8
|
this.encryptor = encryptor;
|
|
18
9
|
}
|
|
19
|
-
getName() {
|
|
20
|
-
return this.providerName;
|
|
21
|
-
}
|
|
22
|
-
loadUser(data) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const user = yield this.userProvider.findOne(this.createUserQuery(data));
|
|
25
|
-
return this.userChecker(data, user);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
10
|
userChecker(data, user) {
|
|
29
11
|
if (user === undefined) {
|
|
30
12
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.memory.user.provider.js","sourceRoot":"","sources":["../../src/providers/auth.memory.user.provider.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.memory.user.provider.js","sourceRoot":"","sources":["../../src/providers/auth.memory.user.provider.ts"],"names":[],"mappings":";;;AAGA,mDAA+C;AAE/C,MAAa,sBAAgD,SAAQ,4BAAkB;IACnF,YAAY,YAAoB,EAAE,YAAiC,EAAqB,SAAqB;QACzG,KAAK,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;QADkD,cAAS,GAAT,SAAS,CAAY;IAE7G,CAAC;IAES,WAAW,CAAC,IAA2C,EAAE,IAAW;QAC1E,IAAI,IAAI,KAAK,SAAS,EAAE;YACpB,OAAO;SACV;QACD,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE;YACnC,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACxD,IAAI,SAAS,KAAK,IAAI,CAAC,iBAAiB,EAAE;gBACtC,OAAO;aACV;SACJ;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IAES,eAAe,CAAC,IAA2C;QACjE,IAAI,IAAI,CAAC,GAAG,KAAK,SAAS,EAAE;YACxB,OAAO,IAAI,CAAC;SACf;QACD,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;IAC1D,CAAC;CACJ;AAxBD,wDAwBC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.provider.d.ts","sourceRoot":"","sources":["../../src/providers/auth.provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,wBAAwB,CA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,CAAC,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,SAAS;IAElF,SAAS,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAE1E,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;CAIjE"}
|
|
@@ -23,11 +23,7 @@ class AuthProvider {
|
|
|
23
23
|
}
|
|
24
24
|
loadUser(data) {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const
|
|
27
|
-
const user = yield this.userProvider.findOne(query);
|
|
28
|
-
if (user === undefined) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
26
|
+
const user = yield this.userProvider.findOne(this.createUserQuery(data));
|
|
31
27
|
return this.userChecker(data, user);
|
|
32
28
|
});
|
|
33
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.provider.js","sourceRoot":"","sources":["../../src/providers/auth.provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,MAAsB,YAAY;IAC9B,YAA+B,YAAoB,EAAqB,YAAiC;QAA1E,iBAAY,GAAZ,YAAY,CAAQ;QAAqB,iBAAY,GAAZ,YAAY,CAAqB;IAAG,CAAC;IAE7G,OAAO;QACH,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED,WAAW;QACP,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAMK,QAAQ,CAAC,IAAmB;;YAC9B,MAAM,
|
|
1
|
+
{"version":3,"file":"auth.provider.js","sourceRoot":"","sources":["../../src/providers/auth.provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,MAAsB,YAAY;IAC9B,YAA+B,YAAoB,EAAqB,YAAiC;QAA1E,iBAAY,GAAZ,YAAY,CAAQ;QAAqB,iBAAY,GAAZ,YAAY,CAAqB;IAAG,CAAC;IAE7G,OAAO;QACH,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED,WAAW;QACP,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAMK,QAAQ,CAAC,IAAmB;;YAC9B,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;YACzE,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACxC,CAAC;KAAA;CACJ;AAnBD,oCAmBC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emailpassword.d.ts","sourceRoot":"","sources":["../../src/strategies/emailpassword.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;;AAEzC,qBACa,qBAAqB,CAAC,IAAI,GAAG,GAAG,CAAE,SAAQ,
|
|
1
|
+
{"version":3,"file":"emailpassword.d.ts","sourceRoot":"","sources":["../../src/strategies/emailpassword.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;;AAEzC,qBACa,qBAAqB,CAAC,IAAI,GAAG,GAAG,CAAE,SAAQ,0BAA2C;IAClF,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,WAAW;gBAAxB,WAAW,EAAE,WAAW;IAQvD,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO;IAI7B,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,IAAI,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CASxG"}
|
|
@@ -23,12 +23,11 @@ const common_1 = require("@nestjs/common");
|
|
|
23
23
|
const passport_1 = require("@nestjs/passport");
|
|
24
24
|
const passport_local_1 = require("passport-local");
|
|
25
25
|
const service_1 = require("../service");
|
|
26
|
-
let EmailPasswordStrategy = class EmailPasswordStrategy extends (0, passport_1.PassportStrategy)(passport_local_1.Strategy, '
|
|
26
|
+
let EmailPasswordStrategy = class EmailPasswordStrategy extends (0, passport_1.PassportStrategy)(passport_local_1.Strategy, 'emailpassword') {
|
|
27
27
|
constructor(authService) {
|
|
28
28
|
super({
|
|
29
29
|
usernameField: 'email',
|
|
30
30
|
passwordField: 'password',
|
|
31
|
-
session: true,
|
|
32
31
|
passReqToCallback: true
|
|
33
32
|
});
|
|
34
33
|
this.authService = authService;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emailpassword.js","sourceRoot":"","sources":["../../src/strategies/emailpassword.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+CAAoD;AAEpD,mDAA0C;AAE1C,wCAAyC;AAGzC,IAAa,qBAAqB,GAAlC,MAAa,qBAAkC,SAAQ,IAAA,2BAAgB,EAAC,yBAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"emailpassword.js","sourceRoot":"","sources":["../../src/strategies/emailpassword.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAC5C,+CAAoD;AAEpD,mDAA0C;AAE1C,wCAAyC;AAGzC,IAAa,qBAAqB,GAAlC,MAAa,qBAAkC,SAAQ,IAAA,2BAAgB,EAAC,yBAAQ,EAAE,eAAe,CAAC;IAC9F,YAA+B,WAAwB;QACnD,KAAK,CAAC;YACF,aAAa,EAAE,OAAO;YACtB,aAAa,EAAE,UAAU;YACzB,iBAAiB,EAAE,IAAI;SAC1B,CAAC,CAAC;QALwB,gBAAW,GAAX,WAAW,CAAa;IAMvD,CAAC;IAES,YAAY,CAAC,GAAY;;QAC/B,OAAO,MAAA,MAAA,GAAG,CAAC,KAAK,CAAC,SAAS,mCAAI,GAAG,CAAC,IAAI,CAAC,SAAS,mCAAI,SAAS,CAAC;IAClE,CAAC;IAEK,QAAQ,CAAC,GAAY,EAAE,KAAa,EAAE,QAAgB;;YACxD,MAAM,KAAK,GAA2D;gBAClE,KAAK;gBACL,QAAQ;gBACR,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC;aACpC,CAAC;YACF,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YACpD,OAAO,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;QAClD,CAAC;KAAA;CACJ,CAAA;AAtBY,qBAAqB;IADjC,IAAA,mBAAU,GAAE;qCAEmC,qBAAW;GAD9C,qBAAqB,CAsBjC;AAtBY,sDAAqB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nestjs-authentication",
|
|
3
|
-
"version": "0.
|
|
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",
|
|
@@ -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,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
|
+
}
|
package/test/module.spec.ts
CHANGED
|
@@ -16,14 +16,16 @@ import {
|
|
|
16
16
|
MemoryUserProvider,
|
|
17
17
|
MODULE_OPTIONS_TOKEN
|
|
18
18
|
} from '../src';
|
|
19
|
-
import {
|
|
19
|
+
import { EmailPaswordController } from './emailpassword.controller';
|
|
20
|
+
import { JwtController } from './jwt.controller';
|
|
21
|
+
import { MixedController } from './mixed.controller';
|
|
20
22
|
|
|
21
23
|
class User implements MemoryUser {
|
|
22
24
|
_id: string;
|
|
23
25
|
email: string;
|
|
24
26
|
passwordEncrypted: string;
|
|
25
27
|
namespace?: string;
|
|
26
|
-
authSerialize()
|
|
28
|
+
authSerialize() {
|
|
27
29
|
return { _id: this._id, namespace: this.namespace ?? 'default' };
|
|
28
30
|
}
|
|
29
31
|
}
|
|
@@ -74,7 +76,7 @@ beforeAll(async () => {
|
|
|
74
76
|
}
|
|
75
77
|
])
|
|
76
78
|
],
|
|
77
|
-
controllers: [
|
|
79
|
+
controllers: [EmailPaswordController, JwtController, MixedController]
|
|
78
80
|
}).compile();
|
|
79
81
|
|
|
80
82
|
app = builder.createNestApplication();
|
|
@@ -137,70 +139,178 @@ describe('AuthModule', () => {
|
|
|
137
139
|
});
|
|
138
140
|
|
|
139
141
|
describe('AuthService', () => {
|
|
140
|
-
test('The AuthService must have the
|
|
142
|
+
test('The AuthService must have the providers registered', async () => {
|
|
141
143
|
const authService = app.get(AuthService);
|
|
144
|
+
|
|
142
145
|
const memoryProvider = authService.getAuthProvider('default');
|
|
143
146
|
expect(memoryProvider).toBeInstanceOf(AuthMemoryUserProvider);
|
|
147
|
+
|
|
148
|
+
const memoryProvider2 = authService.getAuthProvider('user');
|
|
149
|
+
expect(memoryProvider2).toBeInstanceOf(AuthMemoryUserProvider);
|
|
144
150
|
});
|
|
145
151
|
});
|
|
146
152
|
|
|
147
153
|
describe('E2E EmailPasswordStrategy', () => {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
+
});
|
|
153
198
|
});
|
|
154
|
-
test(`POST /test/login_check and log in user and test authenticated/protected`, async () => {
|
|
155
|
-
const user1 = request.agent(app.getHttpServer());
|
|
156
|
-
await user1
|
|
157
|
-
.post('/test/login_check')
|
|
158
|
-
.set('accept', 'application/json')
|
|
159
|
-
.send({ email: userTest.email, password: userTestPassword })
|
|
160
|
-
.expect({ success: true });
|
|
161
|
-
await user1
|
|
162
|
-
.get('/test/authenticated')
|
|
163
|
-
.expect(200)
|
|
164
|
-
.expect((res) => {
|
|
165
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
166
|
-
});
|
|
167
|
-
await user1
|
|
168
|
-
.get('/test/protected')
|
|
169
|
-
.expect(200)
|
|
170
|
-
.expect((res) => {
|
|
171
|
-
expect(res.body).toHaveProperty('user', userTest);
|
|
172
|
-
});
|
|
173
199
|
|
|
174
|
-
|
|
175
|
-
|
|
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
|
+
});
|
|
176
243
|
});
|
|
177
244
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
expect(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
.expect(200)
|
|
194
|
-
.expect((res) => {
|
|
195
|
-
expect(res.body).toHaveProperty('user', userTest2);
|
|
196
|
-
});
|
|
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
|
+
});
|
|
197
260
|
|
|
198
|
-
|
|
199
|
-
|
|
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
|
+
});
|
|
200
292
|
});
|
|
201
293
|
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
});
|
|
204
314
|
});
|
|
205
315
|
});
|
|
206
316
|
|
|
@@ -211,51 +321,215 @@ describe('E2E JWTStrategy', () => {
|
|
|
211
321
|
expect(typeof token).toBe('string');
|
|
212
322
|
});
|
|
213
323
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
expect(res
|
|
224
|
-
|
|
225
|
-
|
|
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
|
+
});
|
|
226
367
|
});
|
|
227
368
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
expect(res
|
|
238
|
-
|
|
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
|
+
});
|
|
239
421
|
|
|
240
|
-
|
|
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
|
+
});
|
|
241
432
|
});
|
|
433
|
+
});
|
|
242
434
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
await
|
|
248
|
-
.
|
|
249
|
-
.set('
|
|
250
|
-
.
|
|
251
|
-
.expect(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
expect(
|
|
259
|
-
|
|
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
|
+
});
|
|
260
534
|
});
|
|
261
535
|
});
|
package/test/controller.ts
DELETED
|
@@ -1,43 +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('/test')
|
|
7
|
-
export class TestAuthController {
|
|
8
|
-
protected authUrl = '/test/login';
|
|
9
|
-
|
|
10
|
-
@Post('login_check')
|
|
11
|
-
@UseGuards(new AuthGuard({ local: { session: true } }, true))
|
|
12
|
-
async loginCheckAction(@Req() req: Request) {
|
|
13
|
-
return { success: req.user !== undefined };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Post('logout')
|
|
17
|
-
@UseGuards(new AuthGuard({ local: { session: true } }))
|
|
18
|
-
async logoutAction(@Req() req: Request) {
|
|
19
|
-
if (req.isAuthenticated()) {
|
|
20
|
-
req.logOut();
|
|
21
|
-
}
|
|
22
|
-
return { success: true };
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
@Get('authenticated')
|
|
26
|
-
@UseGuards(new AuthGuard({ local: { session: true }, jwt: { session: false } }))
|
|
27
|
-
async authenticated(@Req() req: Request) {
|
|
28
|
-
return { user: req.user };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
@Get('protected')
|
|
32
|
-
@UseGuards(new AuthGuard({ local: { session: true }, jwt: { session: false } }, true))
|
|
33
|
-
async protected(@Req() req: Request) {
|
|
34
|
-
return { user: req.user };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
@Get('jwt-session')
|
|
38
|
-
@HttpCode(200)
|
|
39
|
-
@UseGuards(new AuthGuard({ jwt: { session: true } }))
|
|
40
|
-
jwtSession(@Req() req: Request) {
|
|
41
|
-
return { user: req.user };
|
|
42
|
-
}
|
|
43
|
-
}
|