DocSpace-client/common/ASC.Socket.IO/app/middleware/authService.js

36 lines
851 B
JavaScript
Raw Normal View History

2020-08-02 20:12:45 +00:00
module.exports = () => {
const config = require("../../config"),
crypto = require("crypto"),
moment = require("moment");
2020-08-02 20:12:45 +00:00
2022-02-24 13:50:02 +00:00
const skey = config.get("core").machinekey;
const trustInterval = 5 * 60 * 1000;
2020-08-02 20:12:45 +00:00
function check(token) {
if (!token || typeof token !== "string") return false;
2020-08-02 20:12:45 +00:00
const splitted = token.split(":");
if (splitted.length < 3) return false;
2020-08-02 20:12:45 +00:00
const pkey = splitted[0].substr(4);
const date = splitted[1];
const orighash = splitted[2];
2020-08-02 20:12:45 +00:00
const timestamp = moment.utc(date, "YYYYMMDDHHmmss");
if (moment.utc() - timestamp > trustInterval) {
return false;
}
2020-08-02 20:12:45 +00:00
const hasher = crypto.createHmac("sha1", skey);
const hash = hasher.update(date + "\n" + pkey);
2020-08-02 20:12:45 +00:00
if (hash.digest("base64") !== orighash) {
return false;
2020-08-02 20:12:45 +00:00
}
return true;
}
return check;
};