diff --git a/help.md b/help.md index 8d34a56..0650cbf 100644 --- a/help.md +++ b/help.md @@ -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 | diff --git a/include/bank.h b/include/bank.h index 1408233..81a9554 100644 --- a/include/bank.h +++ b/include/bank.h @@ -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; diff --git a/src/bank.cpp b/src/bank.cpp index 59a402c..ad18521 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -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 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 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; } diff --git a/src/bank_f.cpp b/src/bank_f.cpp index aea592a..23a9643 100644 --- a/src/bank_f.cpp +++ b/src/bank_f.cpp @@ -32,7 +32,7 @@ void BankF::Help(req_args) const { auto resp = HttpResponse::newHttpResponse(); auto handlerInfo = app().getHandlersInfo(); - resp->setBody("

Error Responses

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

Things of Note

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

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->setBody("

Error Responses

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

Things of Note

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 1 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 1 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); }