|
| 1 | +module.exports = async function (config,cryptr) { |
| 2 | + return new Promise((resolve,reject) => { |
| 3 | + const bcrypt = require('bcrypt'); |
| 4 | + const deasync = require('deasync'); |
| 5 | + const sqlite3 = require('sqlite3po'); |
| 6 | + const db = new sqlite3.Database(config.database); |
| 7 | + |
| 8 | + class User { |
| 9 | + constructor(first,second,third = 0,fourth = 0) { |
| 10 | + if (first == undefined) return; |
| 11 | + if (first % 1 === 0) { |
| 12 | + this._id = first; |
| 13 | + this._username = second; |
| 14 | + this._password = third; |
| 15 | + this._admin = fourth; |
| 16 | + } else { |
| 17 | + this._username = first; |
| 18 | + this._password = second; |
| 19 | + this._admin = third; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + deserialize(row) { |
| 24 | + return new this.constructor(row.id,row.username,row.password,row.admin); |
| 25 | + } |
| 26 | + |
| 27 | + get asObject() { |
| 28 | + var obj = { |
| 29 | + username: this._username, |
| 30 | + password: this._password, |
| 31 | + admin: this._admin |
| 32 | + }; |
| 33 | + if (this._id) obj.id = this._id; |
| 34 | + return obj; |
| 35 | + } |
| 36 | + |
| 37 | + setPassword(password) { |
| 38 | + var hash = deasync(bcrypt.hash); |
| 39 | + this._password = hash(password, 10); |
| 40 | + } |
| 41 | + |
| 42 | + checkPassword(password) { |
| 43 | + return new Promise((resolve,reject) => { |
| 44 | + bcrypt.compare(password,this._password).then(correct => { |
| 45 | + resolve(correct); |
| 46 | + }); |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + serialize() { |
| 51 | + if (this._id) { |
| 52 | + return { |
| 53 | + id: this._id, |
| 54 | + username: this._username, |
| 55 | + password: this._password, |
| 56 | + admin: this._admin |
| 57 | + } |
| 58 | + } else { |
| 59 | + var hash = deasync(bcrypt.hash); |
| 60 | + |
| 61 | + this._password = hash(this._password, 10); |
| 62 | + |
| 63 | + return { |
| 64 | + username: this._username, |
| 65 | + password: this._password, |
| 66 | + admin: this._admin |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + class Config { |
| 73 | + constructor(id,key,value) { |
| 74 | + this._id = id; |
| 75 | + this._key = key; |
| 76 | + this._value = value; |
| 77 | + } |
| 78 | + |
| 79 | + deserialize(row) { |
| 80 | + return new this.constructor(row.id,row.key,row.value); |
| 81 | + } |
| 82 | + |
| 83 | + serialize() { |
| 84 | + return { |
| 85 | + id: this._id, |
| 86 | + key: this._key, |
| 87 | + value: this._value |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + class Workspace { |
| 93 | + constructor(id,key,workspace,port,username,password) { |
| 94 | + this._id = id; |
| 95 | + this._key = key; |
| 96 | + this._workspace = workspace; |
| 97 | + this._port = port; |
| 98 | + this._username = username; |
| 99 | + this._password = password; |
| 100 | + } |
| 101 | + deserialize(row) { |
| 102 | + return new this.constructor(row.id,row.key,row.workspace,row.port,row.username,cryptr.decrypt(row.password)); |
| 103 | + } |
| 104 | + serialize() { |
| 105 | + return { |
| 106 | + id: this._id, |
| 107 | + key: this._key, |
| 108 | + workspace: this._workspace, |
| 109 | + port: this._port, |
| 110 | + username: this._username, |
| 111 | + password: cryptr.encrypt(this._password) |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + db.on('open', () => { |
| 117 | + db.bindSchema(User,'users',{id:'INTEGER PRIMARY KEY AUTOINCREMENT',username: 'TEXT',password: 'TEXT',admin: 'INTEGER'}).then(() => { |
| 118 | + User.get('SELECT id FROM users WHERE id = ?',1).then(d => { |
| 119 | + if (d == undefined) { |
| 120 | + new User('admin','admin',1).save().then(() => { |
| 121 | + console.log('Initialized Admin account with username and password as admin.'); |
| 122 | + }); |
| 123 | + } |
| 124 | + }); |
| 125 | + }); |
| 126 | + db.bindSchema(Config,'config',{id:'INTEGER PRIMARY KEY AUTOINCREMENT',key: 'TEXT', value: 'TEXT'}).then(() => { |
| 127 | + Config.get('SELECT * FROM config WHERE key = ?','Workspace Directory').then(d => { |
| 128 | + if (d == undefined) { |
| 129 | + new Config(1,'Workspace Directory','./workspaces').save().then(() => { |
| 130 | + console.log('Set config for Server Directory'); |
| 131 | + new Config(2,'Admins Only','false').save().then(() => { |
| 132 | + console.log('Set config for Admins Only'); |
| 133 | + }); |
| 134 | + }); |
| 135 | + } |
| 136 | + }); |
| 137 | + }); |
| 138 | + db.bindSchema(Workspace,'workspaces',{id:'INTEGER PRIMARY KEY AUTOINCREMENT',key: 'TEXT',workspace: 'TEXT',port: 'INTEGER',username: 'TEXT',password: 'TEXT'}).then(); |
| 139 | + resolve({User: User, Config: Config, Workspace: Workspace}); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}; |
0 commit comments