diff options
| author | auth <[email protected]> | 2020-07-14 16:16:02 +0200 |
|---|---|---|
| committer | auth <[email protected]> | 2020-07-14 16:16:02 +0200 |
| commit | 275010eaa0a46012fec547efdd76256b25e47f54 (patch) | |
| tree | 00d9e0a19424c3e3d81e2ed74d7986da987123d1 /server/src/forum | |
| parent | Forum integration. (diff) | |
| download | loader-275010eaa0a46012fec547efdd76256b25e47f54.tar.xz loader-275010eaa0a46012fec547efdd76256b25e47f54.zip | |
Client login handling on server.
More error handling for forum responses.
Diffstat (limited to 'server/src/forum')
| -rw-r--r-- | server/src/forum/forum.cpp | 47 | ||||
| -rw-r--r-- | server/src/forum/forum.h | 9 |
2 files changed, 39 insertions, 17 deletions
diff --git a/server/src/forum/forum.cpp b/server/src/forum/forum.cpp index 57eff72..ed2ae58 100644 --- a/server/src/forum/forum.cpp +++ b/server/src/forum/forum.cpp @@ -2,23 +2,25 @@ #include "../util/io.h" #include "forum.h" -void xenforo_forum::init(const std::string_view link, const std::string_view key) { - m_link = link; - m_api = key; +void xenforo_forum::init(const std::string_view link, + const std::string_view key) { + m_link = link; + m_api = key; - m_header = cpr::Header{{"Content-Type", "application/x-www-form-urlencoded"}, + m_header = cpr::Header{{"Content-Type", "application/x-www-form-urlencoded"}, {"XF-Api-Key", m_api.data()}, {"XF-Api-User", "1"}, {"api_bypass_permissions", "1"}}; } int xenforo_forum::check_login(const std::string_view username, - const std::string_view password, user_data &data) { - auto url = fmt::format("{}{}", m_link, "/auth/"); + const std::string_view password, + user_data &data) { + auto url = fmt::format("{}{}", m_link, "/auth/"); auto post_data = fmt::format("login={}&password={}", username, password); auto req = cpr::Post(cpr::Url{url}, cpr::Body{post_data}, cpr::Timeout{10000}, - m_header); + m_header); if (req.elapsed >= 10) { io::logger->warn("login request on {} timed out.", username); @@ -29,19 +31,36 @@ int xenforo_forum::check_login(const std::string_view username, auto response = req.text; if (!nlohmann::json::accept(response)) { - io::logger->error("login response on {} isnt valid json.", username); - return forum_response::api_fail; + io::logger->error("invalid json response from forum.", username); + return forum_response::api_fail; } if (status_code >= 400) { return forum_response::api_error; } - auto j = nlohmann::json::parse(response); - - data.banned = j["user"]["is_banned"].get<bool>(); - // data.active = check user group - data.hwid = j["user"]["custom_fields"]["hwid"].get<std::string>(); + auto json = nlohmann::json::parse(response); + + data.banned = json["user"]["is_banned"].get<bool>(); + // data.active = check user groupm + auto custom_fields = json["user"]["custom_fields"]; + if (custom_fields.contains("hwid")) { + data.hwid = json["user"]["custom_fields"]["hwid"].get<std::string>(); + } else { + io::logger->warn("hwid field doesn't exist for {}.", username); + } + + data.id = json["user"]["user_id"].get<int>(); return forum_response::api_success; +} + +bool xenforo_forum::edit(const int uid, const std::string_view field, + const std::string_view val) { + const auto url = fmt::format("{}{}{}/", m_link, "/users/", uid); + const auto post = fmt::format("{}={}", field, val); + + auto req = + cpr::Post(cpr::Url{url}, cpr::Body{post}, cpr::Timeout{10000}, m_header); + return req.status_code == 200; }
\ No newline at end of file diff --git a/server/src/forum/forum.h b/server/src/forum/forum.h index df18cb4..d0b1a31 100644 --- a/server/src/forum/forum.h +++ b/server/src/forum/forum.h @@ -4,9 +4,10 @@ // XenForo forum api wrapper struct user_data { - std::string hwid; - bool banned; - bool active; + std::string hwid; + bool banned; + bool active; + int id; }; enum forum_response { api_fail = 0, api_error, api_timeout, api_success }; @@ -21,4 +22,6 @@ class xenforo_forum { void init(const std::string_view link, const std::string_view key); int check_login(const std::string_view username, const std::string_view password, user_data &data); + bool edit(const int uid, const std::string_view field, + const std::string_view val); };
\ No newline at end of file |