Commit b748b7b2 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

Improve the macaroon dialog error reporting

parent 11734f14
......@@ -62,7 +62,7 @@ void SnapTransaction::finishTransaction()
connect(p, static_cast<void(QProcess::*)(int)>(&QProcess::finished), this, [this, p] (int code) {
p->deleteLater();
if (code != 0) {
qWarning() << "login failed..." << p->readAll();
qWarning() << "login failed... code:" << code << p->readAll();
Q_EMIT passiveMessage(m_request->errorString());
setStatus(DoneStatus);
return;
......
......@@ -41,29 +41,36 @@ public:
}
public Q_SLOTS:
ActionReply modify(const QVariantMap &args)
ActionReply login(const QVariantMap &args)
{
const QString user = args[QStringLiteral("user")].toString()
, pass = args[QStringLiteral("password")].toString()
, otp = args[QStringLiteral("otp")].toString();
QSnapdLoginRequest* req = otp.isEmpty() ? m_client.login(user, pass)
: m_client.login(user, pass, otp);
QScopedPointer<QSnapdLoginRequest> req(otp.isEmpty() ? m_client.login(user, pass)
: m_client.login(user, pass, otp));
req->runSync();
auto auth = req->authData();
const QByteArray replyData = QJsonDocument(QJsonObject{
{QStringLiteral("macaroon"), auth->macaroon()},
{QStringLiteral("discharges"), QJsonArray::fromStringList(auth->discharges())}
}).toJson();
ActionReply reply = req->error() == QSnapdRequest::NoError ? ActionReply::SuccessReply() : ActionReply::InvalidActionReply();
ActionReply reply;
bool otpMode = false;
QByteArray replyData;
bool otpMode = req->error() == QSnapdConnectRequest::TwoFactorRequired;
if (req->error() == QSnapdRequest::NoError) {
const auto auth = req->authData();
replyData = QJsonDocument(QJsonObject{
{QStringLiteral("macaroon"), auth->macaroon()},
{QStringLiteral("discharges"), QJsonArray::fromStringList(auth->discharges())}
}).toJson();
reply = ActionReply::SuccessReply();
} else {
otpMode = req->error() == QSnapdConnectRequest::TwoFactorRequired;
reply = ActionReply::InvalidActionReply();
reply.setErrorDescription(req->errorString());
}
reply.setData({
{ QStringLiteral("reply"), replyData },
{ QStringLiteral("errorString"), req->errorString() },
{ QStringLiteral("otpMode"), otpMode }
});
return reply;
......
......@@ -26,6 +26,7 @@
#include <QJsonArray>
#include <QTextStream>
#include <KAuthExecuteJob>
#include <QDebug>
#include "ui_SnapMacaroonDialog.h"
class MacaroonDialog : public QDialog
......@@ -57,11 +58,11 @@ public:
{ QStringLiteral("password"), password },
{ QStringLiteral("otp"), otp }
});
// qDebug() << "snap" << snapAction.isValid() << snapAction.status();
Q_ASSERT(snapAction.isValid());
KAuth::ExecuteJob *reply = snapAction.execute();
connect(reply, &KAuth::ExecuteJob::finished, this, &MacaroonDialog::replied);
connect(reply, &KAuth::ExecuteJob::result, this, &MacaroonDialog::replied);
reply->start();
}
void setOtpMode(bool enabled)
......@@ -74,14 +75,13 @@ public:
void replied(KJob* job)
{
KAuth::ExecuteJob* reply = static_cast<KAuth::ExecuteJob*>(job);
const QVariantMap replyData = reply->data();
if (reply->error() == 0) {
QTextStream(stdout) << reply->data()[QLatin1String("reply")].toString();
QTextStream(stdout) << replyData[QLatin1String("reply")].toString();
QCoreApplication::instance()->exit(0);
} else {
const QString message = reply->data()[QLatin1String("errorString")].toString();
const auto otpMode = reply->data()[QLatin1String("otpMode")].toBool();
setOtpMode(otpMode);
const QString message = replyData.value(QLatin1String("errorString"), reply->errorString()).toString();
setOtpMode(replyData[QLatin1String("otpMode")].toBool());
m_ui.errorMessage->setText(message);
show();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment