{"size": 665, "ext": "js", "max_stars_repo_path": "server/api/$middleware/auth.js", "max_stars_repo_name": "Hoaku/Server", "max_stars_repo_stars_event_min_datetime": "2022-01-15T08:55:22.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-22T20:08:19.000Z", "max_issues_repo_path": "server/api/$middleware/auth.js", "max_issues_repo_name": "Hoaku/Hoaku", "max_issues_repo_issues_event_min_datetime": "2022-01-15T09:56:06.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-21T13:33:42.000Z", "max_forks_repo_path": "server/api/$middleware/auth.js", "max_forks_repo_name": "Hoaku/Hoaku", "max_forks_repo_forks_event_min_datetime": "2022-01-20T17:09:40.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-21T09:24:26.000Z"}
{"max_stars_count": 4, "max_issues_count": 26, "max_forks_count": 2, "avg_line_length": 35.0, "max_line_length": 119, "alphanum_fraction": 0.6496240602}
const aes256 = require('aes256');

const hoakuConfig = require("../../../hoaku.config");

module.exports = async (req, res, next) => {
    const rawToken = req.header("Authorization");
    if (!rawToken) return res.send({ status: "UNAUTHORIZED" });
    const token = aes256.decrypt(hoakuConfig.sensitive.tokenEncryptionKey, Buffer.from(rawToken, "base64")).toString();
    
    const { db } = req;
    const users = db.collection("users");

    const accounts = await users.find({ token }).toArray();
    if (accounts.length == 0) return res.send({ status: "UNAUTHORIZED" });

    req.username = accounts[0].username;
    req.userID = accounts[0]._id;
    next();
}