Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hashing of passwords for its storage in the database #113

Merged
merged 7 commits into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions createUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var User = require('./models/user');
var mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_URL);

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));

const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question('Please enter the username : ', (username) => {
rl.question('Please enter the password : ', (password) => {
var newUser = User({
user : username,
password : password
});

User.createUser(newUser, function(error){
if(error)
throw error;
else
console.log("User with username " + newUser.user + " created..");
rl.close(process.exit(1));
});
});
});
3 changes: 2 additions & 1 deletion initRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ asyncLoop(mines, function(mine, next){
"pass": password
},
}, function(err, res, body){
console.log(mineName + " Added")
console.log(mineName + " ==> " + body.friendlyMessage);
next();
});
})

6 changes: 4 additions & 2 deletions models/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ var schema = new Schema({
main: String,
logo: String
},
twitter: String,
twitter: String,
status: String,
isProduction: Boolean,
maintainerOrgName: String,
maintainerUrl: String
maintainerUrl: String,
maintainerEmail: String,
maintainerGithubUrl: String
},
{
collection: 'instances'
Expand Down
16 changes: 16 additions & 0 deletions models/instance_validate_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ var InstanceSchema = {
id: "/properties/description",
type: "string"
},
maintainerEmail: {
id: "/properties/maintainerEmail",
type: "string"
},
maintainerGithubUrl: {
id: "/properties/maintainerGithubUrl",
type: "string"
},
maintainerOrgName: {
id: "/properties/maintainerOrgName",
type: "string"
Expand Down Expand Up @@ -136,6 +144,14 @@ var InstancePutSchema = {
id: "/properties/maintainerUrl",
type: "string"
},
maintainerEmail: {
id: "/properties/maintainerEmail",
type: "string"
},
maintainerGithubUrl: {
id: "/properties/maintainerGithubUrl",
type: "string"
},
images: {
id: "/properties/images",
properties: {
Expand Down
32 changes: 29 additions & 3 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
/**
* User Model
*/
var bcrypt = require("bcrypt");
var SALT_WORK_FACTOR = 10;
var mongoose = require('mongoose');

// Schema Modeling
var Schema = mongoose.Schema;
var schema = new Schema({
user: String,
user: {
type : String,
required : true,
unique: true
},
password: String
},
{
collection: 'users'
});

var User = mongoose.model("User", schema);

module.exports = User;
var User = module.exports = mongoose.model("User", schema);

module.exports.createUser = function(newUser, callback) {
bcrypt.hash(newUser.password, SALT_WORK_FACTOR, function(err, hash) {
if (err) return err;
// override the cleartext password with the hashed one
newUser.password = hash;
newUser.save(callback);
});
}


module.exports.comparePassword = function(candidatePassowrd, hash, callback){
bcrypt.compare(candidatePassowrd, hash, function(err, isMatch){
if(err) return callback(err);
callback(null, isMatch);
});
}

module.exports.getUserByUsername = function(username, callback){
User.findOne({user:username}, callback);
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@
},
"dependencies": {
"async": "^2.4.1",
"body-parser": "~1.15.2",
"body-parser": "^1.18.3",
"bootstrap": "~3.3.7",
"bootstrap-material-design": "^0.5.10",
"connect-slashes": "^1.3.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"ejs": "~2.5.2",
"express": "~4.14.0",
"express": "^4.16.4",
"express-jsonschema": "~1.1.6",
"express-session": "^1.15.4",
"forever": "^0.15.3",
"jquery": "~3.2.1",
"leaflet-search": "^2.3.2",
"mongoose": "~4.10.3",
"morgan": "~1.7.0",
"morgan": "^1.9.1",
"node-async-loop": "^1.2.2",
"node-cron": "^1.2.0",
"passport": "^0.3.2",
"passport-http": "^0.3.0",
"passport-local": "^1.0.0",
"request": "^2.81.0",
"serve-favicon": "~2.3.0",
"serve-favicon": "^2.5.0",
"swagger-express": "~1.0.5",
"swagger-ui-express": "~2.0.0"
"swagger-ui-express": "~2.0.0",
"bcrypt": "~3.0.0"
}
}
24 changes: 15 additions & 9 deletions public/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ $(document).ready(function() {
$.ajax({
url: 'service/synchronize/',
type: 'PUT',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
},
success: function(result) {
localStorage.setItem("message", "All instances were updated successfully.");
window.location = window.location.pathname;
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
}
});
}
Expand Down Expand Up @@ -313,12 +313,12 @@ function getInstances(search) {
$.ajax({
url: 'service/instances/' + instance.id,
type: 'DELETE',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
},
success: function(result) {
localStorage.setItem("message", "Instance " + instance.name + " was deleted successfully.");
window.location = window.location.pathname;
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
}
});
}
Expand All @@ -330,12 +330,12 @@ function getInstances(search) {
$.ajax({
url: 'service/synchronize/' + instance.id,
type: 'PUT',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
},
success: function(result) {
localStorage.setItem("message", "Instance " + instance.name + " was updated successfully.");
window.location = window.location.pathname;
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user.user + ":" + user.password));
}
});
}
Expand Down Expand Up @@ -369,6 +369,12 @@ function getInstances(search) {
if (instance.maintainerUrl !== undefined) {
$("#mine-modal-body").append('<span class="bold"> Maintainer URL: </span><a target="_blank" id="list-maintainerUrl" href="' + instance.maintainerUrl + '">' + instance.maintainerUrl + '</a><br>');
}
if (instance.maintainerEmail !== undefined) {
$("#mine-modal-body").append('<span class="bold"> Maintainer Email: </span><a target="_blank" id="list-maintainerEmail" href="mailto:' + instance.maintainerEmail + '">' + instance.maintainerEmail + '</a><br>');
}
if (instance.maintainerGithubUrl !== undefined) {
$("#mine-modal-body").append('<span class="bold"> Maintainer Github URL: </span><a target="_blank" id="list-maintainerGithubUrl" href="' + instance.maintainerGithubUrl + '">' + instance.maintainerGithubUrl + '</a><br>');
}
$("#mine-modal-body").append('<span class="bold"> API Version: </span><span id="list-api-version">' + instance.api_version + '</span>')
if (instance.release_version !== "") {
$("#mine-modal-body").append(
Expand Down
50 changes: 33 additions & 17 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ var User = require('../models/user');
* will be set at `req.user` in route handlers after authentication.
*/
passport.use(new Strategy(
function(username, password, done) {
User.findOne({ 'user': username }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect Username.' });
}
if (user.password != password) {
return done(null, false, { message: 'Incorrect Password.' });
}
return done(null, user);
});
}
function(username, password, done){
User.getUserByUsername(username, function(err, user){
if(err) throw err;
else if(!user){
return done(null, false, {message: "Incorrect Username"});
}
else{
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: "Incorrect Password"});
}
});
}
});
}
));


/**
* FRONT END: Sessions Persistence
* In order to restore authentication state across HTTP requests, Passport needs
Expand All @@ -55,13 +62,22 @@ passport.use(new BasicStrategy(
function(username, password, done) {
User.findOne({ 'user': username }, function(err, user) {
if (err) { return done(err); }
if (!user) {
else if (!user) {
return done(null, false, { message: 'Incorrect Username.' });
}
if (user.password != password) {
return done(null, false, { message: 'Incorrect Password.' });
else if (user.password != password) {
User.comparePassword(password, user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
return done(null, user);
} else {
return done(null, false, {message: "Incorrect Password"});
}
});
}
else{
return done(null, user);
}
return done(null, user);
});
}
));
));
8 changes: 8 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function updateInstance(req, res, next){
"description": req.body.newDesc,
"maintainerOrgName": req.body.maintainerOrgName.trim(),
"maintainerUrl": req.body.maintainerUrl.trim(),
"maintainerEmail": req.body.maintainerEmail.trim(),
"maintainerGithubUrl": req.body.maintainerGithubUrl.trim(),
"twitter": req.body.newTwitter.trim(),
"location": {
"latitude": req.body.newLatitude,
Expand Down Expand Up @@ -139,6 +141,8 @@ function updateInstance(req, res, next){
desc: req.body.newDesc,
maintainerOrgName: req.body.maintainerOrgName,
maintainerUrl: req.body.maintainerUrl,
maintainerEmail: req.body.maintainerEmail,
maintainerGithubUrl: req.body.maintainerGithubUrl,
twitter: req.body.newTwitter,
lat: req.body.newLatitude,
lon: req.body.newLongitude,
Expand Down Expand Up @@ -189,6 +193,8 @@ router.post('/instance', function(req, res, next) {
"description": req.body.newDesc.trim(),
"maintainerOrgName": req.body.maintainerOrgName.trim(),
"maintainerUrl": req.body.maintainerUrl.trim(),
"maintainerEmail": req.body.maintainerEmail.trim(),
"maintainerGithubUrl": req.body.maintainerGithubUrl.trim(),
"twitter": req.body.newTwitter.trim(),
"location": {
"latitude": req.body.newLatitude,
Expand Down Expand Up @@ -218,6 +224,8 @@ router.post('/instance', function(req, res, next) {
desc: req.body.newDesc,
maintainerOrgName: req.body.maintainerOrgName,
maintainerUrl: req.body.maintainerUrl,
maintainerEmail: req.body.maintainerEmail,
maintainerGithubUrl: req.body.maintainerGithubUrl,
twitter: req.body.newTwitter,
lat: req.body.newLatitude,
lon: req.body.newLongitude,
Expand Down
4 changes: 4 additions & 0 deletions routes/instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ router.post('/', passport.authenticate('basic', {session: false}), validate({bod
newInstanceObject.location = typeof(req.body.location) !== 'undefined' ? req.body.location : {"latitude": "", "longitude": ""};
newInstanceObject.maintainerOrgName = typeof(req.body.maintainerOrgName) !== 'undefined' ? req.body.maintainerOrgName : "";
newInstanceObject.maintainerUrl = typeof(req.body.maintainerUrl) !== 'undefined' ? req.body.maintainerUrl : "";
newInstanceObject.maintainerEmail = typeof(req.body.maintainerEmail) !== 'undefined' ? req.body.maintainerEmail : "";
newInstanceObject.maintainerGithubUrl = typeof(req.body.maintainerGithubUrl) !== 'undefined' ? req.body.maintainerGithubUrl : "";

// Get the instance Versions & Branding information
var intermine_endpoint = req.body.url + "/service/version/intermine";
Expand Down Expand Up @@ -353,6 +355,8 @@ router.put('/:id', passport.authenticate('basic', {session: false}), validate({b
instance.description = typeof(req.body.description) !== 'undefined' ? req.body.description : instance.description;
instance.maintainerOrgName = typeof(req.body.maintainerOrgName) !== 'undefined' ? req.body.maintainerOrgName : instance.maintainerOrgName;
instance.maintainerUrl = typeof(req.body.maintainerUrl) !== 'undefined' ? req.body.maintainerUrl : instance.maintainerUrl;
instance.maintainerEmail = typeof(req.body.maintainerEmail) !== 'undefined' ? req.body.maintainerEmail : instance.maintainerEmail;
instance.maintainerGithubUrl = typeof(req.body.maintainerGithubUrl) !== 'undefined' ? req.body.maintainerGithubUrl : instance.maintainerGithubUrl;
instance.last_time_updated = new Date();
instance.api_version = typeof(req.body.api_version) !== 'undefined' ? req.body.api_version : instance.api_version;
instance.release_version = typeof(req.body.release_version) !== 'undefined' ? req.body.release_version : instance.release_version;
Expand Down
Loading