UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

234 lines (233 loc) 11.2 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "AuthController", { enumerable: true, get: function() { return AuthController; } }); const _common = require("@nestjs/common"); const _fastify = require("fastify"); const _user = require("../applications/users/constants/user"); const _rolesdecorator = require("../applications/users/decorators/roles.decorator"); const _userdecorator = require("../applications/users/decorators/user.decorator"); const _rolesguard = require("../applications/users/guards/roles.guard"); const _usermodel = require("../applications/users/models/user.model"); const _auth = require("./constants/auth"); const _routes = require("./constants/routes"); const _authtokenskipdecorator = require("./decorators/auth-token-skip.decorator"); const _twofaverifydto = require("./dto/two-fa-verify.dto"); const _authlocalguard = require("./guards/auth-local.guard"); const _authtokenrefreshguard = require("./guards/auth-token-refresh.guard"); const _authtwofaguard = require("./guards/auth-two-fa-guard"); const _authrequestinterface = require("./interfaces/auth-request.interface"); const _tokeninterface = require("./interfaces/token.interface"); const _authmanagerservice = require("./services/auth-manager.service"); const _authmethodtwofaservice = require("./services/auth-methods/auth-method-two-fa.service"); function _ts_decorate(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; } function _ts_metadata(k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); } function _ts_param(paramIndex, decorator) { return function(target, key) { decorator(target, key, paramIndex); }; } let AuthController = class AuthController { login(user, res) { return this.authManager.setCookies(user, res, true); } logout(res) { return this.authManager.clearCookies(res); } refreshCookies(user, res) { return this.authManager.refreshCookies(user, res); } token(user) { return this.authManager.getTokens(user); } refreshToken(user) { return this.authManager.getTokens(user, true); } /* TWO-FA Part */ twoFaInit(user) { return this.authMethod2FA.initTwoFactor(user); } twoFaEnable(body, req) { return this.authMethod2FA.enableTwoFactor(body, req); } twoFaDisable(body, req) { return this.authMethod2FA.disableTwoFactor(body, req); } async twoFaLogin(body, req, res) { const [authStatus, user] = await this.authMethod2FA.verify(body, req, true); if (authStatus.success) { const loginResponseDto = await this.authManager.setCookies(user, res); // clear the temporary 2FA cookie res.clearCookie(_auth.ACCESS_KEY, { path: _auth.TOKEN_PATHS[_tokeninterface.TOKEN_TYPE.ACCESS_2FA], httpOnly: true }); return { ...loginResponseDto, ...authStatus }; } return authStatus; } twoFaReset(userId) { return this.authMethod2FA.adminResetUserTwoFa(userId); } constructor(authManager, authMethod2FA){ this.authManager = authManager; this.authMethod2FA = authMethod2FA; } }; _ts_decorate([ (0, _common.Post)(_routes.AUTH_ROUTE.LOGIN), (0, _authtokenskipdecorator.AuthTokenSkip)(), (0, _common.UseGuards)(_authlocalguard.AuthLocalGuard), _ts_param(0, (0, _userdecorator.GetUser)()), _ts_param(1, (0, _common.Res)({ passthrough: true })), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _usermodel.UserModel === "undefined" ? Object : _usermodel.UserModel, typeof _fastify.FastifyReply === "undefined" ? Object : _fastify.FastifyReply ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "login", null); _ts_decorate([ (0, _common.Post)(_routes.AUTH_ROUTE.LOGOUT), (0, _authtokenskipdecorator.AuthTokenSkip)(), _ts_param(0, (0, _common.Res)({ passthrough: true })), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _fastify.FastifyReply === "undefined" ? Object : _fastify.FastifyReply ]), _ts_metadata("design:returntype", void 0) ], AuthController.prototype, "logout", null); _ts_decorate([ (0, _common.Post)(_routes.AUTH_ROUTE.REFRESH), (0, _authtokenskipdecorator.AuthTokenSkip)(), (0, _common.UseGuards)(_authtokenrefreshguard.AuthTokenRefreshGuard), _ts_param(0, (0, _userdecorator.GetUser)()), _ts_param(1, (0, _common.Res)({ passthrough: true })), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _usermodel.UserModel === "undefined" ? Object : _usermodel.UserModel, typeof _fastify.FastifyReply === "undefined" ? Object : _fastify.FastifyReply ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "refreshCookies", null); _ts_decorate([ (0, _common.Post)(_routes.AUTH_ROUTE.TOKEN), (0, _authtokenskipdecorator.AuthTokenSkip)(), (0, _common.UseGuards)(_authlocalguard.AuthLocalGuard), _ts_param(0, (0, _userdecorator.GetUser)()), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _usermodel.UserModel === "undefined" ? Object : _usermodel.UserModel ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "token", null); _ts_decorate([ (0, _common.Post)(_routes.AUTH_ROUTE.TOKEN_REFRESH), (0, _authtokenskipdecorator.AuthTokenSkip)(), (0, _common.UseGuards)(_authtokenrefreshguard.AuthTokenRefreshGuard), _ts_param(0, (0, _userdecorator.GetUser)()), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _usermodel.UserModel === "undefined" ? Object : _usermodel.UserModel ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "refreshToken", null); _ts_decorate([ (0, _common.Get)(`${_routes.AUTH_ROUTE.TWO_FA_BASE}/${_routes.AUTH_ROUTE.TWO_FA_ENABLE}`), (0, _common.UseGuards)(_rolesguard.UserRolesGuard), (0, _rolesdecorator.UserHaveRole)(_user.USER_ROLE.USER), _ts_param(0, (0, _userdecorator.GetUser)()), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _usermodel.UserModel === "undefined" ? Object : _usermodel.UserModel ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "twoFaInit", null); _ts_decorate([ (0, _common.Post)(`${_routes.AUTH_ROUTE.TWO_FA_BASE}/${_routes.AUTH_ROUTE.TWO_FA_ENABLE}`), (0, _common.UseGuards)(_rolesguard.UserRolesGuard), (0, _rolesdecorator.UserHaveRole)(_user.USER_ROLE.USER), _ts_param(0, (0, _common.Body)()), _ts_param(1, (0, _common.Req)()), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _twofaverifydto.TwoFaVerifyWithPasswordDto === "undefined" ? Object : _twofaverifydto.TwoFaVerifyWithPasswordDto, typeof _authrequestinterface.FastifyAuthenticatedRequest === "undefined" ? Object : _authrequestinterface.FastifyAuthenticatedRequest ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "twoFaEnable", null); _ts_decorate([ (0, _common.Post)(`${_routes.AUTH_ROUTE.TWO_FA_BASE}/${_routes.AUTH_ROUTE.TWO_FA_DISABLE}`), (0, _common.UseGuards)(_rolesguard.UserRolesGuard), (0, _rolesdecorator.UserHaveRole)(_user.USER_ROLE.USER), _ts_param(0, (0, _common.Body)()), _ts_param(1, (0, _common.Req)()), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _twofaverifydto.TwoFaVerifyWithPasswordDto === "undefined" ? Object : _twofaverifydto.TwoFaVerifyWithPasswordDto, typeof _authrequestinterface.FastifyAuthenticatedRequest === "undefined" ? Object : _authrequestinterface.FastifyAuthenticatedRequest ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "twoFaDisable", null); _ts_decorate([ (0, _common.Post)(`${_routes.AUTH_ROUTE.TWO_FA_BASE}/${_routes.AUTH_ROUTE.TWO_FA_LOGIN_VERIFY}`), (0, _common.UseGuards)(_rolesguard.UserRolesGuard), (0, _rolesdecorator.UserHaveRole)(_user.USER_ROLE.USER), _ts_param(0, (0, _common.Body)()), _ts_param(1, (0, _common.Req)()), _ts_param(2, (0, _common.Res)({ passthrough: true })), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _twofaverifydto.TwoFaVerifyDto === "undefined" ? Object : _twofaverifydto.TwoFaVerifyDto, typeof _authrequestinterface.FastifyAuthenticatedRequest === "undefined" ? Object : _authrequestinterface.FastifyAuthenticatedRequest, typeof _fastify.FastifyReply === "undefined" ? Object : _fastify.FastifyReply ]), _ts_metadata("design:returntype", Promise) ], AuthController.prototype, "twoFaLogin", null); _ts_decorate([ (0, _common.Post)(`${_routes.AUTH_ROUTE.TWO_FA_BASE}/${_routes.AUTH_ROUTE.TWO_FA_ADMIN_RESET_USER}/:id`), (0, _common.UseGuards)(_rolesguard.UserRolesGuard, _authtwofaguard.AuthTwoFaGuard), (0, _rolesdecorator.UserHaveRole)(_user.USER_ROLE.ADMINISTRATOR), _ts_param(0, (0, _common.Param)('id', _common.ParseIntPipe)), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ Number ]), _ts_metadata("design:returntype", typeof Promise === "undefined" ? Object : Promise) ], AuthController.prototype, "twoFaReset", null); AuthController = _ts_decorate([ (0, _common.Controller)(_routes.AUTH_ROUTE.BASE), _ts_metadata("design:type", Function), _ts_metadata("design:paramtypes", [ typeof _authmanagerservice.AuthManager === "undefined" ? Object : _authmanagerservice.AuthManager, typeof _authmethodtwofaservice.AuthMethod2FA === "undefined" ? Object : _authmethodtwofaservice.AuthMethod2FA ]) ], AuthController); //# sourceMappingURL=auth.controller.js.map