0

I'm building a function that is checking a user password with linux password shadow file. The problem is that the main (sync) function is returning undefined before the inner async function is finish, and because of that it always return undefined.

How can I change the main function to async so it will wait for the async function to finish?

Please advice. Thnaks in advance

Adrian

  function checkPass(a, b) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassCorrect';
                    return response;
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassIncorrect';
                    return response;
                    break;
                }
            }
        }
    });
}


console.log("Function response: " + checkPass('share', 'qwerty'));
3

1 Answer 1

0

I add a callback to the main function:

function checkPass(a, b, callback) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    response = 'oldPassCorrect';
                    callback(response);
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    response = 'oldPassIncorrect';
                    callback(response);
                    break;
                }
            }
        }
    });
}


checkPass('share', 'qwerty', function (response) {
        console.log("CallBack: " + response);
    });
Sign up to request clarification or add additional context in comments.

4 Comments

It's not only the right way to go, it's the only way to go. You can get a bit more fancy with promises but it's still callback based.
Actually there's one thing you need to add. It's standard in node to have callbacks take two arguments, err and result. So instead of the line if (err) throw err; do if (err) return callback(err); And instead of doing callback(response); do callback(null, response).
Thanks all for all the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.