apxtrib/api/routes/pagans.js

227 lines
7.0 KiB
JavaScript
Raw 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-11-05 11:03:25 +00:00
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-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
/**
* @api {get} /pagans/alias/:alias
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @param {string} alias a alias that exist or not
* @apiSuccess (200) {object} {ref:"pagans",msg:"aliasexist",data: { alias, publicKey } }
* @apiError (404) {object} {ref:"pagans",msg:"aliasdoesnotexist",data: { alias} }
*
**/
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);
});
/**
* @api {get} /pagans/logout
* @apiName Remove token
* @apiGroup Pagans
*
*/
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
/**
* @api {get} /pagans/person:alias
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
* @param {string} alias that exist
* @param {string} tribeId that exist with a person alias
* @apiSuccess (200) {ref:"pagans",msg:"personexist",data: { person } }
* @apiError (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
* */
2023-06-12 05:27:34 +00:00
router.get("/person/:alias", checkHeaders, isAuthenticated, (req, res) => {
2023-11-05 11:03:25 +00:00
const getperson = Pagans.getperson(
req.session.header.xtribe,
req.params.alias,
{ xprofils: req.session.header.xprofils, xalias: req.session.header.xalias }
);
res.status(getperson.status).send(getperson);
2023-06-07 05:32:23 +00:00
});
2023-04-27 04:17:20 +00:00
2023-11-05 11:03:25 +00:00
/**
* @api {get} /pagans/isauth
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
*
* @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-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
/**
* @api {post} /pagans
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
*
* Create a pagan account from alias, publickey, if trusted recovery =>
* Create a person in xtribe/person/xalias.json with profil.auth={email,privatekey, passphrase}
* Middleware isAuthenticated check that:
* - 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-05-12 05:59:32 +00:00
router.post("/", checkHeaders, isAuthenticated, (req, res) => {
2023-06-28 13:23:17 +00:00
//console.log("pass ici", req.body);
2023-11-05 11:03:25 +00:00
const objpagan = { alias: req.body.alias, publickey: req.body.publickey };
const newpagan = Pagans.create(objpagan, {
xalias: req.session.header.xalias,
xprofils: req.session.header.xprofils,
});
2023-05-12 05:59:32 +00:00
if (newpagan.status == 200) {
if (req.body.email) {
2023-11-05 11:03:25 +00:00
const emailsent = Pagans.sendmailkey(
req.body.alias,
req.body.privatekey,
req.session.header.xtribe,
req.body.passphrase,
req.body.publickey,
req.body.email
);
2023-05-12 05:59:32 +00:00
}
if (req.body.trustedtribe) {
2023-11-05 11:03:25 +00:00
const personup = Pagans.personupdate(
req.body.alias,
req.body.trustedtribe,
{
recoveryauth: {
email: req.body.email,
privatekey: req.body.privatekey,
publickey: req.body.publickey,
passphrase: req.body.passphrase,
},
}
);
if (personup.status !== 200)
console.log("Warning no recovery registration", personup);
}
if (emailsent && emailsent.status != 200) {
newpagan.msg = "successfulcreatewithoutemail";
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
/**
* @api {post} /pagans/person
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
*
* add a person = alias + tribe with specific accessright and specific schema link to tribe
* @todo add tribe/schema/person.json
*/
router.post("/person", checkHeaders, isAuthenticated, (req, res) => {
//console.log(req.body);
const persoad = Pagans.personcreate(
req.session.header.xtribe,
req.body.alias,
req.body,
{ xprofils: req.session.header.xprofils, xalias: req.session.header.xalias }
);
res.status(persoad.status).json(persoad);
});
/**
* @api {put} /pagans/person
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
*
* update a person = alias + tribe with specific accessright and specific schema link to tribe
* @todo add tribe/schema/person.json
*/
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-11-05 11:03:25 +00:00
const persoup = Pagans.personupdate(
req.session.header.xtribe,
req.body.alias,
req.body,
{ xprofils: req.session.header.xprofils, xalias: req.session.header.xalias }
);
2023-06-28 13:23:17 +00:00
res.status(persoup.status).json(persoup);
2023-05-12 05:59:32 +00:00
});
2023-11-05 11:03:25 +00:00
/**
* @api {delete} /pagans/alias/:alias
* @apiName Is register check xalias and xhash
* @apiGroup Pagans
* @apiUse apxHeader
* */
router.delete("/alias/:alias", checkHeaders, isAuthenticated, (req, res) => {
2023-05-12 05:59:32 +00:00
console.log(`DELETE pagans nationchains/pagans/${req.params.alias}.json`);
2023-11-05 11:03:25 +00:00
const result = Pagans.deletealias(req.params.id, req.session.header);
2023-05-12 05:59:32 +00:00
res.status(result.status).send(result.data);
});
2023-11-05 11:03:25 +00:00
router.delete("/person/:alias", checkHeaders, isAuthenticated, (req, res) => {
console.log(`DELETE pagans nationchains/pagans/${req.params.alias}.json`);
const result = Pagans.deleteperson(req.params.id, req.session.header);
res.status(result.status).send(result.data);
});
/**
* @api {get} /pagans/keyrecovery/tribe/email
* @apiName apxtrib
* @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;