File: SnapMacaroonDialog.cpp

package info (click to toggle)
plasma-discover 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,288 kB
  • sloc: cpp: 30,576; xml: 2,710; python: 311; sh: 5; makefile: 5
file content (89 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 *   SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
 *
 *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "ui_SnapMacaroonDialog.h"
#include <KAuth/ExecuteJob>
#include <QApplication>
#include <QBuffer>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QPointer>
#include <QTextStream>

class MacaroonDialog : public QDialog
{
public:
    MacaroonDialog()
        : QDialog()
    {
        m_ui.setupUi(this);
        connect(this, &QDialog::accepted, this, &MacaroonDialog::startLogin);
        connect(this, &QDialog::rejected, this, []() {
            qApp->exit(1);
        });

        setOtpMode(false);
    }

    void startLogin()
    {
        login(m_ui.username->text(), m_ui.password->text(), m_ui.otp->text());
    }

    void login(const QString &username, const QString &password, const QString &otp = {})
    {
        KAuth::Action snapAction(QStringLiteral("org.kde.discover.libsnapclient.login"));
        snapAction.setHelperId(QStringLiteral("org.kde.discover.libsnapclient"));
        snapAction.setArguments({
            {QStringLiteral("user"), username},
            {QStringLiteral("password"), password},
            {QStringLiteral("otp"), otp},
        });
        Q_ASSERT(snapAction.isValid());

        KAuth::ExecuteJob *reply = snapAction.execute();
        connect(reply, &KAuth::ExecuteJob::result, this, &MacaroonDialog::replied);
        reply->start();
    }

    void setOtpMode(bool enabled)
    {
        m_ui.password->setEnabled(!enabled);
        m_ui.otp->setVisible(enabled);
        m_ui.otpLabel->setVisible(enabled);
    }

    void replied(KJob *job)
    {
        KAuth::ExecuteJob *reply = static_cast<KAuth::ExecuteJob *>(job);
        const QVariantMap replyData = reply->data();
        if (reply->error() == 0) {
            QTextStream(stdout) << replyData[QLatin1String("reply")].toString();
            QCoreApplication::instance()->exit(0);
        } else {
            const QString message = replyData.value(QLatin1String("errorString"), reply->errorString()).toString();
            setOtpMode(replyData[QLatin1String("otpMode")].toBool());

            m_ui.errorMessage->setText(message);
            show();
        }
    }

    Ui::SnapMacaroonDialog m_ui;
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(false);
    QPointer<MacaroonDialog> dialog = new MacaroonDialog;
    dialog->show();
    auto ret = app.exec();
    delete dialog;
    return ret;
}