From e475f5d51251ae57da0480bbadc9cb32a8bc72b2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 5 Feb 2013 19:22:19 -0600 Subject: [PATCH] Implemented login system. --- CMakeLists.txt | 3 +- data/loginresponse.cpp | 26 +++++++++-- data/loginresponse.h | 10 +++- gui/mainwindow.cpp | 8 ++-- main.cpp | 4 ++ tasks/logintask.cpp | 103 +++++++++++++++++++++++++++++++++++------ tasks/logintask.h | 10 +++- 7 files changed, 142 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b466924c..984bbd05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ include_directories(hacks) #### Find the required Qt parts #### find_package(Qt5Widgets) +find_package(Qt5Network) #find_package(Qt5Declarative) include_directories(${Qt5Widgets_INCLUDE_DIRS}) @@ -171,7 +172,7 @@ QT5_WRAP_UI(MULTIMC_UI ${MULTIMC5_UIS}) QT5_ADD_RESOURCES(MULTIMC_QRC multimc.qrc) add_executable(MultiMC ${MULTIMC_SOURCES} ${MULTIMC_HEADERS} ${MULTIMC_UI} ${MULTIMC_QRC}) -qt5_use_modules(MultiMC Widgets) +qt5_use_modules(MultiMC Widgets Network) target_link_libraries(MultiMC quazip patchlib ${MultiMC_LINK_ADDITIONAL_LIBS}) add_dependencies(MultiMC MultiMCLauncher) install(TARGETS MultiMC RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}) diff --git a/data/loginresponse.cpp b/data/loginresponse.cpp index a3647114..44bc80eb 100644 --- a/data/loginresponse.cpp +++ b/data/loginresponse.cpp @@ -15,17 +15,27 @@ #include "loginresponse.h" -LoginResponse::LoginResponse(const QString& username, const QString& sessionID, QObject *parent) : +LoginResponse::LoginResponse(const QString& username, const QString& sessionID, + qint64 latestVersion, QObject *parent) : QObject(parent) { this->username = username; this->sessionID = sessionID; + this->latestVersion = latestVersion; +} + +LoginResponse::LoginResponse() +{ + this->username = ""; + this->sessionID = ""; + this->latestVersion = 0; } LoginResponse::LoginResponse(const LoginResponse &other) { - this->username = other.username; - this->sessionID = other.sessionID; + this->username = other.getUsername(); + this->sessionID = other.getSessionID(); + this->latestVersion = other.getLatestVersion(); } QString LoginResponse::getUsername() const @@ -47,3 +57,13 @@ void LoginResponse::setSessionID(const QString& sessionID) { this->sessionID = sessionID; } + +qint64 LoginResponse::getLatestVersion() const +{ + return latestVersion; +} + +void LoginResponse::setLatestVersion(qint64 v) +{ + this->latestVersion = v; +} diff --git a/data/loginresponse.h b/data/loginresponse.h index e780750f..bcb77d56 100644 --- a/data/loginresponse.h +++ b/data/loginresponse.h @@ -22,7 +22,9 @@ class LoginResponse : public QObject { Q_OBJECT public: - explicit LoginResponse(const QString &username, const QString &sessionID, QObject *parent = 0); + explicit LoginResponse(const QString &username, const QString &sessionID, + qint64 latestVersion, QObject *parent = 0); + LoginResponse(); LoginResponse(const LoginResponse& other); QString getUsername() const; @@ -31,9 +33,15 @@ public: QString getSessionID() const; void setSessionID(const QString& sessionID); + qint64 getLatestVersion() const; + void setLatestVersion(qint64 v); + private: QString username; QString sessionID; + qint64 latestVersion; }; +Q_DECLARE_METATYPE(LoginResponse) + #endif // LOGINRESPONSE_H diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index c2f0c390..bc0840a0 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -142,14 +142,16 @@ void MainWindow::doLogin(const QString &errorMsg) TaskDialog* tDialog = new TaskDialog(this); LoginTask* loginTask = new LoginTask(uInfo, tDialog); connect(loginTask, SIGNAL(loginComplete(LoginResponse)), - SLOT(onLoginComplete(LoginResponse))); + SLOT(onLoginComplete(LoginResponse)), Qt::QueuedConnection); connect(loginTask, SIGNAL(loginFailed(QString)), - SLOT(doLogin(QString))); + SLOT(doLogin(QString)), Qt::QueuedConnection); tDialog->exec(loginTask); } } void MainWindow::onLoginComplete(LoginResponse response) { - + QMessageBox::information(this, "Login Successful", + QString("Logged in as %1 with session ID %2."). + arg(response.getUsername(), response.getSessionID())); } diff --git a/main.cpp b/main.cpp index 019a8853..6e840317 100644 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,7 @@ #include "data/appsettings.h" +#include "data/loginresponse.h" int main(int argc, char *argv[]) { @@ -28,6 +29,9 @@ int main(int argc, char *argv[]) settings = new AppSettings(&app); + // Register meta types. + qRegisterMetaType("LoginResponse"); + MainWindow mainWin; mainWin.show(); diff --git a/tasks/logintask.cpp b/tasks/logintask.cpp index 211e1c17..f683b811 100644 --- a/tasks/logintask.cpp +++ b/tasks/logintask.cpp @@ -15,6 +15,15 @@ #include "logintask.h" +#include + +#include +#include +#include + +#include +#include + LoginTask::LoginTask(const UserInfo &uInfo, QObject *parent) : Task(parent), uInfo(uInfo) { @@ -25,20 +34,88 @@ void LoginTask::executeTask() { setStatus("Logging in..."); - // TODO: PLACEHOLDER - for (int p = 0; p < 100; p++) + QNetworkAccessManager netMgr; + connect(&netMgr, SIGNAL(finished(QNetworkReply*)), + SLOT(processNetReply(QNetworkReply*))); + + QUrl loginURL("https://login.minecraft.net/"); + QNetworkRequest netRequest(loginURL); + netRequest.setHeader(QNetworkRequest::ContentTypeHeader, + "application/x-www-form-urlencoded"); + + QUrlQuery params; + params.addQueryItem("user", uInfo.getUsername()); + params.addQueryItem("password", uInfo.getPassword()); + params.addQueryItem("version", "13"); + + netReply = netMgr.post(netRequest, params.query(QUrl::EncodeSpaces).toUtf8()); + exec(); +} + +void LoginTask::processNetReply(QNetworkReply *reply) +{ + // Check for errors. + switch (reply->error()) { - msleep(25); - setProgress(p); + case QNetworkReply::NoError: + { + // Check the response code. + int responseCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (responseCode == 200) + { + QString responseStr(reply->readAll()); + + QStringList strings = responseStr.split(":"); + if (strings.count() >= 4) + { + bool parseSuccess; + qint64 latestVersion = strings[0].toLongLong(&parseSuccess); + if (parseSuccess) + { + // strings[1] is the download ticket. It isn't used anymore. + QString username = strings[2]; + QString sessionID = strings[3]; + + LoginResponse response(username, sessionID, latestVersion); + emit loginComplete(response); + } + else + { + emit loginFailed("Failed to parse Minecraft version string."); + } + } + else + { + if (responseStr.toLower() == "bad login") + emit loginFailed("Invalid username or password."); + else if (responseStr.toLower() == "old version") + emit loginFailed("Launcher outdated, please update."); + else + emit loginFailed("Login failed: " + responseStr); + } + } + else if (responseCode == 503) + { + emit loginFailed("The login servers are currently unavailable. " + "Check http://help.mojang.com/ for more info."); + } + else + { + emit loginFailed(QString("Login failed: Unknown HTTP error %1 occurred."). + arg(QString::number(responseCode))); + } + break; + } + + case QNetworkReply::OperationCanceledError: + emit loginFailed("Login canceled."); + break; + + default: + emit loginFailed("Login failed: " + reply->errorString()); + break; } - if (uInfo.getUsername() == "test") - { - LoginResponse response("test", "Fake Session ID"); - emit loginComplete(response); - } - else - { - emit loginFailed("Testing"); - } + quit(); } diff --git a/tasks/logintask.h b/tasks/logintask.h index 93aab9b3..d96bfec7 100644 --- a/tasks/logintask.h +++ b/tasks/logintask.h @@ -21,19 +21,27 @@ #include "data/userinfo.h" #include "data/loginresponse.h" +//class QNetworkAccessManager; +class QNetworkReply; + class LoginTask : public Task { Q_OBJECT public: explicit LoginTask(const UserInfo& uInfo, QObject *parent = 0); +public slots: + void processNetReply(QNetworkReply* reply); + signals: - void loginComplete(const LoginResponse& loginResponse); + void loginComplete(LoginResponse loginResponse); void loginFailed(const QString& errorMsg); protected: void executeTask(); + QNetworkReply* netReply; + UserInfo uInfo; };