From dcbf2dab2cf48a029f8da703506a25214e5fac45 Mon Sep 17 00:00:00 2001 From: Data Date: Mon, 15 Apr 2024 21:32:15 -0700 Subject: [PATCH] Aleeva: Attempt to auth and return result instead of retrying on error. Add Aleeva login error messages to status. --- arcdps_uploader/Aleeva.cpp | 337 ++++++++++++++-------------- arcdps_uploader/Aleeva.h | 4 +- arcdps_uploader/Uploader.cpp | 18 +- arcdps_uploader/arcdps_uploader.cpp | 2 +- 4 files changed, 193 insertions(+), 168 deletions(-) diff --git a/arcdps_uploader/Aleeva.cpp b/arcdps_uploader/Aleeva.cpp index f2eb346..e9443e6 100644 --- a/arcdps_uploader/Aleeva.cpp +++ b/arcdps_uploader/Aleeva.cpp @@ -7,189 +7,200 @@ using json = nlohmann::json; -void Aleeva::login(Settings& settings) { - if (Aleeva::is_refresh_token_valid(settings)) { - Aleeva::authorize(settings); - Aleeva::get_servers(settings); - for (const Aleeva::DiscordId& server : settings.aleeva.server_ids) { - Aleeva::get_channels(settings, server.id); - } - } - else { - Aleeva::authorize(settings); - Aleeva::login(settings); - } +bool Aleeva::login(Settings& settings) { + if (settings.aleeva.access_code.empty()) { + LOG_F(INFO, "Aleeva enabled but access code missing, skipping login."); + return false; + } + + if (Aleeva::authorize(settings)) { + if (Aleeva::is_refresh_token_valid(settings)) { + Aleeva::get_servers(settings); + for (const Aleeva::DiscordId& server : settings.aleeva.server_ids) { + Aleeva::get_channels(settings, server.id); + } + return true; + } + } + + return false; } -std::string Aleeva::authorize(Settings& settings) +bool Aleeva::authorize(Settings& settings) { - std::string grant_type = "access_code"; - - if (is_refresh_token_valid(settings)) { - grant_type = "refresh_token"; - } - - cpr::Response response; - response = cpr::Post( - cpr::Url{"https://api.aleeva.io/auth/token"}, - cpr::Payload{ - {"grant_type", grant_type}, - {"client_id", "arc_dps_uploader"}, - {"client_secret", "9568468d-810a-4ce2-861e-e8011b658a28"}, - {"access_code", settings.aleeva.access_code}, - {"refresh_token", settings.aleeva.refresh_token}, - {"scopes", "report:write server:read channel:read"}}); - - LOG_F(INFO, "Aleeva Auth response: %s", response.text.c_str()); - - if (response.status_code == 200) { - if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { - try { - json parsed = json::parse(response.text); - settings.aleeva.api_key = parsed["accessToken"]; - settings.aleeva.refresh_token = parsed["refreshToken"]; - - auto now = time(nullptr); - int64_t expires_in = parsed["refreshExpiresIn"]; - settings.aleeva.token_expiration = now + expires_in; - - settings.save(); - - settings.aleeva.authorised = true; - return "Aleeva login successful."; - } catch(const json::exception& e) { - LOG_F(ERROR, "Aleeva Auth JSON Parse Fail: %s", e.what()); - } - } - } else if (response.status_code == 401) { - settings.aleeva.authorised = false; - settings.aleeva.refresh_token = ""; - settings.aleeva.token_expiration = 0; - } - - return "Aleeva login failed. See log for details."; + std::string grant_type = "access_code"; + + if (is_refresh_token_valid(settings)) { + grant_type = "refresh_token"; + } + + cpr::Response response; + response = cpr::Post( + cpr::Url{ "https://api.aleeva.io/auth/token" }, + cpr::Payload{ + {"grant_type", grant_type}, + {"client_id", "arc_dps_uploader"}, + {"client_secret", "9568468d-810a-4ce2-861e-e8011b658a28"}, + {"access_code", settings.aleeva.access_code}, + {"refresh_token", settings.aleeva.refresh_token}, + {"scopes", "report:write server:read channel:read"} }); + + LOG_F(INFO, "Aleeva Auth response: %s", response.text.c_str()); + + if (response.status_code == 200) { + if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { + try { + json parsed = json::parse(response.text); + settings.aleeva.api_key = parsed["accessToken"]; + settings.aleeva.refresh_token = parsed["refreshToken"]; + + auto now = time(nullptr); + int64_t expires_in = parsed["refreshExpiresIn"]; + settings.aleeva.token_expiration = now + expires_in; + + settings.save(); + + settings.aleeva.authorised = true; + return true; + } + catch (const json::exception& e) { + LOG_F(ERROR, "Aleeva Auth JSON Parse Fail: %s", e.what()); + } + } + } + else if (response.status_code == 401) { + settings.aleeva.authorised = false; + settings.aleeva.refresh_token = ""; + settings.aleeva.token_expiration = 0; + } + + return false; } void Aleeva::deauthorize(Settings& settings) { - settings.aleeva.authorised = false; - settings.aleeva.refresh_token = ""; - settings.aleeva.token_expiration = 0; + settings.aleeva.authorised = false; + settings.aleeva.refresh_token = ""; + settings.aleeva.token_expiration = 0; } bool Aleeva::is_refresh_token_valid(Settings& settings) { - if (settings.aleeva.refresh_token.length() == 0) { - return false; - } + if (settings.aleeva.refresh_token.length() == 0) { + return false; + } - time_t now = time(nullptr); - if (now > settings.aleeva.token_expiration - 60) { - return false; - } + time_t now = time(nullptr); + if (now > settings.aleeva.token_expiration - 60) { + return false; + } - return true; + return true; } void Aleeva::get_servers(Settings& settings) { - if (!settings.aleeva.authorised) { - return; - } - - cpr::Response response; - response = cpr::Get( - cpr::Url{"https://api.aleeva.io/server"}, - cpr::Bearer(settings.aleeva.api_key), - cpr::Parameters{ - {"mode", "UPLOADS"} - } - ); - - if (response.status_code == 200) { - if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { - try { - json parsed = json::parse(response.text); - for (auto& server : parsed) { - DiscordId server_id; - server_id.id = server["id"]; - server_id.name = server["name"]; - settings.aleeva.server_ids.push_back(server_id); - } - if (settings.aleeva.server_ids.size() > 0 && settings.aleeva.selected_server_id == "") { - settings.aleeva.selected_server_id = settings.aleeva.server_ids[0].id; - } - } catch(const json::exception& e) { - LOG_F(ERROR, "Aleeva Servers JSON Parse Fail: %s", e.what()); - } - } - } else { - LOG_F(INFO, "Aleeva Servers response: %s", response.text.c_str()); - } + if (!settings.aleeva.authorised) { + return; + } + + cpr::Response response; + response = cpr::Get( + cpr::Url{ "https://api.aleeva.io/server" }, + cpr::Bearer(settings.aleeva.api_key), + cpr::Parameters{ + {"mode", "UPLOADS"} + } + ); + + if (response.status_code == 200) { + if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { + try { + json parsed = json::parse(response.text); + for (auto& server : parsed) { + DiscordId server_id; + server_id.id = server["id"]; + server_id.name = server["name"]; + settings.aleeva.server_ids.push_back(server_id); + } + if (settings.aleeva.server_ids.size() > 0 && settings.aleeva.selected_server_id == "") { + settings.aleeva.selected_server_id = settings.aleeva.server_ids[0].id; + } + } + catch (const json::exception& e) { + LOG_F(ERROR, "Aleeva Servers JSON Parse Fail: %s", e.what()); + } + } + } + else { + LOG_F(INFO, "Aleeva Servers response: %s", response.text.c_str()); + } } void Aleeva::get_channels(Settings& settings, const std::string& server_id) { - if (!settings.aleeva.authorised) { - return; - } - - cpr::Response response; - response = cpr::Get( - cpr::Url{"https://api.aleeva.io/server/" + server_id + "/channel"}, - cpr::Bearer{settings.aleeva.api_key}, - cpr::Parameters{ - {"mode", "UPLOADS"} - } - ); - - - if (response.status_code == 200) { - if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { - try { - json parsed = json::parse(response.text); - for (auto& server : parsed) { - DiscordId channel_id; - channel_id.id = server["id"]; - channel_id.name = server["name"]; - - if (settings.aleeva.channel_ids.count(server_id) == 0) { - settings.aleeva.channel_ids.emplace(server_id, std::vector()); - } - - auto& channels = settings.aleeva.channel_ids.at(server_id); - channels.push_back(channel_id); - - if (channels.size() > 0 && settings.aleeva.selected_channel_id == "") { - settings.aleeva.selected_channel_id = channels[0].id; - } - } - } catch(const json::exception& e) { - LOG_F(ERROR, "Aleeva Channels JSON Parse Fail: %s", e.what()); - } - } - } else { - LOG_F(INFO, "Aleeva Channels response: %s", response.text.c_str()); - } + if (!settings.aleeva.authorised) { + return; + } + + cpr::Response response; + response = cpr::Get( + cpr::Url{ "https://api.aleeva.io/server/" + server_id + "/channel" }, + cpr::Bearer{ settings.aleeva.api_key }, + cpr::Parameters{ + {"mode", "UPLOADS"} + } + ); + + + if (response.status_code == 200) { + if (response.header.count("Content-Type") && response.header["Content-Type"] == "application/json") { + try { + json parsed = json::parse(response.text); + for (auto& server : parsed) { + DiscordId channel_id; + channel_id.id = server["id"]; + channel_id.name = server["name"]; + + if (settings.aleeva.channel_ids.count(server_id) == 0) { + settings.aleeva.channel_ids.emplace(server_id, std::vector()); + } + + auto& channels = settings.aleeva.channel_ids.at(server_id); + channels.push_back(channel_id); + + if (channels.size() > 0 && settings.aleeva.selected_channel_id == "") { + settings.aleeva.selected_channel_id = channels[0].id; + } + } + } + catch (const json::exception& e) { + LOG_F(ERROR, "Aleeva Channels JSON Parse Fail: %s", e.what()); + } + } + } + else { + LOG_F(INFO, "Aleeva Channels response: %s", response.text.c_str()); + } } void Aleeva::post_log(AleevaSettings& settings, const std::string& log_path) { - json body; - body["sendNotification"] = settings.should_post; - body["notificationServerId"] = settings.selected_server_id; - body["notificationChannelId"] = settings.selected_channel_id; - body["dpsReportPermalink"] = log_path; - - cpr::Response response; - response = cpr::Post( - cpr::Url{ - "https://api.aleeva.io/report"}, - cpr::Bearer{settings.api_key}, - cpr::Header{ - {"accept", "application/json"}, - {"Content-Type", "application/json"}, - }, - cpr::Body{body.dump()} - ); - if (response.status_code != 200 && response.status_code != 201) { - LOG_F(ERROR, "Aleeva post log failed: %s", response.text.c_str()); - } + json body; + body["sendNotification"] = settings.should_post; + body["notificationServerId"] = settings.selected_server_id; + body["notificationChannelId"] = settings.selected_channel_id; + body["dpsReportPermalink"] = log_path; + + cpr::Response response; + response = cpr::Post( + cpr::Url{ + "https://api.aleeva.io/report" }, + cpr::Bearer{ settings.api_key }, + cpr::Header{ + {"accept", "application/json"}, + {"Content-Type", "application/json"}, + }, + cpr::Body{ body.dump() } + ); + if (response.status_code != 200 && response.status_code != 201) { + LOG_F(ERROR, "Aleeva post log failed: %s", response.text.c_str()); + } } \ No newline at end of file diff --git a/arcdps_uploader/Aleeva.h b/arcdps_uploader/Aleeva.h index 999cf40..beeee44 100644 --- a/arcdps_uploader/Aleeva.h +++ b/arcdps_uploader/Aleeva.h @@ -12,9 +12,9 @@ namespace Aleeva { std::string name; }; - void login(Settings& settings); + bool login(Settings& settings); - std::string authorize(Settings& settings); + bool authorize(Settings& settings); void deauthorize(Settings& settings); bool is_refresh_token_valid(Settings& settings); void get_servers(Settings& settings); diff --git a/arcdps_uploader/Uploader.cpp b/arcdps_uploader/Uploader.cpp index e513f08..d3c2780 100644 --- a/arcdps_uploader/Uploader.cpp +++ b/arcdps_uploader/Uploader.cpp @@ -750,7 +750,14 @@ void Uploader::imgui_draw_options_aleeva() { if (ImGui::Button("Login")) { auto future = std::async( - std::launch::async, [&]() { Aleeva::login(settings); }); + std::launch::async, [&]() { + StatusMessage sm; + sm.msg = "Aleeva login failed. Please check your access code and try to login again."; + if (Aleeva::login(settings)) { + sm.msg = "Aleeva login successful."; + } + status_messages.push_back(sm); + }); } } else { ImGui::SameLine(); @@ -1199,7 +1206,14 @@ void Uploader::start_upload_thread() { // Aleeva Authorise if (settings.aleeva.enabled) { auto future = - std::async(std::launch::async, [&]() { Aleeva::login(settings); }); + std::async(std::launch::async, [&]() { + StatusMessage sm; + sm.msg = "Aleeva login failed. Please check your access code and try to login again."; + if (Aleeva::login(settings)) { + sm.msg = "Aleeva login successful."; + } + status_messages.push_back(sm); + }); } } diff --git a/arcdps_uploader/arcdps_uploader.cpp b/arcdps_uploader/arcdps_uploader.cpp index e0e3108..61f7987 100644 --- a/arcdps_uploader/arcdps_uploader.cpp +++ b/arcdps_uploader/arcdps_uploader.cpp @@ -124,7 +124,7 @@ arcdps_exports* mod_init() { exports.sig = 0x92485179; exports.imguivers = IMGUI_VERSION_NUM; exports.out_name = "uploader"; - exports.out_build = "1.0.1"; + exports.out_build = "1.0.4"; exports.wnd_nofilter = mod_wnd; exports.combat = mod_combat; exports.imgui = mod_imgui;