Allow trusted IDs to add/rm user tags via API
This commit is contained in:
parent
b5cde90e89
commit
5ac3316008
56
public/fn.js
56
public/fn.js
@ -405,4 +405,60 @@ function withdrawRupee(quantity, proxySecret) {
|
|||||||
.catch(error => reject(error)))
|
.catch(error => reject(error)))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function addUserTag(floID, tag, proxySecret) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request = {
|
||||||
|
user: floID,
|
||||||
|
tag: tag,
|
||||||
|
timestamp: Date.now()
|
||||||
|
};
|
||||||
|
request.sign = signRequest({
|
||||||
|
command: "add_Tag",
|
||||||
|
user: request.user,
|
||||||
|
tag: request.tag,
|
||||||
|
timestamp: request.timestamp
|
||||||
|
}, proxySecret);
|
||||||
|
console.debug(request);
|
||||||
|
|
||||||
|
fetch('/add-tag', {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request)
|
||||||
|
}).then(result => responseParse(result, false)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error)))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeUserTag(floID, tag, proxySecret) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let request = {
|
||||||
|
user: floID,
|
||||||
|
tag: tag,
|
||||||
|
timestamp: Date.now()
|
||||||
|
};
|
||||||
|
request.sign = signRequest({
|
||||||
|
command: "remove_Tag",
|
||||||
|
user: request.user,
|
||||||
|
tag: request.tag,
|
||||||
|
timestamp: request.timestamp
|
||||||
|
}, proxySecret);
|
||||||
|
console.debug(request);
|
||||||
|
|
||||||
|
fetch('/remove-tag', {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(request)
|
||||||
|
}).then(result => responseParse(result, false)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error)))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
@ -16,7 +16,9 @@ try {
|
|||||||
"sql_host": "localhost",
|
"sql_host": "localhost",
|
||||||
|
|
||||||
"backup-port": "8081",
|
"backup-port": "8081",
|
||||||
"backup-floIDs": []
|
"backup-floIDs": [],
|
||||||
|
|
||||||
|
"trusted-floIDs": []
|
||||||
};
|
};
|
||||||
flag_new = true;
|
flag_new = true;
|
||||||
}
|
}
|
||||||
@ -32,14 +34,14 @@ function flaggedYesOrNo(text) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBackupIDs(ids) {
|
function get_IDs(ids) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getInput("", "continue").then(id => {
|
getInput.Text("", "continue").then(id => {
|
||||||
if (id === "continue")
|
if (id === "continue")
|
||||||
resolve(Array.from(new Set(ids)));
|
resolve(Array.from(new Set(ids)));
|
||||||
else {
|
else {
|
||||||
ids.push(id);
|
ids.push(id);
|
||||||
getBackupIDs(ids)
|
get_IDs(ids)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
}
|
}
|
||||||
@ -55,8 +57,8 @@ function configureBackup() {
|
|||||||
return resolve(true);
|
return resolve(true);
|
||||||
getInput.YesOrNo('Do you want to add/remove backup floIDs?').then(value => {
|
getInput.YesOrNo('Do you want to add/remove backup floIDs?').then(value => {
|
||||||
if (value) {
|
if (value) {
|
||||||
console("Enter floIDs to add as backup: ");
|
console.log("Enter floIDs to add as backup: ");
|
||||||
getBackupIDs(config["backup-floIDs"]).then(ids => {
|
get_IDs(config["backup-floIDs"]).then(ids => {
|
||||||
//delete backup IDs
|
//delete backup IDs
|
||||||
let tmp_obj = {};
|
let tmp_obj = {};
|
||||||
for (let i in ids) {
|
for (let i in ids) {
|
||||||
@ -84,12 +86,45 @@ function configureBackup() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function configureTrustedIDs() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
getInput.YesOrNo('Do you want to add/remove trusted floIDs?').then(value => {
|
||||||
|
if (value) {
|
||||||
|
console.log("Enter floIDs to add as trusted: ");
|
||||||
|
get_IDs(config["trusted-floIDs"]).then(ids => {
|
||||||
|
//delete trusted IDs
|
||||||
|
let tmp_obj = {};
|
||||||
|
for (let i in ids) {
|
||||||
|
console.log(i + 1, ":", ids[i]);
|
||||||
|
tmp_obj[i + 1] = ids[i];
|
||||||
|
}
|
||||||
|
getInput.Text("Enter numbers to delete (seperated by comma)", "continue").then(ri => {
|
||||||
|
if (ri === "continue")
|
||||||
|
config["trusted-floIDs"] = ids;
|
||||||
|
else {
|
||||||
|
for (let i of ri.split(","))
|
||||||
|
delete tmp_obj[parseInt(i)];
|
||||||
|
let tmp_array = [];
|
||||||
|
for (let id of tmp_obj)
|
||||||
|
tmp_array.push(id);
|
||||||
|
config["trusted-floIDs"] = tmp_array;
|
||||||
|
}
|
||||||
|
resolve(true);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else
|
||||||
|
resolve(true);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function configurePort() {
|
function configurePort() {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
getInput.Text('Enter port', config["port"]).then(port => {
|
getInput.Text('Enter port', config["port"]).then(port => {
|
||||||
config["port"] = port;
|
config["port"] = port;
|
||||||
configureBackup()
|
configureBackup()
|
||||||
.then(result => resolve(true))
|
.then(_ => configureTrustedIDs()
|
||||||
|
.then(_ => resolve(true)));
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ const Request = require('./request');
|
|||||||
|
|
||||||
const REFRESH_INTERVAL = 60 * 1000; //1 min
|
const REFRESH_INTERVAL = 60 * 1000; //1 min
|
||||||
|
|
||||||
module.exports = function App(secret, DB) {
|
module.exports = function App(secret, trustedIDs, DB) {
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
//session middleware
|
//session middleware
|
||||||
@ -63,6 +63,11 @@ module.exports = function App(secret, DB) {
|
|||||||
app.post('/deposit-rupee', Request.DepositRupee);
|
app.post('/deposit-rupee', Request.DepositRupee);
|
||||||
app.post('/withdraw-rupee', Request.WithdrawRupee);
|
app.post('/withdraw-rupee', Request.WithdrawRupee);
|
||||||
|
|
||||||
|
//Manage user tags (Access to trusted IDs only)
|
||||||
|
Request.trustedIDs = trustedIDs;
|
||||||
|
app.post('/add-tag', Request.addUserTag);
|
||||||
|
app.post('/remove-tag', Request.removeUserTag);
|
||||||
|
|
||||||
Request.DB = DB;
|
Request.DB = DB;
|
||||||
Request.periodicProcess();
|
Request.periodicProcess();
|
||||||
let refresher = setInterval(Request.periodicProcess, REFRESH_INTERVAL);
|
let refresher = setInterval(Request.periodicProcess, REFRESH_INTERVAL);
|
||||||
|
|||||||
11
src/group.js
11
src/group.js
@ -6,13 +6,21 @@ function addTag(floID, tag) {
|
|||||||
.then(result => resolve(`Added ${floID} to ${tag}`))
|
.then(result => resolve(`Added ${floID} to ${tag}`))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.code === "ER_DUP_ENTRY")
|
if (error.code === "ER_DUP_ENTRY")
|
||||||
reject(`${floID} already in ${tag}`);
|
reject(INVALID(`${floID} already in ${tag}`));
|
||||||
else
|
else
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function removeTag(floID, tag) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
DB.query("DELETE FROM Tags WHERE floID=? AND tag=?", [floID, tag])
|
||||||
|
.then(result => resolve(`Removed ${floID} from ${tag}`))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function getBestPairs(currentRate) {
|
function getBestPairs(currentRate) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
DB.query("SELECT tag, sellPriority, buyPriority FROM TagList").then(result => {
|
DB.query("SELECT tag, sellPriority, buyPriority FROM TagList").then(result => {
|
||||||
@ -331,6 +339,7 @@ function verifyBuyOrder(buyOrder, cur_price) {
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
addTag,
|
addTag,
|
||||||
|
removeTag,
|
||||||
getBestPairs,
|
getBestPairs,
|
||||||
set DB(db) {
|
set DB(db) {
|
||||||
DB = db;
|
DB = db;
|
||||||
|
|||||||
@ -36,7 +36,7 @@ module.exports = function startServer(public_dir) {
|
|||||||
console.debug(PUBLIC_DIR, global.myFloID);
|
console.debug(PUBLIC_DIR, global.myFloID);
|
||||||
|
|
||||||
Database(config["sql_user"], config["sql_pwd"], config["sql_db"], config["sql_host"]).then(DB => {
|
Database(config["sql_user"], config["sql_pwd"], config["sql_db"], config["sql_host"]).then(DB => {
|
||||||
const app = App(config['secret'], DB);
|
const app = App(config['secret'], config['trusted-floIDs'], DB);
|
||||||
app.listen(PORT, () => console.log(`Server Running at port ${PORT}`));
|
app.listen(PORT, () => console.log(`Server Running at port ${PORT}`));
|
||||||
//start backup
|
//start backup
|
||||||
if (config["backup-port"] && config["backup-floIDs"].length) {
|
if (config["backup-port"] && config["backup-floIDs"].length) {
|
||||||
|
|||||||
@ -632,6 +632,7 @@ module.exports = {
|
|||||||
depositRupee,
|
depositRupee,
|
||||||
withdrawRupee,
|
withdrawRupee,
|
||||||
periodicProcess,
|
periodicProcess,
|
||||||
|
group,
|
||||||
set DB(db) {
|
set DB(db) {
|
||||||
DB = db;
|
DB = db;
|
||||||
group.DB = db;
|
group.DB = db;
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const market = require("./market");
|
const market = require("./market");
|
||||||
var DB; //container for database
|
var DB, trustedIDs; //container for database
|
||||||
|
|
||||||
global.INVALID = function(message) {
|
global.INVALID = function(message) {
|
||||||
if (!(this instanceof INVALID))
|
if (!(this instanceof INVALID))
|
||||||
@ -276,8 +276,11 @@ function Account(req, res) {
|
|||||||
setLogin("Session Expired! Re-login required");
|
setLogin("Session Expired! Re-login required");
|
||||||
else {
|
else {
|
||||||
let floID = req.session.user_id;
|
let floID = req.session.user_id;
|
||||||
market.getAccountDetails(floID)
|
market.getAccountDetails(floID).then(result => {
|
||||||
.then(result => res.send(result));
|
if (trustedIDs.includes(floID))
|
||||||
|
result.subAdmin = true;
|
||||||
|
res.send(result)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}).catch(_ => res.status(INTERNAL.e_code).send("Try again later!"));
|
}).catch(_ => res.status(INTERNAL.e_code).send("Try again later!"));
|
||||||
}
|
}
|
||||||
@ -407,6 +410,75 @@ function WithdrawRupee(req, res) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addUserTag(req, res) {
|
||||||
|
let data = req.body,
|
||||||
|
session = req.session;
|
||||||
|
if (!session.user_id)
|
||||||
|
return res.status(INVALID.e_code).send("Login required");
|
||||||
|
else if (!trustedIDs.includes(session.user_id))
|
||||||
|
return res.status(INVALID.e_code).send("Access Denied");
|
||||||
|
validateRequestFromFloID({
|
||||||
|
command: "add_Tag",
|
||||||
|
user: data.user,
|
||||||
|
tag: data.tag,
|
||||||
|
timestamp: data.timestamp
|
||||||
|
}, data.sign, session.user_id).then(req_str => {
|
||||||
|
market.group.addTag(data.user, data.tag).then(result => {
|
||||||
|
storeRequest(session.user_id, req_str, data.sign);
|
||||||
|
res.send(result);
|
||||||
|
}).catch(error => {
|
||||||
|
if (error instanceof INVALID)
|
||||||
|
res.status(INVALID.e_code).send(error.message);
|
||||||
|
else {
|
||||||
|
console.error(error);
|
||||||
|
res.status(INTERNAL.e_code).send("Request processing failed! Try again later!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).catch(error => {
|
||||||
|
if (error instanceof INVALID)
|
||||||
|
res.status(INVALID.e_code).send(error.message);
|
||||||
|
else {
|
||||||
|
console.error(error);
|
||||||
|
res.status(INTERNAL.e_code).send("Request processing failed! Try again later!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeUserTag(req, res) {
|
||||||
|
let data = req.body,
|
||||||
|
session = req.session;
|
||||||
|
if (!session.user_id)
|
||||||
|
return res.status(INVALID.e_code).send("Login required");
|
||||||
|
else if (!trustedIDs.includes(session.user_id))
|
||||||
|
return res.status(INVALID.e_code).send("Access Denied");
|
||||||
|
else
|
||||||
|
validateRequestFromFloID({
|
||||||
|
command: "remove_Tag",
|
||||||
|
user: data.user,
|
||||||
|
tag: data.tag,
|
||||||
|
timestamp: data.timestamp
|
||||||
|
}, data.sign, session.user_id).then(req_str => {
|
||||||
|
market.group.removeTag(data.user, data.tag).then(result => {
|
||||||
|
storeRequest(session.user_id, req_str, data.sign);
|
||||||
|
res.send(result);
|
||||||
|
}).catch(error => {
|
||||||
|
if (error instanceof INVALID)
|
||||||
|
res.status(INVALID.e_code).send(error.message);
|
||||||
|
else {
|
||||||
|
console.error(error);
|
||||||
|
res.status(INTERNAL.e_code).send("Request processing failed! Try again later!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).catch(error => {
|
||||||
|
if (error instanceof INVALID)
|
||||||
|
res.status(INVALID.e_code).send(error.message);
|
||||||
|
else {
|
||||||
|
console.error(error);
|
||||||
|
res.status(INTERNAL.e_code).send("Request processing failed! Try again later!");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
SignUp,
|
SignUp,
|
||||||
Login,
|
Login,
|
||||||
@ -424,6 +496,11 @@ module.exports = {
|
|||||||
DepositRupee,
|
DepositRupee,
|
||||||
WithdrawRupee,
|
WithdrawRupee,
|
||||||
periodicProcess: market.periodicProcess,
|
periodicProcess: market.periodicProcess,
|
||||||
|
addUserTag,
|
||||||
|
removeUserTag,
|
||||||
|
set trustedIDs(ids) {
|
||||||
|
trustedIDs = ids;
|
||||||
|
},
|
||||||
set DB(db) {
|
set DB(db) {
|
||||||
DB = db;
|
DB = db;
|
||||||
market.DB = db;
|
market.DB = db;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user