lokinet/llarp/service/auth.cpp

62 lines
1.7 KiB
C++
Raw Normal View History

2020-05-28 11:07:32 +00:00
#include "auth.hpp"
#include <unordered_map>
namespace llarp::service
{
/// maybe get auth result from string
2021-02-24 12:14:15 +00:00
std::optional<AuthResultCode>
ParseAuthResultCode(std::string data)
2020-05-28 11:07:32 +00:00
{
2021-02-24 12:14:15 +00:00
std::unordered_map<std::string, AuthResultCode> values = {
{"OKAY", AuthResultCode::eAuthAccepted},
{"REJECT", AuthResultCode::eAuthRejected},
{"PAYME", AuthResultCode::eAuthPaymentRequired},
{"LIMITED", AuthResultCode::eAuthRateLimit}};
2020-05-28 11:07:32 +00:00
auto itr = values.find(data);
if (itr == values.end())
return std::nullopt;
return itr->second;
}
AuthType
ParseAuthType(std::string data)
{
2021-03-05 17:31:52 +00:00
std::unordered_map<std::string, AuthType> values = {
{"lmq", AuthType::eAuthTypeLMQ},
{"whitelist", AuthType::eAuthTypeWhitelist},
{"none", AuthType::eAuthTypeNone}};
const auto itr = values.find(data);
if (itr == values.end())
throw std::invalid_argument("no such auth type: " + data);
return itr->second;
}
2021-02-24 18:41:23 +00:00
/// turn an auth result code into an int
uint64_t
AuthResultCodeAsInt(AuthResultCode code)
{
2021-02-24 18:50:45 +00:00
return static_cast<std::underlying_type_t<AuthResultCode>>(code);
2021-02-24 18:41:23 +00:00
}
/// may turn an int into an auth result code
std::optional<AuthResultCode>
AuthResultCodeFromInt(uint64_t code)
{
switch (code)
{
case 0:
return AuthResultCode::eAuthAccepted;
case 1:
return AuthResultCode::eAuthRejected;
case 2:
return AuthResultCode::eAuthFailed;
case 3:
return AuthResultCode::eAuthRateLimit;
case 4:
return AuthResultCode::eAuthPaymentRequired;
default:
return std::nullopt;
}
}
2020-05-28 11:07:32 +00:00
} // namespace llarp::service