summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorouwou <26526779+ouwou@users.noreply.github.com>2023-07-01 00:56:09 -0400
committerouwou <26526779+ouwou@users.noreply.github.com>2023-07-01 00:56:09 -0400
commit4ac7ca9245016616bbc77acf8e989e41729e1afd (patch)
tree4e7fdf999bec15cb1f4c7783e76a4ac8d496e317
parent3b206e11216b383fae0e860e0092f71301fd4425 (diff)
downloadabaddon-portaudio-4ac7ca9245016616bbc77acf8e989e41729e1afd.tar.gz
abaddon-portaudio-4ac7ca9245016616bbc77acf8e989e41729e1afd.zip
finalize login
-rw-r--r--src/abaddon.cpp3
-rw-r--r--src/discord/discord.cpp14
-rw-r--r--src/discord/discord.hpp2
-rw-r--r--src/remoteauth/remoteauthclient.cpp32
-rw-r--r--src/remoteauth/remoteauthclient.hpp6
-rw-r--r--src/remoteauth/remoteauthdialog.cpp7
-rw-r--r--src/remoteauth/remoteauthdialog.hpp1
7 files changed, 65 insertions, 0 deletions
diff --git a/src/abaddon.cpp b/src/abaddon.cpp
index 545a3c2..e0d39b5 100644
--- a/src/abaddon.cpp
+++ b/src/abaddon.cpp
@@ -841,9 +841,12 @@ void Abaddon::ActionLoginQR() {
auto response = dlg.run();
if (response == Gtk::RESPONSE_OK) {
m_discord_token = dlg.GetToken();
+ m_discord.UpdateToken(m_discord_token);
m_main_window->UpdateComponents();
+ GetSettings().DiscordToken = m_discord_token;
}
m_main_window->UpdateMenus();
+ ActionConnect();
}
void Abaddon::ActionChannelOpened(Snowflake id, bool expand_to) {
diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp
index 817aca8..5dc0464 100644
--- a/src/discord/discord.cpp
+++ b/src/discord/discord.cpp
@@ -1197,6 +1197,20 @@ void DiscordClient::AcceptVerificationGate(Snowflake guild_id, VerificationGateI
});
}
+void DiscordClient::RemoteAuthLogin(const std::string &ticket, const sigc::slot<void(std::optional<std::string>, DiscordError code)> &callback) {
+ http::request req(http::REQUEST_POST, "https://discord.com/api/v9/users/@me/remote-auth/login");
+ req.set_header("Content-Type", "application/json");
+ req.set_user_agent(Abaddon::Get().GetSettings().UserAgent);
+ req.set_body("{\"ticket\":\"" + ticket + "\"}");
+ m_http.Execute(std::move(req), [this, callback](const http::response_type &r) {
+ if (CheckCode(r)) {
+ callback(nlohmann::json::parse(r.text).at("encrypted_token").get<std::string>(), DiscordError::NONE);
+ } else {
+ callback(std::nullopt, GetCodeFromResponse(r));
+ }
+ });
+}
+
#ifdef WITH_VOICE
void DiscordClient::ConnectToVoice(Snowflake channel_id) {
auto channel = GetChannel(channel_id);
diff --git a/src/discord/discord.hpp b/src/discord/discord.hpp
index d2435dd..438e4e6 100644
--- a/src/discord/discord.hpp
+++ b/src/discord/discord.hpp
@@ -184,6 +184,8 @@ public:
void GetVerificationGateInfo(Snowflake guild_id, const sigc::slot<void(std::optional<VerificationGateInfoObject>)> &callback);
void AcceptVerificationGate(Snowflake guild_id, VerificationGateInfoObject info, const sigc::slot<void(DiscordError code)> &callback);
+ void RemoteAuthLogin(const std::string &ticket, const sigc::slot<void(std::optional<std::string>, DiscordError code)> &callback);
+
#ifdef WITH_VOICE
void ConnectToVoice(Snowflake channel_id);
void DisconnectFromVoice();
diff --git a/src/remoteauth/remoteauthclient.cpp b/src/remoteauth/remoteauthclient.cpp
index 1ebb7cb..390f42d 100644
--- a/src/remoteauth/remoteauthclient.cpp
+++ b/src/remoteauth/remoteauthclient.cpp
@@ -1,4 +1,5 @@
#include "remoteauthclient.hpp"
+#include "http.hpp"
#include <nlohmann/json.hpp>
#include <spdlog/fmt/bin_to_hex.h>
@@ -65,6 +66,14 @@ struct RemoteAuthPendingTicketMessage {
}
};
+struct RemoteAuthPendingLoginMessage {
+ std::string Ticket;
+
+ friend void from_json(const nlohmann::json &j, RemoteAuthPendingLoginMessage &m) {
+ j.at("ticket").get_to(m.Ticket);
+ }
+};
+
RemoteAuthClient::RemoteAuthClient()
: m_ws("remote-auth-ws")
, m_log(spdlog::get("remote-auth")) {
@@ -116,6 +125,8 @@ void RemoteAuthClient::OnGatewayMessage(const std::string &str) {
HandleGatewayPendingRemoteInit(j);
} else if (msg.Opcode == "pending_ticket") {
HandleGatewayPendingTicket(j);
+ } else if (msg.Opcode == "pending_login") {
+ HandleGatewayPendingLogin(j);
}
}
@@ -162,6 +173,23 @@ void RemoteAuthClient::HandleGatewayPendingTicket(const nlohmann::json &j) {
m_log->trace("User payload: {}", std::string(payload.begin(), payload.end()));
}
+void RemoteAuthClient::HandleGatewayPendingLogin(const nlohmann::json &j) {
+ RemoteAuthPendingLoginMessage msg = j;
+
+ Abaddon::Get().GetDiscordClient().RemoteAuthLogin(msg.Ticket, sigc::mem_fun(*this, &RemoteAuthClient::OnRemoteAuthLoginResponse));
+}
+
+void RemoteAuthClient::OnRemoteAuthLoginResponse(const std::optional<std::string> &encrypted_token, DiscordError err) {
+ if (!encrypted_token.has_value()) {
+ m_log->error("Remote auth login failed: {}", static_cast<int>(err));
+ return;
+ }
+
+ const auto encrypted = Glib::Base64::decode(*encrypted_token);
+ const auto token = Decrypt(reinterpret_cast<const unsigned char *>(encrypted.data()), encrypted.size());
+ m_signal_token.emit(std::string(token.begin(), token.end()));
+}
+
void RemoteAuthClient::Init() {
RemoteAuthInitMessage msg;
@@ -319,3 +347,7 @@ void RemoteAuthClient::OnDispatch() {
RemoteAuthClient::type_signal_fingerprint RemoteAuthClient::signal_fingerprint() {
return m_signal_fingerprint;
}
+
+RemoteAuthClient::type_signal_token RemoteAuthClient::signal_token() {
+ return m_signal_token;
+}
diff --git a/src/remoteauth/remoteauthclient.hpp b/src/remoteauth/remoteauthclient.hpp
index 178e10a..8fb8bc3 100644
--- a/src/remoteauth/remoteauthclient.hpp
+++ b/src/remoteauth/remoteauthclient.hpp
@@ -22,6 +22,9 @@ private:
void HandleGatewayNonceProof(const nlohmann::json &j);
void HandleGatewayPendingRemoteInit(const nlohmann::json &j);
void HandleGatewayPendingTicket(const nlohmann::json &j);
+ void HandleGatewayPendingLogin(const nlohmann::json &j);
+
+ void OnRemoteAuthLoginResponse(const std::optional<std::string> &encrypted_token, DiscordError err);
void Init();
@@ -57,8 +60,11 @@ private:
public:
using type_signal_fingerprint = sigc::signal<void(std::string)>;
+ using type_signal_token = sigc::signal<void(std::string)>;
type_signal_fingerprint signal_fingerprint();
+ type_signal_token signal_token();
private:
type_signal_fingerprint m_signal_fingerprint;
+ type_signal_token m_signal_token;
};
diff --git a/src/remoteauth/remoteauthdialog.cpp b/src/remoteauth/remoteauthdialog.cpp
index db154c4..ea250d6 100644
--- a/src/remoteauth/remoteauthdialog.cpp
+++ b/src/remoteauth/remoteauthdialog.cpp
@@ -24,6 +24,7 @@ RemoteAuthDialog::RemoteAuthDialog(Gtk::Window &parent)
m_bbox.set_layout(Gtk::BUTTONBOX_END);
m_ra.signal_fingerprint().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnFingerprint));
+ m_ra.signal_token().connect(sigc::mem_fun(*this, &RemoteAuthDialog::OnToken));
m_ra.Start();
@@ -77,3 +78,9 @@ void RemoteAuthDialog::OnFingerprint(const std::string &fingerprint) {
std::string RemoteAuthDialog::GetToken() {
return m_token;
}
+
+void RemoteAuthDialog::OnToken(const std::string &token) {
+ m_token = token;
+ m_ra.Stop();
+ response(Gtk::RESPONSE_OK);
+}
diff --git a/src/remoteauth/remoteauthdialog.hpp b/src/remoteauth/remoteauthdialog.hpp
index 24fad56..44af821 100644
--- a/src/remoteauth/remoteauthdialog.hpp
+++ b/src/remoteauth/remoteauthdialog.hpp
@@ -18,6 +18,7 @@ private:
RemoteAuthClient m_ra;
void OnFingerprint(const std::string &fingerprint);
+ void OnToken(const std::string &token);
std::string m_token;
};