2015-02-16 03:56:00 +00:00
|
|
|
var dbAccounts = require("./database/accounts");
|
|
|
|
var crypto = require("crypto");
|
|
|
|
|
|
|
|
function sha256(input) {
|
|
|
|
var hash = crypto.createHash("sha256");
|
|
|
|
hash.update(input);
|
|
|
|
return hash.digest("base64");
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.genSession = function (account, expiration, cb) {
|
|
|
|
if (expiration instanceof Date) {
|
|
|
|
expiration = Date.parse(expiration);
|
|
|
|
}
|
|
|
|
|
|
|
|
var salt = crypto.pseudoRandomBytes(24).toString("base64");
|
|
|
|
var hashInput = [account.name, account.password, expiration, salt].join(":");
|
|
|
|
var hash = sha256(hashInput);
|
|
|
|
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
cb(null, [account.name, expiration, salt, hash, account.global_rank].join(":"));
|
2015-02-16 03:56:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.verifySession = function (input, cb) {
|
2015-02-20 02:30:35 +00:00
|
|
|
if (typeof input !== "string") {
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
return cb(new Error("Invalid auth string"));
|
2015-02-20 02:30:35 +00:00
|
|
|
}
|
|
|
|
|
2015-02-16 03:56:00 +00:00
|
|
|
var parts = input.split(":");
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
if (parts.length !== 4 && parts.length !== 5) {
|
|
|
|
return cb(new Error("Invalid auth string"));
|
2015-02-16 03:56:00 +00:00
|
|
|
}
|
|
|
|
|
2018-04-07 22:30:30 +00:00
|
|
|
const [name, expiration, salt, hash, _global_rank] = parts;
|
2015-02-16 03:56:00 +00:00
|
|
|
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
if (Date.now() > parseInt(expiration, 10)) {
|
|
|
|
return cb(new Error("Session expired"));
|
2015-02-16 03:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbAccounts.getUser(name, function (err, account) {
|
|
|
|
if (err) {
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
if (!(err instanceof Error)) err = new Error(err);
|
2015-02-16 03:56:00 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
var hashInput = [account.name, account.password, expiration, salt].join(":");
|
|
|
|
if (sha256(hashInput) !== hash) {
|
Skip full user auth for most page renders
Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
2017-08-02 04:40:26 +00:00
|
|
|
return cb(new Error("Invalid auth string"));
|
2015-02-16 03:56:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, account);
|
|
|
|
});
|
|
|
|
};
|