diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,8 +41,10 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) + find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/") +find_package(PolkitQt5-1 REQUIRED) include(KDEInstallDirs) include(KDECMakeSettings) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,6 +43,7 @@ Qt5::Core PRIVATE ${BLKID_LIBRARIES} + ${POLKITQT-1_LIBRARIES} Qt5::DBus Qt5::Gui qca-qt5 diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -19,6 +19,7 @@ set(UTIL_SRC ${HelperInterface_SRCS} util/capacity.cpp + util/externalcommand_authagent.cpp util/externalcommand.cpp util/globallog.cpp util/helpers.cpp @@ -29,6 +30,7 @@ set(UTIL_LIB_HDRS util/libpartitionmanagerexport.h util/capacity.h + util/externalcommand_authagent.h util/externalcommand.h util/globallog.h util/helpers.h @@ -43,6 +45,7 @@ target_link_libraries(kpmcore_externalcommand qca-qt5 + ${POLKITQT-1_LIBRARIES} Qt5::Core Qt5::DBus KF5::AuthCore diff --git a/src/util/externalcommand_authagent.h b/src/util/externalcommand_authagent.h new file mode 100644 --- /dev/null +++ b/src/util/externalcommand_authagent.h @@ -0,0 +1,73 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 3 of * + * the License, or (at your option) any later version. * + * * + * 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 .* + *************************************************************************/ + +#ifndef EXTERNALCOMMAND_AUTHAGENT_H +#define EXTERNALCOMMAND_AUTHAGENT_H + +#include + +#include +#include +#include +#include + +using namespace PolkitQt1; + +/** An Authentication agent/listener class. + + This class implements the Listener class + of PolkitQt1. It is used to spawn a fresh + authentication agent which can be used to + authenticate privileged actions. + + @author Shubham +*/ +class Listener : public Agent::Listener +{ + Q_OBJECT + Q_DISABLE_COPY(Listener) +public: + Listener(QObject *parent = nullptr); + ~Listener() override; + +public Q_SLOTS: + bool registerListener(const Subject &subject, const QString &locale, const QString &objectPath); + bool unregisterListener(const Subject &subject, const QString &objectPath); + + void initiateAuthentication(const QString &actionId, + const QString &message, + const QString &iconName, + const PolkitQt1::Details &details, + const QString &cookie, + const Identity::List &identities, + Agent::AsyncResult *result) override; + //bool initiateAuthenticationFinish(const Subject &subject, const QString &objectPath) override; + void cancelAuthentication() override; + + void setResponse(const QString &response); + void request(const QString &request, bool echo); + void completed(bool gainedAuthorization); + void showError(const QString& text) const; + void showInfo(const QString& text) const; + +private: + Agent::Session *m_authenticationSession; + bool m_agentRegistered; +}; + +#endif // EXTERNALCOMMAND_AUTHAGENT_H + diff --git a/src/util/externalcommand_authagent.cpp b/src/util/externalcommand_authagent.cpp new file mode 100644 --- /dev/null +++ b/src/util/externalcommand_authagent.cpp @@ -0,0 +1,132 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 3 of * + * the License, or (at your option) any later version. * + * * + * 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 .* + *************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "util/externalcommand_authagent.h" + +using namespace PolkitQt1; + +Listener::Listener(QObject *parent) + : Agent::Listener(parent), + m_agentRegistered(false) +{ + qDebug() << "Registration of Authentication started"; +} + +Listener::~Listener() +{ + delete m_authenticationSession; +} + +bool Listener::registerListener(const Subject &subject, const QString &locale, const QString &objectPath) +{ + Authority::instance()->registerAuthenticationAgent(subject, locale, objectPath); + m_agentRegistered = true; + return true; +} + +bool Listener::unregisterListener(const Subject &subject, const QString &objectPath) +{ + qDebug() << "Unregistering Agent Listener"; + Authority::instance()->unregisterAuthenticationAgent(subject, objectPath); + m_agentRegistered = false; + return true; +} + +void Listener::initiateAuthentication(const QString &actionId, + const QString &message, + const QString &iconName, + const Details &details, + const QString &cookie, + const Identity::List &identities, + Agent::AsyncResult *result) +{ + qDebug() << "initiateAuthentication for " << actionId << " with message " << message; + qDebug() << "iconName " << iconName; + qDebug() << details.keys(); + qDebug() << "cookie" << cookie; + + if (m_agentRegistered) { + for (const Identity &identity : identities) { + qDebug() << identity.toString(); + m_authenticationSession = new Agent::Session(identity, cookie, result); + + connect(m_authenticationSession, &Agent::Session::request, this, &Listener::request); + connect(m_authenticationSession, &Agent::Session::completed, this, &Listener::completed); + connect(m_authenticationSession, &Agent::Session::showError, this, &Listener::showError); + connect(m_authenticationSession, &Agent::Session::showInfo, this, &Listener::showInfo); + + // Using PolkitQt1::Session class member functions/API to initiate the authentication process + m_authenticationSession->initiate(); + } + } +} + +/*bool Listener::initiateAuthenticationFinish(const Subject &subject, const QString &objectPath) +{ + qDebug() << "Initializing Authentication finish"; + Authority::instance()->unregisterAuthenticationAgent(subject, objectPath); + return true; +}*/ + +void Listener::cancelAuthentication() +{ + qDebug() << "Cancelling the Authentication session"; + m_authenticationSession->cancel(); +} + +void Listener::setResponse(const QString &response) +{ + + m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->setResponse(response); +} + +void Listener::request(const QString &request, bool echo) +{ + Q_UNUSED(echo) + + qDebug() << "Request: " << request; + + m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->setResponse(request); +} + +void Listener::completed(bool gainedAuthorization) +{ + qDebug() << "Action completed: " << gainedAuthorization; + + m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->result()->setCompleted(); +} + +void Listener::showError(const QString& text) const +{ + qDebug() << "Error: " << text; +} + +void Listener::showInfo(const QString& text) const +{ + qDebug() << "Information: " << text; +}