mirror of
https://gitlab.com/niansa/qcommsy.git
synced 2025-03-06 20:53:33 +01:00
Updated
This commit is contained in:
parent
1d26801a26
commit
fec77a7967
3 changed files with 22 additions and 16 deletions
|
@ -15,15 +15,20 @@ class authFailureError : public std::exception {};
|
|||
|
||||
class libCommsyAuth : QObject {
|
||||
public:
|
||||
QNetworkAccessManager *mNetMan = nullptr;
|
||||
QNetworkRequest *mNetReq = nullptr;
|
||||
|
||||
libCommsyAuth() {
|
||||
mNetMan = new QNetworkAccessManager(this);
|
||||
mNetReq = new QNetworkRequest();
|
||||
}
|
||||
|
||||
QNetworkReply *connect(const QUrl& serverBaseUrl) {
|
||||
// Initialise variables
|
||||
QNetworkAccessManager mNetMan(this);
|
||||
QNetworkRequest mNetReq;
|
||||
// Initialise request
|
||||
//mNetReq->setTransferTimeout(15000);
|
||||
mNetReq.setUrl(serverBaseUrl);
|
||||
mNetReq.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
||||
return mNetMan.get(mNetReq);
|
||||
mNetReq->setUrl(serverBaseUrl);
|
||||
mNetReq->setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
|
||||
return mNetMan->get(*mNetReq);
|
||||
}
|
||||
|
||||
QString getAuthUrl(const QNetworkReply *reply) {
|
||||
|
@ -31,23 +36,22 @@ public:
|
|||
}
|
||||
|
||||
QNetworkReply *sendAuth(const QString& authUrl, const QString& username, const QString& password) {
|
||||
QNetworkAccessManager mNetMan(this);
|
||||
QNetworkRequest mNetReq;
|
||||
QUrlQuery mNetQuery;
|
||||
// Set auth credentials
|
||||
mNetQuery.addQueryItem("user_id", username);
|
||||
mNetQuery.addQueryItem("password", password);
|
||||
// Create request
|
||||
mNetReq.setUrl(authUrl + "&mod=context&fct=login");
|
||||
mNetReq.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
mNetReq->setUrl(authUrl + "&mod=context&fct=login");
|
||||
mNetReq->setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
// Perform the request
|
||||
return mNetMan.post(mNetReq, mNetQuery.toString(QUrl::FullyEncoded).toUtf8());
|
||||
return mNetMan->post(*mNetReq, mNetQuery.toString(QUrl::FullyEncoded).toUtf8());
|
||||
}
|
||||
|
||||
QString getSID(QNetworkReply *reply) {
|
||||
auto cookies = QNetworkCookie::parseCookies(reply->rawHeader("Set-Cookie"));
|
||||
for (const auto& cookie : cookies) {
|
||||
if (cookie.name() == "SID") {
|
||||
std::cout << cookie.value().toStdString() << std::endl;
|
||||
return cookie.value();
|
||||
}
|
||||
}
|
||||
|
|
2
login.ui
2
login.ui
|
@ -91,7 +91,7 @@
|
|||
<enum>QLineEdit::Password</enum>
|
||||
</property>
|
||||
<property name="placeholderText">
|
||||
<string>SID</string>
|
||||
<string>Passwort</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
|
10
main.cpp
10
main.cpp
|
@ -10,6 +10,7 @@
|
|||
#include <QUrl>
|
||||
#include <QMainWindow>
|
||||
#include <QStyleFactory>
|
||||
#include <QNetworkReply>
|
||||
#include <QDesktopServices>
|
||||
#include <QSettings>
|
||||
#include <limits.h>
|
||||
|
@ -137,13 +138,14 @@ void _UILoader::loginWindow(QMainWindow *w, const QString& failure) {
|
|||
settings->setValue("server_room", thisui->roomLine->text());
|
||||
// Connect
|
||||
QNetworkReply *netReply = auther->connect(thisui->adressLine->text());
|
||||
w->connect(netReply, &QNetworkReply::readyRead, [this, thisui, w, &netReply] () { // SIGSEGV
|
||||
w->connect(netReply, &QNetworkReply::finished, [this, thisui, w, netReply] () { // SIGSEGV
|
||||
std::cout << auther->getAuthUrl(netReply).toStdString() << std::endl;
|
||||
// Get SID
|
||||
netReply = auther->sendAuth(auther->getAuthUrl(netReply), thisui->usernameLine->text(), thisui->passwordLine->text());
|
||||
w->connect(netReply, &QNetworkReply::readyRead, [this, thisui, w, netReply] () {
|
||||
QNetworkReply *netReply2 = auther->sendAuth(auther->getAuthUrl(netReply), thisui->usernameLine->text(), thisui->passwordLine->text());
|
||||
w->connect(netReply2, &QNetworkReply::readyRead, [this, thisui, w, netReply2] () {
|
||||
// Login
|
||||
try {
|
||||
settings->setValue("server_sid", auther->getSID(netReply));
|
||||
settings->setValue("server_sid", auther->getSID(netReply2));
|
||||
cacheInvalidate = true;
|
||||
overviewWindow(w, false);
|
||||
} catch (authFailureError&) {
|
||||
|
|
Loading…
Add table
Reference in a new issue