UNPKG

5.14 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.AuthGuard = void 0;
25const common_1 = require("@nestjs/common");
26const passport = require("passport");
27const auth_module_options_1 = require("./interfaces/auth-module.options");
28const options_1 = require("./options");
29const memoize_util_1 = require("./utils/memoize.util");
30exports.AuthGuard = (0, memoize_util_1.memoize)(createAuthGuard);
31const NO_STRATEGY_ERROR = `In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.`;
32function createAuthGuard(type) {
33 let MixinAuthGuard = class MixinAuthGuard {
34 constructor(options) {
35 this.options = {};
36 this.options = options !== null && options !== void 0 ? options : this.options;
37 if (!type && !this.options.defaultStrategy) {
38 new common_1.Logger('AuthGuard').error(NO_STRATEGY_ERROR);
39 }
40 }
41 canActivate(context) {
42 return __awaiter(this, void 0, void 0, function* () {
43 const options = Object.assign(Object.assign(Object.assign({}, options_1.defaultOptions), this.options), (yield this.getAuthenticateOptions(context)));
44 const [request, response] = [
45 this.getRequest(context),
46 this.getResponse(context)
47 ];
48 const passportFn = createPassportContext(request, response);
49 const user = yield passportFn(type || this.options.defaultStrategy, options, (err, user, info, status) => this.handleRequest(err, user, info, context, status));
50 request[options.property || options_1.defaultOptions.property] = user;
51 return true;
52 });
53 }
54 getRequest(context) {
55 return context.switchToHttp().getRequest();
56 }
57 getResponse(context) {
58 return context.switchToHttp().getResponse();
59 }
60 logIn(request) {
61 return __awaiter(this, void 0, void 0, function* () {
62 const user = request[this.options.property || options_1.defaultOptions.property];
63 yield new Promise((resolve, reject) => request.logIn(user, (err) => (err ? reject(err) : resolve())));
64 });
65 }
66 handleRequest(err, user, info, context, status) {
67 if (err || !user) {
68 throw err || new common_1.UnauthorizedException();
69 }
70 return user;
71 }
72 getAuthenticateOptions(context) {
73 return undefined;
74 }
75 };
76 __decorate([
77 (0, common_1.Optional)(),
78 (0, common_1.Inject)(auth_module_options_1.AuthModuleOptions),
79 __metadata("design:type", auth_module_options_1.AuthModuleOptions)
80 ], MixinAuthGuard.prototype, "options", void 0);
81 MixinAuthGuard = __decorate([
82 __param(0, (0, common_1.Optional)()),
83 __metadata("design:paramtypes", [auth_module_options_1.AuthModuleOptions])
84 ], MixinAuthGuard);
85 const guard = (0, common_1.mixin)(MixinAuthGuard);
86 return guard;
87}
88const createPassportContext = (request, response) => (type, options, callback) => new Promise((resolve, reject) => passport.authenticate(type, options, (err, user, info, status) => {
89 try {
90 request.authInfo = info;
91 return resolve(callback(err, user, info, status));
92 }
93 catch (err) {
94 reject(err);
95 }
96})(request, response, (err) => (err ? reject(err) : resolve())));