@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
49 lines (48 loc) • 2.7 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Affirm_1 = __importDefault(require("../core/Affirm"));
const DatabaseEngine_1 = __importDefault(require("../database/DatabaseEngine"));
const UserManager_1 = __importDefault(require("../engines/UserManager"));
const Settings_1 = __importDefault(require("../helper/Settings"));
const bcryptjs_1 = __importDefault(require("bcryptjs"));
const JWTManager_1 = __importDefault(require("./JWTManager"));
class AdminManagerClass {
constructor() {
this.COLLECTION = Settings_1.default.db.collections.users;
this.rootSignIn = (password) => __awaiter(this, void 0, void 0, function* () {
(0, Affirm_1.default)(password, 'Invalid password');
const rootUser = yield DatabaseEngine_1.default.findOne(this.COLLECTION, { isRoot: true });
(0, Affirm_1.default)(rootUser, 'Incorrect system configuration: root user not found');
const isSame = yield bcryptjs_1.default.compare(password, rootUser.rootPasswordHash);
if (!isSame)
throw new Error('Invalid credentials');
rootUser.lastLogin = new Date().toJSON();
yield UserManager_1.default.update(rootUser);
const adminSecret = process.env.ADMIN_JWT_SECRET;
(0, Affirm_1.default)(adminSecret, 'Wrong system config: missing admin jwt secret');
const payload = {
apiKeyId: rootUser._id,
installationId: process.env.INSTALLATION_ID,
name: rootUser.name,
scopes: { consumers: ['*'], projects: ['*'] },
isAdmin: true
};
const token = JWTManager_1.default.issue(adminSecret, payload, 8);
return token;
});
}
}
const AdminManager = new AdminManagerClass();
exports.default = AdminManager;