mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 08:12:12 +11:00
🎨 changed admin verification to be account name rather then password
This commit is contained in:
parent
c9da3eab04
commit
1a77ca43dc
5 changed files with 38 additions and 53 deletions
|
|
@ -41,19 +41,19 @@ private:
|
|||
std::shared_mutex send_funds_l;
|
||||
|
||||
public:
|
||||
std::string admin_pass;
|
||||
std::string admin_account;
|
||||
|
||||
bool GetChangeState() const noexcept;
|
||||
|
||||
BankResponse GetBal(const std::string &name) const noexcept;
|
||||
BankResponse GetLogs(const std::string &name) noexcept;
|
||||
BankResponse SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept;
|
||||
bool VerifyPassword(const std::string &name, const std::string &attempt) const noexcept;
|
||||
bool VerifyPassword(std::string_view name, std::string_view attempt) const noexcept;
|
||||
|
||||
void ChangePassword(const std::string &name, std::string &&new_pass) noexcept;
|
||||
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
|
||||
bool Contains(const std::string &name) const noexcept;
|
||||
bool AdminVerifyPass(const std::string &attempt) noexcept;
|
||||
bool AdminVerifyAccount(std::string_view name) noexcept;
|
||||
|
||||
BankResponse AddUser(const std::string &name, std::string &&init_pass) noexcept;
|
||||
BankResponse AdminAddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public:
|
|||
void Ping(req_args) const;
|
||||
void Close(req_args) const;
|
||||
void Contains(req_args, const std::string &name) const;
|
||||
void AdminVerifyPass(req_args);
|
||||
void AdminVerifyAccount(req_args);
|
||||
|
||||
void AddUser(req_args) const;
|
||||
void AdminAddUser(req_args) const;
|
||||
|
|
@ -54,7 +54,7 @@ public:
|
|||
METHOD_ADD(api::Ping, "/v1/ping", Get, Options);
|
||||
METHOD_ADD(api::Close, "/v1/admin/shutdown", Post, Options, "AdminFilter");
|
||||
METHOD_ADD(api::Contains, "/v1/user/exists?name={name}", Get, Options);
|
||||
METHOD_ADD(api::AdminVerifyPass, "/v1/admin/verify_password", Get, Options, "AdminFilter");
|
||||
METHOD_ADD(api::AdminVerifyAccount, "/v1/admin/verify_account", Get, Options, "AdminFilter");
|
||||
|
||||
//User Managment
|
||||
METHOD_ADD(api::AddUser, "/v1/user/register", Post, Options); //expects ["name"](string) ["pass"](string)
|
||||
|
|
|
|||
42
main.cpp
42
main.cpp
|
|
@ -44,7 +44,7 @@ int main(int argc, char **argv)
|
|||
|
||||
if (argc != 3)
|
||||
{
|
||||
std::cerr << "Usage: sudo ./bank <admin password> <saving frequency in minutes>\n";
|
||||
std::cerr << "Usage: sudo ./bank <admin account> <saving frequency in minutes>\n";
|
||||
return 0;
|
||||
}
|
||||
if (geteuid() != 0)
|
||||
|
|
@ -65,30 +65,29 @@ int main(int argc, char **argv)
|
|||
|
||||
sigaction(SIGINT, &sigIntHandler, NULL);
|
||||
|
||||
//Admin Password
|
||||
bank.admin_pass = argv[1];
|
||||
//Admin account
|
||||
bank.admin_account = argv[1];
|
||||
|
||||
//Auto Saving
|
||||
const unsigned long saving_freq = std::stoul(std::string(argv[2]));
|
||||
if (saving_freq) //if saving frequency is 0 then auto saving is turned off
|
||||
{
|
||||
std::thread([saving_freq]()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||
std::cout << "Saving " << std::time(0) << '\n';
|
||||
if (bank.GetChangeState())
|
||||
{
|
||||
std::cout << " to disk...\n";
|
||||
bank.Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << " no changes...\n";
|
||||
}
|
||||
}
|
||||
})
|
||||
std::thread([saving_freq]() {
|
||||
while (1)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||
std::cout << "Saving " << std::time(0) << '\n';
|
||||
if (bank.GetChangeState())
|
||||
{
|
||||
std::cout << " to disk...\n";
|
||||
bank.Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << " no changes...\n";
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
|
|
@ -97,8 +96,7 @@ int main(int argc, char **argv)
|
|||
auto admin_filter = std::make_shared<AdminFilter>(bank);
|
||||
|
||||
app().registerPostHandlingAdvice(
|
||||
[](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp)
|
||||
{
|
||||
[](const drogon::HttpRequestPtr &req, const drogon::HttpResponsePtr &resp) {
|
||||
resp->addHeader("Access-Control-Allow-Origin", "*"); //CORS
|
||||
});
|
||||
app()
|
||||
|
|
|
|||
37
src/bank.cpp
37
src/bank.cpp
|
|
@ -23,18 +23,14 @@ bool Bank::GetChangeState() const noexcept { return save_flag.GetChangeState();
|
|||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||
{
|
||||
uint64_t res = 0;
|
||||
users.if_contains(name, [&res](const User &u) {
|
||||
res = u.balance + 1;
|
||||
});
|
||||
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
|
||||
return res ? BankResponse(k200OK, res - 1) : BankResponse(k404NotFound, "User not found");
|
||||
}
|
||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||
{
|
||||
BankResponse res;
|
||||
#if MAX_LOG_SIZE > 0
|
||||
if (!users.modify_if(name, [&res](User &u) {
|
||||
res = {k200OK, u.log.GetLog()};
|
||||
}))
|
||||
if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLog()}; }))
|
||||
{
|
||||
return BankResponse(k404NotFound, "User not found");
|
||||
}
|
||||
|
|
@ -91,7 +87,8 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
#if MAX_LOG_SIZE > 0
|
||||
users.modify_if(b_name, [&temp, amount](User &b) {
|
||||
b.balance += amount;
|
||||
b.log.AddTrans(std::move(temp)); }); //about 40% of this function's cost
|
||||
b.log.AddTrans(std::move(temp));
|
||||
}); //about 40% of this function's cost
|
||||
#else
|
||||
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||
#endif
|
||||
|
|
@ -102,29 +99,23 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
|||
}
|
||||
return state;
|
||||
}
|
||||
bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) const noexcept
|
||||
bool Bank::VerifyPassword(std::string_view name, std::string_view attempt) const noexcept
|
||||
{
|
||||
bool res = false;
|
||||
users.if_contains(name, [&res, &attempt](const User &u) {
|
||||
res = (u.password == xxHashStringGen{}(attempt));
|
||||
});
|
||||
users.if_contains(std::string(name), [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt)); });
|
||||
return res;
|
||||
}
|
||||
|
||||
void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexcept
|
||||
{
|
||||
users.modify_if(name, [&new_pass](User &u) {
|
||||
u.password = xxHashStringGen{}(new_pass);
|
||||
});
|
||||
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
save_flag.SetChangesOn();
|
||||
#endif
|
||||
}
|
||||
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
||||
{
|
||||
if (users.modify_if(name, [amount](User &u) {
|
||||
u.balance = amount;
|
||||
}))
|
||||
if (users.modify_if(name, [amount](User &u) { u.balance = amount; }))
|
||||
{
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
save_flag.SetChangesOn();
|
||||
|
|
@ -140,9 +131,9 @@ bool Bank::Contains(const std::string &name) const noexcept
|
|||
{
|
||||
return users.contains(name);
|
||||
}
|
||||
bool Bank::AdminVerifyPass(const std::string &attempt) noexcept
|
||||
bool Bank::AdminVerifyAccount(std::string_view name) noexcept
|
||||
{
|
||||
return (admin_pass == attempt);
|
||||
return (name == admin_account);
|
||||
}
|
||||
|
||||
BankResponse Bank::AddUser(const std::string &name, std::string &&init_pass) noexcept
|
||||
|
|
@ -187,9 +178,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
|||
return BankResponse(k404NotFound, "User not found");
|
||||
}
|
||||
#if RETURN_ON_DEL
|
||||
users.modify_if(return_account, [&bal](User &u) {
|
||||
u.balance += bal;
|
||||
});
|
||||
users.modify_if(return_account, [&bal](User &u) { u.balance += bal; });
|
||||
#endif
|
||||
return BankResponse(k200OK, "User deleted!");
|
||||
}
|
||||
|
|
@ -207,9 +196,7 @@ void Bank::Save()
|
|||
for (const auto &u : users)
|
||||
{
|
||||
//we know it contains this key but we call this func to grab mutex
|
||||
users.if_contains(u.first, [&temp, &u](const User &u_val) {
|
||||
temp[u.first] = u_val.Serialize();
|
||||
});
|
||||
users.if_contains(u.first, [&temp, &u](const User &u_val) { temp[u.first.data()] = u_val.Serialize(); });
|
||||
}
|
||||
}
|
||||
if (temp.isNull())
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ void api::Contains(req_args, const std::string &name) const
|
|||
resp->setStatusCode(k200OK);
|
||||
callback(resp);
|
||||
}
|
||||
void api::AdminVerifyPass(req_args)
|
||||
void api::AdminVerifyAccount(req_args)
|
||||
{
|
||||
RESPOND_TRUE //filter handles admin creds
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue