Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I store salts and passwords in MongoDB

I am trying to store passwords and salts in MongoDB and I'm not sure which datatype should be used. When I use strings, the encrypted password appears to be stored correctly, but the generated salt, which is created with new Buffer(crypto.randomBytes(16).toString('base64'), 'base64');, seems to have characters that weren't recognized. For example, I have a salt stored as �y_�6j(�l~Z}0ۡ\" and I don't think this is correct.

Is the problem that it's stored as a string?

like image 867
RUEMACHINE Avatar asked Mar 29 '17 11:03

RUEMACHINE


People also ask

Is it safe to store passwords in MongoDB?

While MongoDB's default security is based on modern industry standards, such as TLS for the transport-layer and SCRAM-SHA-2356 for password exchange, it's still possible for someone to get into your database, either by attacking your server through a different vector, or by somehow obtaining your security credentials.

Where are MongoDB passwords stored?

For users created in MongoDB, MongoDB stores all user information, including name , password , and the user's authentication database , in the system. users collection in the admin database.


1 Answers

While registering a user, you can generate a hashed password using bcrypt. Let's call this password as P#1. Save this hashed password (P#1) in your database only, and not the salt.

While logging in a user, generate hashed version of the password which the user sends, let's call it P#2. Now you just have to match P# and P#2. If they match, the user is authenticated. This way you can perform authentication without actually saving the salt in your database.

I will try to put it in simple way with the help of an example.

// My user schema

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

var userSchema = new Schema({
  username: String
  password: String,
});

// hash the password
userSchema.methods.generateHash = function(password) {
  return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
  return bcrypt.compareSync(password, this.password);
};
var User = mongoose.model('user', userSchema);
module.exports = User;


// My APIs for registering and authenticating a user

var User = require('/path/to/user/model');

app.post('/register', function(req, res) {
  var new_user = new User({
    username: req.username
  });

  new_user.password = new_user.generateHash(userInfo.password);
  new_user.save();
});

app.post('/login', function(req, res) {
  User.findOne({username: req.body.username}, function(err, user) {

    if (!user.validPassword(req.body.password)) {
      //password did not match
    } else {
      // password matched. proceed forward
    }
  });
});

Hope it helps you!

like image 113
Ankit Gomkale Avatar answered Oct 12 '22 14:10

Ankit Gomkale