File: SnapAuthHelper.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 (68 lines) | stat: -rw-r--r-- 2,050 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
/*
 *   SPDX-FileCopyrightText: 2016 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 <KAuth/ActionReply>
#include <KAuth/HelperSupport>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <Snapd/Client>
#include <stdlib.h>
#include <unistd.h>

using namespace KAuth;

class SnapAuthHelper : public QObject
{
    Q_OBJECT
    QSnapdClient m_client;

public:
    SnapAuthHelper()
    {
    }

public Q_SLOTS:
    KAuth::ActionReply login(const QVariantMap &args)
    {
        const QString user = args[QStringLiteral("user")].toString(), pass = args[QStringLiteral("password")].toString(),
                      otp = args[QStringLiteral("otp")].toString();

        QScopedPointer<QSnapdLoginRequest> req(otp.isEmpty() ? m_client.login(user, pass) : m_client.login(user, pass, otp));

        req->runSync();

        ActionReply reply;
        bool otpMode = false;
        QByteArray replyData;

        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("otpMode"), otpMode},
        });
        return reply;
    }
};

KAUTH_HELPER_MAIN("org.kde.discover.libsnapclient", SnapAuthHelper)

#include "SnapAuthHelper.moc"