2023-04-27 06:17:20 +02:00
|
|
|
const fs = require("fs-extra");
|
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const glob = require("glob");
|
2023-05-12 07:59:32 +02:00
|
|
|
const openpgp = require("openpgp");
|
2023-04-27 06:17:20 +02:00
|
|
|
|
2023-05-16 10:31:27 +02:00
|
|
|
const conf = require(`${process.env.dirtown}/conf.json`);
|
2023-04-27 06:17:20 +02:00
|
|
|
|
2023-05-12 07:59:32 +02:00
|
|
|
const isAuthenticated = async (req, res, next) => {
|
|
|
|
// once a day rm oldest tokens than 24hours tag job by adding tmp/tokensmenagedone{day}
|
2023-04-27 06:17:20 +02:00
|
|
|
const currentday = dayjs().date();
|
|
|
|
console.log(
|
2023-05-12 07:59:32 +02:00
|
|
|
"if menagedone" + currentday,
|
2023-05-16 10:31:27 +02:00
|
|
|
!fs.existsSync(`${process.env.dirtown}/tmp/tokensmenagedone${currentday}`)
|
2023-04-27 06:17:20 +02:00
|
|
|
);
|
2023-05-16 10:31:27 +02:00
|
|
|
if (!fs.existsSync(`${process.env.dirtown}/tmp/tokens`))
|
|
|
|
fs.mkdirSync(`${process.env.dirtown}/tmp/tokens`);
|
|
|
|
if (!fs.existsSync(`${process.env.dirtown}/tmp/tokensmenagedone${currentday}`)) {
|
2023-04-27 06:17:20 +02:00
|
|
|
// clean oldest
|
2023-05-12 07:59:32 +02:00
|
|
|
const tsday = dayjs().valueOf(); // now in timestamp format
|
2023-05-16 10:31:27 +02:00
|
|
|
glob.sync(`${process.env.dirtown}/tmp/tokensmenagedone*`).forEach((f) => {
|
2023-04-27 06:17:20 +02:00
|
|
|
fs.removeSync(f);
|
|
|
|
});
|
2023-05-16 10:31:27 +02:00
|
|
|
glob.sync(`${process.env.dirtown}/tmp/tokens/*.json`).forEach((f) => {
|
2023-05-12 07:59:32 +02:00
|
|
|
if (tsday - parseInt(f.split("_")[1]) > 86400000) fs.remove(f);
|
2023-04-27 06:17:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
//Check register in tmp/tokens/
|
2023-05-12 07:59:32 +02:00
|
|
|
console.log("isAuthenticate?");
|
2023-04-27 06:17:20 +02:00
|
|
|
const resnotauth = {
|
|
|
|
ref: "headers",
|
|
|
|
msg: "notauthenticated",
|
|
|
|
data: {
|
|
|
|
xalias: req.session.header.xalias,
|
2023-05-12 07:59:32 +02:00
|
|
|
xaliasexists: true,
|
2023-04-27 06:17:20 +02:00
|
|
|
},
|
|
|
|
};
|
2023-05-12 07:59:32 +02:00
|
|
|
console.log(req.session.header);
|
|
|
|
if (req.session.header.xalias == "anonymous") {
|
|
|
|
console.log("alias anonymous means not auth");
|
|
|
|
return res.status(401).json(resnotauth);
|
|
|
|
}
|
2023-04-27 06:17:20 +02:00
|
|
|
|
2023-05-16 10:31:27 +02:00
|
|
|
const tmpfs = `${process.env.dirtown}/tmp/tokens/${req.session.header.xalias}_${
|
2023-05-12 07:59:32 +02:00
|
|
|
req.session.header.xdays
|
|
|
|
}_${req.session.header.xhash.substring(20, 200)}`;
|
|
|
|
console.log(tmpfs);
|
|
|
|
if (!fs.existsSync(tmpfs)) {
|
|
|
|
// need to check detached sign
|
|
|
|
let publickey;
|
2023-04-27 06:17:20 +02:00
|
|
|
if (
|
2023-05-12 07:59:32 +02:00
|
|
|
fs.existsSync(
|
2023-05-16 10:31:27 +02:00
|
|
|
`${conf.dirapi}/nationchains/pagans/itm/${req.session.header.xalias}.json`
|
2023-04-27 06:17:20 +02:00
|
|
|
)
|
|
|
|
) {
|
2023-05-12 07:59:32 +02:00
|
|
|
const pagan = fs.readJsonSync(
|
2023-05-16 10:31:27 +02:00
|
|
|
`${conf.dirapi}nationchains/pagans/itm/${req.session.header.xalias}.json`
|
2023-04-27 06:17:20 +02:00
|
|
|
);
|
2023-05-12 07:59:32 +02:00
|
|
|
publickey = pagan.publicKey;
|
2023-04-27 06:17:20 +02:00
|
|
|
} else {
|
2023-05-12 07:59:32 +02:00
|
|
|
resnotauth.data.xaliasexists = false;
|
|
|
|
if (req.body.publickey) {
|
|
|
|
publickey = req.body.publickey;
|
|
|
|
} else {
|
|
|
|
console.log("alias unknown");
|
|
|
|
return res.status(404).send(resnotauth);
|
2023-04-27 06:17:20 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-12 07:59:32 +02:00
|
|
|
console.log(publickey);
|
|
|
|
console.log(Buffer.from(req.session.header.xhash, "base64").toString());
|
|
|
|
const publicKey = await openpgp.readKey({ armoredKey: publickey });
|
|
|
|
const msg = await openpgp.createMessage({
|
|
|
|
text: `${req.session.header.xalias}_${req.session.header.xdays}`,
|
|
|
|
});
|
|
|
|
const signature = await openpgp.readSignature({
|
|
|
|
armoredSignature: Buffer.from(
|
|
|
|
req.session.header.xhash,
|
|
|
|
"base64"
|
|
|
|
).toString(),
|
|
|
|
});
|
|
|
|
console.log(msg);
|
|
|
|
console.log(signature);
|
|
|
|
console.log(publicKey);
|
|
|
|
const checkauth = await openpgp.verify({
|
|
|
|
message: msg,
|
|
|
|
signature: signature,
|
|
|
|
verificationKeys: publicKey,
|
|
|
|
});
|
|
|
|
console.log(checkauth);
|
|
|
|
console.log(checkauth.signatures[0].keyID);
|
|
|
|
//console.log(await checkauth.signatures[0].signature);
|
|
|
|
//console.log(await checkauth.signatures[0].verified);
|
|
|
|
|
|
|
|
const { check, keyID } = checkauth.signatures[0];
|
|
|
|
try {
|
|
|
|
await check; // raise an error if necessary
|
|
|
|
fs.outputFileSync(tmpfs, req.session.header.xhash, "utf8");
|
|
|
|
} catch (e) {
|
|
|
|
resnotauth.msg = "signaturefailed";
|
|
|
|
console.log("not auth fail sign");
|
|
|
|
return res.status(401).send(resnotauth);
|
|
|
|
}
|
2023-04-27 06:17:20 +02:00
|
|
|
}
|
2023-05-12 07:59:32 +02:00
|
|
|
console.log("Authenticated");
|
|
|
|
next();
|
2023-04-27 06:17:20 +02:00
|
|
|
};
|
|
|
|
module.exports = isAuthenticated;
|