making error returns uniform

This commit is contained in:
EntireTwix 2021-06-09 16:12:33 -07:00
parent b6b1c0b78e
commit 86ab9a0e8e
7 changed files with 176 additions and 110 deletions

View file

@ -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": ""
}
]
}

View file

@ -2,6 +2,7 @@
#include <fstream>
#include <shared_mutex>
#include "xxhash.h"
#include "endpoints.hpp"
#include "parallel-hashmap/parallel_hashmap/phmap.h"
#include "user.hpp"
@ -32,63 +33,116 @@ 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<std::shared_mutex> 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<std::shared_mutex> 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<std::shared_mutex> 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<std::shared_mutex> 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;
}
bool AdminDelUser(const std::string &name, const std::string &attempt)
else
{
return state * Endpoint::WrongPassword;
}
}
int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt)
{
std::shared_lock<std::shared_mutex> 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<std::shared_mutex> 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())))
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 (state)
}
}))
{
return Endpoint::UserNotFound;
}
else
{
if (!state)
{
return state;
}
else
{
//if B doesnt exist
if (!users.modify_if(b_name, [amount](User &b) {
@ -99,13 +153,11 @@ public:
users.modify_if(a_name, [amount](User &a) {
a.balance += amount;
});
return false; //because had to refund transaction
return Endpoint::UserNotFound; //because had to refund transaction
}
}
}
if constexpr (max_log_size)
else
{
if (state)
if constexpr (max_log_size)
{
Transaction temp(a_name, b_name, amount);
Transaction temp2 = temp;
@ -116,41 +168,54 @@ public:
b.log.AddTrans(std::move(temp2));
});
}
return true;
}
}
}
}
}
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

View file

@ -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
};

View file

@ -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;

12
include/endpoints.hpp Normal file
View file

@ -0,0 +1,12 @@
#pragma once
enum Endpoint
{
UserNotFound = -1,
WrongPassword = -2,
Refunded = -3,
InvalidRequest = -4,
WrongAdminPassword = -5,
NameTooLong = -6,
UserAlreadyExists = -7,
InsufficientFunds = -8,
};

View file

@ -1,7 +1,7 @@
#pragma once
#include <array>
#include <algorithm>
#include "log_consts.hpp"
#include "consts.hpp"
#include "transactions.hpp"
struct Log

View file

@ -1,2 +0,0 @@
{
}