Search code examples
node.jsexpressqpbkdf2

No callback provided to pbkdf2


I have this code, which works just fine on my development computer, but not on the server.

db.admin.verify([req.body.username]).then(function(data){
    if (data[0].length == 0){       //if there is no user with that username
        console.log("bad username");
        res.status(401).send('Incorrect username or password');
    }

    var creds = data[0][0];

    return myCrypt.pbkdf2(req.body.password, creds.salt).then(function(key){
        if (creds.password === key.toString('base64')){            //correct password
            console.log("correct pw");
            return db.admin.getUser([req.body.username])
        } else {
            console.log("bad pw");
            res.status(401).send('Incorrect password or username');
        }
    });
}).then(function(dbData){
    var user = dbData[0][0];
    var profile = {
        firstName: user.firstName,
        lastName: user.lastName,
        email: user.email,
        username: user.username,
        type: user.type,
        id: user.adminId
    };

    var token = jwt.sign(profile, 'secrets');

    res.json({token:token, user: profile});
}).catch(function(err){
    console.log('ERROR');
    console.log(err);
    throw err;
    res.status(500).json(err);
});

It is supposed to authenticate a user from a database by comparing hashes. I am using node.js crypto library implementation of pbkdf2 wrapped in a Q promise.

module.exports.pbkdf2 = function(password, salt) {

    var pbkdf2 = Q.denodeify(crypto.pbkdf2);

    return pbkdf2(password, salt, 4096, 512, 'sha512')
};

When I try running it on the server, it returns [Error: No callback provided to pbkdf2], caught from the promises catch function.

I've tried copying my entire project from the development to the server, just in case I didn't have any dependencies installed, but the error is still thrown.


Solution

  • Turns out that the pbkdf2 function signature broke between node 0.10 and 0.12, and it wasn't receiving the callback because I was passing the digest instead of a function callback. Updating nodejs to 0.12 fixed the issue.