made responses more inuitive for vpass and admin/vpass & fixed send funds bug

This commit is contained in:
EntireTwix 2021-06-12 20:39:07 -07:00
parent 5e8541c2a6
commit c7695dd6db
4 changed files with 43 additions and 27 deletions

24
help.md
View file

@ -15,12 +15,12 @@
* "**A**" denotes requiring Authentication in the form of a header titled "**Password**"
# Usage
| Name | Path | Method | A | Description |
| :------------: | :-------------------------------- | :----: | :---: | --------------------------------------------------------------------------------------------------------------------------- |
| GetBal | /{name}/bal | GET | false | 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 | true | 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 |
| Name | Path | Method | A | Description |
| :------------: | :-------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------- |
| GetBal | /{name}/bal | GET | false | 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 | true | sends `{amount}` from user `{name}` to user `{to}` |
| VerifyPassword | /{name}/pass/verify | GET | true | returns `1` if the supplied user `{name}`'s password matches the password supplied in the header |
# Meta Usage
| Name | Path | Method | A | Description |
@ -29,12 +29,12 @@
| 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 |
| 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 `1` if the password supplied in the header matches the admin password |
# User Management
| Name | Path | Method | A | Description |

View file

@ -42,7 +42,7 @@ public:
int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt);
bool Contains(const std::string &name) const;
bool AdminVerifyPass(const std::string &attempt);
int_fast8_t AdminVerifyPass(const std::string &attempt);
int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount);
int_fast64_t GetBal(const std::string &name) const;

View file

@ -27,12 +27,12 @@ int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, u
}
if (admin_pass != attempt)
{
return ErrorResponse::WrongAdminPassword;
return ErrorResponse::WrongPassword;
}
{
std::shared_lock<std::shared_mutex> lock{size_l};
if (users.try_emplace_l(
name, [](User &) {}, std::move(init_pass)))
name, [](User &) {}, init_bal, std::move(init_pass)))
{
return true;
}
@ -58,7 +58,7 @@ int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt)
}
else
{
return ErrorResponse::WrongAdminPassword;
return ErrorResponse::WrongPassword;
}
}
}
@ -78,7 +78,7 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attem
}
else
{
return ErrorResponse::WrongAdminPassword;
return ErrorResponse::WrongPassword;
}
}
}
@ -90,7 +90,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
{
return ErrorResponse::InvalidRequest;
}
int_fast8_t state = false;
{
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //because SendFunds requires 3 locking operations
@ -113,10 +112,6 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
}
}
}))
{
return ErrorResponse::UserNotFound;
}
else
{
if (state)
{
@ -152,6 +147,13 @@ int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name
return state;
}
}
else
{
{
std::cout << "b\n";
return ErrorResponse::UserNotFound;
}
}
}
}
@ -159,16 +161,23 @@ bool Bank::Contains(const std::string &name) const
{
return users.contains(name);
}
bool Bank::AdminVerifyPass(const std::string &attempt)
int_fast8_t Bank::AdminVerifyPass(const std::string &attempt)
{
return (admin_pass == attempt);
if (admin_pass == attempt)
{
return true;
}
else
{
return ErrorResponse::WrongPassword;
}
}
int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount)
{
if (admin_pass != attempt)
{
return ErrorResponse::WrongAdminPassword;
return ErrorResponse::WrongPassword;
}
if (users.modify_if(name, [amount](User &u) {
u.balance = amount;
@ -194,7 +203,14 @@ int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &att
{
int_fast8_t res = ErrorResponse::UserNotFound;
users.if_contains(name, [&res, &attempt](const User &u) {
res = u.password == XXH3_64bits(attempt.data(), attempt.size());
if (u.password == XXH3_64bits(attempt.data(), attempt.size()))
{
res = true;
}
else
{
res = ErrorResponse::WrongPassword;
}
});
return res;
}

File diff suppressed because one or more lines are too long