summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleix Pol <[email protected]>2016-10-15 03:28:53 +0200
committerAleix Pol <[email protected]>2016-10-21 00:49:42 +0200
commitd342e894752fc8a705ba0ed06bb56084789ff7ea (patch)
treedce7cf57a05a1352f1ad19eae75eca439925dc21
parentfa0aa558bb8f11e37418a06be7995536a84eeade (diff)
Make it possible to send policykit-based queries
-rw-r--r--libdiscover/backends/SnapBackend/libsnapclient/SnapAuthHelper.cpp55
-rw-r--r--libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.cpp74
-rw-r--r--libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.h4
-rw-r--r--libdiscover/backends/SnapBackend/libsnapclient/org.kde.discover.libsnapclient.actions5
4 files changed, 121 insertions, 17 deletions
diff --git a/libdiscover/backends/SnapBackend/libsnapclient/SnapAuthHelper.cpp b/libdiscover/backends/SnapBackend/libsnapclient/SnapAuthHelper.cpp
new file mode 100644
index 0000000..b45066b
--- /dev/null
+++ b/libdiscover/backends/SnapBackend/libsnapclient/SnapAuthHelper.cpp
@@ -0,0 +1,55 @@
+/***************************************************************************
+ * Copyright © 2016 Aleix Pol Gonzalez <[email protected]> *
+ * *
+ * This program is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 2 of *
+ * the License or (at your option) version 3 or any later version *
+ * accepted by the membership of KDE e.V. (or its successor approved *
+ * by the membership of KDE e.V.), which shall act as a proxy *
+ * defined in Section 14 of version 3 of the license. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QProcess>
+#include <QDebug>
+#include <QLocalSocket>
+#include <unistd.h>
+#include <stdlib.h>
+#include <kauthhelpersupport.h>
+#include <kauthactionreply.h>
+
+using namespace KAuth;
+
+class SnapAuthHelper : public QObject
+{
+ Q_OBJECT
+public Q_SLOTS:
+ ActionReply modify(const QVariantMap &args)
+ {
+ QLocalSocket socket;
+ socket.connectToServer(QStringLiteral("/run/snapd.socket"), QIODevice::ReadWrite);
+ const bool b = socket.waitForConnected();
+ Q_ASSERT(b);
+
+ const QByteArray request = args[QStringLiteral("request")].toByteArray();
+ socket.write(request);
+ socket.waitForReadyRead();
+ const auto replyData = socket.readAll();
+
+ ActionReply reply = ActionReply::SuccessReply();
+ reply.setData({ { QStringLiteral("reply"), replyData } });
+ return reply;
+ }
+};
+
+KAUTH_HELPER_MAIN("org.kde.discover.libsnapclient", SnapAuthHelper)
+
+#include "SnapAuthHelper.moc"
diff --git a/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.cpp b/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.cpp
index 89f3a45..afeb180 100644
--- a/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.cpp
+++ b/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.cpp
@@ -22,7 +22,9 @@
#include <QDebug>
#include <QJsonDocument>
#include <QUrlQuery>
+#include <QBuffer>
#include <QRegularExpression>
+#include <kauthexecutejob.h>
class LocalSnapJob : public SnapJob
{
@@ -52,6 +54,50 @@ private:
QLocalSocket * const m_socket;
};
+class AuthSnapJob : public SnapJob
+{
+public:
+ AuthSnapJob(const QByteArray& request, QObject* parent = nullptr)
+ : SnapJob(parent)
+ {
+ KAuth::Action snapAction(QStringLiteral("org.kde.discover.libsnapclient.modify"));
+ snapAction.setHelperId(QStringLiteral("org.kde.discover.libsnapclient"));
+ qDebug() << "requesting through kauth" << request;
+ snapAction.setArguments({ { QStringLiteral("request"), request } });
+ Q_ASSERT(snapAction.isValid());
+ m_reply = snapAction.execute();
+// m_reply->start();
+
+ connect(m_reply, &KAuth::ExecuteJob::finished, this, &AuthSnapJob::authJobFinished);
+ }
+
+ bool exec() override
+ {
+ m_reply->exec();
+ return isSuccessful();
+ }
+
+private:
+ void authJobFinished()
+ {
+ if (m_reply->error() == KJob::NoError) {
+ const auto reply = m_reply->data()[QStringLiteral("reply")].toByteArray();
+ QBuffer buffer;
+ buffer.setData(reply);
+ qDebug() << "replied!" << reply;
+
+ bool b = buffer.open(QIODevice::ReadOnly);
+ Q_ASSERT(b);
+ processReply(&buffer);
+ } else {
+ qDebug() << "kauth error" << m_reply->error() << m_reply->errorString();
+ Q_EMIT finished(this);
+ }
+ }
+
+ KAuth::ExecuteJob* m_reply = nullptr;
+};
+
SnapSocket::SnapSocket(QObject* parent)
: QObject(parent)
{
@@ -61,14 +107,18 @@ SnapSocket::~SnapSocket()
{
}
-QByteArray SnapSocket::createRequest(const QByteArray& method, const QByteArray& path, const QUrlQuery& content) const
+QByteArray SnapSocket::createRequest(const QByteArray& method, const QByteArray& path, const QJsonObject& content) const
{
QByteArray ret;
- const auto query = content.toString().toUtf8();
- if (method == "GET")
+ if (method == "GET") {
+ QUrlQuery uq;
+ for(auto it = content.constBegin(), itEnd = content.constEnd(); it!=itEnd; ++it) {
+ uq.addQueryItem(it.key(), it.value().toString());
+ }
+ const auto query = uq.toString().toUtf8();
ret = createRequest(method, path+'?'+query, QByteArray());
- else if(method == "POST")
- ret = createRequest(method, path, query);
+ } else if(method == "POST")
+ ret = createRequest(method, path, QJsonDocument(content).toJson());
else
qWarning() << "unknown method" << method;
return ret;
@@ -107,9 +157,7 @@ SnapJob* SnapSocket::snapByName(const QString& name)
SnapJob* SnapSocket::find(const QString& query)
{
- QUrlQuery uq;
- uq.addQueryItem(QStringLiteral("q"), query);
- return new LocalSnapJob(createRequest("GET", "/v2/find", uq), this);
+ return new LocalSnapJob(createRequest("GET", "/v2/find", {{ QStringLiteral("q"), query }}), this);
}
SnapJob* SnapSocket::findByName(const QString& name)
@@ -130,12 +178,10 @@ SnapJob * SnapSocket::snapAction(const QString& name, SnapSocket::SnapAction act
default:
Q_UNREACHABLE();
}
- QUrlQuery uq;
- uq.addQueryItem(QStringLiteral("action"), actionStr);
+ QJsonObject query = {{ QStringLiteral("action"), actionStr }};
if (!channel.isEmpty())
- uq.addQueryItem(QStringLiteral("channel"), channel);
-
- return new LocalSnapJob(createRequest("POST", "/v2/snaps/"+name.toUtf8(), uq), this);
+ query.insert(QStringLiteral("channel"), channel);
+ return new AuthSnapJob(createRequest("POST", "/v2/snaps/"+name.toUtf8(), query), this);
}
void SnapSocket::login(const QString& username, const QString& password, const QString& otp)
@@ -144,7 +190,7 @@ void SnapSocket::login(const QString& username, const QString& password, const Q
if (!otp.isEmpty())
obj.insert(QStringLiteral("obj"), obj);
- auto job = new LocalSnapJob(createRequest("POST", "/v2/login", QJsonDocument(obj).toJson()), this);
+ auto job = new LocalSnapJob(createRequest("POST", "/v2/login", obj), this);
connect(job, &SnapJob::finished, this, &SnapSocket::includeCredentials);
}
diff --git a/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.h b/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.h
index adfa386..cbe16f3 100644
--- a/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.h
+++ b/libdiscover/backends/SnapBackend/libsnapclient/SnapSocket.h
@@ -26,8 +26,6 @@
#include <QJsonArray>
#include <QLocalSocket>
-class QUrlQuery;
-
class Q_DECL_EXPORT SnapJob : public QObject
{
Q_OBJECT
@@ -95,7 +93,7 @@ Q_SIGNALS:
private:
void includeCredentials(SnapJob* job);
- QByteArray createRequest(const QByteArray &method, const QByteArray &path, const QUrlQuery &content) const;
+ QByteArray createRequest(const QByteArray &method, const QByteArray &path, const QJsonObject &content) const;
QByteArray createRequest(const QByteArray &method, const QByteArray &path, const QByteArray &content = {}) const;
QByteArray m_macaroon;
diff --git a/libdiscover/backends/SnapBackend/libsnapclient/org.kde.discover.libsnapclient.actions b/libdiscover/backends/SnapBackend/libsnapclient/org.kde.discover.libsnapclient.actions
new file mode 100644
index 0000000..9d60717
--- /dev/null
+++ b/libdiscover/backends/SnapBackend/libsnapclient/org.kde.discover.libsnapclient.actions
@@ -0,0 +1,5 @@
+[org.kde.discover.libsnapclient.modify]
+Name=Snappy remote actions
+Description=Modifying the system-wide font configuration requires privileges.
+Policy=auth_self
+Persistence=session