Task #3964 - Make premium login error more verbose

This commit is contained in:
Nick Bolton 2014-03-20 10:33:33 +00:00
parent 44a98c6c9d
commit f9fe1130ac
4 changed files with 98 additions and 39 deletions

View File

@ -20,6 +20,7 @@
#include <QProcess> #include <QProcess>
#include <QCoreApplication> #include <QCoreApplication>
#include <stdexcept>
// we use syntool to authenticate because Qt's http library is very // we use syntool to authenticate because Qt's http library is very
// unreliable, and since we're writing platform specific code, use the // unreliable, and since we're writing platform specific code, use the
@ -32,17 +33,35 @@ QString PremiumAuth::request(const QString& email, const QString& password)
QProcess process; QProcess process;
process.setReadChannel(QProcess::StandardOutput); process.setReadChannel(QProcess::StandardOutput);
process.start(program, args); process.start(program, args);
bool success = process.waitForStarted();
if (process.waitForStarted()) QString out, error;
if (success)
{ {
// hash password in case it contains interesting chars. // hash password in case it contains interesting chars.
QString credentials(email + ":" + hash(password) + "\n"); QString credentials(email + ":" + hash(password) + "\n");
process.write(credentials.toStdString().c_str()); process.write(credentials.toStdString().c_str());
if (process.waitForFinished()) { if (process.waitForFinished()) {
return process.readAll(); out = process.readAllStandardOutput();
error = process.readAllStandardError();
} }
} }
return ""; out = out.trimmed();
error = error.trimmed();
if (out.isEmpty() ||
!error.isEmpty() ||
!success ||
process.exitCode() != 0)
{
throw std::runtime_error(
QString("Code: %1\nError: %2")
.arg(process.exitCode())
.arg(error.isEmpty() ? "Unknown" : error)
.toStdString());
}
return out;
} }

View File

@ -252,12 +252,20 @@ bool SetupWizard::isPremiumLoginValid(QMessageBox& message)
QString email = m_pLineEditPremiumEmail->text(); QString email = m_pLineEditPremiumEmail->text();
QString password = m_pLineEditPremiumPassword->text(); QString password = m_pLineEditPremiumPassword->text();
PremiumAuth auth; QString responseJson;
QString responseJson = auth.request(email, password); try
{
if (responseJson.trimmed() == "") { PremiumAuth auth;
message.setText(tr("Login failed, could not communicate with server.")); responseJson = auth.request(email, password);
message.exec(); }
catch (std::exception& e)
{
message.critical(
this, "Error",
tr("Sorry, an error occured while trying to sign in. "
"Please contact the help desk, and provide the "
"following details.\n\n%1")
.arg(e.what()));
return false; return false;
} }
@ -268,8 +276,9 @@ bool SetupWizard::isPremiumLoginValid(QMessageBox& message)
return true; return true;
} }
else if (boolString == "false") { else if (boolString == "false") {
message.setText(tr("Login failed, invalid email or password.")); message.critical(
message.exec(); this, "Error",
tr("Login failed, invalid email or password."));
return false; return false;
} }
} }
@ -280,13 +289,16 @@ bool SetupWizard::isPremiumLoginValid(QMessageBox& message)
// replace "\n" with real new lines. // replace "\n" with real new lines.
QString error = errorRegex.cap(1).replace("\\n", "\n"); QString error = errorRegex.cap(1).replace("\\n", "\n");
message.setText(tr("Login failed, an error occurred.\n\n%1").arg(error)); message.critical(
message.exec(); this, "Error",
tr("Login failed, an error occurred.\n\n%1").arg(error));
return false; return false;
} }
} }
message.setText(tr("Login failed, an error occurred.\n\nServer response:\n\n%1").arg(responseJson)); message.critical(
message.exec(); this, "Error",
tr("Login failed, an error occurred.\n\nServer response:\n\n%1")
.arg(responseJson));
return false; return false;
} }

View File

@ -25,36 +25,59 @@
//#define PREMIUM_AUTH_URL "http://localhost/synergy/premium/json/auth/" //#define PREMIUM_AUTH_URL "http://localhost/synergy/premium/json/auth/"
#define PREMIUM_AUTH_URL "https://synergy-foss.org/premium/json/auth/" #define PREMIUM_AUTH_URL "https://synergy-foss.org/premium/json/auth/"
int enum {
kErrorOk,
kErrorArgs,
kErrorException,
kErrorUnknown
};
UInt32
CToolApp::run(int argc, char** argv) CToolApp::run(int argc, char** argv)
{ {
if (argc <= 1) { if (argc <= 1) {
std::cerr << "no args" << std::endl; std::cerr << "no args" << std::endl;
return 1; return kErrorArgs;
} }
for (int i = 1; i < argc; i++) { try {
if (strcmp(argv[i], "--premium-auth") == 0) { for (int i = 1; i < argc; i++) {
CString credentials; if (strcmp(argv[i], "--premium-auth") == 0) {
std::cin >> credentials; premiumAuth();
return kErrorOk;
size_t separator = credentials.find(':'); }
CString email = credentials.substr(0, separator); else {
CString password = credentials.substr(separator + 1, credentials.length()); std::cerr << "unknown arg: " << argv[i] << std::endl;
return kErrorArgs;
std::stringstream ss; }
ss << PREMIUM_AUTH_URL;
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
return 0;
}
else {
std::cerr << "unknown arg: " << argv[i] << std::endl;
return 1;
} }
} }
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return kErrorException;
}
catch (...) {
std::cerr << "unknown error" << std::endl;
return kErrorUnknown;
}
return 0; return kErrorOk;
}
void
CToolApp::premiumAuth()
{
CString credentials;
std::cin >> credentials;
size_t separator = credentials.find(':');
CString email = credentials.substr(0, separator);
CString password = credentials.substr(separator + 1, credentials.length());
std::stringstream ss;
ss << PREMIUM_AUTH_URL;
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
} }

View File

@ -17,7 +17,12 @@
#pragma once #pragma once
#include "common/basic_types.h"
class CToolApp { class CToolApp {
public: public:
int run(int argc, char** argv); UInt32 run(int argc, char** argv);
private:
void premiumAuth();
}; };