Commit a593e81f authored by Anupam Basak's avatar Anupam Basak

Updated DBus Methods and Implementation

parent cf00cd75
......@@ -19,12 +19,10 @@ find_package(
COMPONENTS
Core
DBus
Network
)
find_package(
KF5 REQUIRED
COMPONENTS
Wallet
libdavclient REQUIRED
)
set(
......@@ -53,7 +51,45 @@ target_link_libraries(
Qt5::Core
Qt5::DBus
KF5::Wallet
Qt5::Network
libdavclient.so
)
install(
TARGETS ${PROJECT_NAME}
DESTINATION bin
)
install(
FILES dbus-org.maui-accounts-dbus-daemon.service
DESTINATION /etc/systemd/system/
)
install(
FILES org.mauikit.accounts.conf
DESTINATION /etc/dbus-1/system.d/
)
### CPack Configurations
include(InstallRequiredSystemLibraries)
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Anupam Basak")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MAUI Accounts DBus Daemon")
set(CPACK_PACKAGE_VENDOR "Anupam Basak")
set(CPACK_PACKAGE_CONTACT "Anupam Basak <anupam.basak27@gmail.com>")
#set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/ReadMe.txt")
#set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_PACKAGE_INSTALL_DIRECTORY "CMake ${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}")
set(CPACK_SOURCE_STRIP_FILES "")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/cpack-package-control-extra-scripts/preinst;${CMAKE_CURRENT_SOURCE_DIR}/cpack-package-control-extra-scripts/postinst;${CMAKE_CURRENT_SOURCE_DIR}/cpack-package-control-extra-scripts/prerm;${CMAKE_CURRENT_SOURCE_DIR}/cpack-package-control-extra-scripts/postrm")
include(CPack)
### END CPack Configurations
feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)
echo "POSTinst $@"
systemctl start dbus-org.maui-accounts-dbus-daemon.service
systemctl enable dbus-org.maui-accounts-dbus-daemon.service
echo "POSTrm $@"
\ No newline at end of file
echo "PREinst $@"
systemctl is-active --quiet dbus-org.maui-accounts-dbus-daemon.service && systemctl stop dbus-org.maui-accounts-dbus-daemon.service
systemctl is-active --quiet dbus-org.maui-accounts-dbus-daemon.service && systemctl disable dbus-org.maui-accounts-dbus-daemon.service
exit 0
\ No newline at end of file
echo "PRErm $@"
systemctl stop dbus-org.maui-accounts-dbus-daemon.service
systemctl disable dbus-org.maui-accounts-dbus-daemon.service
[Unit]
Description=MAUI Accounts DBus Service
[Service]
Type=dbus
BusName=org.mauikit.accounts
ExecStart=/usr/bin/maui-accounts-dbus-daemon
Restart=on-failure
[Install]
WantedBy=multi-user.target
<busconfig>
<!-- Only root user can own the service -->
<policy user="root">
<allow own="org.mauikit.accounts"/>
</policy>
<policy context="default">
<allow send_destination="org.mauikit.accounts"/>
<allow receive_sender="org.mauikit.accounts"/>
</policy>
</busconfig>
#include "RootDBusInterface.hpp"
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QDebug>
#include <QDir>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QNetworkReply>
#include <QStandardPaths>
#include <QTimer>
#include <QUuid>
#include <libdavclient/CardDAV.hpp>
#include <libdavclient/utils/CardDAVReply.hpp>
QString RootDBusInterface::name() { return "org.mauikit.accounts"; }
RootDBusInterface::RootDBusInterface() {
qDebug() << QStandardPaths::writableLocation(
QStandardPaths::StandardLocation::AppDataLocation);
QDir appDataFolder(QStandardPaths::writableLocation(
QStandardPaths::StandardLocation::AppDataLocation));
if (!appDataFolder.exists()) {
......@@ -20,13 +30,6 @@ RootDBusInterface::RootDBusInterface() {
QStandardPaths::writableLocation(
QStandardPaths::StandardLocation::AppDataLocation) +
"/accounts.json";
wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0,
KWallet::Wallet::OpenType::Synchronous);
if (!wallet->hasFolder(WALLET_FOLDER_NAME)) {
wallet->createFolder(WALLET_FOLDER_NAME);
}
wallet->setFolder(WALLET_FOLDER_NAME);
QFile accountJsonFile(accountsJsonFilePath);
......@@ -51,23 +54,38 @@ void RootDBusInterface::writeAccountsJsonObjectToFile() {
accountJsonFile.close();
}
QList<QVariant> RootDBusInterface::getAccountNames() {
qDebug() << "getAccountNames :";
QString RootDBusInterface::getManifestPath(QString appId) {
return "/usr/share/maui-accounts/manifests/" + appId + ".json";
}
QList<QVariant> RootDBusInterface::getAccountIds() {
// TODO : Return a List of Maps containing the account data except passwords
return getAccountIdsByType("");
}
QList<QVariant> RootDBusInterface::getAccountIdsByType(QString type) {
qDebug().noquote() << "* List of account names requested of type `" + type +
"`";
QList<QVariant> list;
QJsonArray accounts = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QList<QString> accountsStringArray;
for (int i = 0; i < accounts.size(); i++) {
QJsonObject account = accounts[i].toObject();
list.append(account[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME].toString());
if (type == "" ||
type == account[JSON_ACCOUNT_ARRAY_FIELD_TYPE].toString()) {
list.append(account[JSON_ACCOUNT_ARRAY_FIELD_ID].toString());
}
}
return list;
}
QMap<QString, QVariant> RootDBusInterface::getAccount(QString id) {
qDebug() << "getAccount :" << id;
// TODO : Create a ENUM for the return value map KEY
qDebug().noquote() << "* Account data requested";
QMap<QString, QVariant> returnVal;
QJsonArray accounts = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
......@@ -76,55 +94,132 @@ QMap<QString, QVariant> RootDBusInterface::getAccount(QString id) {
for (int i = 0; i < accounts.size(); i++) {
QJsonObject account = accounts[i].toObject();
if (account[JSON_ACCOUNT_ARRAY_FIELD_ID] == id) {
QByteArray password;
wallet->readEntry(account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString(),
password);
if (account[JSON_ACCOUNT_ARRAY_FIELD_ID].toString() == id) {
qDebug().noquote()
<< " Found user `" +
account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString() + "`";
returnVal.insert(
"name", account[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME].toString());
returnVal.insert("id", account[JSON_ACCOUNT_ARRAY_FIELD_ID].toString());
returnVal.insert("appId",
account[JSON_ACCOUNT_ARRAY_FIELD_APPID].toString());
returnVal.insert("type",
account[JSON_ACCOUNT_ARRAY_FIELD_TYPE].toString());
returnVal.insert("username",
account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString());
returnVal.insert("password",
QString::fromStdString(password.toStdString()));
returnVal.insert("extras",
account[JSON_ACCOUNT_ARRAY_FIELD_EXTRAS].toString());
returnVal.insert("url", account[JSON_ACCOUNT_ARRAY_FIELD_URL].toString());
}
}
if (returnVal.size() <= 0) {
qDebug().noquote() << " [ERROR] Account not found";
}
return returnVal;
}
QString RootDBusInterface::createAccount(QString name, QString username,
QString password, QString extras) {
qDebug() << "createAccount :" << name << username << password << extras;
QString RootDBusInterface::getAccountPassword(QString secret) {
qDebug().noquote() << "* Account password requested";
if (!accountsJsonObject.contains(JSON_FIELD_ACCOUNTS)) {
accountsJsonObject[JSON_FIELD_ACCOUNTS] = QJsonArray();
}
QString secretHashed =
QCryptographicHash::hash(QByteArray::fromStdString(secret.toStdString()),
QCryptographicHash::Sha256);
QString id = QUuid::createUuid().toString(QUuid::StringFormat::WithoutBraces);
QJsonArray accountsArray = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QJsonArray accounts = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QJsonObject accountObject;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_EXTRAS] = extras;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME] = username;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME] = name;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_ID] = id;
for (int i = 0; i < accounts.size(); i++) {
QJsonObject account = accounts[i].toObject();
wallet->writeEntry(username,
QByteArray::fromStdString(password.toStdString()));
if (account[JSON_ACCOUNT_ARRAY_FIELD_SECRET].toString() == secretHashed) {
qDebug().noquote()
<< " Found user `" +
account[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString() + "`";
accountsArray.append(accountObject);
accountsJsonObject[JSON_FIELD_ACCOUNTS] = accountsArray;
return account[JSON_ACCOUNT_ARRAY_FIELD_PASSWORD].toString();
}
}
writeAccountsJsonObjectToFile();
qDebug().noquote() << " [ERROR] Account not found";
return "";
}
QString RootDBusInterface::createWebDAVAccount(QString appId, QString username,
QString password, QString url) {
qDebug().noquote() << "* Create Request for WebDAV Account `" + username +
"` Received";
return id;
return nullptr;
}
QString RootDBusInterface::createCardDAVAccount(QString appId, QString username,
QString password, QString url) {
qDebug().noquote() << "* Create Request for CardDAV Account `" + username +
"` Received";
QString secret = "";
QEventLoop *qloop = new QEventLoop();
if (QFile(getManifestPath(appId)).exists()) {
CardDAV *m_CardDAV = new CardDAV(url, username, password);
CardDAVReply *reply = m_CardDAV->testConnection();
this->connect(
reply, &CardDAVReply::testConnectionResponse,
[=, &secret](bool isSuccess) {
if (isSuccess) {
qDebug().noquote() << " Success connecting to Server";
if (!accountsJsonObject.contains(JSON_FIELD_ACCOUNTS)) {
accountsJsonObject[JSON_FIELD_ACCOUNTS] = QJsonArray();
}
secret = QUuid::createUuid().toString(
QUuid::StringFormat::WithoutBraces);
QString secretHashed = QCryptographicHash::hash(
QByteArray::fromStdString(secret.toStdString()),
QCryptographicHash::Sha256);
QJsonArray accountsArray =
accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
QJsonObject accountObject;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_ID] =
QUuid::createUuid().toString(
QUuid::StringFormat::WithoutBraces);
accountObject[JSON_ACCOUNT_ARRAY_FIELD_SECRET] = secretHashed;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_APPID] = appId;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_TYPE] = "CARDDAV";
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME] = username;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_PASSWORD] = password;
accountObject[JSON_ACCOUNT_ARRAY_FIELD_URL] = url;
accountsArray.append(accountObject);
accountsJsonObject[JSON_FIELD_ACCOUNTS] = accountsArray;
writeAccountsJsonObjectToFile();
qDebug().noquote() << " Account Created";
QTimer::singleShot(0, qloop, &QEventLoop::quit);
}
});
this->connect(reply, &CardDAVReply::error,
[=](QNetworkReply::NetworkError err) {
qDebug().noquote()
<< " [ERROR] Could not connect to server" << err;
qDebug().noquote() << " [ERROR] Account not created";
QTimer::singleShot(0, qloop, &QEventLoop::quit);
});
qloop->exec();
} else {
qDebug().noquote() << " [ERROR] Invalid `appId`";
}
return secret;
}
bool RootDBusInterface::removeAccount(QString id) {
qDebug() << "removeAccount :" << id;
qDebug().noquote() << "* Remove Account Requested";
bool accountDeleted = false;
QJsonArray accountsArray = accountsJsonObject[JSON_FIELD_ACCOUNTS].toArray();
......@@ -133,12 +228,13 @@ bool RootDBusInterface::removeAccount(QString id) {
QJsonObject accountObject = accountsArray[i].toObject();
if (accountObject[JSON_ACCOUNT_ARRAY_FIELD_ID].toString() == id) {
qDebug() << "Removing account" << id;
qDebug().noquote()
<< " Account Removed with username `" +
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString() +
"`";
accountsArray.removeAt(i);
accountsJsonObject[JSON_FIELD_ACCOUNTS] = accountsArray;
wallet->removeEntry(
accountObject[JSON_ACCOUNT_ARRAY_FIELD_USERNAME].toString());
accountDeleted = true;
break;
......@@ -147,5 +243,9 @@ bool RootDBusInterface::removeAccount(QString id) {
writeAccountsJsonObjectToFile();
if (!accountDeleted) {
qDebug().noquote() << " [ERROR] Account not found";
}
return accountDeleted;
}
......@@ -3,7 +3,6 @@
#include "DBusInterface.hpp"
#include <KWallet>
#include <QJsonObject>
#include <QList>
#include <QString>
......@@ -18,26 +17,31 @@ class RootDBusInterface : public DBusInterface {
RootDBusInterface();
private:
KWallet::Wallet *wallet;
QJsonObject accountsJsonObject;
const QString WALLET_FOLDER_NAME = "org.mauikit.accounts";
const QString WALLET_ENTRY_ACCOUNTS = "accounts";
const QString JSON_FIELD_ACCOUNTS = "accounts";
const QString JSON_ACCOUNT_ARRAY_FIELD_EXTRAS = "extras";
const QString JSON_ACCOUNT_ARRAY_FIELD_USERNAME = "username";
const QString JSON_ACCOUNT_ARRAY_FIELD_ACCOUNTNAME = "account_name";
const QString JSON_ACCOUNT_ARRAY_FIELD_ID = "_id";
const QString JSON_ACCOUNT_ARRAY_FIELD_SECRET = "secret";
const QString JSON_ACCOUNT_ARRAY_FIELD_APPID = "appId";
const QString JSON_ACCOUNT_ARRAY_FIELD_TYPE = "type";
const QString JSON_ACCOUNT_ARRAY_FIELD_USERNAME = "username";
const QString JSON_ACCOUNT_ARRAY_FIELD_PASSWORD = "password";
const QString JSON_ACCOUNT_ARRAY_FIELD_URL = "url";
QString accountsJsonFilePath;
void writeAccountsJsonObjectToFile();
QString getManifestPath(QString appId);
public slots:
Q_SCRIPTABLE QList<QVariant> getAccountNames();
Q_SCRIPTABLE QList<QVariant> getAccountIds();
Q_SCRIPTABLE QList<QVariant> getAccountIdsByType(QString type);
Q_SCRIPTABLE QMap<QString, QVariant> getAccount(QString id);
Q_SCRIPTABLE QString createAccount(QString name, QString username,
QString password, QString extras);
Q_SCRIPTABLE QString getAccountPassword(QString secret);
Q_SCRIPTABLE QString createWebDAVAccount(QString appId, QString username,
QString password, QString url);
Q_SCRIPTABLE QString createCardDAVAccount(QString appId, QString username,
QString password, QString url);
Q_SCRIPTABLE bool removeAccount(QString id);
};
......
......@@ -10,23 +10,20 @@
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
fprintf(stderr,
"Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
if (!QDBusConnection::systemBus().isConnected()) {
fprintf(stderr, "Cannot connect to the D-Bus system bus.\n");
return 1;
}
if (!QDBusConnection::sessionBus().registerService(SERVICE_NAME)) {
if (!QDBusConnection::systemBus().registerService(SERVICE_NAME)) {
fprintf(stderr, "%s\n",
qPrintable(QDBusConnection::sessionBus().lastError().message()));
qPrintable(QDBusConnection::systemBus().lastError().message()));
exit(1);
}
RootDBusInterface *interface = new RootDBusInterface();
QDBusConnection::sessionBus().registerObject(
"/", interface->name(), interface, QDBusConnection::ExportAllSlots);
QDBusConnection::systemBus().registerObject("/", interface->name(), interface,
QDBusConnection::ExportAllSlots);
return app.exec();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment