/*
Copyright 2016 Santtu Lakkala <inz@inz.fi>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
(function (xprt) {
var mysql = require('mysql');
var crypto = require('crypto');
var fs = require('fs');
var groan = require('groan');
function Validator(conffile) {
var conf_data = fs.readFileSync(conffile, { encoding: 'utf8' });
var re = /define\s*\(\s*(?:'([^']+)'|"([^"]+)")\s*,\s*(?:'([^']+)'|"([^"]+)")\)/gm;
var mtch;
var required_fields = [
'DB_HOST',
'DB_USER',
'DB_PASSWORD',
'DB_NAME',
'LOGGED_IN_KEY',
'LOGGED_IN_SALT',
'table_prefix'
];
this.config = {};
while ((mtch = re.exec(conf_data)))
this.config[mtch[1] || mtch[2]] = mtch[3] || mtch[4];
if ((mtch = conf_data.match(/(?:;|^|\n)\s*\$table_prefix\s*=\s*(['"])([a-zA-Z0-9_]+)\1/m)))
this.config.table_prefix = mtch[2];
var missing = required_fields.filter((function (rf) { return !this.config.hasOwnProperty(rf); }).bind(this));
if (missing.length > 0)
throw new Error('Fields missing from config: ' + missing.join(', '))
this.c = mysql.createPool({
connectionLimit: 10,
host: this.config.DB_HOST,
user: this.config.DB_USER,
password: this.config.DB_PASSWORD,
database: this.config.DB_NAME,
insecureAuth: true
});
if (!this.config.COOKIEHASH) {
this.c.query('SELECT MD5(option_value) AS hash FROM ?? WHERE option_name = ?;', [this.config.table_prefix + 'options', 'siteurl'], (function (e, r, f) {
this.config.COOKIEHASH = r[0].hash;
}).bind(this));
}
}
Validator.prototype.cookieHash = function () {
return this.config.COOKIEHASH;
}
Validator.prototype.authenticate = function (cookies, cb) {
var token;
if ((token = cookies['wordpress_logged_in_' + this.cookieHash()]))
this.validate(token, cb);
else
cb('No cookie', null);
}
Validator.prototype.validate = function (token, cb) {
var parts = token.split('|');
if (parts.length != 4) {
cb('Invalid cookie', null);
return;
}
if (parts[1] * 1000 + 3600000 < new Date()) {
cb('Expired session', null);
return;
}
this.c.query('SELECT SUBSTRING(u.user_pass, 9, 4) AS frag, m.meta_value AS tokens FROM ?? u LEFT JOIN ?? m ON u.ID = m.user_id WHERE u.user_login = ? AND m.meta_key = ?;', [this.config.table_prefix + 'users', this.config.table_prefix + 'usermeta', parts[0], 'session_tokens'], (function (e, r, f) {
if (e || r.length == 0) {
cb('User not found', null);
return;
}
try {
var hs_sha256 = crypto.createHash('sha256');
hs_sha256.update(parts[2]);
var tokendata = (groan(r[0].tokens) || {})[hs_sha256.digest('hex')];
if (parseInt(parts[1]) !== tokendata.expiration) {
cb('Session data mismatch', null);
return;
}
var hm_md5 = crypto.createHmac('md5', this.config.LOGGED_IN_KEY + this.config.LOGGED_IN_SALT);
hm_md5.update(parts[0] + '|' + r[0].frag + '|' + parts[1] + '|' + parts[2]);
var key = hm_md5.digest('hex');
var hm_sha256 = crypto.createHmac('sha256', key);
hm_sha256.update(parts[0] + '|' + parts[1] + '|' + parts[2]);
} catch (e) {
cb(e.message, null);
return;
}
if (parts[3] === hm_sha256.digest('hex'))
cb(null, parts[0]);
else
cb('Cookie checksum mismatch', null);
}).bind(this));
};
xprt.validator = Validator;
})(this);