<commit_before>var express = require('express');
var crypto = require('crypto');
var sqlite3 = require('sqlite3');
var app = express();

// Set a few internal variables
app.configure(function() {
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(app.router);
});

app.configure('development', function() {
    app.enable('debug');
    app.set('db', new sqlite3.Database(__dirname + '/dev.db'));
});

app.configure('production', function() {
    app.disable('debug');
    app.set('db', new sqlite3.Database(__dirname + '/prod.db'));
});

// Set API Security
app.all('/api/*', function(req, res, next) {
    app.get('db').each("SELECT TOKEN AS token FROM USERS WHERE EMAIL='" + req.body.email + "' LIMIT 1;", function(err, row) {
        if (app.enabled('debug')) {
            console.log("*** Auth Attempt ***");
            console.log("** SELECT TOKEN AS token FROM USERS WHERE EMAIL='" + req.body.email + "' LIMIT 1;");
            console.log("* Received token: " + req.body.token + ", Retrieved Token: " + row.token + " Match? " + (req.body.token == row.token).toString());
        }

        // No token set means user is not logged in
        if (!row.token || (req.body.token != row.token)) {
            next(new Error(401));
        } else {
            next();
        }
    });
});

// Login to system
app.post('/login', function(req, res, next) {
    if (!req.body.email || !req.body.password) {
        res.send({ 'status': 'ERROR', 'message': 'Invalid parameters.' });
    }

    app.get('db').all("SELECT EMAIL AS email, PASSWORD AS password, SALT AS salt from USERS WHERE EMAIL = '" + req.body.email + "' LIMIT 1;", function(err, rows) {
        if (!rows[0]) {
            res.send({ 'status': 'ERROR', 'message': 'Oops, that user doesn\'t exist yet.' });
            return;
        }

        var row = rows[0];
        var tmpHash = crypto.createHash('sha256').update(req.body.password + "." + row.salt).digest('base64');

        if (app.enabled('debug')) {
            console.log("*** Login Attempt ***");
            console.log("** SELECT EMAIL AS email, PASSWORD AS password, SALT AS salt from USERS WHERE EMAIL = '" + req.body.email + "' LIMIT 1;");
            console.log("* Calculated hash: " + tmpHash + ", DB Hash: " + row.password + ", Match? " + (row.password == tmpHash).toString());
        }
        
        if (row.password == tmpHash) {
            var tmpToken = '';
            crypto.randomBytes(24, function(ex, buf) {
                tmpToken = buf.toString('hex');
                
                app.get('db').run("UPDATE USERS SET TOKEN='" + tmpToken + "' WHERE EMAIL = '" + req.body.email + "';");
                if (app.enabled('debug')) {
                    console.log("** UPDATE USERS SET TOKEN = '" + tmpToken + "' WHERE EMAIL = '" + req.body.email + "';");
                }

                res.send({ 'status': 'SUCCESS', 'token': tmpToken });
            });
        } else {
            res.send({ 'status': 'ERROR', 'message': 'Email and/or password may be incorrect.' });
        }
    });
});

// Logout of system
app.post('/api/logout', function(req, res, next) {
    app.get('db').run("UPDATE USERS SET TOKEN='' WHERE EMAIL = '" + req.body.email + "';");
    if (app.enabled('debug')) {
        console.log("*** Logout Attempt ***");
        console.log("** UPDATE USERS SET TOKEN='' WHERE EMAIL = '" + req.body.email + "';");
    }

    res.send({ 'status': 'SUCCESS' });
});

// Regisiter
// TODO: Check for existing user
app.post('/register', function(req, res, next) {
    if (!req.body.type || !req.body.email || !req.body.password || !req.body.postal) {
        res.send({ 'status': 'ERROR', 'message': 'Invalid parameters.' });
    } else {
        crypto.randomBytes(12, function(ex, buf) {
            app.get('db').run("INSERT INTO USERS (TYPE, EMAIL, PASSWORD, SALT, POSTAL, TOKEN) VALUES($type, $email, $password, $salt, $postal, $token)", {
                $type: req.body.type,
                $email: req.body.email,
                $password: crypto.createHash('sha256').update(req.body.password + "." + buf.toString('hex')).digest('base64'),
                $salt: buf.toString('hex'),
                $postal: req.body.postal,
                $token: ''
            });

            if (app.enabled('debug')) {
                console.log("*** Register Attempt ***");
            }<commit_msg>Changed auth handler to return JSON object on error<commit_after>var express = require('express');
var crypto = require('crypto');
var sqlite3 = require('sqlite3');
var app = express();

// Set a few internal variables
app.configure(function() {
    app.use(express.json());
    app.use(express.urlencoded());
    app.use(app.router);
});

app.configure('development', function() {
    app.enable('debug');
    app.set('db', new sqlite3.Database(__dirname + '/dev.db'));
});

app.configure('production', function() {
    app.disable('debug');
    app.set('db', new sqlite3.Database(__dirname + '/prod.db'));
});

// Set API Security
app.all('/api/*', function(req, res, next) {
    app.get('db').each("SELECT TOKEN AS token FROM USERS WHERE EMAIL='" + req.body.email + "' LIMIT 1;", function(err, row) {
        if (app.enabled('debug')) {
            console.log("*** Auth Attempt ***");
            console.log("** SELECT TOKEN AS token FROM USERS WHERE EMAIL='" + req.body.email + "' LIMIT 1;");
            console.log("* Received token: " + req.body.token + ", Retrieved Token: " + row.token + " Match? " + (req.body.token == row.token).toString());
        }

        // No token set means user is not logged in
        if (!row.token || (req.body.token != row.token)) {
            res.send({ 'status': 'ERROR', 'message': 'Token mismatch: ' + req.body.token });
            return;
        } else {
            next();
        }
    });
});

// Login to system
app.post('/login', function(req, res, next) {
    if (!req.body.email || !req.body.password) {
        res.send({ 'status': 'ERROR', 'message': 'Invalid parameters.' });
    }

    app.get('db').all("SELECT EMAIL AS email, PASSWORD AS password, SALT AS salt from USERS WHERE EMAIL = '" + req.body.email + "' LIMIT 1;", function(err, rows) {
        if (!rows[0]) {
            res.send({ 'status': 'ERROR', 'message': 'Oops, that user doesn\'t exist yet.' });
            return;
        }

        var row = rows[0];
        var tmpHash = crypto.createHash('sha256').update(req.body.password + "." + row.salt).digest('base64');

        if (app.enabled('debug')) {
            console.log("*** Login Attempt ***");
            console.log("** SELECT EMAIL AS email, PASSWORD AS password, SALT AS salt from USERS WHERE EMAIL = '" + req.body.email + "' LIMIT 1;");
            console.log("* Calculated hash: " + tmpHash + ", DB Hash: " + row.password + ", Match? " + (row.password == <PASSWORD>).toString());
        }
        
        if (row.password == <PASSWORD>) {
            var tmpToken = '';
            crypto.randomBytes(24, function(ex, buf) {
                tmpToken = buf.toString('hex');
                
                app.get('db').run("UPDATE USERS SET TOKEN='" + tmpToken + "' WHERE EMAIL = '" + req.body.email + "';");
                if (app.enabled('debug')) {
                    console.log("** UPDATE USERS SET TOKEN = '" + tmpToken + "' WHERE EMAIL = '" + req.body.email + "';");
                }

                res.send({ 'status': 'SUCCESS', 'token': tmpToken });
            });
        } else {
            res.send({ 'status': 'ERROR', 'message': 'Email and/or password may be incorrect.' });
        }
    });
});

// Logout of system
app.post('/api/logout', function(req, res, next) {
    app.get('db').run("UPDATE USERS SET TOKEN='' WHERE EMAIL = '" + req.body.email + "';");
    if (app.enabled('debug')) {
        console.log("*** Logout Attempt ***");
        console.log("** UPDATE USERS SET TOKEN='' WHERE EMAIL = '" + req.body.email + "';");
    }

    res.send({ 'status': 'SUCCESS' });
});

app.post('/api/changePassword', function(req, res, next) {
    app.get('db')
});

// Register
// TODO: Check for existing user
app.post('/register', function(req, res, next) {
    if (!req.body.type || !req.body.email || !req.body.password || !req.body.postal) {
        res.send({ 'status': 'ERROR', 'message': 'Invalid parameters.' });
    } else {
        crypto.randomBytes(12, function(ex, buf) {
            app.get('db').run("INSERT INTO USERS (TYPE, EMAIL, PASSWORD, SALT, POSTAL, TOKEN) VALUES($type, $email, $password, $salt, $postal, $token)", {
                $type: req.body.type,
                $email: req.body.email,
                $password: crypto.createHash('sha256').update(req.body.password + "." + buf.toString('hex')).digest('base64'),
                $salt: buf.toString('hex'),
                $postal: req.body.postal,
                $token: ''
            });

            if (app.enabled('debug')) {
                console.log("*** Register Attempt ***");
            }
{'fraction_non_alphanumeric': 0.19491901486576438, 'fraction_numerical': 0.005657865542489461, 'mean_word_length': 2.342602892102336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660741', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2713, 'n_words': 657}