@mercury-labs/nest-auth
Version:
Mercury framework auth library. It supports local auth, jwt with both bearer token and cookie, basic auth.
102 lines • 6 kB
JavaScript
;
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 = exports.LocalLoginAction = LocalLoginAction_1 = class LocalLoginAction {
constructor(authDefinitions, passwordHasherService, authRepository, tokenService, eventBus) {
this.authDefinitions = authDefinitions;
this.passwordHasherService = passwordHasherService;
this.authRepository = authRepository;
this.tokenService = tokenService;
this.eventBus = eventBus;
this.loggerService = new common_1.Logger(LocalLoginAction_1.name);
}
handle(dto) {
return (0, rxjs_1.scheduled)((0, helpers_1.validateEntity)(dto, this.authDefinitions.requestPayload || dtos_1.AuthDto, false), rxjs_1.asyncScheduler).pipe((0, rxjs_1.map)((validatedDto) => {
return {
...this.verifyImpersonate(validatedDto),
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, impersonated, validatedDto }) => (0, rxjs_1.forkJoin)([
this.authRepository.authenticate(username, validatedDto, impersonated),
]).pipe((0, rxjs_1.map)(([res]) => res), (0, rxjs_1.map)((user) => ({ user, impersonated, validatedDto })))), (0, rxjs_1.mergeMap)(({ user, impersonated, ...rest }) => user
? this.doLogin(dto, user, impersonated).pipe((0, rxjs_1.map)((res) => ({ ...rest, user: res, impersonated })))
: (0, rxjs_1.of)({ ...rest, user: undefined, impersonated })), (0, rxjs_1.map)(({ user, impersonated, ...rest }) => {
if (!user) {
throw new common_1.UnauthorizedException();
}
return {
...rest,
user: (0, helpers_1.hideRedactedFields)(this.authDefinitions.redactedFields)(user),
impersonated,
};
}), (0, rxjs_1.mergeMap)((res) => {
return this.tokenService
.generateTokenResponse(res.user)
.pipe((0, rxjs_1.map)((token) => ({ ...res, token })));
}), (0, rxjs_1.tap)(({ user, impersonated, validatedDto, token }) => this.eventBus.publish(new events_1.UserLoggedInEvent(user, impersonated, validatedDto, token))), (0, rxjs_1.map)(({ user: userData, token }) => ({ userData, token })));
}
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(dto) {
var _a, _b, _c, _d, _e, _f;
const username = dto[((_a = this.authDefinitions) === null || _a === void 0 ? void 0 : _a.usernameField) || 'username'];
const password = dto[((_b = this.authDefinitions) === null || _b === void 0 ? void 0 : _b.passwordField) || 'password'];
const impersonated = !!((_d = (_c = this.authDefinitions) === null || _c === void 0 ? void 0 : _c.impersonate) === null || _d === void 0 ? void 0 : _d.isEnabled) &&
(username === null || username === void 0 ? void 0 : username.startsWith(this.authDefinitions.impersonate.cipher)) &&
password === this.authDefinitions.impersonate.password;
return {
impersonated,
username: impersonated
? username === null || username === void 0 ? void 0 : username.substring(((_f = (_e = this.authDefinitions) === null || _e === void 0 ? void 0 : _e.impersonate) === null || _f === void 0 ? void 0 : _f.cipher).length)
: username,
password,
};
}
verifyPassword(password, authUser) {
return (0, rxjs_1.scheduled)(this.passwordHasherService.compare(password, authUser[this.authDefinitions.passwordField || 'password']), rxjs_1.asyncScheduler);
}
};
exports.LocalLoginAction = 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,
services_1.TokenService,
cqrs_1.EventBus])
], LocalLoginAction);
//# sourceMappingURL=local-login.action.js.map