From 86ab9a0e8efad90d288f4896a1db9d903fdc79d8 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 16:12:33 -0700 Subject: [PATCH 01/20] making error returns uniform --- config.json | 16 -- include/bank.hpp | 227 ++++++++++++++++--------- include/bank_f.hpp | 26 +-- include/{log_consts.hpp => consts.hpp} | 1 + include/endpoints.hpp | 12 ++ include/log.hpp | 2 +- users.json | 2 - 7 files changed, 176 insertions(+), 110 deletions(-) delete mode 100644 config.json rename include/{log_consts.hpp => consts.hpp} (79%) create mode 100644 include/endpoints.hpp delete mode 100644 users.json diff --git a/config.json b/config.json deleted file mode 100644 index fa95be9..0000000 --- a/config.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "listeners": [ - { - "address": "0.0.0.0", - "port": 80, - "https": false - }, - { - "address": "0.0.0.0", - "port": 443, - "https": true, - "cert": "", - "key": "" - } - ] -} diff --git a/include/bank.hpp b/include/bank.hpp index 03e5e4a..0178a93 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -2,6 +2,7 @@ #include #include #include "xxhash.h" +#include "endpoints.hpp" #include "parallel-hashmap/parallel_hashmap/phmap.h" #include "user.hpp" @@ -32,125 +33,189 @@ private: public: std::string admin_pass; - bool AddUser(const std::string &name, std::string &&init_pass) + int_fast8_t AddUser(const std::string &name, std::string &&init_pass) { - if (name.size() > 50) + if (name.size() > max_name_size) { - return false; + return Endpoint::NameTooLong; } - std::shared_lock lock{size_l}; - return users.try_emplace_l( - name, [](User &) {}, std::move(init_pass)); - } - bool AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) - { - if (name.size() > 50) - { - return false; - } - bool state = (admin_pass == attempt); - if (state) { std::shared_lock lock{size_l}; - state = users.try_emplace_l( - name, [](User &) {}, init_bal, std::move(init_pass)); + if (!users.try_emplace_l( + name, [](User &) {}, std::move(init_pass))) + { + return Endpoint::UserAlreadyExists; + } + else + { + return true; + } + } + } + int_fast8_t AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) + { + if (name.size() > max_name_size) + { + return Endpoint::NameTooLong; + } + if (admin_pass != attempt) + { + return Endpoint::WrongAdminPassword; + } + { + std::shared_lock lock{size_l}; + if (!users.try_emplace_l( + name, [](User &) {}, init_bal, std::move(init_pass))) + { + return Endpoint::UserAlreadyExists; + } + else + { + return true; + } } - return state; } - bool DelUser(const std::string &name, const std::string &attempt) + int_fast8_t DelUser(const std::string &name, const std::string &attempt) { std::shared_lock lock{size_l}; - return users.erase_if(name, [&attempt](User &u) { return (XXH3_64bits(attempt.data(), attempt.size()) == u.password); }); + bool state = false; + if (!users.erase_if(name, [&state, &attempt](User &u) { return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); })) + { + return Endpoint::UserNotFound; + } + else + { + return state * Endpoint::WrongPassword; + } } - bool AdminDelUser(const std::string &name, const std::string &attempt) + int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt) { std::shared_lock lock{size_l}; - return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); + bool state = false; + if (!users.erase_if(name, [&state, this, &attempt](const User &) { return state = (admin_pass == attempt); })) + { + return Endpoint::UserNotFound; + } + else + { + return state * Endpoint::WrongAdminPassword; + } } - bool SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) + int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt) { //cant send money to self, from self or amount is 0 if (a_name == b_name || !amount) { - return false; + return Endpoint::InvalidRequest; } - //if A exists, A can afford it, and A's password matches - bool state = false; + int_fast8_t state = false; { std::shared_lock lock{send_funds_l}; //because SendFunds requires 3 locking operations - users.modify_if(a_name, [&state, amount, &attempt](User &a) { - if (state = (a.balance >= amount) && (a.password == XXH3_64bits(attempt.data(), attempt.size()))) - { - a.balance -= amount; - } - }); - - if (state) + if (users.modify_if(a_name, [&state, amount, &attempt](User &a) { + //if A exists, A can afford it, and A's password matches + if (a.balance < amount) + { + state = Endpoint::InsufficientFunds; + } + else + { + if (a.password != XXH3_64bits(attempt.data(), attempt.size())) + { + state = Endpoint::WrongPassword; + } + else + { + a.balance -= amount; + state = true; + } + } + })) { - //if B doesnt exist - if (!users.modify_if(b_name, [amount](User &b) { - b.balance += amount; - })) + return Endpoint::UserNotFound; + } + else + { + if (!state) { - //attempt to refund if A exist - users.modify_if(a_name, [amount](User &a) { - a.balance += amount; - }); - return false; //because had to refund transaction + return state; + } + else + { + //if B doesnt exist + if (!users.modify_if(b_name, [amount](User &b) { + b.balance += amount; + })) + { + //attempt to refund if A exist + users.modify_if(a_name, [amount](User &a) { + a.balance += amount; + }); + return Endpoint::UserNotFound; //because had to refund transaction + } + else + { + if constexpr (max_log_size) + { + Transaction temp(a_name, b_name, amount); + Transaction temp2 = temp; + users.modify_if(a_name, [&temp](User &a) { + a.log.AddTrans(std::move(temp)); + }); + users.modify_if(b_name, [&temp2](User &b) { + b.log.AddTrans(std::move(temp2)); + }); + } + return true; + } } } } - if constexpr (max_log_size) - { - if (state) - { - Transaction temp(a_name, b_name, amount); - Transaction temp2 = temp; - users.modify_if(a_name, [&temp](User &a) { - a.log.AddTrans(std::move(temp)); - }); - users.modify_if(b_name, [&temp2](User &b) { - b.log.AddTrans(std::move(temp2)); - }); - } - } - - return state; } + bool Contains(const std::string &name) const { return users.contains(name); } - bool SetBal(const std::string &name, const std::string &attempt, uint32_t amount) - { - bool state = (admin_pass == attempt); - if (state) - { - users.modify_if(name, [amount](User &u) { - u.balance = amount; - }); - } - return state; - } bool AdminVerifyPass(const std::string &attempt) { - return admin_pass == attempt; + return (admin_pass != attempt); } + int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount) + { + if (admin_pass != attempt) + { + return Endpoint::WrongAdminPassword; + } + else + { + if (!users.modify_if(name, [amount](User &u) { + u.balance = amount; + })) + { + return Endpoint::UserNotFound; + } + else + { + return true; + } + } + } int_fast64_t GetBal(const std::string &name) const { - int_fast64_t res = -1; + int_fast64_t res = Endpoint::UserNotFound; users.if_contains(name, [&res](const User &u) { res = u.balance; }); return res; } + int_fast8_t VerifyPassword(const std::string &name, const std::string &attempt) const { - int_fast8_t res = -1; + int_fast8_t res = Endpoint::UserNotFound; users.if_contains(name, [&res, &attempt](const User &u) { res = u.password == XXH3_64bits(attempt.data(), attempt.size()); }); @@ -158,10 +223,13 @@ public: } int_fast8_t ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass) { - int_fast8_t res = -1; + int_fast8_t res = Endpoint::UserNotFound; users.modify_if(name, [&res, &attempt, &new_pass](User &u) { - res = (u.password == XXH3_64bits(attempt.data(), attempt.size())); - if (res) + if (u.password != XXH3_64bits(attempt.data(), attempt.size())) + { + res = Endpoint::WrongPassword; + } + else { u.password = XXH3_64bits(new_pass.data(), new_pass.size()); } @@ -175,7 +243,7 @@ public: if (!users.if_contains(name, [&res, &attempt](const User &u) { if (u.password != XXH3_64bits(attempt.data(), attempt.size())) { - res = 0; + res = Endpoint::WrongPassword; } else { @@ -191,7 +259,7 @@ public: } })) { - return -1; + return Endpoint::UserNotFound; } return res; } @@ -218,6 +286,7 @@ public: if (!temp.isNull()) { writer->write(temp, &user_save); + throw std::invalid_argument("Saving Failed\n"); } user_save.close(); } @@ -254,3 +323,5 @@ public: } } } bank; + +//TODO make branchless \ No newline at end of file diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 795400d..885aa36 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -68,25 +68,25 @@ public: GEN_BODY JSON(bank.AdminAddUser(body["attempt"].asCString(), body["name"].asCString(), body["init_bal"].asUInt(), body["init_pass"].asCString())); } - void DelUser(req_args) const + void DelUser(req_args, const std::string &name) const { GEN_BODY - JSON(bank.DelUser(body["name"].asCString(), body["attempt"].asCString())); + JSON(bank.DelUser(name, body["attempt"].asCString())); } - void AdminDelUser(req_args) const + void AdminDelUser(req_args, const std::string &name) const { GEN_BODY - JSON(bank.AdminDelUser(body["name"].asCString(), body["attempt"].asCString())); + JSON(bank.AdminDelUser(name, body["attempt"].asCString())); } void SendFunds(req_args) const { GEN_BODY JSON(bank.SendFunds(body["a_name"].asCString(), body["b_name"].asCString(), body["amount"].asUInt(), body["attempt"].asCString())); } - void ChangePassword(req_args) const + void ChangePassword(req_args, const std::string &name) const { GEN_BODY - JSON(bank.ChangePassword(body["name"].asCString(), body["attempt"].asCString(), body["new_pass"].asCString())); + JSON(bank.ChangePassword(name, body["attempt"].asCString(), body["new_pass"].asCString())); } void Contains(req_args, const std::string &name) const { @@ -96,10 +96,10 @@ public: { JSON(bank.GetBal(name)); } - void VerifyPassword(req_args) const + void VerifyPassword(req_args, const std::string &name) const { GEN_BODY - JSON(bank.VerifyPassword(body["name"].asCString(), body["attempt"].asCString())); + JSON(bank.VerifyPassword(name, body["attempt"].asCString())); } void SetBal(req_args, const std::string &name) const { @@ -132,17 +132,17 @@ public: METHOD_ADD(BankF::AdminAddUser, "/admin/user", Post, Options); METHOD_ADD(BankF::SendFunds, "/sendfunds", Post, Options); - METHOD_ADD(BankF::ChangePassword, "/changepass", Patch, Options); + METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); METHOD_ADD(BankF::SetBal, "/admin/{name}/bal", Patch, Options); METHOD_ADD(BankF::Help, "/help", Get, Options); - METHOD_ADD(BankF::VerifyPassword, "/vpass", Post, Options); + METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Post, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); - METHOD_ADD(BankF::AdminVerifyPass, "/admin/vpass", Post, Options); + METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Post, Options); METHOD_ADD(BankF::GetLog, "/{name}/log", Post, Options); - METHOD_ADD(BankF::DelUser, "/user", Delete, Options); - METHOD_ADD(BankF::AdminDelUser, "/admin/user", Delete, Options); + METHOD_ADD(BankF::DelUser, "/user/{name}", Delete, Options); + METHOD_ADD(BankF::AdminDelUser, "/admin/user/{name}", Delete, Options); METHOD_LIST_END }; \ No newline at end of file diff --git a/include/log_consts.hpp b/include/consts.hpp similarity index 79% rename from include/log_consts.hpp rename to include/consts.hpp index b0c40bd..fc66c35 100644 --- a/include/log_consts.hpp +++ b/include/consts.hpp @@ -3,3 +3,4 @@ // Setting both values to 0 does not compile logging constexpr unsigned max_log_size = 100; constexpr unsigned pre_log_size = 10; +constexpr unsigned max_name_size = 50; \ No newline at end of file diff --git a/include/endpoints.hpp b/include/endpoints.hpp new file mode 100644 index 0000000..db6ff99 --- /dev/null +++ b/include/endpoints.hpp @@ -0,0 +1,12 @@ +#pragma once +enum Endpoint +{ + UserNotFound = -1, + WrongPassword = -2, + Refunded = -3, + InvalidRequest = -4, + WrongAdminPassword = -5, + NameTooLong = -6, + UserAlreadyExists = -7, + InsufficientFunds = -8, +}; \ No newline at end of file diff --git a/include/log.hpp b/include/log.hpp index 15674eb..2fbaa5f 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -1,7 +1,7 @@ #pragma once #include #include -#include "log_consts.hpp" +#include "consts.hpp" #include "transactions.hpp" struct Log diff --git a/users.json b/users.json deleted file mode 100644 index 2c63c08..0000000 --- a/users.json +++ /dev/null @@ -1,2 +0,0 @@ -{ -} From 39583693825990fb174beb73a4162c6fc39fb49e Mon Sep 17 00:00:00 2001 From: William Katz Date: Wed, 9 Jun 2021 16:13:49 -0700 Subject: [PATCH 02/20] Create config.json --- config.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..fa95be9 --- /dev/null +++ b/config.json @@ -0,0 +1,16 @@ +{ + "listeners": [ + { + "address": "0.0.0.0", + "port": 80, + "https": false + }, + { + "address": "0.0.0.0", + "port": 443, + "https": true, + "cert": "", + "key": "" + } + ] +} From 729a3748c043caa390ef5def20f9dd0029a6505d Mon Sep 17 00:00:00 2001 From: William Katz Date: Wed, 9 Jun 2021 16:14:31 -0700 Subject: [PATCH 03/20] Create users.json --- users.json | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 users.json diff --git a/users.json b/users.json new file mode 100644 index 0000000..2c63c08 --- /dev/null +++ b/users.json @@ -0,0 +1,2 @@ +{ +} From 6bc3a38aa2753e495cab15139aba47eb7df6bd91 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 16:20:21 -0700 Subject: [PATCH 04/20] :bug: load/save error caught --- include/bank.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/bank.hpp b/include/bank.hpp index 0178a93..f1a2f10 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -286,6 +286,9 @@ public: if (!temp.isNull()) { writer->write(temp, &user_save); + } + else + { throw std::invalid_argument("Saving Failed\n"); } user_save.close(); @@ -303,8 +306,8 @@ public: if (!parseFromStream(builder, user_save, &temp, &errs)) { std::cerr << errs << '\n'; - throw std::invalid_argument("Parsing Failed\n"); user_save.close(); + throw std::invalid_argument("Parsing Failed\n"); } else { From 7646d4861e58afccfae279ef36a72e65259e2f33 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 17:33:51 -0700 Subject: [PATCH 05/20] further path changes --- include/bank_f.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 885aa36..988eb93 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -78,10 +78,10 @@ public: GEN_BODY JSON(bank.AdminDelUser(name, body["attempt"].asCString())); } - void SendFunds(req_args) const + void SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const { GEN_BODY - JSON(bank.SendFunds(body["a_name"].asCString(), body["b_name"].asCString(), body["amount"].asUInt(), body["attempt"].asCString())); + JSON(bank.SendFunds(name, to, amount, body["attempt"].asCString())); } void ChangePassword(req_args, const std::string &name) const { @@ -101,10 +101,10 @@ public: GEN_BODY JSON(bank.VerifyPassword(name, body["attempt"].asCString())); } - void SetBal(req_args, const std::string &name) const + void SetBal(req_args, const std::string &name, uint32_t amount) const { GEN_BODY - JSON(bank.SetBal(name, body["attempt"].asCString(), body["amount"].asUInt())); + JSON(bank.SetBal(name, body["attempt"].asCString(), amount)); } void AdminVerifyPass(req_args) { @@ -128,12 +128,12 @@ public: METHOD_LIST_BEGIN METHOD_ADD(BankF::Close, "/admin/close", Post, Options); - METHOD_ADD(BankF::AddUser, "/user", Post, Options); - METHOD_ADD(BankF::AdminAddUser, "/admin/user", Post, Options); - METHOD_ADD(BankF::SendFunds, "/sendfunds", Post, Options); + METHOD_ADD(BankF::AddUser, "/user/{name}", Post, Options); + METHOD_ADD(BankF::AdminAddUser, "/admin/user/{name}", Post, Options); + METHOD_ADD(BankF::SendFunds, "{name}/send/{to}/amount={amount}", Post, Options); - METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); - METHOD_ADD(BankF::SetBal, "/admin/{name}/bal", Patch, Options); + METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); //sub optimal + METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); METHOD_ADD(BankF::Help, "/help", Get, Options); METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Post, Options); From 37dbe3a3c6f563782f6dd66acbfbd686645b15d6 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 19:36:39 -0700 Subject: [PATCH 06/20] made system indepdent --- include/bank_f.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 988eb93..f93979a 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -19,7 +19,7 @@ INLINE Json::Value JsonReturn(T &&val) { res["value"] = (int)val; //becuase of json lib interpreting 67 as 'A' for example } - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) { res["value"] = (Json::Int64)val; } From 803aeb11c3a85bb05e0825aacd9e03b1c833a93f Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 20:44:44 -0700 Subject: [PATCH 07/20] implementing password in header --- include/bank.hpp | 3 ++- include/bank_f.hpp | 33 +++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/bank.hpp b/include/bank.hpp index f1a2f10..97a1cbe 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -286,12 +286,13 @@ public: if (!temp.isNull()) { writer->write(temp, &user_save); + user_save.close(); } else { + user_save.close(); throw std::invalid_argument("Saving Failed\n"); } - user_save.close(); } //NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS diff --git a/include/bank_f.hpp b/include/bank_f.hpp index f93979a..05cd00d 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -10,6 +10,7 @@ using namespace drogon; #define GEN_BODY \ const auto temp_req = req->getJsonObject(); \ const auto body = temp_req ? *temp_req : Json::Value(); +#define PASS_HEADER std::string pass = req->getHeader("Password"); template INLINE Json::Value JsonReturn(T &&val) @@ -43,9 +44,9 @@ public: } void Close(req_args) const { - GEN_BODY + PASS_HEADER bool res; - if (body["attempt"].asCString() == bank.admin_pass) + if (pass == bank.admin_pass) { bank.Save(); @@ -58,35 +59,35 @@ public: } JSON(res); } - void AddUser(req_args) const + void AddUser(req_args, std::string &&name) const { - GEN_BODY - JSON(bank.AddUser(body["name"].asCString(), body["init_pass"].asCString())); + PASS_HEADER + JSON(bank.AddUser(std::move(name), std::move(pass))); } - void AdminAddUser(req_args) const + void AdminAddUser(req_args, std::string &&name, uint32_t init_bal, std::string &&init_pass) const { - GEN_BODY - JSON(bank.AdminAddUser(body["attempt"].asCString(), body["name"].asCString(), body["init_bal"].asUInt(), body["init_pass"].asCString())); + PASS_HEADER + JSON(bank.AdminAddUser(pass, std::move(name), init_bal, std::move(init_pass))); } void DelUser(req_args, const std::string &name) const { - GEN_BODY - JSON(bank.DelUser(name, body["attempt"].asCString())); + PASS_HEADER + JSON(bank.DelUser(name, pass)); } void AdminDelUser(req_args, const std::string &name) const { - GEN_BODY - JSON(bank.AdminDelUser(name, body["attempt"].asCString())); + PASS_HEADER + JSON(bank.AdminDelUser(name, pass)); } void SendFunds(req_args, const std::string name, const std::string to, uint32_t amount) const { - GEN_BODY - JSON(bank.SendFunds(name, to, amount, body["attempt"].asCString())); + PASS_HEADER + JSON(bank.SendFunds(name, to, amount, pass)); } void ChangePassword(req_args, const std::string &name) const { - GEN_BODY - JSON(bank.ChangePassword(name, body["attempt"].asCString(), body["new_pass"].asCString())); + PASS_HEADER + JSON(bank.ChangePassword(name, pass, std::string(req->getBody()))); } void Contains(req_args, const std::string &name) const { From 3b6cf887c8c7e7425879a2f4e1536e7cc0c70700 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 20:47:42 -0700 Subject: [PATCH 08/20] :bug: saving bug foundg --- include/bank.hpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/include/bank.hpp b/include/bank.hpp index 97a1cbe..3012065 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -266,10 +266,6 @@ public: void Save() { - Json::StreamWriterBuilder builder; - const std::unique_ptr writer(builder.newStreamWriter()); - - std::ofstream user_save("../users.json"); Json::Value temp; //loading info into json temp @@ -285,12 +281,14 @@ public: } if (!temp.isNull()) { + std::ofstream user_save("../users.json"); + Json::StreamWriterBuilder builder; + const std::unique_ptr writer(builder.newStreamWriter()); writer->write(temp, &user_save); user_save.close(); } else { - user_save.close(); throw std::invalid_argument("Saving Failed\n"); } } From ba21d5b6aa8e4bfee91efcb6d44024e950ff23ee Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 21:07:06 -0700 Subject: [PATCH 09/20] finished password header --- include/bank_f.hpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 05cd00d..ddd9a0b 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -99,25 +99,25 @@ public: } void VerifyPassword(req_args, const std::string &name) const { - GEN_BODY - JSON(bank.VerifyPassword(name, body["attempt"].asCString())); + PASS_HEADER + JSON(bank.VerifyPassword(name, pass)); } void SetBal(req_args, const std::string &name, uint32_t amount) const { - GEN_BODY - JSON(bank.SetBal(name, body["attempt"].asCString(), amount)); + PASS_HEADER + JSON(bank.SetBal(name, pass, amount)); } void AdminVerifyPass(req_args) { - GEN_BODY - JSON(bank.AdminVerifyPass(body["attempt"].asCString())); + PASS_HEADER + JSON(bank.AdminVerifyPass(pass)); } void GetLog(req_args, const std::string &name) { if constexpr (max_log_size) { - GEN_BODY - JSON(bank.GetLogs(name, body["attempt"].asCString())); + PASS_HEADER + JSON(bank.GetLogs(name, pass)); } else { @@ -137,10 +137,10 @@ public: METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); METHOD_ADD(BankF::Help, "/help", Get, Options); - METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Post, Options); + METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Post, Options); //maybe get? METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); - METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Post, Options); + METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Post, Options); //maybe get? METHOD_ADD(BankF::GetLog, "/{name}/log", Post, Options); METHOD_ADD(BankF::DelUser, "/user/{name}", Delete, Options); From c0439ed6a8fe9020c58ee99658b03d4f45ef4be7 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 21:20:58 -0700 Subject: [PATCH 10/20] renamed to error responses --- include/bank.hpp | 44 +++++++++---------- include/bank_f.hpp | 2 +- .../{endpoints.hpp => error_responses.hpp} | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) rename include/{endpoints.hpp => error_responses.hpp} (91%) diff --git a/include/bank.hpp b/include/bank.hpp index 3012065..23728df 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -37,14 +37,14 @@ public: { if (name.size() > max_name_size) { - return Endpoint::NameTooLong; + return ErrorResponse::NameTooLong; } { std::shared_lock lock{size_l}; if (!users.try_emplace_l( name, [](User &) {}, std::move(init_pass))) { - return Endpoint::UserAlreadyExists; + return ErrorResponse::UserAlreadyExists; } else { @@ -56,18 +56,18 @@ public: { if (name.size() > max_name_size) { - return Endpoint::NameTooLong; + return ErrorResponse::NameTooLong; } if (admin_pass != attempt) { - return Endpoint::WrongAdminPassword; + return ErrorResponse::WrongAdminPassword; } { std::shared_lock lock{size_l}; if (!users.try_emplace_l( name, [](User &) {}, init_bal, std::move(init_pass))) { - return Endpoint::UserAlreadyExists; + return ErrorResponse::UserAlreadyExists; } else { @@ -82,11 +82,11 @@ public: bool state = false; if (!users.erase_if(name, [&state, &attempt](User &u) { return state = (XXH3_64bits(attempt.data(), attempt.size()) == u.password); })) { - return Endpoint::UserNotFound; + return ErrorResponse::UserNotFound; } else { - return state * Endpoint::WrongPassword; + return state * ErrorResponse::WrongPassword; } } int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt) @@ -95,11 +95,11 @@ public: bool state = false; if (!users.erase_if(name, [&state, this, &attempt](const User &) { return state = (admin_pass == attempt); })) { - return Endpoint::UserNotFound; + return ErrorResponse::UserNotFound; } else { - return state * Endpoint::WrongAdminPassword; + return state * ErrorResponse::WrongAdminPassword; } } @@ -108,7 +108,7 @@ public: //cant send money to self, from self or amount is 0 if (a_name == b_name || !amount) { - return Endpoint::InvalidRequest; + return ErrorResponse::InvalidRequest; } int_fast8_t state = false; @@ -118,13 +118,13 @@ public: //if A exists, A can afford it, and A's password matches if (a.balance < amount) { - state = Endpoint::InsufficientFunds; + state = ErrorResponse::InsufficientFunds; } else { if (a.password != XXH3_64bits(attempt.data(), attempt.size())) { - state = Endpoint::WrongPassword; + state = ErrorResponse::WrongPassword; } else { @@ -134,7 +134,7 @@ public: } })) { - return Endpoint::UserNotFound; + return ErrorResponse::UserNotFound; } else { @@ -153,7 +153,7 @@ public: users.modify_if(a_name, [amount](User &a) { a.balance += amount; }); - return Endpoint::UserNotFound; //because had to refund transaction + return ErrorResponse::UserNotFound; //because had to refund transaction } else { @@ -188,7 +188,7 @@ public: { if (admin_pass != attempt) { - return Endpoint::WrongAdminPassword; + return ErrorResponse::WrongAdminPassword; } else { @@ -196,7 +196,7 @@ public: u.balance = amount; })) { - return Endpoint::UserNotFound; + return ErrorResponse::UserNotFound; } else { @@ -206,7 +206,7 @@ public: } int_fast64_t GetBal(const std::string &name) const { - int_fast64_t res = Endpoint::UserNotFound; + int_fast64_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res](const User &u) { res = u.balance; }); @@ -215,7 +215,7 @@ public: int_fast8_t VerifyPassword(const std::string &name, const std::string &attempt) const { - int_fast8_t res = Endpoint::UserNotFound; + int_fast8_t res = ErrorResponse::UserNotFound; users.if_contains(name, [&res, &attempt](const User &u) { res = u.password == XXH3_64bits(attempt.data(), attempt.size()); }); @@ -223,11 +223,11 @@ public: } int_fast8_t ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass) { - int_fast8_t res = Endpoint::UserNotFound; + int_fast8_t res = ErrorResponse::UserNotFound; users.modify_if(name, [&res, &attempt, &new_pass](User &u) { if (u.password != XXH3_64bits(attempt.data(), attempt.size())) { - res = Endpoint::WrongPassword; + res = ErrorResponse::WrongPassword; } else { @@ -243,7 +243,7 @@ public: if (!users.if_contains(name, [&res, &attempt](const User &u) { if (u.password != XXH3_64bits(attempt.data(), attempt.size())) { - res = Endpoint::WrongPassword; + res = ErrorResponse::WrongPassword; } else { @@ -259,7 +259,7 @@ public: } })) { - return Endpoint::UserNotFound; + return ErrorResponse::UserNotFound; } return res; } diff --git a/include/bank_f.hpp b/include/bank_f.hpp index ddd9a0b..28521bc 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -133,7 +133,7 @@ public: METHOD_ADD(BankF::AdminAddUser, "/admin/user/{name}", Post, Options); METHOD_ADD(BankF::SendFunds, "{name}/send/{to}/amount={amount}", Post, Options); - METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); //sub optimal + METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); METHOD_ADD(BankF::Help, "/help", Get, Options); diff --git a/include/endpoints.hpp b/include/error_responses.hpp similarity index 91% rename from include/endpoints.hpp rename to include/error_responses.hpp index db6ff99..04126ca 100644 --- a/include/endpoints.hpp +++ b/include/error_responses.hpp @@ -1,5 +1,5 @@ #pragma once -enum Endpoint +enum ErrorResponse { UserNotFound = -1, WrongPassword = -2, From 01bf8bea9a6a53949bd195341c28fb8885f92541 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 22:00:50 -0700 Subject: [PATCH 11/20] made methods GET --- include/bank_f.hpp | 6 +++--- include/error_responses.hpp | 11 +++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 28521bc..9884010 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -137,11 +137,11 @@ public: METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); METHOD_ADD(BankF::Help, "/help", Get, Options); - METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Post, Options); //maybe get? + METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Get, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); - METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Post, Options); //maybe get? - METHOD_ADD(BankF::GetLog, "/{name}/log", Post, Options); + METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Get, Options); + METHOD_ADD(BankF::GetLog, "/{name}/log", Get, Options); METHOD_ADD(BankF::DelUser, "/user/{name}", Delete, Options); METHOD_ADD(BankF::AdminDelUser, "/admin/user/{name}", Delete, Options); diff --git a/include/error_responses.hpp b/include/error_responses.hpp index 04126ca..5050d33 100644 --- a/include/error_responses.hpp +++ b/include/error_responses.hpp @@ -3,10 +3,9 @@ enum ErrorResponse { UserNotFound = -1, WrongPassword = -2, - Refunded = -3, - InvalidRequest = -4, - WrongAdminPassword = -5, - NameTooLong = -6, - UserAlreadyExists = -7, - InsufficientFunds = -8, + InvalidRequest = -3, + WrongAdminPassword = -4, + NameTooLong = -5, + UserAlreadyExists = -6, + InsufficientFunds = -7, }; \ No newline at end of file From c0002d1e7b5d207e27704abd51f06511d108a6b1 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:47:21 -0700 Subject: [PATCH 12/20] seperated Connected Services into another file --- README.md | 36 +++++------------------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index f54bdab..977341e 100644 --- a/README.md +++ b/README.md @@ -48,33 +48,7 @@ sudo ./bank ## Connected Services -Go to `{ip}/BankF/help` to see the bank's methods (also found in releases as help.html). Using the Bank's API allows (you/others) to (make/use) connected services that utilize the bank, a couple ideas are - -### Implemented: - -- [Web Frontend](https://github.com/Expand-sys/ccashfrontend) - ![image](https://user-images.githubusercontent.com/31377881/116965729-4ab44500-ac63-11eb-9f11-dc04be6b3d63.png) -- [CC Frontend](https://github.com/Reactified/rpm/blob/main/packages/ccash-wallet) - ![image](https://user-images.githubusercontent.com/31377881/116967157-8b618d80-ac66-11eb-8f2e-4a6297ef0b16.png) -- [CC Shop](https://github.com/Reactified/rpm/tree/main/packages/ccash-shop) - ![image](https://user-images.githubusercontent.com/31377881/120050327-de163700-bfd1-11eb-9d5a-f75c003e867c.png) - ![image](https://user-images.githubusercontent.com/31377881/120050367-09992180-bfd2-11eb-9a22-449d73c196cf.png) -- [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) -- [CC ATM](https://github.com/Reactified/misc/tree/main/lua/ccash-bank) an ATM for economies allowing for an initial exchange to start up - -### In-Dev: - -- [a Market](https://github.com/STBoyden/market-api-2.0) -- [Ebay equivelant](https://github.com/EntireTwix/CSHBay) - -### Ideas: - -- Gambling -- Shipping -- High-level bank operations such as loans -- Some trust based system for transactions similiar to Paypal - -`**WARNING** : abruptly killing the program will result in data loss, use Close() method to close safely` +Go to `{ip}/BankF/help` to see the bank's methods (also found in releases as help.html). Using the Bank's API allows (you/others) to (make/use) connected services that utilize the bank, a couple ideas can be found [here](services.md) ## FAQ **Q:** how is money initially injected into the economy @@ -83,10 +57,10 @@ Go to `{ip}/BankF/help` to see the bank's methods (also found in releases as hel ## [Contributions](https://github.com/EntireTwix/CCash/graphs/contributors) Thank you to the contributors -| Name | Work | -| :--- | --- | -| [Expand](https://github.com/Expand-sys) | Frontend | -| [React](https://github.com/Reactified) | CC {API, Shops, and ATM} | +| Name | Work | +| :------------------------------------------ | ----------------------------- | +| [Expand](https://github.com/Expand-sys) | Frontend | +| [React](https://github.com/Reactified) | CC {API, Shops, and ATM} | | [Doggo](https://github.com/FearlessDoggo21) | Logs loading/adding Optimized | From 23e13060e5c6c71ea15bf9d9d0485fddae2aef92 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:47:46 -0700 Subject: [PATCH 13/20] :sparkles: new docs! --- help.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 help.md diff --git a/help.md b/help.md new file mode 100644 index 0000000..3eeb8c0 --- /dev/null +++ b/help.md @@ -0,0 +1,45 @@ +# Error Responses + +| # | meaning | +| --- | ------------------ | +| -1 | UserNotFound | +| -2 | WrongPassword | +| -3 | InvalidRequest | +| -4 | WrongAdminPassword | +| -5 | NameTooLong | +| -6 | UserAlreadyExists | +| -7 | InsufficientFunds | + +# Things of Note +* all endpoints respond with **JSON** file type +* "**A**" denotes requiring Authentication in the form of a header titled "**Password**" + +# Usage +| Name | Path | Method | A | Description | +| :------------: | :-------------------------------- | :----: | :---: | --------------------------------------------------------------------------------------------------------------------------- | +| GetBal | /{name}/bal | GET | true | returns the balance of a given user `{name}` | +| GetLog | /{name}/log | GET | true | returns a list of last `n` number of transactions (a configurable amount) of a given user `{name}` | +| SendFunds | /{name}/send/{to}/amount={amount} | POST | false | sends `{amount}` from user `{name}` to user `{to}` | +| VerifyPassword | /{name}/pass/verify | GET | true | returns `true` or `false` depending on if the supplied user `{name}`'s password matches the password supplied in the header | + +# Meta Usage +| Name | Path | Method | A | Description | +| :------------: | :-------------------------------- | :----: | :---: | ---------------------------------------------------------------------------------------------------------------------------------------- | +| ChangePassword | /{name}/pass/change | PATCH | true | if the password supplied in the header matches the user `{name}`'s password, the user's password is changed to the one given in the body | +| SetBal | /admin/{name}/bal/amount={amount} | PATCH | true | sets the balance of a give user `{name}` if the supplied password matches the admin password | + +# System Usage +| Name | Path | Method | A | Description | +| :-------------: | :--------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------- | +| Help | /help | GET | false | the page you're looking at right now! | +| Close | /admin/close | POST | true | saves and then closes the program if the supplied password matches the admin password | +| Contains | /contains/{name} | GET | false | returns `true` or `false` depending on if the supplied user `{name}` exists | +| AdminVerifyPass | /admin/verify | GET | true | returns `true` or `false` depending on if the password supplied in the header matches the admin password | + +# User Management +| Name | Path | Method | A | Description | +| :----------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| AddUser | /user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header | +| AdminAddUser | /admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request | +| DelUser | /user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted | +| AdminDelUser | /admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted | \ No newline at end of file From a59e8ae245f36ce45a1c32751c041433b64f7585 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:48:26 -0700 Subject: [PATCH 14/20] seperated README connected services into another file --- services.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 services.md diff --git a/services.md b/services.md new file mode 100644 index 0000000..cbfb9f8 --- /dev/null +++ b/services.md @@ -0,0 +1,25 @@ +# Connected Services + +### Implemented: + +- [Web Frontend](https://github.com/Expand-sys/ccashfrontend) + ![image](https://user-images.githubusercontent.com/31377881/116965729-4ab44500-ac63-11eb-9f11-dc04be6b3d63.png) +- [CC Frontend](https://github.com/Reactified/rpm/blob/main/packages/ccash-wallet) + ![image](https://user-images.githubusercontent.com/31377881/116967157-8b618d80-ac66-11eb-8f2e-4a6297ef0b16.png) +- [CC Shop](https://github.com/Reactified/rpm/tree/main/packages/ccash-shop) + ![image](https://user-images.githubusercontent.com/31377881/120050327-de163700-bfd1-11eb-9d5a-f75c003e867c.png) + ![image](https://user-images.githubusercontent.com/31377881/120050367-09992180-bfd2-11eb-9a22-449d73c196cf.png) +- [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) +- [CC ATM](https://github.com/Reactified/misc/tree/main/lua/ccash-bank) an ATM for economies allowing for an initial exchange to start up + +### In-Dev: + +- [a Market](https://github.com/STBoyden/market-api-2.0) +- [Ebay equivelant](https://github.com/EntireTwix/CSHBay) + +### Ideas: + +- Gambling +- Shipping +- High-level bank operations such as loans +- Some trust based system for transactions similiar to Paypal \ No newline at end of file From 179deb85cc6e712a6f3c122f74a22ba5380e7ab2 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:49:07 -0700 Subject: [PATCH 15/20] re-ordered maps --- include/bank_f.hpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 9884010..6973b57 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -64,10 +64,10 @@ public: PASS_HEADER JSON(bank.AddUser(std::move(name), std::move(pass))); } - void AdminAddUser(req_args, std::string &&name, uint32_t init_bal, std::string &&init_pass) const + void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const { PASS_HEADER - JSON(bank.AdminAddUser(pass, std::move(name), init_bal, std::move(init_pass))); + JSON(bank.AdminAddUser(pass, std::move(name), init_bal, std::string(req->getBody()))); } void DelUser(req_args, const std::string &name) const { @@ -128,22 +128,28 @@ public: } METHOD_LIST_BEGIN - METHOD_ADD(BankF::Close, "/admin/close", Post, Options); - METHOD_ADD(BankF::AddUser, "/user/{name}", Post, Options); - METHOD_ADD(BankF::AdminAddUser, "/admin/user/{name}", Post, Options); - METHOD_ADD(BankF::SendFunds, "{name}/send/{to}/amount={amount}", Post, Options); + //Usage + METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); + METHOD_ADD(BankF::GetLog, "/{name}/log", Get, Options); + METHOD_ADD(BankF::SendFunds, "/{name}/send/{to}/amount={amount}", Post, Options); + METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Get, Options); + + //Meta Usage METHOD_ADD(BankF::ChangePassword, "/{name}/pass/change", Patch, Options); METHOD_ADD(BankF::SetBal, "/admin/{name}/bal/amount={amount}", Patch, Options); + //System Usage METHOD_ADD(BankF::Help, "/help", Get, Options); - METHOD_ADD(BankF::VerifyPassword, "/{name}/pass/verify", Get, Options); + METHOD_ADD(BankF::Close, "/admin/close", Post, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); - METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); METHOD_ADD(BankF::AdminVerifyPass, "/admin/verify", Get, Options); - METHOD_ADD(BankF::GetLog, "/{name}/log", Get, Options); + //User Managment + METHOD_ADD(BankF::AddUser, "/user/{name}", Post, Options); + METHOD_ADD(BankF::AdminAddUser, "/admin/user/{name}?init_bal={init_bal}", Post, Options); METHOD_ADD(BankF::DelUser, "/user/{name}", Delete, Options); METHOD_ADD(BankF::AdminDelUser, "/admin/user/{name}", Delete, Options); + METHOD_LIST_END }; \ No newline at end of file From 169a13be2efe5ce198ec153d4648cfd577365f83 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:50:15 -0700 Subject: [PATCH 16/20] :sparkles: new docs! --- help.html | 283 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 198 insertions(+), 85 deletions(-) diff --git a/help.html b/help.html index 15acbe4..a233d6e 100644 --- a/help.html +++ b/help.html @@ -1,85 +1,198 @@ - -

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE

-

/BankF/admin/close (POST)

-

 attempt  - admin password

-
-

Closes and Saves the server.

-
-

/BankF/user (POST)

-

 name  - name of the user being added (must be less then 50 characters)

-

 init_pass  - initial password for the user being added

-
-

Adds a user to the bank

-
-

/BankF/admin/user (POST)

-

 name  - name of the user being added

-

 attempt  - admin password required to add user with balance

-

 init_bal  - initial balance for user being added

-

 init_pass  - initial password for user being added

-
-

Adds a user with initial balance

-
-

/BankF/sendfunds (POST)

-

 a_name  - sender's name

-

 b_name  - reciever's name

-

 amount  - amount being sent

-

 attempt  - password of sender

-
-

Sends money from one user to another

-
-

/BankF/changepass (PATCH)

-

 name  - name of user's password being changes

-

 attempt  - password of user being changed

-

 new_pass  - new password to replace the current user's password

-
-

Changes password of a user, returns -1 if user doesnt exist

-
-

/BankF/admin/{name}/bal (PATCH)

-

 name  - the name of the user being set

-

 attempt  - the admin password required

-

 amount  - the new balance of the user

-
-

Sets the balance of a user

-
-

/BankF/help (GET)

-
-

the page you're looking at right now!

-
-

/BankF/vpass (POST)

-

 name  - name of user being verified

-

 attempt  - password being verified

-
-

returns 0 or 1 based on if [attempt] is equal to the password of the user [name], or -1 if user does not exist. The intended usage for this function is for connected services

-
-

/BankF/contains/{name} (GET)

-
-

returns a 0 or 1 based on if the bank contains the user

-
-

/BankF/{name}/bal (GET)

-
-

returns the balance of a given user's name, if -1 that means the user does not exist

-
-

/BankF/admin/vpass (POST)

-

 attempt  - admin password

-
-

Verifies if password entered is admin password

-
-

/BankF/{name}/log (POST)

-

 attempt  - user password

-
-

returns a list of last 100 transactions, -1 if user not found, 0 if invalid password

-
-

/BankF/user (DELETE)

-

 name  - name of user being deleted

-

 attempt  - password of user being deleted

-
-

Deletes a user with the password of the user as verification

-
-

/BankF/admin/user (DELETE)

-

 name  - name of user being deleted

-

 attempt  - admin password

-
-

Deletes a user with admin password as verification

-
- - +

Error Responses

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
#meaning
-1UserNotFound
-2WrongPassword
-3InvalidRequest
-4WrongAdminPassword
-5NameTooLong
-6UserAlreadyExists
-7InsufficientFunds
+

Things of Note

+
    +
  • all endpoints respond with JSON file type
  • +
  • "A" denotes requiring Authentication in the form of a header titled "Password"
  • +
+

Usage

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NamePathMethodADescription
GetBal/{name}/balGETtruereturns the balance of a given user {name}
GetLog/{name}/logGETtruereturns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds/{name}/send/{to}/amount={amount}POSTfalsesends {amount} from user {name} to user {to}
VerifyPassword/{name}/pass/verifyGETtruereturns true or false depending on if the supplied user {name}'s password matches the password supplied in the header
+

Meta Usage

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
NamePathMethodADescription
ChangePassword/{name}/pass/changePATCHtrueif the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal/admin/{name}/bal/amount={amount}PATCHtruesets the balance of a give user {name} if the supplied password matches the admin password
+

System Usage

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NamePathMethodADescription
Help/helpGETfalsethe page you’re looking at right now!
Close/admin/closePOSTtruesaves and then closes the program if the supplied password matches the admin password
Contains/contains/{name}GETfalsereturns true or false depending on if the supplied user {name} exists
AdminVerifyPass/admin/verifyGETtruereturns true or false depending on if the password supplied in the header matches the admin password
+

User Management

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NamePathMethodADescription
AddUser/user/{name}POSTtrueregisters a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser/admin/user/{name}?init_bal={init_bal}POSTtrueif the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser/user/{name}DELETEtrueif the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser/admin/user/{name}DELETEtrueif the password supplied in the header matches the admin password, then the user is deleted
\ No newline at end of file From 44949d20125ad192bf5ce3d98d66880e070834f7 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Wed, 9 Jun 2021 23:58:19 -0700 Subject: [PATCH 17/20] updated connected services --- services.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services.md b/services.md index cbfb9f8..c7f1da0 100644 --- a/services.md +++ b/services.md @@ -3,23 +3,25 @@ ### Implemented: - [Web Frontend](https://github.com/Expand-sys/ccashfrontend) - ![image](https://user-images.githubusercontent.com/31377881/116965729-4ab44500-ac63-11eb-9f11-dc04be6b3d63.png) + ![image](https://user-images.githubusercontent.com/31377881/121337724-afe9fe80-c8d1-11eb-8851-23ec5e74cd26.png) - [CC Frontend](https://github.com/Reactified/rpm/blob/main/packages/ccash-wallet) - ![image](https://user-images.githubusercontent.com/31377881/116967157-8b618d80-ac66-11eb-8f2e-4a6297ef0b16.png) + + ![image](https://user-images.githubusercontent.com/31377881/121338034-fb041180-c8d1-11eb-8640-b18c141eb980.png) - [CC Shop](https://github.com/Reactified/rpm/tree/main/packages/ccash-shop) ![image](https://user-images.githubusercontent.com/31377881/120050327-de163700-bfd1-11eb-9d5a-f75c003e867c.png) ![image](https://user-images.githubusercontent.com/31377881/120050367-09992180-bfd2-11eb-9a22-449d73c196cf.png) -- [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) - [CC ATM](https://github.com/Reactified/misc/tree/main/lua/ccash-bank) an ATM for economies allowing for an initial exchange to start up + ![image](https://user-images.githubusercontent.com/31377881/121277361-4d6b1100-c885-11eb-87c8-cfebcf58da4f.png) +- [CC API](https://github.com/Reactified/rpm/blob/main/packages/ccash-api/api.lua) ### In-Dev: - [a Market](https://github.com/STBoyden/market-api-2.0) -- [Ebay equivelant](https://github.com/EntireTwix/CSHBay) +- [Python API](https://github.com/fearlessdoggo21/ccashpythonclient) ### Ideas: - Gambling - Shipping - High-level bank operations such as loans -- Some trust based system for transactions similiar to Paypal \ No newline at end of file +- Some trust based system for transactions similiar to Paypal From 437c8b5f20b38f8cb43c3305d33a6f52f7ab6830 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Thu, 10 Jun 2021 00:02:02 -0700 Subject: [PATCH 18/20] updated logs and fixed renamed header --- include/bank_f.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bank_f.hpp b/include/bank_f.hpp index 6973b57..c318865 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -38,7 +38,7 @@ public: { auto resp = HttpResponse::newHttpResponse(); auto handlerInfo = app().getHandlersInfo(); - resp->setBody("

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE

/BankF/admin/close (POST)

 attempt  - admin password

Closes and Saves the server.

/BankF/user (POST)

 name  - name of the user being added (must be less then 50 characters)

 init_pass  - initial password for the user being added

Adds a user to the bank

/BankF/admin/user (POST)

 name  - name of the user being added

 attempt  - admin password required to add user with balance

 init_bal  - initial balance for user being added

 init_pass  - initial password for user being added

Adds a user with initial balance

/BankF/sendfunds (POST)

 a_name  - sender's name

 b_name  - reciever's name

 amount  - amount being sent

 attempt  - password of sender

Sends money from one user to another

/BankF/changepass (PATCH)

 name  - name of user's password being changes

 attempt  - password of user being changed

 new_pass  - new password to replace the current user's password

Changes password of a user, returns -1 if user doesnt exist

/BankF/admin/{name}/bal (PATCH)

 name  - the name of the user being set

 attempt  - the admin password required

 amount  - the new balance of the user

Sets the balance of a user

/BankF/help (GET)

the page you're looking at right now!

/BankF/vpass (POST)

 name  - name of user being verified

 attempt  - password being verified

returns 0 or 1 based on if [attempt] is equal to the password of the user [name], or -1 if user does not exist. The intended usage for this function is for connected services

/BankF/contains/{name} (GET)

returns a 0 or 1 based on if the bank contains the user

/BankF/{name}/bal (GET)

returns the balance of a given user's name, if -1 that means the user does not exist

/BankF/admin/vpass (POST)

 attempt  - admin password

Verifies if password entered is admin password

/BankF/{name}/log (POST)

 attempt  - user password

returns a list of last 100 transactions, -1 if user not found, 0 if invalid password

/BankF/user (DELETE)

 name  - name of user being deleted

 attempt  - password of user being deleted

Deletes a user with the password of the user as verification

/BankF/admin/user (DELETE)

 name  - name of user being deleted

 attempt  - admin password

Deletes a user with admin password as verification

"); + resp->setBody("

Error Responses

# meaning
-1 UserNotFound
-2 WrongPassword
-3 InvalidRequest
-4 WrongAdminPassword
-5 NameTooLong
-6 UserAlreadyExists
-7 InsufficientFunds

Things of Note

  • all endpoints respond with JSON file type
  • "A" denotes requiring Authentication in the form of a header titled "Password"

Usage

Name Path Method A Description
GetBal /{name}/bal GET true returns the balance of a given user {name}
GetLog /{name}/log GET true returns a list of last n number of transactions (a configurable amount) of a given user {name}
SendFunds /{name}/send/{to}/amount={amount} POST false sends {amount} from user {name} to user {to}
VerifyPassword /{name}/pass/verify GET true returns true or false depending on if the supplied user {name}'s password matches the password supplied in the header

Meta Usage

Name Path Method A Description
ChangePassword /{name}/pass/change PATCH true if the password supplied in the header matches the user {name}'s password, the user’s password is changed to the one given in the body
SetBal /admin/{name}/bal/amount={amount} PATCH true sets the balance of a give user {name} if the supplied password matches the admin password

System Usage

Name Path Method A Description
Help /help GET false the page you’re looking at right now!
Close /admin/close POST true saves and then closes the program if the supplied password matches the admin password
Contains /contains/{name} GET false returns true or false depending on if the supplied user {name} exists
AdminVerifyPass /admin/verify GET true returns true or false depending on if the password supplied in the header matches the admin password

User Management

Name Path Method A Description
AddUser /user/{name} POST true registers a user with the name {name}, balance of 0 and a password of the password supplied in the header
AdminAddUser /admin/user/{name}?init_bal={init_bal} POST true if the password supplied in the header matches the admin password, then it registers a user with the name {name}, balance of init_bal and a password supplied by the body of the request
DelUser /user/{name} DELETE true if the password supplied in the header matches the user {name}'s password, then the user is deleted
AdminDelUser /admin/user/{name} DELETE true if the password supplied in the header matches the admin password, then the user is deleted
"); resp->setExpiredTime(0); callback(resp); } From b1e802182164272bec66bd65c1a5d683fc226231 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Thu, 10 Jun 2021 00:02:29 -0700 Subject: [PATCH 19/20] fixed header for now renamed endpoint.h --- include/bank.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bank.hpp b/include/bank.hpp index 23728df..b95ce85 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -2,7 +2,7 @@ #include #include #include "xxhash.h" -#include "endpoints.hpp" +#include "error_responses.hpp" #include "parallel-hashmap/parallel_hashmap/phmap.h" #include "user.hpp" From c7139953f633f9ff2f953404dec2acedb4f1a6b0 Mon Sep 17 00:00:00 2001 From: William Katz Date: Thu, 10 Jun 2021 00:29:51 -0700 Subject: [PATCH 20/20] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 977341e..4bb5809 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ building the project ``` git clone --recurse-submodule https://github.com/EntireTwix/CCash/ +cd CCash mkdir build cd build cmake ..