File indexing completed on 2025-02-16 05:00:58

0001 /*
0002  *   SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "ui_SnapMacaroonDialog.h"
0008 #include <KAuth/ExecuteJob>
0009 #include <QApplication>
0010 #include <QBuffer>
0011 #include <QDebug>
0012 #include <QJsonArray>
0013 #include <QJsonDocument>
0014 #include <QJsonObject>
0015 #include <QPointer>
0016 #include <QTextStream>
0017 
0018 class MacaroonDialog : public QDialog
0019 {
0020 public:
0021     MacaroonDialog()
0022         : QDialog()
0023     {
0024         m_ui.setupUi(this);
0025         connect(this, &QDialog::accepted, this, &MacaroonDialog::startLogin);
0026         connect(this, &QDialog::rejected, this, []() {
0027             qApp->exit(1);
0028         });
0029 
0030         setOtpMode(false);
0031     }
0032 
0033     void startLogin()
0034     {
0035         login(m_ui.username->text(), m_ui.password->text(), m_ui.otp->text());
0036     }
0037 
0038     void login(const QString &username, const QString &password, const QString &otp = {})
0039     {
0040         KAuth::Action snapAction(QStringLiteral("org.kde.discover.libsnapclient.login"));
0041         snapAction.setHelperId(QStringLiteral("org.kde.discover.libsnapclient"));
0042         snapAction.setArguments({
0043             {QStringLiteral("user"), username},
0044             {QStringLiteral("password"), password},
0045             {QStringLiteral("otp"), otp},
0046         });
0047         Q_ASSERT(snapAction.isValid());
0048 
0049         KAuth::ExecuteJob *reply = snapAction.execute();
0050         connect(reply, &KAuth::ExecuteJob::result, this, &MacaroonDialog::replied);
0051         reply->start();
0052     }
0053 
0054     void setOtpMode(bool enabled)
0055     {
0056         m_ui.password->setEnabled(!enabled);
0057         m_ui.otp->setVisible(enabled);
0058         m_ui.otpLabel->setVisible(enabled);
0059     }
0060 
0061     void replied(KJob *job)
0062     {
0063         KAuth::ExecuteJob *reply = static_cast<KAuth::ExecuteJob *>(job);
0064         const QVariantMap replyData = reply->data();
0065         if (reply->error() == 0) {
0066             QTextStream(stdout) << replyData[QLatin1String("reply")].toString();
0067             QCoreApplication::instance()->exit(0);
0068         } else {
0069             const QString message = replyData.value(QLatin1String("errorString"), reply->errorString()).toString();
0070             setOtpMode(replyData[QLatin1String("otpMode")].toBool());
0071 
0072             m_ui.errorMessage->setText(message);
0073             show();
0074         }
0075     }
0076 
0077     Ui::SnapMacaroonDialog m_ui;
0078 };
0079 
0080 int main(int argc, char **argv)
0081 {
0082     QApplication app(argc, argv);
0083     app.setQuitOnLastWindowClosed(false);
0084     QPointer<MacaroonDialog> dialog = new MacaroonDialog;
0085     dialog->show();
0086     auto ret = app.exec();
0087     delete dialog;
0088     return ret;
0089 }