File indexing completed on 2024-11-24 04:55:01
0001 /* 0002 * SPDX-FileCopyrightText: 2016 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 <KAuth/ActionReply> 0008 #include <KAuth/HelperSupport> 0009 #include <QDebug> 0010 #include <QJsonArray> 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QProcess> 0014 #include <Snapd/Client> 0015 #include <stdlib.h> 0016 #include <unistd.h> 0017 0018 using namespace KAuth; 0019 0020 class SnapAuthHelper : public QObject 0021 { 0022 Q_OBJECT 0023 QSnapdClient m_client; 0024 0025 public: 0026 SnapAuthHelper() 0027 { 0028 } 0029 0030 public Q_SLOTS: 0031 KAuth::ActionReply login(const QVariantMap &args) 0032 { 0033 const QString user = args[QStringLiteral("user")].toString(), pass = args[QStringLiteral("password")].toString(), 0034 otp = args[QStringLiteral("otp")].toString(); 0035 0036 QScopedPointer<QSnapdLoginRequest> req(otp.isEmpty() ? m_client.login(user, pass) : m_client.login(user, pass, otp)); 0037 0038 req->runSync(); 0039 0040 ActionReply reply; 0041 bool otpMode = false; 0042 QByteArray replyData; 0043 0044 if (req->error() == QSnapdRequest::NoError) { 0045 const auto auth = req->authData(); 0046 replyData = QJsonDocument(QJsonObject{ 0047 {QStringLiteral("macaroon"), auth->macaroon()}, 0048 {QStringLiteral("discharges"), QJsonArray::fromStringList(auth->discharges())}, 0049 }) 0050 .toJson(); 0051 0052 reply = ActionReply::SuccessReply(); 0053 } else { 0054 otpMode = req->error() == QSnapdConnectRequest::TwoFactorRequired; 0055 reply = ActionReply::InvalidActionReply(); 0056 reply.setErrorDescription(req->errorString()); 0057 } 0058 reply.setData({ 0059 {QStringLiteral("reply"), replyData}, 0060 {QStringLiteral("otpMode"), otpMode}, 0061 }); 0062 return reply; 0063 } 0064 }; 0065 0066 KAUTH_HELPER_MAIN("org.kde.discover.libsnapclient", SnapAuthHelper) 0067 0068 #include "SnapAuthHelper.moc"