Genesis commit
This commit is contained in:
30
persistence/models/authenticationMethod.js
Normal file
30
persistence/models/authenticationMethod.js
Normal file
@@ -0,0 +1,30 @@
|
||||
module.exports = function (sequelize, DataTypes, options) {
|
||||
const model = sequelize.define("authenticationMethod", {
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: function() {
|
||||
return options.generateUniqueId()
|
||||
},
|
||||
primaryKey: true,
|
||||
unique: true
|
||||
},
|
||||
method: {
|
||||
type: DataTypes.ENUM,
|
||||
values: ['hd-auth', 'password', 'google-oauth']
|
||||
},
|
||||
order: {
|
||||
type: DataTypes.INTEGER,
|
||||
defaultValue: function() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
comment: "The supported authentication methods for a user of the system."
|
||||
});
|
||||
|
||||
model.associate = (db) => {
|
||||
model.User = model.belongsTo(db.User);
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
25
persistence/models/extendedPublicKey.js
Normal file
25
persistence/models/extendedPublicKey.js
Normal file
@@ -0,0 +1,25 @@
|
||||
module.exports = function (sequelize, DataTypes, options) {
|
||||
const model = sequelize.define("extendedPublicKey", {
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: function() {
|
||||
return options.generateUniqueId()
|
||||
},
|
||||
primaryKey: true,
|
||||
unique: true
|
||||
},
|
||||
xpub: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
}
|
||||
}, {
|
||||
comment: "Extended Public Key belonging to the users of the system"
|
||||
});
|
||||
|
||||
model.associate = (db) => {
|
||||
model.User = model.belongsTo(db.User);
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
36
persistence/models/password.js
Normal file
36
persistence/models/password.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
module.exports = function (sequelize, DataTypes, options) {
|
||||
const model = sequelize.define("password", {
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: function() {
|
||||
return options.generateUniqueId()
|
||||
},
|
||||
primaryKey: true,
|
||||
unique: true
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
// primaryKey: true,
|
||||
unique: true,
|
||||
allowNull: false,
|
||||
set(val) {
|
||||
var hashedPassword = options != null && val != null && val.length > 0 ?
|
||||
bcrypt.hashSync(val, bcrypt.genSaltSync(11)) :
|
||||
null
|
||||
;
|
||||
this.setDataValue('password', hashedPassword);
|
||||
}
|
||||
// should have an accessor method the redacts this failed...
|
||||
}
|
||||
}, {
|
||||
comment: "hashed password"
|
||||
});
|
||||
|
||||
model.associate = (db) => {
|
||||
model.User = model.belongsTo(db.User);
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
19
persistence/models/session.js
Normal file
19
persistence/models/session.js
Normal file
@@ -0,0 +1,19 @@
|
||||
module.exports = function (sequelize, DataTypes, options) {
|
||||
var model = sequelize.define("session", {
|
||||
sid: {
|
||||
type: DataTypes.STRING,
|
||||
primaryKey: true,
|
||||
unique: true,
|
||||
},
|
||||
expires: DataTypes.DATE,
|
||||
data: DataTypes.STRING(50000)
|
||||
}, {
|
||||
comment: "Each session in the system will be stored in this table..."
|
||||
});
|
||||
|
||||
model.associate = (db) => {
|
||||
model.User = model.belongsTo(db.User);
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
28
persistence/models/user.js
Normal file
28
persistence/models/user.js
Normal file
@@ -0,0 +1,28 @@
|
||||
module.exports = function (sequelize, DataTypes, options) {
|
||||
const model = sequelize.define("user", {
|
||||
id: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: function() {
|
||||
return options.generateUniqueId()
|
||||
},
|
||||
primaryKey: true,
|
||||
unique: true
|
||||
},
|
||||
|
||||
displayName: {
|
||||
type: DataTypes.STRING,
|
||||
// allowNull: false
|
||||
}
|
||||
}, {
|
||||
comment: "The individual users of the system..."
|
||||
});
|
||||
|
||||
model.associate = (db) => {
|
||||
model.Sessions = model.hasMany(db.Session);
|
||||
model.ExtendedPublicKeys = model.hasMany(db.ExtendedPublicKey);
|
||||
model.Passwords = model.hasMany(db.Password);
|
||||
model.AuthenticationMethods = model.hasMany(db.AuthenticationMethod);
|
||||
}
|
||||
|
||||
return model;
|
||||
};
|
||||
128
persistence/persistence.js
Normal file
128
persistence/persistence.js
Normal file
@@ -0,0 +1,128 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function () {
|
||||
var db = {};
|
||||
|
||||
const config = require('config');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const nanoid = require('nanoid');
|
||||
const tunnel = require('tunnel-ssh');
|
||||
|
||||
var dbOptions = {
|
||||
db: db,
|
||||
moment: require('moment'),
|
||||
generateUniqueId: (customIdSize) => {
|
||||
var idSize = customIdSize || 11;
|
||||
return nanoid(idSize); //=> "hsCc0ocrXkc"
|
||||
}
|
||||
};
|
||||
|
||||
dbOptions.Sequelize = require('sequelize');
|
||||
dbOptions.bcrypt = require('bcrypt');
|
||||
|
||||
dbOptions.sequelize = new dbOptions.Sequelize(config.get('database'));
|
||||
|
||||
// Load all the models...
|
||||
var modelDir = path.join(__dirname, './models/');
|
||||
fs.readdirSync(modelDir)
|
||||
.filter(file => {
|
||||
return (file.indexOf('.') !== 0) && (file !== "index.js") && (file.slice(-3) === '.js');
|
||||
})
|
||||
.forEach(file => {
|
||||
// const model = sequelize['import'](path.join(modelDir, file));
|
||||
const model = require(path.join(modelDir, file))(dbOptions.sequelize, dbOptions.Sequelize, dbOptions);
|
||||
|
||||
// Format the Model Name like we have for this project...
|
||||
var modelName = model.name;
|
||||
modelName = modelName.replace(modelName[0], modelName[0].toUpperCase());
|
||||
|
||||
// Add model to the db...
|
||||
db[modelName] = model;
|
||||
});
|
||||
|
||||
// Time for the associations...
|
||||
|
||||
Object.keys(db).forEach(modelName => {
|
||||
if (db[modelName].associate) {
|
||||
console.log("Loading associations for " + modelName);
|
||||
db[modelName].associate(db);
|
||||
}
|
||||
});
|
||||
|
||||
db.LoadDB = () => {
|
||||
db.Sequelize = dbOptions.Sequelize;
|
||||
db.sequelize = dbOptions.sequelize;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
tunneling()
|
||||
.then(() => {
|
||||
return dbOptions.sequelize.sync()
|
||||
}).then(() => {
|
||||
resolve(db);
|
||||
}).catch(error => {
|
||||
// Do the things...
|
||||
reject(error);
|
||||
console.error('Unable to connect to the database:', error);
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
db.updateDatabaseFromModels = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
dbOptions.sequelize.sync({
|
||||
alter: true
|
||||
})
|
||||
.then(() => {
|
||||
resolve(true);
|
||||
console.log("DB update successful.");
|
||||
})
|
||||
.catch(error => {
|
||||
// Do the things...
|
||||
reject(error);
|
||||
console.error('Unable to connect to the database:', error);
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
var tunneling = () => {
|
||||
const tunnelConfigs = config.get("tunnel");
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if(tunnelConfigs.useTunnel) {
|
||||
const privateKeyFilePath = path.resolve(tunnelConfigs["privateKeyFilePath"]);
|
||||
// Run the tunnel...
|
||||
var sshConfig = {
|
||||
"keepAlive": tunnelConfigs["keepAlive"],
|
||||
"username": tunnelConfigs["username"],
|
||||
// "password": tunnelConfigs["password"],
|
||||
"host": tunnelConfigs["host"],
|
||||
"port": tunnelConfigs["port"],
|
||||
"dstHost": tunnelConfigs["dstHost"],
|
||||
"dstPort": tunnelConfigs["dstPort"],
|
||||
// "localHost": tunnelConfigs["localHost"],
|
||||
"localPort": tunnelConfigs["localPort"],
|
||||
"privateKey": fs.readFileSync(privateKeyFilePath),
|
||||
"passphrase": tunnelConfigs["passphrase"]
|
||||
}
|
||||
console.log("Private Key path: ", privateKeyFilePath);
|
||||
tunnel(sshConfig, (error, server) => {
|
||||
if(error) {
|
||||
console.error("Tunneling: ", error);
|
||||
reject(error);
|
||||
} else {
|
||||
console.log('Tunnel server:', server);
|
||||
resolve(server)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Continue without setting up a tunnel...
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
}
|
||||
return db;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user