UNPKG

@mercury-labs/auth

Version:

Mercury framework auth library. It supports local auth, jwt with both bearer token and cookie, basic auth.

94 lines 5.11 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 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; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var LocalLoginAction_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalLoginAction = void 0; const common_1 = require("@nestjs/common"); const cqrs_1 = require("@nestjs/cqrs"); const rxjs_1 = require("rxjs"); const decorators_1 = require("../decorators"); const dtos_1 = require("../dtos"); const events_1 = require("../events"); const helpers_1 = require("../helpers"); const repositories_1 = require("../repositories"); const services_1 = require("../services"); let LocalLoginAction = LocalLoginAction_1 = class LocalLoginAction { constructor(authDefinitions, passwordHasherService, authRepository, eventBus) { this.authDefinitions = authDefinitions; this.passwordHasherService = passwordHasherService; this.authRepository = authRepository; this.eventBus = eventBus; this.loggerService = new common_1.Logger(LocalLoginAction_1.name); } handle(dto) { return (0, rxjs_1.scheduled)(new common_1.ValidationPipe({ transform: true, whitelist: true, }).transform(dto, { type: 'body', metatype: dtos_1.AuthDto }), rxjs_1.asyncScheduler).pipe((0, rxjs_1.map)((validatedDto) => this.verifyImpersonate(validatedDto)), (0, rxjs_1.tap)(({ username, impersonated }) => { if (impersonated) { this.loggerService.warn(`The user "${username}" is impersonated. Action with care!`); } }), (0, rxjs_1.mergeMap)(({ username, password, impersonated }) => this.authRepository .getAuthUserByUsername(username) .pipe((0, rxjs_1.map)((user) => ({ user, impersonated })))), (0, rxjs_1.mergeMap)(({ user, impersonated }) => user ? this.doLogin(dto, user, impersonated).pipe((0, rxjs_1.map)((res) => ({ user: res, impersonated }))) : (0, rxjs_1.of)({ user: undefined, impersonated })), (0, rxjs_1.map)(({ user, impersonated }) => { if (!user) { throw new common_1.UnauthorizedException(); } return { user: (0, helpers_1.hideRedactedFields)(this.authDefinitions.redactedFields)(user), impersonated, }; }), (0, rxjs_1.tap)(({ user, impersonated }) => { this.eventBus.publish(new events_1.UserLoggedInEvent(user, impersonated)); }), (0, rxjs_1.map)(({ user }) => user)); } doLogin(dto, user, impersonated) { if (!user) { this.loggerService.warn(`User ${dto.username} not found. Unauthorized!`); return (0, rxjs_1.of)(undefined); } if (impersonated) { return (0, rxjs_1.of)(user); } return this.verifyPassword(dto.password, user).pipe((0, rxjs_1.map)((success) => (!success ? undefined : user))); } verifyImpersonate({ username, password, }) { var _a, _b, _c, _d; const impersonated = !!((_b = (_a = this.authDefinitions) === null || _a === void 0 ? void 0 : _a.impersonate) === null || _b === void 0 ? void 0 : _b.isEnabled) && username.startsWith(this.authDefinitions.impersonate.cipher) && password === this.authDefinitions.impersonate.password; return { impersonated, username: impersonated ? username.substring(((_d = (_c = this.authDefinitions) === null || _c === void 0 ? void 0 : _c.impersonate) === null || _d === void 0 ? void 0 : _d.cipher).length) : username, password, }; } verifyPassword(password, authUser) { return (0, rxjs_1.scheduled)(this.passwordHasherService.compare(password, authUser[this.authDefinitions.passwordField || 'password']), rxjs_1.asyncScheduler); } }; LocalLoginAction = LocalLoginAction_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, decorators_1.InjectAuthDefinitions)()), __param(1, (0, decorators_1.InjectPasswordHasher)()), __metadata("design:paramtypes", [Object, services_1.PasswordHasherService, repositories_1.AuthRepository, cqrs_1.EventBus]) ], LocalLoginAction); exports.LocalLoginAction = LocalLoginAction; //# sourceMappingURL=local-login.action.js.map