diff --git a/include/gui_login.h b/include/gui_login.h index e3590da..0a8c758 100644 --- a/include/gui_login.h +++ b/include/gui_login.h @@ -25,6 +25,7 @@ class GuiLogin : public QObject virtual ~GuiLogin(); void Login(); + void Login(const std::string& username, const std::string& password); std::string getCode(); std::vector getCookies(); @@ -32,6 +33,8 @@ class GuiLogin : public QObject QWebEngineCookieStore *cookiestore; std::vector cookies; std::string auth_code; + std::string login_username; + std::string login_password; public slots: void loadFinished(bool success); diff --git a/src/gui_login.cpp b/src/gui_login.cpp index 55fda03..545071b 100644 --- a/src/gui_login.cpp +++ b/src/gui_login.cpp @@ -28,7 +28,20 @@ void GuiLogin::loadFinished(bool success) { QWebEngineView *view = qobject_cast(sender()); std::string url = view->page()->url().toString().toUtf8().constData(); - if (success && url.find("https://embed.gog.com/on_login_success") != std::string::npos) + + // Autofill login form + if (success && url.find("https://login.gog.com/auth?client_id=") != std::string::npos) + { + if (!this->login_username.empty() && !this->login_password.empty()) + { + std::string js_fill_username = "document.getElementById(\"login_username\").value = \"" + this->login_username + "\";"; + std::string js_fill_password = "document.getElementById(\"login_password\").value = \"" + this->login_password + "\";"; + std::string js = js_fill_username + js_fill_password; + view->page()->runJavaScript(QString::fromStdString(js)); + } + } + // Get auth code + else if (success && url.find("https://embed.gog.com/on_login_success") != std::string::npos) { std::string find_str = "code="; auto pos = url.find(find_str); @@ -67,10 +80,17 @@ void GuiLogin::cookieAdded(const QNetworkCookie& cookie) } void GuiLogin::Login() +{ + this->Login(std::string(), std::string()); +} + +void GuiLogin::Login(const std::string& username, const std::string& password) { QByteArray redirect_uri = QUrl::toPercentEncoding(QString::fromStdString(Globals::galaxyConf.getRedirectUri())); std::string auth_url = "https://auth.gog.com/auth?client_id=" + Globals::galaxyConf.getClientId() + "&redirect_uri=" + redirect_uri.toStdString() + "&response_type=code"; QUrl url = QString::fromStdString(auth_url); + this->login_username = username; + this->login_password = password; std::vector version_string( Globals::globalConfig.sVersionString.c_str(), diff --git a/src/website.cpp b/src/website.cpp index 68d1daa..df0979b 100644 --- a/src/website.cpp +++ b/src/website.cpp @@ -323,7 +323,7 @@ int Website::Login(const std::string& email, const std::string& password) return res = 0; } GuiLogin gl; - gl.Login(); + gl.Login(email, password); auto cookies = gl.getCookies(); for (auto cookie : cookies)