Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use QtWebEngine for getting cookies and authorization code if login form contains recaptcha and downloader is compiled with -DUSE_QT_GUI=ON
- Loading branch information
Showing
5 changed files
with
347 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* This program is free software. It comes without any warranty, to | ||
* the extent permitted by applicable law. You can redistribute it | ||
* and/or modify it under the terms of the Do What The Fuck You Want | ||
* To Public License, Version 2, as published by Sam Hocevar. See | ||
* http://www.wtfpl.net/ for more details. */ | ||
|
||
#ifndef GUI_LOGIN_H | ||
#define GUI_LOGIN_H | ||
|
||
#include "config.h" | ||
#include "util.h" | ||
#include "globals.h" | ||
|
||
#include <QObject> | ||
#include <QWebEngineCookieStore> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
class GuiLogin : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
GuiLogin(); | ||
virtual ~GuiLogin(); | ||
|
||
void Login(); | ||
std::string getCode(); | ||
std::vector<std::string> getCookies(); | ||
|
||
private: | ||
QWebEngineCookieStore *cookiestore; | ||
std::vector<std::string> cookies; | ||
std::string auth_code; | ||
|
||
public slots: | ||
void loadFinished(bool success); | ||
void cookieAdded(const QNetworkCookie &cookie); | ||
}; | ||
|
||
#endif // GUI_LOGIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* This program is free software. It comes without any warranty, to | ||
* the extent permitted by applicable law. You can redistribute it | ||
* and/or modify it under the terms of the Do What The Fuck You Want | ||
* To Public License, Version 2, as published by Sam Hocevar. See | ||
* http://www.wtfpl.net/ for more details. */ | ||
|
||
#include "gui_login.h" | ||
|
||
#include <QApplication> | ||
#include <QtWidgets/QWidget> | ||
#include <QtWebEngineWidgets/QWebEngineView> | ||
#include <QStyle> | ||
#include <QLayout> | ||
#include <QDesktopWidget> | ||
#include <QWebEngineProfile> | ||
|
||
GuiLogin::GuiLogin() | ||
{ | ||
// constructor | ||
} | ||
|
||
GuiLogin::~GuiLogin() | ||
{ | ||
// destructor | ||
} | ||
|
||
void GuiLogin::loadFinished(bool success) | ||
{ | ||
QWebEngineView *view = qobject_cast<QWebEngineView*>(sender()); | ||
std::string url = view->page()->url().toString().toUtf8().constData(); | ||
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); | ||
if (pos != std::string::npos) | ||
{ | ||
pos += find_str.length(); | ||
std::string code; | ||
code.assign(url.begin()+pos, url.end()); | ||
if (!code.empty()) | ||
{ | ||
this->auth_code = code; | ||
QCoreApplication::exit(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void GuiLogin::cookieAdded(const QNetworkCookie& cookie) | ||
{ | ||
std::string raw_cookie = cookie.toRawForm().toStdString(); | ||
if (!raw_cookie.empty()) | ||
{ | ||
std::string set_cookie = "Set-Cookie: " + raw_cookie; | ||
bool duplicate = false; | ||
for (auto cookie : this->cookies) | ||
{ | ||
if (set_cookie == cookie) | ||
{ | ||
duplicate = true; | ||
break; | ||
} | ||
} | ||
if (!duplicate) | ||
this->cookies.push_back(set_cookie); | ||
} | ||
} | ||
|
||
void GuiLogin::Login() | ||
{ | ||
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); | ||
|
||
std::vector<char> version_string( | ||
Globals::globalConfig.sVersionString.c_str(), | ||
Globals::globalConfig.sVersionString.c_str() + Globals::globalConfig.sVersionString.size() + 1 | ||
); | ||
|
||
int argc = 1; | ||
char *argv[] = {&version_string[0]}; | ||
QApplication app(argc, argv); | ||
|
||
QWidget window; | ||
QVBoxLayout *layout = new QVBoxLayout; | ||
QSize window_size(440, 540); | ||
|
||
window.setGeometry( | ||
QStyle::alignedRect( | ||
Qt::LeftToRight, | ||
Qt::AlignCenter, | ||
window_size, | ||
qApp->desktop()->availableGeometry() | ||
) | ||
); | ||
|
||
QWebEngineView *webengine = new QWebEngineView(&window); | ||
layout->addWidget(webengine); | ||
QWebEngineProfile profile; | ||
profile.setHttpUserAgent(QString::fromStdString(Globals::globalConfig.curlConf.sUserAgent)); | ||
QWebEnginePage page(&profile); | ||
cookiestore = profile.cookieStore(); | ||
|
||
QObject::connect( | ||
webengine, SIGNAL(loadFinished(bool)), | ||
this, SLOT(loadFinished(bool)) | ||
); | ||
|
||
QObject::connect( | ||
this->cookiestore, SIGNAL(cookieAdded(const QNetworkCookie&)), | ||
this, SLOT(cookieAdded(const QNetworkCookie&)) | ||
); | ||
|
||
webengine->resize(window.frameSize()); | ||
webengine->setPage(&page); | ||
webengine->setUrl(url); | ||
|
||
window.setLayout(layout); | ||
window.show(); | ||
|
||
app.exec(); | ||
} | ||
|
||
std::string GuiLogin::getCode() | ||
{ | ||
return this->auth_code; | ||
} | ||
|
||
std::vector<std::string> GuiLogin::getCookies() | ||
{ | ||
return this->cookies; | ||
} | ||
|
||
#include "moc_gui_login.cpp" |
Oops, something went wrong.