apxtrib/api/routes/pagans.js

283 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2023-05-12 05:59:32 +00:00
const express = require("express");
2023-11-05 11:03:25 +00:00
const fs = require("fs-extra");
2023-05-12 05:59:32 +00:00
const path = require("path");
2023-01-22 09:53:09 +00:00
// Classes
2023-05-12 05:59:32 +00:00
const Pagans = require("../models/Pagans.js");
2023-12-05 06:42:35 +00:00
const Odmdb = require("../models/Odmdb.js");
2023-01-22 09:53:09 +00:00
// Middlewares
2023-05-12 05:59:32 +00:00
const checkHeaders = require("../middlewares/checkHeaders");
const isAuthenticated = require("../middlewares/isAuthenticated");
2023-01-22 09:53:09 +00:00
2023-12-05 06:42:35 +00:00
const conf = require(`${process.env.dirtown}/conf.json`);
2023-11-05 11:03:25 +00:00
const router = express.Router();
/**
* /api/models/Pagans.js
*
* Managed:
2023-01-22 09:53:09 +00:00
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* Alias exist then return public key or not
* @api {get} /pagans/alias/:alias - alias Get
* @apiName isalias
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
2023-11-19 15:34:37 +00:00
* @apiDescription If alias exist return its publickey
*
* @param {string} alias
*
* @apiError {json} aliasdoesnotexist
* @apiErrorExample {json}
* HTTP/1.1 404 Not Found
{"status":404,"ref":"pagans","msg":"aliasdoesnotexist","data": { alias}}
2023-11-05 11:03:25 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiSuccess {object} indexfile content
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {"status":200, ref:"pagans","msg":"aliasexist","data": { alias, publicKey }}
* *
2023-11-05 11:03:25 +00:00
**/
2023-06-07 05:32:23 +00:00
router.get("/alias/:alias", (req, res) => {
2023-11-05 11:03:25 +00:00
const getalias = Pagans.getalias(req.params.alias);
res.status(getalias.status).send(getalias);
});
/**
2023-11-19 15:34:37 +00:00
* Remove serveur token
* @api {get} /pagans/logout - pagan Logout
* @apiName Removetoken
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
2023-12-05 06:42:35 +00:00
* @apiDescription Remove token
*
2023-11-19 15:34:37 +00:00
* @apiSuccess {object} indexfile content
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {status: 200, ref: "Pagans", msg: "logout"
2023-11-05 11:03:25 +00:00
*
*/
router.get("/logout", checkHeaders, isAuthenticated, (req, res) => {
console.log(req.session.header);
const logout = Pagans.logout(
req.session.header.xalias,
req.session.header.xtribe,
req.session.header.xdays,
req.session.header.xhash
);
res.status(logout.status).json(logout);
2023-06-07 05:32:23 +00:00
});
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api {get} /pagans/isauth - pagan isAuthenticated?
* @apiName isAuth
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
2023-11-19 15:34:37 +00:00
* @apiDescription Check if pagan's token is still valid
2023-11-05 11:03:25 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiError (400) missingheaders
* @apiError (400) xaliasdoesnotexist
* @apiError (400) signaturefailled
* @apiError (401) aliasanonymous
* @apiError (404) tribedoesnotexist
2023-11-05 11:03:25 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiSuccess (200) valid
* {object} data contains indexfile requested
2023-11-05 11:03:25 +00:00
*
*/
2023-05-12 05:59:32 +00:00
router.get("/isauth", checkHeaders, isAuthenticated, (req, res) => {
2023-11-05 11:03:25 +00:00
res.status(200).send({
2023-05-12 05:59:32 +00:00
status: 200,
ref: "headers",
msg: "authenticated",
data: {
xalias: req.session.header.xalias,
2023-11-05 11:03:25 +00:00
xprofils: req.session.header.xprofils,
2023-05-12 05:59:32 +00:00
},
});
});
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api {post} /pagans - pagan Post
* @apiName addpagan
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
2023-11-19 15:34:37 +00:00
* @apiDescription
* Create a pagan account from alias, publickey, if trusted recovery =>
2023-11-05 11:03:25 +00:00
* Create a person in xtribe/person/xalias.json with profil.auth={email,privatekey, passphrase}
2023-11-19 15:34:37 +00:00
* Middleware isAuthenticated check that:
2023-11-05 11:03:25 +00:00
* - xhash is well signed from private key linked to the publickey of alias
* - check that alias does not already exist (if yes then verifiedsigne would be false)
* Need to wait next block chain to be sure that alias is register in the blokchain
2023-12-05 06:42:35 +00:00
* @apiBody {string} alias available (that does not already exist check get /api/alias/:alias that must return 404).
* @apiBody {string} publickey
* @apiBody {string} [email] if specified then an email is sent to it with public and privatekey
* @apiBody {string} [privatekey]
* @apiBody {string} [passphrase] if not specidied => passphrase=""
* @apiBody {string} [trustedtribe] the tribename if not specified then the process will only create a pagan identity, else an item person is create for trustedtribe (that must exist with profil 'person'). To create a person with an existing pagan identity use put /api/person/:alias after authenticated you (headers). In case a person is created then we use all valid other apiBody respecting rules https://smatchit.io/api/odmdb/schema/persons.json
2023-11-19 15:34:37 +00:00
*
* @apiError {json} objectNotfound the file does not exist
* @apiErrorExample {json}
* HTTP/1.1 404 Not Found
{"status":404,"ref":"Odmdb","msg":"pathnamedoesnotexist","data":{indexpath}}
*
* @apiSuccess {object} indexfile content
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {"status":200, "ref":"Odmdb", "msg":"indexexist", "data":{indexname,content:{index file}}
*
2023-11-05 11:03:25 +00:00
*/
2023-12-05 06:42:35 +00:00
router.post("/", checkHeaders, isAuthenticated, async (req, res) => {
console.log("pass ici", req.body);
const role = {
2023-11-05 11:03:25 +00:00
xalias: req.session.header.xalias,
xprofils: req.session.header.xprofils,
2023-12-05 06:42:35 +00:00
};
const emailregex =
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!(req.body.trustedtribe && req.body.email && emailregex.test(req.body.email) )) {
res.status(400).json({status:400,ref:"Pagans",msg:"emailerr", data:{email:req.body.email}})
return
}
const objpagan = { alias: req.body.alias, publickey: req.body.publickey };
const newpagan = Odmdb.cud(`${conf.dirapi}/nationchains/pagans`, "C", objpagan, role);
const createprocess={status:200, ref:"Pagans", msg:"successfulcreate",data:{alias:req.body.alias}};
2023-05-12 05:59:32 +00:00
if (newpagan.status == 200) {
if (req.body.email) {
2023-12-05 06:42:35 +00:00
const emailsent = await Pagans.sendmailkey(
2023-11-05 11:03:25 +00:00
req.body.alias,
req.body.privatekey,
req.session.header.xtribe,
req.body.passphrase,
req.body.publickey,
2023-12-05 06:42:35 +00:00
req.body.email,
req.session.header.xlang
2023-11-05 11:03:25 +00:00
);
2023-12-05 06:42:35 +00:00
createprocess.data.emailsent = (emailsent.status == 200);
createprocess.data.email=req.body.email
createprocess.data.tribe=req.session.header.xtribe;
if (emailsent.status!=200) {
console.log("err emailsent: ",emailsent)
createprocess.data.emailerror = emailsent.data.err;
}
2023-05-12 05:59:32 +00:00
}
if (req.body.trustedtribe) {
2023-12-05 06:42:35 +00:00
const persondata = {
alias: req.body.alias,
owner: req.body.alias,
profils: ["pagans", "persons"],
recoveryauth: {
email: req.body.email,
privatekey: req.body.privatekey,
publickey: req.body.publickey,
passphrase: req.body.passphrase,
},
};
const personup = Odmdb.cud(`${conf.dirtown}/tribes/${req.body.trustedtribe}/objects/persons`, "C", persondata, {xprofils:["pagan"],xalias:req.body.alias});
console.log('personup',personup)
if (personup.status==200){
createprocess.data.createperson=true;
}else{
createprocess.data.createperson=false;
createprocess.data.errorperson=true;
createprocess.data.errpersonup=personup.data;
console.log("Warning pagan created but person not created and no recovery registration", personup);
}
res.status(createprocess.status).json(createprocess);
}else{
2023-06-28 13:23:17 +00:00
res.status(newpagan.status).json(newpagan);
2023-05-12 05:59:32 +00:00
}
} else {
2023-11-05 11:03:25 +00:00
//error to create pagan certaily already exist
res.status(newpagan.status).json(newpagan);
2023-05-12 05:59:32 +00:00
}
});
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api {delete} /pagans/alias/:alias - pagan Delete
* @apiName deletepagan
* @apiGroup Pagans
* @apiDescription
2023-12-05 06:42:35 +00:00
* Delete an alias and his publickey, this mean that publickey disapear as well as alias. All tribe will be inform and will delete person of this alias if they have. This alias will be availlable after 1 year.
2023-11-19 15:34:37 +00:00
* */
router.delete("/alias/:alias", checkHeaders, isAuthenticated, (req, res) => {
console.log(`DELETE pagans nationchains/pagans/${req.params.alias}.json`);
const result = Pagans.deletealias(req.params.id, req.session.header);
2023-12-05 06:42:35 +00:00
res.status(result.status).send(result);
2023-11-19 15:34:37 +00:00
});
2023-12-05 06:42:35 +00:00
/**
* @api {delete} /pagans/person/:alias - person Delete
* @apiName deleteperson
* @apiGroup Pagans
* @apiDescription
* Unsubscribe a person to a tribe => remove a person item and all data link to this alias
* */
2023-11-19 15:34:37 +00:00
router.delete("/person/:alias", checkHeaders, isAuthenticated, (req, res) => {
2023-12-05 06:42:35 +00:00
const personpath=`${conf.dirtown}/tribes/${req.session.header.xtribe}/objects/persons`;
const role = {
xalias: req.session.header.xalias,
xprofils: req.session.header.xprofils,
};
req.session.header.role
const delperson = Odmdb.cud(personpath,"D",{alias:req.params.alias},role,true);
console.log(`DELETE person ${personpath}/${req.params.alias}.json `);
console.log(delperson)
res.status(delperson.status).json(delperson);
2023-11-19 15:34:37 +00:00
});
/**
2023-12-05 06:42:35 +00:00
* @api {get} /pagans/person/:alias - person Get
2023-11-19 15:34:37 +00:00
* @apiName getpersondata
2023-12-05 06:42:35 +00:00
* @apiDescription Get person information from his alias for a xtribe (data and profils per apps)
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
*
2023-11-19 15:34:37 +00:00
* @apiParam {string} alias
2023-12-05 06:42:35 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiSuccess (200) personExist
* @apiSuccessExample {json}
* {status:200, ref:"pagans",msg:"personexist",data: { person } }
2023-12-05 06:42:35 +00:00
*
2023-11-19 15:34:37 +00:00
* @apiError (404) Notfound
* @apiErrorExample {json}
* {status: 404, ref:"pagans",msg:"persondoesnotexist",data: { person } }
*
* @todo check accessright for req.session.header.xalias to see if jhe can get person data
* if req.param.alias == req.session.header.xalias => Owner
* else need accessright to on person set at R
* */
router.get("/person/:alias", checkHeaders, isAuthenticated, (req, res) => {
2023-12-05 06:42:35 +00:00
const getperson=Odmdb.r( `${conf.dirtown}/tribes/${req.session.header.xtribe}/objects/persons`,req.params.alias,{ xprofils: req.session.header.xprofils, xalias: req.session.header.xalias })
2023-11-19 15:34:37 +00:00
res.status(getperson.status).send(getperson);
});
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api {put} /pagans/person - person Put
* @apiName updateperson
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
2023-12-05 06:42:35 +00:00
* @apiDescription add or update a person = alias in a tribe. alias authenticated must have a profil with accessright into schema person.
* @apiHeader {string} xalias
* @apiParam {object} in line with schema in https://smatchit.io/api/odmdb/schema/persons
*
2023-11-05 11:03:25 +00:00
*/
2023-05-12 05:59:32 +00:00
router.put("/person", checkHeaders, isAuthenticated, (req, res) => {
2023-06-28 13:23:17 +00:00
//console.log(req.body);
2023-12-05 06:42:35 +00:00
const pathobj=`${conf.dirtown}/tribes/${req.session.header.xtribe}/objects/persons`;
const action = (fs.existsSync(`${pathobj}/itm/${req.body.alias}.json`))? "U":"C";
//set req.body to be in line with schema
if (!req.body.profils){
req.body.profils=["anonymous","pagans","persons"]
}
const personup = Odmdb.cud(pathobj, action, req.body, {xprofils:req.session.header.xprofils, xalias:req.session.header.xalias});
console.log('personup',personup)
res.status(personup.status).json(personup);
2023-05-12 05:59:32 +00:00
});
2023-11-05 11:03:25 +00:00
/**
2023-11-19 15:34:37 +00:00
* @api {get} /pagans/keyrecovery/tribe/email - recovery key by email
* @apiName recoveryKey
2023-11-05 11:03:25 +00:00
* @apiGroup Pagans
*
* @apiError (400) {object} status missingheaders / xalias does not exist / signaturefailled
* @apiError (401) {object} alias anonymous (not authenticated)
* @apiError (404) {string} tribe does not exist
*
* @apiSuccess (200) {object} data contains indexfile requested
*
*/
2023-06-12 05:27:34 +00:00
router.get("/keyrecovery/:tribeid/:email", checkHeaders, (req, res) => {
res.send(Pagans.keyrecovery(req.params.tribeId, req.params.email));
2023-05-12 05:59:32 +00:00
});
2023-01-22 09:53:09 +00:00
module.exports = router;