1
0
mirror of https://github.com/QRouland/UTPass.git synced 2025-07-01 09:42:29 +00:00

20 Commits

Author SHA1 Message Date
0cf07b1b7a Bump version 0.0.3 2025-02-04 17:51:14 +01:00
6b5d9fb8e2 Fix #2 Password names containing periods are truncated 2025-02-04 17:44:42 +01:00
c106bbec19 Fix main page not reloading on imports 2025-02-04 17:27:01 +01:00
bcc6d7c316 Update Readme 2025-02-04 17:25:37 +01:00
fc02000b89 Fix decrypt issue 2025-02-04 16:36:00 +01:00
72a3a8fbcc Fix password page color issues with dark theme 2025-02-04 16:17:28 +01:00
efb57dd70c Fix ui issues on imports 2025-02-04 15:51:47 +01:00
bef910bce3 Update readme for export with new git functionnality 2025-02-04 14:25:31 +01:00
bf7c2091b7 Fix bug causing crashes during git clone when no username is provided 2025-02-04 14:09:02 +01:00
5582b4dd70 Ensure that key and zip are deleted after import 2025-02-04 13:37:25 +01:00
ba52ddac5c Complete rewrite from gpgme to rnp 2025-02-03 21:46:21 +01:00
e56c16f27b Fix rnp passphrase provider version 2025-02-03 20:06:15 +01:00
93361f9ba5 Fix build rnp for arm64 2025-02-03 17:48:30 +01:00
b9b038b1ae Rewrite get all key with rnp 2025-02-01 14:12:18 +01:00
74a001eefc WIP Rewrite get all key with rnp 2025-01-30 22:46:46 +01:00
4bec3dcbc9 First draft Rewrite get all key with rnp 2025-01-30 16:25:29 +01:00
2c9d82e0b1 Rewrite import key with rnp 2025-01-29 16:42:37 +01:00
c01fae0c58 Add rnp lib 2025-01-28 12:45:31 +01:00
630d707190 Some ui improvements 2025-01-28 10:17:57 +01:00
7a2b12419d Refactor unzip password store 2025-01-20 15:58:34 +01:00
73 changed files with 2133 additions and 1135 deletions

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "libs/rnp"]
path = libs/rnp
url = https://github.com/rnpgp/rnp
[submodule "libs/botan"]
path = libs/botan
url = https://github.com/randombit/botan

24
CHANGELOG.md Normal file
View File

@ -0,0 +1,24 @@
# 0.0.3: Git Initial Support and Move to RNP
- Port app to Focal
- Improve UI :
- Follow human interface guidelines
- Fix various components color to work with black theme
- Rewrite of Pass Plugin:
- Move from GPGMe to RNP for GPG operations due to issues running GPG agent in a confined app
- Improve multithreading code for GPG operations
- Add Git HTTP and HTTP AUTH clone for password store import feature
- Add delete password store feature
# 0.0.2 : Added translations
- Added French by Anne17 and Reda
- Added Catalan by Joan CiberSheep
- Added Spanish by Advocatux and Reda
Thanks to all the translators !
# 0.0.1 : Initial Release
- Import of gpg keys via file
- Suppression of gpg keys
- Import of password via a password store zip
- Password decryption
- Password copy to clipboard

View File

@ -39,9 +39,13 @@ if(NOT TESTS_PATH)
set(TESTS_PATH "./tests")
endif()
configure_file(main.in.cpp main.cpp)
if(TESTS_RUNNER)
configure_file(tests.in.cpp tests.cpp)
add_executable(${PROJECT_NAME} tests.cpp)
else()
add_executable(${PROJECT_NAME} main.cpp)
endif()
qt5_use_modules(${PROJECT_NAME} Gui Qml Quick QuickTest)
if(TESTS_RUNNER)

View File

@ -20,10 +20,13 @@ Assuming that there are already passwords in another device using [ZX2C4s pas
Export gpg private keys in order to decrypt passwords:
```
gpg --output keys.gpg --export-secret-keys
gpg --output keys.gpg --export-secret-keys <key>
```
Export passwords, assuming they reside in *.password-store* folder:
If your password store is already hosted in a Git repository that provides HTTP or HTTP with authentication for cloning (we're working to have support for SSH soon), you can clone your password store directly from the app.
Otherwise, follow these steps to export it to a ZIP file for importing.
Export passwords to a ZIP archive, assuming they reside in the *.password-store* folder:
```
zip passwords.zip -r .password-store/
```
@ -44,11 +47,9 @@ See [Contributing wiki page](https://github.com/QRouland/UTPass/wiki/Contributin
Some useful links related to UTPass development :
* [Ubports](https://ubports.com/) : Ubuntu Touch Community
* [ZX2C4s pass command line application](https://www.passwordstore.org/) : the standard unix password manager.
* [ZX2C4s pass command line application](https://www.passwordstore.org/) : The standard unix password manager.
* [Clickable](https://github.com/bhdouglass/clickable) : Compile, build, and deploy Ubuntu Touch click packages
* [GnuPG](https://gnupg.org/): The GNU Privacy Guard
* [Gpgme](https://www.gnupg.org/software/gpgme/index.html) : GnuPG Made Easy (GPGME) is a library designed to make access to GnuPG easier for applications
* [Rnp](https://github.com/rnpgp/rnp) : High performance C++ OpenPGP library used by Mozilla Thunderbird
## License

View File

@ -7,28 +7,38 @@ scripts:
echo 'Running Astyle :' && astyle --options=.astylerc --recursive '*.cpp,*.h' --exclude=build && echo 'Running QmlFormat' && find . -name "*.qml" -exec qmlformat -i {} \; && echo 'Success'
dependencies_target:
- libgpgmepp-dev
- libgpgme-dev
- libgit2-dev
- libquazip5-dev
- gpg
- libjson-c-dev
libraries:
botan:
builder: custom
make_jobs: 2
dependencies_host:
- python
prebuild:
- $SRC_DIR/configure.py --cpu $ARCH --prefix $INSTALL_DIR --with-build-dir $BUILD_DIR
build:
- make
- make install
rnp:
builder: cmake
make_jobs: 2
dependencies_target:
- libbz2-dev
- zlib1g-dev
- libjson-c-dev
build_args: -DBUILD_TESTING=off -DCRYPTO_BACKEND=botan
install_lib:
- "libgpg-error.so.0.28.0"
- "libassuan.so*"
- "libgpgme.so*"
- "libgpgmepp.so*"
- "libqgpgme.so*"
- "libgit2.so*"
- "libquazip5.so*"
- "libmbedtls.so*"
- "libmbedx509.so*"
- "libmbedcrypto.so*"
- "libhttp_parser.so*"
- "libssh2.so*"
- "libquazip5.so*"
install_bin:
- "gpg"

127
cmake/FindJSON-C.cmake Normal file
View File

@ -0,0 +1,127 @@
# Copyright (c) 2018, 2024 Ribose Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#.rst:
# FindJSON-C
# -----------
#
# Find the json-c library.
#
# IMPORTED Targets
# ^^^^^^^^^^^^^^^^
#
# This module defines :prop_tgt:`IMPORTED` targets:
#
# ``JSON-C::JSON-C``
# The json-c library, if found.
#
# Result variables
# ^^^^^^^^^^^^^^^^
#
# This module defines the following variables:
#
# ::
#
# JSON-C_FOUND - true if the headers and library were found
# JSON-C_INCLUDE_DIRS - where to find headers
# JSON-C_LIBRARIES - list of libraries to link
# JSON-C_VERSION - library version that was found, if any
# use pkg-config to get the directories and then use these values
# in the find_path() and find_library() calls
find_package(PkgConfig)
pkg_check_modules(PC_JSON-C QUIET json-c)
# RHEL-based systems may have json-c12
if (NOT PC_JSON-C_FOUND)
pkg_check_modules(PC_JSON-C QUIET json-c12)
endif()
# ..or even json-c13, accompanied by non-develop json-c (RHEL 8 ubi)
if (NOT PC_JSON-C_FOUND)
pkg_check_modules(PC_JSON-C QUIET json-c13)
endif()
# find the headers
find_path(JSON-C_INCLUDE_DIR
NAMES json_c_version.h
HINTS
${PC_JSON-C_INCLUDEDIR}
${PC_JSON-C_INCLUDE_DIRS}
PATH_SUFFIXES json-c json-c12 json-c13
)
# find the library
find_library(JSON-C_LIBRARY
NAMES json-c libjson-c json-c12 libjson-c12 json-c13 libjson-c13
HINTS
${PC_JSON-C_LIBDIR}
${PC_JSON-C_LIBRARY_DIRS}
)
# determine the version
if(PC_JSON-C_VERSION)
set(JSON-C_VERSION ${PC_JSON-C_VERSION})
elseif(JSON-C_INCLUDE_DIR AND EXISTS "${JSON-C_INCLUDE_DIR}/json_c_version.h")
file(STRINGS "${JSON-C_INCLUDE_DIR}/json_c_version.h" _json-c_version_h
REGEX "^#define[\t ]+JSON_C_VERSION[\t ]+\"[^\"]*\"$")
string(REGEX REPLACE ".*#define[\t ]+JSON_C_VERSION[\t ]+\"([^\"]*)\".*"
"\\1" _json-c_version_str "${_json-c_version_h}")
set(JSON-C_VERSION "${_json-c_version_str}"
CACHE INTERNAL "The version of json-c which was detected")
endif()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(JSON-C
REQUIRED_VARS JSON-C_LIBRARY JSON-C_INCLUDE_DIR JSON-C_VERSION
VERSION_VAR JSON-C_VERSION
)
if (JSON-C_FOUND)
set(JSON-C_INCLUDE_DIRS ${JSON-C_INCLUDE_DIR} ${PC_JSON-C_INCLUDE_DIRS})
set(JSON-C_LIBRARIES ${JSON-C_LIBRARY})
endif()
if (JSON-C_FOUND AND NOT TARGET JSON-C::JSON-C)
# create the new library target
add_library(JSON-C::JSON-C UNKNOWN IMPORTED)
# set the required include dirs for the target
if (JSON-C_INCLUDE_DIRS)
set_target_properties(JSON-C::JSON-C
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${JSON-C_INCLUDE_DIRS}"
)
endif()
# set the required libraries for the target
if (EXISTS "${JSON-C_LIBRARY}")
set_target_properties(JSON-C::JSON-C
PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${JSON-C_LIBRARY}"
)
endif()
endif()
mark_as_advanced(JSON-C_INCLUDE_DIR JSON-C_LIBRARY)

1
libs/botan Submodule

Submodule libs/botan added at 935055e839

1
libs/rnp Submodule

Submodule libs/rnp added at 2e249423d6

View File

@ -6,10 +6,6 @@
#include <QtQml>
#ifdef TEST_RUNNER
#include <QtQuickTest/quicktest.h>
#endif
int main(int argc, char *argv[])
{
qDebug() << "Starting app from main.cpp";
@ -17,7 +13,6 @@ int main(int argc, char *argv[])
QGuiApplication::setApplicationName("utpass.qrouland");
#ifndef TEST_RUNNER
auto *view = new QQuickView();
view->setSource(QUrl(QStringLiteral("qml/Main.qml")));
view->setResizeMode(QQuickView::SizeRootObjectToView);
@ -28,7 +23,4 @@ int main(int argc, char *argv[])
Q_ARG(QVariant, QVariant::fromValue(mainView))
);
return QGuiApplication::exec();
#else
return quick_test_main(argc, argv, "@TESTS_PATH@", "@TESTS_PATH@");
#endif
}

View File

@ -10,7 +10,7 @@
"content-hub": "UTPass.contenthub"
}
},
"version": "0.0.3-dev",
"version": "0.0.3",
"maintainer": "Quentin Rouland <quentin@qrouland.com>",
"framework" : "ubuntu-sdk-20.04"
}

View File

@ -22,13 +22,13 @@ CloneJob::CloneJob(QString url, QString path, cred_type cred):
void CloneJob::run()
{
auto tmp_dir = this->cloneSetup();
auto err = this->clone(this->m_url, tmp_dir.absolutePath(), this->m_cred, this->credentialsCB);
if (!err) {
auto ret = this->clone(this->m_url, tmp_dir.absolutePath(), this->m_cred, this->credentialsCB);
if (ret) {
this->moveToDestination(tmp_dir, this->m_path);
}
this->cloneTearDown(tmp_dir);
this->cloneCleanUp(tmp_dir);
emit resultReady(err); // TODO Clean error handling to return specifics errors for the ui
emit resultReady(!ret); // TODO Clean error handling to return specifics errors for the ui
}
@ -37,26 +37,26 @@ QDir CloneJob::cloneSetup()
QDir tmp_dir(QStandardPaths::writableLocation( QStandardPaths::CacheLocation).append("/clone"));
tmp_dir.removeRecursively();
qDebug() << "Temp dir path is " << tmp_dir.absolutePath();
qDebug() << "[CloneJob]Temp dir path is " << tmp_dir.absolutePath();
return tmp_dir;
}
bool CloneJob::cloneTearDown(QDir tmp_dir)
bool CloneJob::cloneCleanUp(QDir tmp_dir)
{
return tmp_dir.removeRecursively();
}
bool CloneJob::moveToDestination(QDir tmp_dir, QString path)
{
qDebug() << "Removing password_store " << path;
qDebug() << "[CloneJob] Removing password_store " << path;
QDir destination_dir(path);
destination_dir.removeRecursively();
qDebug() << "Moving cloned content to destination dir";
qDebug() << "[CloneJob] Moving cloned content to destination dir";
QDir dir;
qDebug() << tmp_dir.absolutePath() << " to " << destination_dir.absolutePath();
qDebug() << "[CloneJob]" << tmp_dir.absolutePath() << " to " << destination_dir.absolutePath();
return dir.rename(tmp_dir.absolutePath(), destination_dir.absolutePath()); // TODO Better error handling
}
@ -69,12 +69,17 @@ bool CloneJob::clone(QString url, QString path, cred_type cred, git_cred_acquire
opts.fetch_opts.callbacks.payload = &cred;
int ret = git_clone(&repo, url.toLocal8Bit().data(), path.toLocal8Bit().data(), &opts);
if (ret != 0) {
qDebug() << git_error_last()->message;
if (ret == GIT_EUSER ) {
qDebug() << "[CloneJob] CallBack Error";
} else if (ret != 0) {
auto err = git_error_last(); // TODO Better error handling for return ui messages
if (err) {
qDebug() << "[CloneJob]" << git_error_last()->message;
}
}
if (repo) {
git_repository_free(repo);
}// TODO Better error handling
return ret != 0;
}
return ret == 0;
}

View File

@ -70,7 +70,7 @@ private:
* @param tmp_dir The temporary directory to tear down.
* @return `true` if the teardown was successful, `false` otherwise.
*/
static bool cloneTearDown(QDir tmp_dir);
static bool cloneCleanUp(QDir tmp_dir);
/**
* @brief Clones a repository from a specified URL.

View File

@ -25,27 +25,27 @@ int GitJob::credentialsCB(git_cred **out, const char *url, const char *username_
auto v = overload {
[](const HTTP & x)
{
qDebug() << "credentialsCB : HTTP ";
qWarning() << "credentialsCB : callback should never be call for HTTP ";
qDebug() << "[GitJob] credentialsCB : HTTP ";
qWarning() << "[GitJob] credentialsCB : callback should never be call for HTTP ";
return (int) GIT_EUSER;
},
[&out, &username_from_url](const HTTPUserPass & x)
{
qDebug() << "credentialsCB : HTTPUserPass ";
qDebug() << "[GitJob] credentialsCB : HTTPUserPass ";
if (!username_from_url) {
qWarning() << "credentials_cb : no username provided ";
qWarning() << "[GitJob] credentials_cb : no username provided ";
return (int) GIT_EUSER;
}
return git_cred_userpass_plaintext_new(out, username_from_url, x.pass.toLocal8Bit().constData());
},
[](const SSHPass & x)
{
qWarning() << "credentials_cb : SSHAuth to be implemented ";
qWarning() << "[GitJob] credentials_cb : SSHAuth to be implemented ";
return (int) GIT_EUSER;
}, // TODO
[](const SSHKey & x)
{
qWarning() << "credentials_cb : SSHKey to be implemented ";
qWarning() << "[GitJob] credentials_cb : SSHKey to be implemented ";
return (int) GIT_EUSER;
} // TODO
};

View File

@ -1,7 +1,7 @@
#ifndef GITJOB_H
#define GITJOB_H
#include "qthread.h"
#include <QThread>
extern "C" {
#include <git2.h>
}

View File

@ -5,10 +5,14 @@ set(
SRC
plugin.cpp
pass.cpp
gpg.cpp
passkeymodel.h
passkeyringmodel.h
passphraseprovider.h
jobs/decryptjob.cpp
jobs/deletekeyjob.cpp
jobs/getkeysjob.cpp
jobs/importkeyjob.cpp
jobs/rmjob.cpp
jobs/rnpjob.cpp
)
set(CMAKE_AUTOMOC ON)
@ -28,24 +32,23 @@ set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
add_library(gpgerror SHARED IMPORTED)
set_property(TARGET gpgerror PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpg-error.so.0.28.0")
set(RNP_INSTALL_DIR "${CMAKE_SOURCE_DIR}/build/${ARCH_TRIPLET}/rnp/install")
set(BOTAN_INSTALL_DIR "${CMAKE_SOURCE_DIR}/build/${ARCH_TRIPLET}/botan/install")
add_library(libassuan SHARED IMPORTED)
set_property(TARGET libassuan PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libassuan.so")
find_package(JSON-C 0.11)
add_library(libgpgme SHARED IMPORTED)
set_property(TARGET libgpgme PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpgme.so")
INCLUDE_DIRECTORIES(${RNP_INSTALL_DIR}/include)
add_library(libgpgmepp SHARED IMPORTED)
set_property(TARGET libgpgmepp PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpgmepp.so")
add_library(rnp STATIC IMPORTED)
set_property(TARGET rnp PROPERTY IMPORTED_LOCATION "${RNP_INSTALL_DIR}/lib/librnp.a")
add_library(libqgpgme SHARED IMPORTED)
set_property(TARGET libqgpgme PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libqgpgme.so")
add_library(sexpp STATIC IMPORTED)
set_property(TARGET sexpp PROPERTY IMPORTED_LOCATION "${RNP_INSTALL_DIR}/lib/libsexpp.a")
add_library(botan STATIC IMPORTED)
set_property(TARGET botan PROPERTY IMPORTED_LOCATION "${BOTAN_INSTALL_DIR}/lib/libbotan-2.a")
target_link_libraries(${PLUGIN} gpgerror libassuan libgpgme libgpgmepp libqgpgme)
target_link_libraries(${PLUGIN} rnp sexpp botan JSON-C::JSON-C)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")
install(TARGETS ${PLUGIN} DESTINATION ${QT_IMPORTS_DIR}/${PLUGIN}/)

View File

@ -1,301 +0,0 @@
#include <memory>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QtCore/QStandardPaths>
#include <QProcess>
#include <gpgme.h>
#include <gpgme++/data.h>
#include <gpgme++/global.h>
#include <gpgme++/context.h>
#include <gpgme++/engineinfo.h>
#include <gpgme++/keylistresult.h>
#include <gpgme++/importresult.h>
#include <gpgme++/encryptionresult.h>
#include <gpgme++/decryptionresult.h>
#include <qgpgme/importjob.h>
#include <qgpgme/deletejob.h>
#include <qgpgme/decryptjob.h>
#include <qgpgme/encryptjob.h>
#include <qgpgme/protocol.h>
#include <qgpgme/keylistjob.h>
#include <qgpgme/changeownertrustjob.h>
#include "gpg.h"
#include "passphraseprovider.h"
using namespace GpgME;
using namespace QGpgME;
Gpg::Gpg(QObject* windows)
{
this->m_passphrase_provider = new UTPassphraseProvider(windows);
Gpg::initGpgConfig();
auto error = checkEngine(OpenPGP);
if (error) {
qDebug() << "Code Error : " << error.code();
qDebug() << "Error str : " << error.asString();
qFatal("GNUPG Engine check Fail");
}
qDebug() << "GNUPG Engine Version is :" << engineInfo(OpenPGP).version();
qDebug() << "GNUPG Executable is :" << engineInfo(OpenPGP).fileName();
qDebug() << "GNUPG Home is :" << engineInfo(OpenPGP).homeDirectory();
}
Gpg::~Gpg()
{
delete this->m_passphrase_provider;
}
QString Gpg::initGpgHome()
{
QString path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.gpghome");
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(".");
}
return path;
}
QString Gpg::findCommandPath(const QString &command)
{
// Retrieve the PATH environment variable
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString pathEnv = env.value("PATH");
// Split the PATH by colon
QStringList pathDirs = pathEnv.split(":", QString::SkipEmptyParts);
// Check each directory in the PATH
foreach (const QString &dir, pathDirs) {
QFileInfo fileInfo(QDir(dir).filePath(command));
// If the file exists and is executable, return the path
if (fileInfo.exists() && fileInfo.isExecutable()) {
return fileInfo.absoluteFilePath();
}
}
return QString::null;
}
QString Gpg::initGpgExec()
{
QString path = findCommandPath("gpg");
if (path.isNull()) {
qFatal("No valid gpg exec found !");
}
return path;
}
void Gpg::initGpgConfig()
{
initializeLibrary();
gpgme_set_global_flag("disable-gpgconf", "1");
QString home = initGpgHome();
qDebug() << "Gpg home is " << home;
QString exec = initGpgExec();
qDebug() << "Gpg exec is " << exec;
QFile agentConf(home + QStringLiteral("/gpg-agent.conf"));
agentConf.remove();
agentConf.open(QIODevice::WriteOnly);
agentConf.write("allow-loopback-pinentry\n");
agentConf.close();
auto err = gpgme_set_engine_info (
GPGME_PROTOCOL_OpenPGP,
exec.toLocal8Bit().data(),
home.toLocal8Bit().data()
);
if (err != GPG_ERR_NO_ERROR) {
qDebug() << "Error code : " << err;
qDebug() << "Error str : " << gpg_strerror(err);
qFatal("GPGME set engine info failed !");
}
}
Error Gpg::decrypt(QByteArray cipher_text)
{
auto job = openpgp()->decryptJob();
auto ctx = DecryptJob::context(job);
ctx->setPassphraseProvider(this->m_passphrase_provider);
ctx->setPinentryMode(Context::PinentryLoopback);
QObject::connect(job, &DecryptJob::result,
this, &Gpg::decryptResultSlot);
return job->start(cipher_text);
}
Error Gpg::decryptFromFile(QString path)
{
qDebug() << "Decrypt from " << path;
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open the File";
return Error();
}
QByteArray cipherText = file.readAll();
file.close();
return decrypt(cipherText);
}
void Gpg::decryptResultSlot(const GpgME::DecryptionResult &result, const QByteArray &plainText,
const QString &auditLogAsHtml, const GpgME::Error &auditLogError)
{
if (result.error()) {
qWarning() << "Something gone wrong on decrypt";
qDebug() << "Code Error : " << result.error().code();
qDebug() << "Error str : " << result.error().asString();
}
qDebug() << "Cancelled : " << result.error().isCanceled();
emit decryptResult(result.error(), QString::fromUtf8(plainText));
}
// QPair<Error, QByteArray> Gpg::encrypt(QString str, QString uid, bool ascii_armor, bool text_mode)
// {
// qDebug() << "Encrypt to QByteArray";
// auto keys = getKeys(uid);
// if (keys.first) {
// return QPair<Error, QByteArray>(keys.first, QByteArray());
// }
// auto job = std::unique_ptr<EncryptJob>(openpgp()->encryptJob(ascii_armor, text_mode));
// QByteArray cipherText;
// auto result = job->exec(keys.second, str.toUtf8(), Context::AlwaysTrust, cipherText);
// qDebug() << "Encrypted to QByteArray";
// return QPair<Error, QByteArray>(result.error(), cipherText);
// }
// Error Gpg::encryptToFile(QString str, QString path, QString uid, bool ascii_armor,
// bool text_mode)
// {
// qDebug() << "Encrypting to file " << path;
// QFile file(path);
// if (!file.open(QIODevice::WriteOnly)) {
// qWarning() << "Can't open the file to write it" ;
// return Error();
// }
// auto encrypt_ret = encrypt(str, uid, ascii_armor, text_mode);
// if (encrypt_ret.first) {
// file.write(encrypt_ret.second);
// }
// qDebug() << "Encrypting to file " << path;
// return encrypt_ret.first;
// }
Error Gpg::getAllKeys ( bool remote, const bool include_sigs,
bool validate )
{
return getKeys(QString(""), remote, include_sigs, validate);
}
Error Gpg::getKeys(QString pattern_uid, bool remote, bool include_sigs,
bool validate)
{
qDebug() << "Getting the keys " << pattern_uid;
auto job = openpgp()->keyListJob(remote, include_sigs, validate);
QObject::connect(job, &KeyListJob::result,
this, &Gpg::getKeysJobResultSlot);
return job->start(QStringList() << pattern_uid, false);
}
void Gpg::getKeysJobResultSlot(const GpgME::KeyListResult &result, const std::vector<GpgME::Key> &keys,
const QString &auditLogAsHtml, const GpgME::Error &auditLogError)
{
if (result.error()) {
qWarning() << "Something gone wrong on getKeys";
qDebug() << "Code Error : " << result.error().code();
qDebug() << "Error str : " << result.error().asString();
}
emit getKeysResult(result.error(), keys);
}
Error Gpg::importKeysFromFile(QString path)
{
qDebug() << "Importing the key file" << path;
qDebug() << "Decrypt from " << path;
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open the File";
return Error();
}
auto data = file.readAll();
file.close();
auto job = openpgp()->importJob();
QObject::connect(job, &ImportJob::result,
this, &Gpg::importKeysFromFileSlot);
return job->start(data);
}
void Gpg::importKeysFromFileSlot(const GpgME::ImportResult &result, const QString &auditLogAsHtml,
const GpgME::Error &auditLogError)
{
qDebug() << "numImported" << result.numImported();
qDebug() << "numSecretKeysImported" << result.numSecretKeysImported();
qDebug() << "numSecretKeysConsidered" << result.numSecretKeysConsidered();
qDebug() << "numSecretKeysUnchanged" << result.numSecretKeysUnchanged();
qDebug() << "numUnchanged" << result.numUnchanged();
if (result.error()) {
qWarning() << "Something gone wrong on decrypt";
qDebug() << "Code Error : " << result.error().code();
qDebug() << "Error str : " << result.error().asString();
}
emit importKeysFromFileResult(result.error());
}
Error Gpg::deleteKey(const Key key)
{
auto job = openpgp()->deleteJob();
QObject::connect(job, &DeleteJob::result,
this, &Gpg::deleteKeySlot);
return job->start(key, true);
}
void Gpg::deleteKeySlot(const GpgME::Error &error, const QString &auditLogAsHtml, const GpgME::Error &auditLogError)
{
if (error) {
qWarning() << "Something gone wrong on deleteKey";
qDebug() << "Code Error : " << error.code();
qDebug() << "Error str : " << error.asString();
}
emit deleteKeyResult(error);
}

View File

@ -1,227 +0,0 @@
#ifndef GPG_H
#define GPG_H
#include "passkeymodel.h"
#include "passphraseprovider.h"
#include <memory>
#include <QQuickWindow>
#include <gpgme++/context.h>
#include <qgpgme/changeownertrustjob.h>
#include <QSemaphore>
#include <gpgme.h>
#include <qgpgme/importjob.h>
#include <qgpgme/deletejob.h>
#include <qgpgme/decryptjob.h>
#include <qgpgme/encryptjob.h>
#include <qgpgme/protocol.h>
#include <qgpgme/keylistjob.h>
#include <qgpgme/changeownertrustjob.h>
using namespace GpgME;
using namespace QGpgME;
/**
* @class Gpg
* @brief A class for managing GPG key operations such as key import, decryption, and deletion.
*
* This class integrates with the GPGME (GnuPG Made Easy) library to provide functionalities
* for interacting with GPG keys, including decrypting messages, importing keys from files,
* listing keys, and deleting keys.
*/
class Gpg: public QObject
{
Q_OBJECT
Q_PROPERTY(UTPassphraseProvider* passphrase_provider READ passphrase_provider MEMBER m_passphrase_provider )
private slots:
/**
* @brief Slot to handle the result of a decryption operation.
* @param result The result of the decryption operation.
* @param plain_text The decrypted text.
* @param auditLogAsHtml The HTML formatted audit log for the operation.
* @param auditLogError The error associated with the audit log, if any.
*/
void decryptResultSlot(
const DecryptionResult &result,
const QByteArray &plain_text,
const QString &auditLogAsHtml,
const Error &auditLogError
);
/**
* @brief Slot to handle the result of a key retrieval operation.
* @param result The result of the key retrieval operation.
* @param keys A vector of keys retrieved.
* @param auditLogAsHtml The HTML formatted audit log for the operation, if any.
* @param auditLogError The error associated with the audit log, if any.
*/
void getKeysJobResultSlot(
const GpgME::KeyListResult &result,
const std::vector<GpgME::Key> &keys,
const QString &auditLogAsHtml,
const GpgME::Error &auditLogError
);
/**
* @brief Slot to handle the result of a key import operation.
* @param result The result of the import operation.
* @param auditLogAsHtml The HTML formatted audit log for the operation, if any.
* @param auditLogError The error associated with the audit log, if any.
*/
void importKeysFromFileSlot(
const GpgME::ImportResult &result,
const QString &auditLogAsHtml,
const GpgME::Error &auditLogError
);
/**
* @brief Slot to handle the result of a key deletion operation.
* @param result The error result of the deletion operation.
* @param auditLogAsHtml The HTML formatted audit log for the operation, if any.
* @param auditLogError The error associated with the audit log, if any.
*/
void deleteKeySlot(
const GpgME::Error &result,
const QString &auditLogAsHtml,
const GpgME::Error &auditLogError
);
signals:
/**
* @brief Signal emitted when keys are imported from a file.
* @param err The error that occurred during the import operation, if any.
*/
void importKeysFromFileResult(Error err);
/**
* @brief Signal emitted when keys are retrieved.
* @param err The error that occurred during the retrieval operation, if any.
* @param keys A vector of keys retrieved.
*/
void getKeysResult(Error err, std::vector<GpgME::Key> keys);
/**
* @brief Signal emitted when a key is deleted.
* @param err The error that occurred during the deletion operation, if any.
*/
void deleteKeyResult(Error err);
/**
* @brief Signal emitted when a decryption operation completes.
* @param err The error that occurred during decryption, if any.
* @param plain_text The decrypted message.
*/
void decryptResult(Error err, QString plain_text);
private:
UTPassphraseProvider *m_passphrase_provider; /**< The passphrase provider used for authentication. */
/**
* @brief Finds the path of a command in the system's environment.
* @param command The name of the command to find.
* @return The full path to the command.
*/
QString findCommandPath(const QString &command);
/**
* @brief Initializes the GPG home directory.
* @return The path to the GPG home directory.
*/
QString initGpgHome();
/**
* @brief Initializes the GPG executable path.
* @return The path to the GPG executable.
*/
QString initGpgExec();
/**
* @brief Initializes the GPG configuration.
*/
void initGpgConfig();
/**
* @brief Retrieves a GPG key by UID.
* @param uid The UID of the key to retrieve.
* @param remote Whether to fetch the key from a remote keyserver (default: false).
* @param include_sigs Whether to include signatures (default: false).
* @param validate Whether to validate the key (default: false).
* @return The error result of the operation.
*/
Error getKey(QString uid, bool remote = false, bool include_sigs = false, bool validate = false);
public:
/**
* @brief Constructs a Gpg object and initializes necessary resources.
* @param window The window object for interacting with the user interface.
*/
Gpg(QObject* window);
/**
* @brief Destroys the Gpg object and cleans up resources.
*/
~Gpg();
/**
* @brief Gets the passphrase provider used for GPG authentication.
* @return The passphrase provider.
*/
UTPassphraseProvider *passphrase_provider() const
{
return m_passphrase_provider;
}
/**
* @brief Imports GPG keys from a file.
* @param path The path to the file containing the keys.
* @return The error result of the import operation.
*/
Error importKeysFromFile(const QString path);
/**
* @brief Retrieves keys matching the provided UID pattern.
* @param pattern_uid The UID pattern to search for.
* @param remote Whether to fetch the key from a remote keyserver (default: false).
* @param include_sigs Whether to include signatures (default: false).
* @param validate Whether to validate the key (default: false).
* @return The error result of the operation.
*/
Error getKeys(const QString pattern_uid, bool remote = false, bool include_sigs = false, bool validate = false);
/**
* @brief Retrieves all keys from the GPG keyring.
* @param remote Whether to fetch the keys from a remote keyserver (default: false).
* @param include_sigs Whether to include signatures (default: false).
* @param validate Whether to validate the keys (default: false).
* @return The error result of the operation.
*/
Error getAllKeys(bool remote = false, bool include_sigs = false, bool validate = false);
/**
* @brief Deletes a specified GPG key.
* @param key The key to delete.
* @return The error result of the deletion operation.
*/
Error deleteKey(const Key key);
/**
* @brief Decrypts a given ciphertext.
* @param cipher_text The ciphertext to decrypt.
* @return The error result of the decryption operation.
*/
Error decrypt(const QByteArray cipher_text);
/**
* @brief Decrypts the contents of a file.
* @param path The path to the file to decrypt.
* @return The error result of the decryption operation.
*/
Error decryptFromFile(const QString path);
// Error encrypt (QString str, QString uid, bool ascii_armor = true, bool text_mode = true);
};
#endif

View File

@ -0,0 +1,47 @@
#include "decryptjob.h"
#include "qdebug.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
DecryptJob::DecryptJob(QDir rnp_homedir, QString path):
RnpJob(rnp_homedir),
m_encrypted_file_path(path)
{
this->setObjectName("DecryptJob");
}
void DecryptJob::run()
{
qDebug() << "[DecryptJob] Starting";
this->loadFullKeyring(NULL);
rnp_input_t input = NULL;
rnp_output_t output = NULL;
uint8_t *buf = NULL;
size_t buf_len = 0;
QString data = QString::Null();
auto ret = rnp_input_from_path(&input, this->m_encrypted_file_path.toLocal8Bit().data());
if (ret == RNP_SUCCESS) {
ret = rnp_output_to_memory(&output, 0);
}
if (ret == RNP_SUCCESS) {
ret = rnp_decrypt(this->m_ffi, input, output);
}
if (ret == RNP_SUCCESS) {
ret = rnp_output_memory_get_buf(output, &buf, &buf_len, false);
}
if (ret == RNP_SUCCESS) {
data = QString::fromUtf8((char*)buf, buf_len);
}
rnp_input_destroy(input);
rnp_output_destroy(output);
terminateOnError(ret);
emit resultSuccess(this->m_encrypted_file_path, data);
qDebug() << "[DecryptJob] Finished Successfully ";
}

View File

@ -0,0 +1,54 @@
#ifndef DECRYPTJOB_H
#define DECRYPTJOB_H
#include "rnpjob.h"
#include <QThread>
#include <QDir>
/**
* @class DecryptJob
* @brief A job to handle the decryption of a file in a separate thread.
*
*/
class DecryptJob : public RnpJob
{
Q_OBJECT
/**
* @brief Executes the decryption operation.
*
* This method performs the actual decryption of the encrypted file specified during
* object construction.
*/
void run() override;
signals:
/**
* @brief Emitted when the decryption operation is complete.
*
* This signal is emitted once the decryption operation finishes, providing the results.
* It indicates whether the decryption was successful and provides the clear-text output
* if the decryption was successful.
*
* @param encrypted_file_path The path to the encrypted file that was decrypted.
* @param clear_txt The decrypted content in clear-text. If an error occurs, this may be empty.
*/
void resultSuccess(QString encrypted_file_path, QString clear_txt);
private:
QString m_encrypted_file_path; /**< The path to the encrypted file that is to be decrypted. */
public:
/**
* @brief Constructs a DecryptJob object with the specified encrypted file.
*
* This constructor initializes the DecryptJob with the encrypted file path. The decryption
* operation will be executed in a background thread when the job is started.
*
* @param rnp_homedir The directory containing the keyrings.
* @param path The path to the encrypted file that needs to be decrypted.
*/
DecryptJob(QDir rnp_homedir, QString path);
};
#endif // DECRYPTJOB_H

View File

@ -0,0 +1,42 @@
#include <QDebug>
#include <QString>
#include <QJsonDocument>
#include "deletekeyjob.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
DeleteKeyJob::DeleteKeyJob(QDir rnp_homedir, QString fingerprint):
RnpJob(rnp_homedir),
m_fingerprint(fingerprint)
{
this->setObjectName("ImportKeyJob");
}
void DeleteKeyJob::run()
{
qDebug() << "[DeleteKeyJob] Starting";
// Loading keyring
this->loadFullKeyring(NULL);
// Delete key
rnp_key_handle_t key = NULL;
auto ret = rnp_locate_key(this->m_ffi, "fingerprint", this->m_fingerprint.toLocal8Bit().data(), &key);
if (ret == RNP_SUCCESS) {
ret = rnp_key_remove(key, RNP_KEY_REMOVE_PUBLIC | RNP_KEY_REMOVE_SECRET | RNP_KEY_REMOVE_SUBKEYS);
};
rnp_key_handle_destroy(key);
// Save resulting keyring
this->saveFullKeyring();
// Emit result
terminateOnError(ret);
emit resultSuccess();
qDebug() << "[DeleteKeyJob] Finished Successfully ";
}

View File

@ -0,0 +1,46 @@
#ifndef DELETEKEYJOB_H
#define DELETEKEYJOB_H
#include "rnpjob.h"
/**
* @class DeleteKeyJob
* @brief A job to handle the deletion of a key in a separate thread.
*
*/
class DeleteKeyJob : public RnpJob
{
Q_OBJECT
/**
* @brief Executes the key deletion operation.
*
* This function performs the actual process of deleting the GPG key from the keyring.
*/
void run() override;
signals:
/**
* @brief Emitted when the key deletion operation is successful.
*
* This signal is emitted when the key is successfully deleted from the keyring..
*/
void resultSuccess();
private:
QString m_fingerprint; /**< The fingerprint of the key to delete. */
public:
/**
* @brief Constructs a DeleteKeyJob object with the specified fingerprint and keyring directory.
*
* This constructor initializes the DeleteKeyJob instance with the directory containing
* the keyrings and the fingerprint of the GPG key to delete.
*
* @param rnp_homedir The directory containing the keyrings where the key will be deleted.
* @param fingerprint The fingerprint of the key to delete.
*/
DeleteKeyJob(QDir rnp_homedir, QString fingerprint);
};
#endif // DELETEKEYJOB_H

View File

@ -0,0 +1,45 @@
#include <QDebug>
#include "getkeysjob.h"
#include <QJsonDocument>
#include <QJsonObject>
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
GetKeysJob::GetKeysJob(QDir rnp_homedir):
RnpJob(rnp_homedir)
{
this->setObjectName("GetKeysJob");
}
QJsonDocument GetKeysJob::fingerprint_map_key_info(const QString fingerprint)
{
rnp_key_handle_t handle;
rnp_locate_key(this->m_ffi, "fingerprint", fingerprint.toLocal8Bit().data(), &handle);
char *result;
rnp_key_to_json( handle, 0, &result);
return QJsonDocument::fromJson(result);
}
void GetKeysJob::run()
{
qDebug() << "[GetKeysJob] Starting";
// Loading keyring
QSet<QString> fingerprints = QSet<QString>();
this->loadFullKeyring(&fingerprints);
//Get infos keys
auto key_infos = QList<QJsonDocument>();
QList<QJsonDocument>::iterator i;
for (auto i = fingerprints.begin(), end = fingerprints.end(); i != end; ++i) {
key_infos.append(this->fingerprint_map_key_info(*i));
}
// Emit result
emit resultSuccess(key_infos);
qDebug() << "[GetKeysJob] Finished Successfully ";
}

View File

@ -0,0 +1,58 @@
#ifndef GETKEYSJOB_H
#define GETKEYSJOB_H
#include <QJsonDocument>
#include "rnpjob.h"
/**
* @class GetKeysJob
* @brief A job to retrieve all GPG keys from keyrings in a separate thread.
*
*/
class GetKeysJob : public RnpJob
{
Q_OBJECT
/**
* @brief Executes the process of fetching all GPG keys.
*
* This function performs the task of retrieving all keys from the keyrings.
*/
void run() override;
signals:
/**
* @brief Emitted when the key retrieval operation completes successfully.
*
* This signal is emitted when the keys are successfully fetched. It passes a list of
* JSON documents representing the retrieved keys.
*
* @param result A list of QJsonDocument objects containing the key information.
*/
void resultSuccess(const QList<QJsonDocument> result);
private:
/**
* @brief Retrieves key information for a specific key fingerprint.
*
* This helper function fetches the key data corresponding to the given fingerprint.
* The returned information is packaged in a JSON document.
*
* @param fingerprint The fingerprint of the key to fetch information for.
* @return A QJsonDocument containing the key's information.
*/
QJsonDocument fingerprint_map_key_info(const QString fingerprint);
public:
/**
* @brief Constructs a GetKeysJob object with the specified keyring directory.
*
* This constructor initializes the job with the directory containing the keyrings to
* search for GPG keys.
*
* @param rnp_homedir The directory that contains the keyrings.
*/
GetKeysJob(QDir rnp_homedir);
};
#endif // GETKEYSJOB_H

View File

@ -0,0 +1,72 @@
#include <QDebug>
#include <QString>
#include <QJsonDocument>
#include "importkeyjob.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
ImportKeyJob::ImportKeyJob(QDir rnp_homedir, QString key_file_path):
RnpJob(rnp_homedir),
m_key_file_path(key_file_path)
{
this->setObjectName("ImportKeyJob");
}
void ImportKeyJob::run()
{
qDebug() << "[ImportKeyJob] Starting";
// Loading keyring
this->loadFullKeyring(NULL);
// Import new key
rnp_input_t input = NULL;
auto ret = rnp_input_from_path(&input, this->m_key_file_path.toLocal8Bit().constData());
if (ret == RNP_SUCCESS) {
char *r = NULL;
ret = rnp_import_keys(this->m_ffi,
input,
RNP_LOAD_SAVE_PUBLIC_KEYS | RNP_LOAD_SAVE_SECRET_KEYS,
&r);
qDebug() << "[ImportKeyJob]" << QJsonDocument::fromJson(r);
rnp_buffer_destroy(r);
}
rnp_input_destroy(input);
terminateOnError(ret);
// Save resulting keyring
this->saveFullKeyring();
// rnp_output_t output = NULL;
// qDebug() << "[ImportKeyJob] Writing pubring to " << this->pubringPath();
// ret = rnp_output_to_file(&output, this->pubringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_OVERWRITE);
// if (ret == RNP_SUCCESS) {
// qDebug() << "[ImportKeyJob] Saving key pubring ";
// ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_PUBLIC_KEYS);
// }
// if (ret == RNP_SUCCESS) {
// ret = rnp_output_finish(output);
// }
// rnp_output_destroy(output);
// terminateOnError(ret);
// qDebug() << "[ImportKeyJob] Writing secring to " << this->secringPath();
// ret = rnp_output_to_file(&output, this->secringPath().toLocal8Bit().constData(), RNP_OUTPUT_FILE_OVERWRITE);
// if (ret == RNP_SUCCESS) {
// qDebug() << "[ImportKeyJob] Saving key secring ";
// ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, RNP_LOAD_SAVE_SECRET_KEYS);
// }
// rnp_output_destroy(output);
// terminateOnError(ret);
// Emit result
emit resultSuccess();
qDebug() << "[ImportKeyJob] Finished Successfully ";
}

View File

@ -0,0 +1,47 @@
#ifndef IMPORTKEYJOB_H
#define IMPORTKEYJOB_H
#include "rnpjob.h"
/**
* @class ImportKeyJob
* @brief A job to handle the import of a key file in a separate thread.
*
*/
class ImportKeyJob : public RnpJob
{
Q_OBJECT
/**
* @brief Executes the key import operation.
*
* This function handles the actual process of importing the GPG key file into the
* keyring.
*/
void run() override;
signals:
/**
* @brief Emitted when the key import operation is successful.
*
* This signal is emitted when the key file is successfully imported into the keyring.
*/
void resultSuccess();
private:
QString m_key_file_path; /**< The path of the key file to import. */
public:
/**
* @brief Constructs an ImportKeyJob object with the specified key file and keyring directory.
*
* This constructor initializes the ImportKeyJob instance with the directory containing
* the keyrings and the file path of the GPG key to import.
*
* @param rnp_homedir The directory containing the keyrings.
* @param path The path to the key file to import.
*/
ImportKeyJob(QDir rnp_homedir, QString path);
};
#endif // IMPORTKEYJOB_H

View File

@ -6,7 +6,7 @@
/**
* @class RmJob
* @brief A class to handle removing recursively a path in a separate thread.
* @brief A job to handle the recursive removal of a path in a separate thread.
*
*/
class RmJob : public QThread
@ -14,31 +14,35 @@ class RmJob : public QThread
Q_OBJECT
/**
* @brief The main function that performs the rm operation.
* @brief Executes the recursive remove operation.
*
* Handles the process of removing recursively a target path.
* This method performs the recursive removal of the specified target path.
*/
void run() override;
signals:
/**
* @brief Signal emitted when the rm operation is complete.
* @brief Emitted when the remove operation completes.
*
* @param err A boolean indicating whether an error occurred during cloning.
* `true` if an error occurred, `false` if the clone was successful.
* This signal is emitted once the removal process is complete, indicating
* whether the operation succeeded or failed.
*
* @param err A boolean indicating whether an error occurred during the removal.
* `true` if an error occurred, `false` if the operation was successful.
*/
void resultReady(const bool err);
private:
QString m_path; ///< The path to be removed.
QString m_path; /**< The path to be removed. */
public:
/**
* @brief Constructor for the RmJob class.
* @brief Constructs an RmJob object with the specified path.
*
* Initializes the RmJob with the specified path to be removed.
* This constructor initializes the job with the path of the directory or file to be
* removed. The job will be executed in a separate thread when started.
*
* @param path Path to be remove.
* @param path The path to the file or directory that should be removed.
*/
RmJob(QString path);
};

View File

@ -0,0 +1,134 @@
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSet>
#include "qjsonarray.h"
#include "rnpjob.h"
extern "C" {
#include <rnp/rnp.h>
#include <rnp/rnp_err.h>
}
RnpJob::RnpJob(QDir rnp_homedir):
m_rnp_homedir(rnp_homedir)
{
qRegisterMetaType<rnp_result_t>("rnp_result_t");
qRegisterMetaType<QList<QJsonDocument >> ("QList<QJsonDocument>");
qRegisterMetaType<QString *>("QString*");
auto ret = rnp_ffi_create(&this->m_ffi,
RNP_KEYSTORE_GPG,
RNP_KEYSTORE_GPG);
if (ret != RNP_SUCCESS) {
qDebug() << "[RnpJob] Err : " << ret;
qFatal("Error on rnp ffi init!");
}
}
RnpJob::~RnpJob()
{
auto ret = rnp_ffi_destroy(this->m_ffi);
if (ret != RNP_SUCCESS) {
qDebug() << "[RnpJob] Err : " << ret;
qFatal("Something go wrong on rnp ffi detroy");
}
}
bool RnpJob::passProvider(rnp_ffi_t ffi,
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len)
{
if (strcmp(pgp_context, "protect")) {
return false;
}
strncpy(buf, "password", buf_len);
return true;
}
void RnpJob::loadKeyFile(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags)
{
qDebug() << "[RnpJob] Load keyring at" << path;
rnp_input_t input = NULL;
if (QFileInfo::exists(this->pubringPath())) {
auto ret = rnp_input_from_path(&input, path.toLocal8Bit().constData());
char *json = NULL;
if (ret == RNP_SUCCESS) {
ret = rnp_import_keys(this->m_ffi,
input,
flags,
&json);
}
QJsonDocument json_document = QJsonDocument::fromJson(json);
qDebug() << "[RnpJob] json" << json_document;
if (result_fingerprints) {
foreach (const QJsonValue fingerprint, json_document.object()["keys"].toArray()) {
qDebug() << "[RnpJob] Add fingerprint" << fingerprint["fingerprint"].toString();
result_fingerprints->insert(fingerprint["fingerprint"].toString());
}
}
rnp_input_destroy(input);
rnp_buffer_destroy(json);
terminateOnError(ret);
qDebug() << "[RnpJob] Keyring loaded successfully";
} else {
qDebug() << "[RnpJob] Keyring" << path << "not found";
}
}
void RnpJob::loadPubKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->loadKeyFile(result_fingerprints, this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
}
void RnpJob::loadSecKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->loadKeyFile(result_fingerprints, this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
}
void RnpJob::loadFullKeyring(QSet<QString> *result_fingerprints = NULL)
{
this->loadPubKeyring(result_fingerprints);
this->loadSecKeyring(result_fingerprints);
}
void RnpJob::saveKeyFile(const QString path, const uint32_t flags)
{
qDebug() << "[RnpJob] Saving keyring at" << path;
rnp_output_t output = NULL;
auto ret = rnp_output_to_file(&output, path.toLocal8Bit().data(), RNP_OUTPUT_FILE_OVERWRITE);
if (ret == RNP_SUCCESS) {
qDebug() << "[ImportKeyJob] Saving key pubring ";
ret = rnp_save_keys(this->m_ffi, RNP_KEYSTORE_GPG, output, flags);
}
if (ret == RNP_SUCCESS) {
ret = rnp_output_finish(output);
}
rnp_output_destroy(output);
terminateOnError(ret);
}
void RnpJob::savePubKeyring()
{
this->saveKeyFile(this->pubringPath(), RNP_LOAD_SAVE_PUBLIC_KEYS);
}
void RnpJob::saveSecKeyring()
{
this->saveKeyFile(this->secringPath(), RNP_LOAD_SAVE_SECRET_KEYS);
}
void RnpJob::saveFullKeyring()
{
this->savePubKeyring();
this->saveSecKeyring();
}

185
plugins/Pass/jobs/rnpjob.h Normal file
View File

@ -0,0 +1,185 @@
#ifndef RNPJOB_H
#define RNPJOB_H
#include <QThread>
#include <QDir>
extern "C" {
#include <rnp/rnp.h>
}
#include <variant>
#define terminateOnError(ret) \
if(ret != RNP_SUCCESS) { \
qDebug() << "[RnpJob] Err : " << ret; \
qDebug() << "[RnpJob] Err Msg : " << rnp_result_to_string(ret); \
emit resultError(ret); \
return; \
} \
/**
* @class RnpJob
* @brief A base class that manages OpenPGP-related tasks using the librnp library.
*
* The RnpJob class serves as an abstraction for performing OpenPGP (RNP) operations, such as
* encryption, decryption, and key management, using the RNP library.
*/
class RnpJob : public QThread
{
Q_OBJECT
signals:
/**
* @brief Signal emitted when an error occurs in the RNP job.
*
* This signal is emitted when an error occurs during an RNP operation, such as key loading
* or encryption/decryption failure. The error code is passed to indicate the specific issue.
*
* @param err The error code returned by the RNP operation.
*/
void resultError(const rnp_result_t err);
private:
/**
* @brief A callback function for providing the passphrase to RNP.
*
* This static function is used as a callback to provide a passphrase to RNP when required
* during key operations such as decryption or signing. It allows the library to continue the
* operation with the necessary passphrase.
*
* @param ffi The RNP FFI handle.
* @param app_ctx The application context, used for accessing application-specific data.
* @param key The key for which the passphrase is required.
* @param pgp_context The context string (e.g., "decrypt").
* @param buf The buffer to fill with the passphrase.
* @param buf_len The length of the buffer.
*
* @return true if the passphrase was successfully provided, false otherwise.
*/
static bool passProvider(rnp_ffi_t ffi,
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len);
QDir m_rnp_homedir; /**< Directory that contains the keyrings and RNP configuration. */
/**
* @brief Loads a key file into the keyring.
*
* This method loads a key file into the keyring, adding keys specified by their fingerprints.
*
* @param result_fingerprints A set to hold the fingerprints of the keys loaded into the keyring.
* @param path The path to the key file.
* @param flags Flags specifying options for loading keys (e.g., overwrite, secret keys, etc.).
*/
void loadKeyFile(QSet<QString> *result_fingerprints, const QString path, const uint32_t flags);
/**
* @brief Saves keys to the keyring file in the specified directory.
*
* This method saves a keyring to a file. It allows you to specify options such as overwriting
* existing files or including secret keys.
*
* @param path The path to the keyring file where the keys should be saved.
* @param flags Flags specifying options for saving the keys (e.g., overwrite, include secret keys, etc.).
*/
void saveKeyFile(const QString path, const uint32_t flags);
protected:
rnp_ffi_t m_ffi; /**< RNP FFI (Foreign Function Interface) handle, used for interacting with the RNP library. */
/**
* @brief Get the path to the public keyring.
*
* This method returns the file path to the public keyring (where public keys are stored).
* It combines the directory and file name to provide the full path.
*
* @return The path to the public keyring file.
*/
QString pubringPath()
{
return this->m_rnp_homedir.filePath("pubring.pgp");
}
/**
* @brief Get the path to the secret keyring.
*
* This method returns the file path to the secret keyring (where private keys are stored).
* It combines the directory and file name to provide the full path.
*
* @return The path to the secret keyring file.
*/
QString secringPath()
{
return this->m_rnp_homedir.filePath("secring.pgp");
}
/**
* @brief Loads the secret keyring into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of the loaded secret keys.
*/
void loadSecKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Loads the public keyring into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of the loaded public keys.
*/
void loadPubKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Loads both the public and secret keyrings into RNP.
*
* @param result_fingerprints A set that will hold the fingerprints of all loaded keys.
*/
void loadFullKeyring(QSet<QString> *result_fingerprints);
/**
* @brief Saves the secret keyring to the RNP homedir.
*
*/
void saveSecKeyring();
/**
* @brief Saves the public keyring to the RNP homedir.
*
*/
void savePubKeyring();
/**
* @brief Saves both the public and secret keyrings to the RNP homedir.
*
*/
void saveFullKeyring();
public:
/**
* @brief Constructs an RnpJob object with the specified RNP home directory.
*
* This constructor initializes the RnpJob instance with the directory that contains the
* keyrings and RNP configuration. Keyring files (pubring.pgp and secring.pgp) will be found
* in this directory.
*
* @param rnp_homedir The directory containing the RNP keyrings and configuration files.
*/
RnpJob(QDir rnp_homedir);
/**
* @brief Destructor for the RnpJob class.
*
* The destructor cleans up any resources used by the RnpJob instance, including releasing
* the RNP FFI handle.
*/
~RnpJob();
void setPassProvider(rnp_password_cb pass_provider_cb)
{
rnp_ffi_set_pass_provider(this->m_ffi, pass_provider_cb, NULL);
}
};
#endif // RNPJOB_H

View File

@ -2,166 +2,218 @@
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include "jobs/decryptjob.h"
#include "jobs/deletekeyjob.h"
#include "jobs/getkeysjob.h"
#include "jobs/importkeyjob.h"
#include "jobs/rmjob.h"
#include "pass.h"
#include "gpg.h"
#include "passkeymodel.h"
#include "passphraseprovider.h"
Pass::Pass():
m_password_store (QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.password-store")),
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1))),
m_show_filename(QString())
{}
m_gpg_home (QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.rnp")),
m_passphrase_provider(&UTPassphraseProvider::get_pass_provider),
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
{
this->initGpgHome();
this->initPasswordStore();
QObject::connect(this, &Pass::responsePassphraseDialogPropagate,
&UTPassphraseProvider::instance(), &UTPassphraseProvider::handleResponse);
}
void Pass::initialize(QObject *window)
{
if (!window) {
qFatal("window is invalid. Abording.");
qWarning("[Pass] Window should be null only for testing");
} else {
UTPassphraseProvider::instance().setWindow(window);
}
}
this->m_gpg = std::unique_ptr<Gpg>(new Gpg(window));
QObject::connect(this, &Pass::responsePassphraseDialogPropagate, this->m_gpg->passphrase_provider(),
&UTPassphraseProvider::handleResponse);
QObject::connect(this->m_gpg.get(), &Gpg::importKeysFromFileResult, this, &Pass::importGPGKeyResult);
QObject::connect(this->m_gpg.get(), &Gpg::getKeysResult, this, &Pass::getAllGPGKeysResult);
QObject::connect(this->m_gpg.get(), &Gpg::deleteKeyResult, this, &Pass::deleteGPGKeyResult);
QObject::connect(this->m_gpg.get(), &Gpg::decryptResult, this, &Pass::showResult);
void Pass::initGpgHome()
{
// delete gpghome from previous version using GPGME
QString path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation).append("/.gpghome");
QDir dir(path);
dir.removeRecursively();
QDir dir(m_password_store);
if (!dir.exists()) {
dir.mkpath(".");
// create gpghome for rnp
QDir dir_gpg_home(this->m_gpg_home);
if (!dir_gpg_home.exists()) {
dir_gpg_home.mkpath(".");
}
qInfo() << "Password Store is :" << m_password_store;
qInfo() << "[Pass] GPG Home is :" << m_gpg_home;
}
void Pass::initPasswordStore()
{
QDir dir_password_store(this->m_password_store);
if (!dir_password_store.exists()) {
dir_password_store.mkpath(".");
}
qInfo() << "[Pass] Password Store is :" << m_password_store;
}
bool Pass::show(QUrl url)
{
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A command is already running";
return false;
}
auto path = url.toLocalFile();
qInfo() << "Pass show " << path;
QFileInfo file_info(path);
this->m_show_filename = file_info.completeBaseName();
return this->m_gpg->decryptFromFile(path);
auto job = new DecryptJob(this->m_gpg_home, url.toLocalFile());
job->setPassProvider(this->m_passphrase_provider);
QObject::connect(job, &DecryptJob::resultError, this, &Pass::slotShowError);
QObject::connect(job, &DecryptJob::resultSuccess, this, &Pass::slotShowSucceed);
connect(job, &DecryptJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::showResult(Error err, QString plain_text)
void Pass::slotShowError(rnp_result_t err)
{
qDebug() << "Pass show Result";
if (err) {
qInfo() << "Pass show Failed";
emit showFailed(err.asString());
} else if (err.isCanceled()) {
qInfo() << "Pass show Cancelled";
emit showCancelled();
} else {
qInfo() << "Pass show Succeed";
emit showSucceed(this->m_show_filename, plain_text);
qInfo() << "[Pass] Show Failed";
emit showFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
this->m_show_filename = QString();
void Pass::slotShowSucceed(QString encrypted_file_path, QString plain_text)
{
qDebug() << "[Pass] Show Succeed";
QFileInfo file_info(encrypted_file_path);
emit showSucceed(file_info.completeBaseName(), plain_text);
this->m_sem->release(1);
}
bool Pass::deletePasswordStore()
{
qInfo() << "Pass delete Password Store";
auto job = new RmJob(this->password_store());
qDebug() << "Delete Password Store at " << this->password_store();
connect(job, &RmJob::resultReady, this, &Pass::deletePasswordStoreResult);
qInfo() << "[Pass] Delete Password Store at" << this->m_password_store;
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A command is already running";
return false;
}
auto job = new RmJob(this->m_password_store);
connect(job, &RmJob::resultReady, this, &Pass::slotDeletePasswordStoreResult);
connect(job, &RmJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::deletePasswordStoreResult(bool err)
void Pass::slotDeletePasswordStoreResult(bool err)
{
qDebug() << "Pass delete Password StoreResult";
if (err) { //dir.removeRecursively()) {
qInfo() << "Pass delete Password Store Failed";
this->initPasswordStore(); // reinit an empty password-store
if (err) {
qInfo() << "[Pass] delete Password Store Failed";
emit deletePasswordStoreFailed("failed to delete password store");
} else {
qInfo() << "Pass delete Password Store Succeed";
qInfo() << "[Pass] Delete Password Store Succeed";
emit deletePasswordStoreSucceed();
}
this->m_sem->release(1);
}
bool Pass::deleteGPGKey(PassKeyModel* key)
{
qInfo() << "[Pass] Delete GPG key fingerprint " << key->property("keyid").toString();
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A command is already running";
return false;
}
qInfo() << "Delete Key " << key->uid();
return this->m_gpg->deleteKey(key->key());
auto job = new DeleteKeyJob(this->m_gpg_home, key->property("fingerprint").toString());
QObject::connect(job, &DeleteKeyJob::resultError, this, &Pass::slotDeleteGPGKeyError);
QObject::connect(job, &DeleteKeyJob::resultSuccess, this, &Pass::slotDeleteGPGKeySucceed);
connect(job, &DeleteKeyJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::deleteGPGKeyResult(Error err)
void Pass::slotDeleteGPGKeyError(rnp_result_t err)
{
qDebug() << "Delete Ke yResult";
if (err) {
qInfo() << "Delete Key Failed";
emit deleteGPGKeyFailed(err.asString());
} else {
qInfo() << "Delete Key Succeed";
emit deleteGPGKeySucceed();
}
qInfo() << "[Pass] Delete GPG key Failed";
emit deleteGPGKeyFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
bool Pass::importGPGKey(QUrl url)
void Pass::slotDeleteGPGKeySucceed()
{
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
qInfo() << "Import GPG Key from " << url;
return this->m_gpg->importKeysFromFile(url.toLocalFile());
qInfo() << "[Pass] Delete GPG key Succesfull";
emit deleteGPGKeySucceed();
this->m_sem->release(1);
}
void Pass::importGPGKeyResult(Error err)
bool Pass::importGPGKey(QUrl url)
{
qDebug() << "Import GPG Key Result";
if (err) {
qInfo() << "Delete Key Failed";
emit importGPGKeyFailed(err.asString());
} else {
qInfo() << "Delete Key Succeed";
emit importGPGKeySucceed();
qInfo() << "[Pass] Import GPG Key from " << url;
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A command is already running";
return false;
}
auto job = new ImportKeyJob(this->m_gpg_home, url.toLocalFile());
QObject::connect(job, &ImportKeyJob::resultError, this, &Pass::slotImportGPGKeyError);
QObject::connect(job, &ImportKeyJob::resultSuccess, this, &Pass::slotImportGPGKeySucceed);
connect(job, &ImportKeyJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::slotImportGPGKeyError(rnp_result_t err)
{
qInfo() << "[Pass] Import GPG Key Failed";
emit importGPGKeyFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
void Pass::slotImportGPGKeySucceed()
{
qInfo() << "[Pass] Import GPG Key Succesfull";
emit importGPGKeySucceed();
this->m_sem->release(1);
}
bool Pass::getAllGPGKeys()
{
qInfo() << "[Pass] Get all GPG Keys";
if (!this->m_sem->tryAcquire(1, 500)) {
qInfo() << "[Pass] A command is already running";
return false;
}
qInfo() << "Get GPG keys";
return this->m_gpg->getAllKeys();
this->m_keyring_model = nullptr;
auto job = new GetKeysJob(this->m_gpg_home);
QObject::connect(job, &GetKeysJob::resultError, this, &Pass::slotGetAllGPGKeysError);
QObject::connect(job, &GetKeysJob::resultSuccess, this, &Pass::slotGetAllGPGKeysSucceed);
connect(job, &ImportKeyJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
void Pass::getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info)
void Pass::slotGetAllGPGKeysError(rnp_result_t err)
{
qDebug() << "Get GPG keys Result";
if (err) {
qInfo() << "Get GPG Failed";
emit getAllGPGKeysFailed(err.asString());
} else {
qInfo() << "Get GPG Succeed";
emit getAllGPGKeysSucceed(QVariant::fromValue(PassKeyModel::keysToPassKey(keys_info)));
qInfo() << "[Pass] Get all GPG Keys Failed";
this->m_keyring_model = nullptr;
emit getAllGPGKeysFailed(rnp_result_to_string(err));
this->m_sem->release(1);
}
void Pass::slotGetAllGPGKeysSucceed(QList<QJsonDocument> result)
{
qInfo() << "[Pass] Get all GPG Keys Succeed";
this->m_keyring_model = std::unique_ptr<PassKeyringModel>(new PassKeyringModel(result));
emit getAllGPGKeysSucceed(this->m_keyring_model.get());
this->m_sem->release(1);
}
void Pass::responsePassphraseDialog(bool cancel, QString passphrase)
{
qDebug() << "Propagate responsePassphraseDialog";
qDebug() << "[Pass] Propagate responsePassphraseDialog to UTPassphraseProvider";
emit responsePassphraseDialogPropagate(cancel, passphrase);
}

View File

@ -1,15 +1,17 @@
#ifndef PASS_H
#define PASS_H
#include <QDebug>
#include <QObject>
#include <QUrl>
#include <QVariant>
#include <gpgme++/context.h>
#include "gpg.h"
using namespace GpgME;
#include <QSemaphore>
#include <memory>
extern "C" {
#include <rnp/rnp.h>
}
#include "passkeyringmodel.h"
/**
* @class Pass
* @brief A class for managing password storage using GPG encryption.
@ -20,7 +22,8 @@ using namespace GpgME;
class Pass : public QObject
{
Q_OBJECT
Q_PROPERTY(QString password_store READ password_store MEMBER m_password_store CONSTANT)
Q_PROPERTY(QString password_store MEMBER m_password_store WRITE set_password_store )
Q_PROPERTY(QString gpg_home MEMBER m_gpg_home WRITE set_gpg_home )
private slots:
/**
@ -28,32 +31,41 @@ private slots:
* @param err The error that occurred during the operation.
* @param plain_text The decrypted plain text (password).
*/
void showResult(Error err, QString plain_text);
void slotShowError(rnp_result_t err);
void slotShowSucceed(QString encrypted_file_path, QString plain_text);
void slotDeleteGPGKeyError(rnp_result_t err);
void slotDeleteGPGKeySucceed();
/**
* @brief Slot to handle the result of a GPG key deletion operation.
* @brief Slot to handle the error result of a GPG key import operation.
* @param err The error that occurred during the operation.
*/
void deleteGPGKeyResult(Error err);
void slotImportGPGKeyError(rnp_result_t err);
/**
* @brief Slot to handle the result of a GPG key import operation.
* @param err The error that occurred during the operation.
* @brief Slot to handle the succeed result of a GPG key import operation.
*/
void importGPGKeyResult(Error err);
void slotImportGPGKeySucceed();
/**
* @brief Slot to handle the result of retrieving all GPG keys.
* @param err The error that occurred during the operation.
* @param keys_info The list of GPG keys retrieved.
*/
void getAllGPGKeysResult(Error err, std::vector<GpgME::Key> keys_info);
void slotGetAllGPGKeysError(rnp_result_t err);
/**
* @brief Slot to handle the succeed result of a GPG key get all keys operation.
*/
void slotGetAllGPGKeysSucceed(QList<QJsonDocument> result);
/**
* @brief Slot to handle the result of a delete Password Store operation.
* @param err True if an error occurred during the operation.
*/
void deletePasswordStoreResult(bool err);
void slotDeletePasswordStoreResult(bool err);
signals:
// GPG-related signals
@ -83,7 +95,7 @@ signals:
* @brief Emitted when all GPG keys are successfully retrieved.
* @param keys_info The list of retrieved keys.
*/
void getAllGPGKeysSucceed(QVariant keys_info);
void getAllGPGKeysSucceed(QObject* keys_info);
/**
* @brief Emitted when retrieving GPG keys fails.
@ -131,25 +143,55 @@ signals:
private:
QString m_password_store; /**< The path to the password store. */
std::unique_ptr<Gpg> m_gpg; /**< The GPG instance used for encryption/decryption. */
QString m_gpg_home; /**< The path to the gpg home. */
std::unique_ptr<PassKeyringModel>
m_keyring_model; /**< Meta data on the keyring uid, name, secrecy ... of the availble keys. */
rnp_password_cb m_passphrase_provider; /**< Pointer on passphrase povider for operations using secret keys. */
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing concurrent operations. */
QString m_show_filename; /**< The filename associated with the password to show. */
/**
* @brief Initialize gpg home.
*/
void initGpgHome();
/**
* @brief Initialize password store.
*/
void initPasswordStore();
public:
/**
* @brief Constructs the Pass object and initializes necessary resources.
* @brief Constructs the Pass object.
*/
Pass();
/**
* @brief Gets the path to the password store.
* @return The path to the password store.
* @brief Set the path to the password store.
* @param The path to the password store.
*/
QString password_store() const
void set_password_store(QString password_store)
{
return m_password_store;
qInfo() << "[Pass] Password Store changed to :" << password_store;
this->m_password_store = password_store;
};
/**
* @brief Set the path to the gpg hom.
* @param The path to the gpg hom
*/
void set_gpg_home(QString gpg_home)
{
qInfo() << "[Pass] GPG Home changed to :" << gpg_home;
this->m_gpg_home = gpg_home;
};
void set_passphrase_provider(rnp_password_cb passphrase_provider)
{
this->m_passphrase_provider = passphrase_provider;
}
/**
* @brief Initializes the Pass object with the given window.
* @param window The QObject window to interact with.

View File

@ -1,153 +0,0 @@
#ifndef PASSKEYMODEL_H
#define PASSKEYMODEL_H
#include <QObject>
#include <gpgme++/key.h>
using namespace GpgME;
/**
* @class UserIdModel
* @brief A model representing a user ID associated with a GPG key.
*
* This class encapsulates the user ID information (UID) for a GPG key, providing access
* to the UID's identifier, name, and email.
*/
class UserIdModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QString uid READ uid CONSTANT)
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(QString email READ email CONSTANT)
private:
UserID m_user_id; /**< The GPG UserID associated with the model. */
public:
/**
* @brief Constructs a UserIdModel for the given UserID.
* @param key The GPG UserID to model.
*/
UserIdModel(UserID key) : m_user_id(key) {}
/**
* @brief Gets the unique identifier (UID) for this user ID.
* @return The UID as a QString.
*/
QString uid() const
{
return QString::fromUtf8(m_user_id.id());
};
/**
* @brief Gets the name associated with this user ID.
* @return The name as a QString.
*/
QString name() const
{
return QString::fromUtf8(m_user_id.name());
};
/**
* @brief Gets the email associated with this user ID.
* @return The email as a QString.
*/
QString email() const
{
return QString::fromUtf8(m_user_id.email());
};
};
/**
* @class PassKeyModel
* @brief A model representing a GPG key.
*
* This class encapsulates the properties of a GPG key, including its key ID, associated
* user IDs, secret key status, and expiration status. It is used as a model for managing
* GPG keys within an application, providing access to the key's data and its associated user IDs.
*/
class PassKeyModel : public QObject
{
Q_OBJECT
Q_PROPERTY(Key key READ key MEMBER m_key CONSTANT)
Q_PROPERTY(QString uid READ uid CONSTANT)
Q_PROPERTY(QList<QObject *> userIds READ userIds CONSTANT)
Q_PROPERTY(bool isSecret READ isSecret CONSTANT)
Q_PROPERTY(bool isExpired READ isExpired CONSTANT)
private:
Key m_key; /**< The GPG key associated with the model. */
public:
/**
* @brief Constructs a PassKeyModel for the given GPG key.
* @param key The GPG key to model.
*/
PassKeyModel(Key key) : m_key(key) {}
/**
* @brief Converts a vector of GPG keys into a list of PassKeyModel objects.
* @param keys The vector of GPG keys to convert.
* @return A QList of PassKeyModel objects representing the keys.
*/
static QList<QObject *> keysToPassKey(std::vector<Key> keys)
{
QList<QObject *> ret;
std::for_each(keys.begin(), keys.end(), [&ret](Key k) {
ret.append(new PassKeyModel(k));
});
return ret;
};
/**
* @brief Gets the GPG key associated with this model.
* @return The GPG key.
*/
Key key() const
{
return m_key;
};
/**
* @brief Gets the unique identifier (UID) for this GPG key.
* @return The UID as a QString.
*/
QString uid() const
{
return QString::fromUtf8(m_key.keyID());
};
/**
* @brief Gets the list of user IDs associated with this GPG key.
* @return A list of UserIdModel objects representing the user IDs.
*/
QList<QObject *> userIds() const
{
auto user_ids = m_key.userIDs();
QList<QObject *> ret;
std::for_each(user_ids.begin(), user_ids.end(), [&ret](UserID k) {
ret.append(new UserIdModel(k));
});
return ret;
};
/**
* @brief Checks if the GPG key is a secret key.
* @return True if the key is a secret key, false otherwise.
*/
bool isSecret() const
{
return m_key.hasSecret();
};
/**
* @brief Checks if the GPG key is expired.
* @return True if the key is expired, false otherwise.
*/
bool isExpired() const
{
return m_key.isExpired();
};
};
#endif

View File

@ -0,0 +1,129 @@
#ifndef PASSKEYRINGMODEL_H
#define PASSKEYRINGMODEL_H
#include <QDebug>
#include <QObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QSet>
/**
* @class PassKeyModel
* @brief A model representing a GPG (GNU Privacy Guard) key.
*
*/
class PassKeyModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QString fingerprint MEMBER m_fingerprint CONSTANT)
Q_PROPERTY(QString keyid MEMBER m_keyid CONSTANT)
Q_PROPERTY(QVariant userids MEMBER m_userids CONSTANT)
Q_PROPERTY(bool hasSecret MEMBER m_hasSecret CONSTANT)
private:
QString m_fingerprint; /**< The fingerprint of the GPG key, used to uniquely identify the key. */
QString m_keyid; /**< The unique ID associated with the GPG key. */
QVariant m_userids; /**< A list of user IDs associated with the GPG key. */
bool m_hasSecret; /**< Indicates whether the GPG key has an associated secret key. */
public:
/**
* @brief Constructs a PassKeyModel object using the provided GPG key information.
*
* This constructor initializes the PassKeyModel based on a JSON document containing GPG key data.
* The key data typically includes the key's fingerprint, key ID, associated user IDs, and secret key status.
*
* @param key_info A JSON document containing the GPG key data.
*/
PassKeyModel(QJsonDocument key_info)
{
this->m_fingerprint = key_info["fingerprint"].toString();
qDebug() << "[PassKeyModel] fingerprint : " << this->m_fingerprint;
this->m_keyid = key_info["keyid"].toString();
qDebug() << "[PassKeyModel] keyid : " << this->m_keyid;
auto user_ids_json_array = key_info["userids"].toArray();
auto userids = QList<QString>();
for (auto i = user_ids_json_array.begin(), end = user_ids_json_array.end(); i != end; ++i) {
userids.append((*i).toString());
}
this->m_userids = QVariant(userids);
qDebug() << "[PassKeyModel] userids : " << this->m_userids;
this->m_hasSecret = key_info["secret key"]["present"].toBool();
qDebug() << "[PassKeyModel] hasSecret : " << this->m_hasSecret;
}
};
/**
* @class PassKeyringModel
* @brief A model representing a collection of GPG keys.
*
* This class serves as a container for multiple GPG keys, typically representing an entire
* keyring. It provides functionality to manage and retrieve keys, such as fetching all keys
* in the keyring and determining the length of the keyring.
*
* The class also includes logic to distinguish between primary and sub keys, with an option
* to ignore subkeys if desired.
*/
class PassKeyringModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<QObject *> keys MEMBER m_keys CONSTANT)
Q_PROPERTY(int length READ length CONSTANT)
private:
QList<QObject *> m_keys; /**< A list of PassKeyModel objects representing the keys in the keyring. */
public:
/**
* @brief Constructs a PassKeyringModel from a list of GPG key JSON documents.
*
* This constructor initializes the PassKeyringModel by parsing a list of JSON documents
* that represent multiple GPG keys. It filters out subkeys and only retains primary keys
* for inclusion in the keyring.
*
* @param key_infos A list of JSON documents representing GPG keys.
*/
PassKeyringModel(QList<QJsonDocument> key_infos)
{
for (auto i = key_infos.begin(), end = key_infos.end(); i != end; ++i) {
qDebug() << "[PassKeyringModel]" << *i;
// Ignore subkeys and only add primary keys to the model.
if ((*i)["primary key grip"].isUndefined()) {
this->m_keys.append(new PassKeyModel(*i));
} else {
qDebug() << "[PassKeyringModel] Subkey info " << (*i)["keyid"].toString() << "ignored";
}
}
}
/**
* @brief Destructor for PassKeyringModel.
*
* Cleans up dynamically allocated PassKeyModel objects within the keyring.
*/
~PassKeyringModel()
{
qDeleteAll(this->m_keys);
}
/**
* @brief Retrieves the number of keys in the keyring.
*
* This function returns the number of primary keys present in the keyring.
*
* @return The number of keys in the keyring.
*/
int length()
{
return this->m_keys.length();
}
};
#endif // PASSKEYRINGMODEL_H

View File

@ -2,22 +2,25 @@
#define UTPASSPHRASEPROVIDER_H
#include <QDebug>
#include <memory>
#include <stdio.h>
#include <QObject>
#include <QQmlProperty>
#include <QEventLoop>
#include <QSemaphore>
#include <gpgme++/interfaces/passphraseprovider.h>
#include "gpg.h"
extern "C" {
#include <rnp/rnp.h>
}
/**
* @class UTPassphraseProvider
* @brief A passphrase provider for GPG operations that interacts with a QML dialog.
*
* This class implements the `PassphraseProvider` interface from GPGME and is responsible for
* obtaining passphrases for GPG operations.
* This class is used to prompt the user for a passphrase through a QML-based dialog. It manages
* the passphrase entry process and signals whether the user has provided a passphrase or canceled
* the operation.
*/
class UTPassphraseProvider : public QObject, public PassphraseProvider
class UTPassphraseProvider : public QObject
{
Q_OBJECT
@ -25,20 +28,21 @@ public slots:
/**
* @brief Slot to handle the user's response from the passphrase dialog.
*
* This method processes the response from the passphrase dialog. If the user provides a passphrase,
* it is stored; if the operation is canceled, a flag is set.
* This slot is called when the user provides a passphrase or cancels the passphrase entry.
* If the user provides a passphrase, it is stored; if the user cancels, a cancellation flag
* is set.
*
* @param canceled Whether the user canceled the passphrase entry.
* @param passphrase The passphrase entered by the user.
* @param canceled Indicates whether the user canceled the passphrase entry.
* @param passphrase The passphrase entered by the user (if not canceled).
*/
void handleResponse(bool canceled, QString passphrase)
{
qDebug() << "call handleResponse";
if (!canceled)
gpgrt_asprintf(&m_passphrase, "%s", passphrase.toUtf8().constData());
else
m_canceled = true;
emit unlockEventLoop();
qDebug() << "[UTPassphraseProvider] Call handleResponse";
if (!canceled) {
this->m_canceled = false;
this->m_passphrase = passphrase;
}
unlockEventLoop();
};
signals:
@ -46,91 +50,141 @@ signals:
* @brief Signal to unlock the event loop.
*
* This signal is emitted when the passphrase has been entered or the operation has been canceled,
* unlocking the event loop waiting for the response.
* causing the event loop waiting for the response to exit.
*/
void unlockEventLoop();
private:
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing access. */
char *m_passphrase; /**< The passphrase provided by the user. */
bool m_canceled; /**< Flag indicating whether the passphrase operation was canceled. */
/**
* @brief Private constructor for singleton pattern.
*
* Initializes the passphrase provider with a semaphore to manage access, and a flag to indicate
* whether the operation was canceled.
*
* @param parent Parent QObject (default is nullptr).
*/
explicit UTPassphraseProvider(QObject * parent = nullptr)
: m_sem(std::make_unique<QSemaphore>(1)),
m_passphrase(QString::Null()),
m_canceled(true)
{}
QObject *m_window; /**< The window object that triggers the QML dialog. */
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing access to the passphrase entry process. */
QString m_passphrase; /**< The passphrase provided by the user. */
bool m_canceled; /**< Flag indicating whether the passphrase operation was canceled. */
public:
/**
* @brief Constructs a UTPassphraseProvider.
*
* Initializes the semaphore, passphrase, and canceled flag. Sets the window object that will
* trigger the passphrase dialog.
*
* @param window The QObject representing the window that interacts with QML.
*/
UTPassphraseProvider(QObject* window)
: m_sem(std::make_unique<QSemaphore>(1)),
m_passphrase(nullptr),
m_canceled(false),
m_window(window)
{
qDebug() << "Initialize UTPassphraseProvider";
}
~UTPassphraseProvider() = default;
/**
* @brief Implements the PassphraseProvider's `getPassphrase` method.
* @brief Gets the singleton instance of UTPassphraseProvider.
*
* This method is called by GPGME to retrieve the passphrase needed for GPG operations. It triggers
* a QML dialog for the user to input their passphrase. The method waits for the response and returns
* the passphrase if successful, or null if canceled.
* This method ensures that only one instance of the passphrase provider exists throughout the application.
*
* @param useridHint A hint for the user ID to which the passphrase corresponds.
* @param description A description of the passphrase request.
* @param previousWasBad Flag indicating whether the previous passphrase attempt was incorrect.
* @param canceled Reference to a boolean flag that will be set if the operation is canceled.
*
* @return The passphrase as a `char *` or `nullptr` if canceled.
* @return The singleton instance of UTPassphraseProvider.
*/
char *getPassphrase(const char *useridHint,
const char *description,
bool previousWasBad,
bool &canceled) Q_DECL_OVERRIDE {
qDebug() << "Call the getPassphrase";
if (!this->m_sem->tryAcquire(1, 500))
static UTPassphraseProvider &instance()
{
qWarning() << "Cannot acquire UTPassphraseProvider semaphore.";
canceled = true;
return nullptr;
static UTPassphraseProvider instance;
return instance;
}
this->m_passphrase = nullptr;
this->m_canceled = false;
UTPassphraseProvider(UTPassphraseProvider const &) = delete; /**< Prevents copying of the instance. */
void operator=(UTPassphraseProvider const &) = delete; /**< Prevents assignment of the instance. */
qDebug() << "Call the QML Dialog Passphrase Provider";
/**
* @brief Callback function to retrieve the passphrase for GPG operations.
*
* This static method is called by the GPG library when it requires a passphrase for a specific key operation.
* It triggers a QML dialog to prompt the user for the passphrase and waits for a response.
*
* @param ffi The RNP FFI instance.
* @param app_ctx provided by application
* @param key the key, if any, for which the password is being requested.
* Note: this key handle should not be held by the application,
* it is destroyed after the callback. It should only be used to
* retrieve information like the userids, grip, etc.
* @param pgp_context a descriptive string on why the password is being
* requested, may have one of the following values:
* - "add subkey": add subkey to the encrypted secret key
* - "add userid": add userid to the encrypted secret key
* - "sign": sign data
* - "decrypt": decrypt data using the encrypted secret key
* - "unlock": temporary unlock secret key (decrypting its fields), so it may be used
* later without need to decrypt
* - "protect": encrypt secret key fields
* - "unprotect": decrypt secret key fields, leaving those in a raw format
* - "decrypt (symmetric)": decrypt data, using the password
* - "encrypt (symmetric)": encrypt data, using the password
* @param buf to which the callback should write the returned password, NULL terminated.
* @param buf_len the size of buf
*
* @return true if a password was provided, false otherwise
*/
static bool
get_pass_provider( rnp_ffi_t ffi,
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len)
{
qDebug() << "[UTPassphraseProvider] Call the getPassphrase";
if (!UTPassphraseProvider::instance().m_window) {
qWarning() << "[UTPassphraseProvider] Aborting : window is not set";
return false;
}
if (!UTPassphraseProvider::instance().m_sem->tryAcquire(1, 500)) {
qWarning() << "[UTPassphraseProvider] Aborting : Cannot acquire UTPassphraseProvider semaphore";
return false;
}
UTPassphraseProvider::instance().m_passphrase = QString::Null();
UTPassphraseProvider::instance().m_canceled = true;
qDebug() << "[UTPassphraseProvider] Call the QML Dialog Passphrase Provider";
QMetaObject::invokeMethod(
this->m_window, "callPassphraseDialog",
Q_ARG(QVariant, useridHint),
Q_ARG(QVariant, description),
Q_ARG(QVariant, previousWasBad)
UTPassphraseProvider::instance().m_window, "callPassphraseDialog",
Q_ARG(QVariant, "useridHint"), // TODO
Q_ARG(QVariant, "description"), // TODO
Q_ARG(QVariant, "previousWasBad") // TODO
);
qDebug() << "Waiting for response";
qDebug() << "[UTPassphraseProvider] Waiting for response";
QEventLoop loop;
QObject::connect(this, &UTPassphraseProvider::unlockEventLoop, &loop, &QEventLoop::quit);
QObject::connect(&UTPassphraseProvider::instance(), &UTPassphraseProvider::unlockEventLoop, &loop, &QEventLoop::quit);
loop.exec();
qDebug() << "Prepare Returns";
char *ret;
gpgrt_asprintf(&ret, "%s", m_passphrase);
canceled = this->m_canceled;
qDebug() << "[UTPassphraseProvider] Prepare Returns";
auto ret = false;
if (!UTPassphraseProvider::instance().m_canceled) {
strncpy(buf, UTPassphraseProvider::instance().m_passphrase.toLocal8Bit().data(), buf_len);
ret = true;
};
qDebug() << "Clean";
if (this->m_passphrase)
{
free(m_passphrase);
}
this->m_canceled = false;
this->m_sem->release(1);
qDebug() << "[UTPassphraseProvider] Clean Up";
UTPassphraseProvider::instance().m_passphrase = QString::Null();
UTPassphraseProvider::instance().m_canceled = true;
UTPassphraseProvider::instance().m_sem->release(1);
return ret;
};
}
/**
* @brief Sets the window object that triggers the passphrase dialog.
*
* This method allows the passphrase provider to know which window should invoke the QML dialog.
*
* @param window The window object to set.
*/
void setWindow(QObject* window)
{
this->m_window = window;
}
};
#endif
#endif // UTPASSPHRASEPROVIDER_H

View File

@ -5,6 +5,7 @@ set(
SRC
plugin.cpp
utils.cpp
jobs/unzipjob.cpp
)
set(CMAKE_AUTOMOC ON)

View File

@ -0,0 +1,56 @@
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <quazip5/JlCompress.h>
#include "qdebug.h"
#include "unzipjob.h"
UnzipJob::UnzipJob(QUrl zip_url, QDir dir_out):
m_zip_url(zip_url),
m_dir_out(dir_out)
{
this->setObjectName("UnzipJob");
}
void UnzipJob::run()
{
auto tmp_dir_path = QStandardPaths::writableLocation(
QStandardPaths::CacheLocation).append("/unzip");
QDir tmp_dir(tmp_dir_path);
tmp_dir.removeRecursively();
tmp_dir.mkpath(".");
qDebug() << "Temp dir path is " << tmp_dir_path;
auto status = !JlCompress::extractDir(
this->m_zip_url.toLocalFile(),
tmp_dir_path
).isEmpty();
if (!status) {
tmp_dir.removeRecursively();
emit resultReady(false);
return;
}
qDebug() << "Guessing if it should remove a single root folder";
QStringList files_in_tmp_dir = tmp_dir.entryList(QDir::AllEntries | QDir::Hidden |
QDir::NoDotAndDotDot);
auto dir_import_path =
files_in_tmp_dir.length() == 1 ?
tmp_dir_path.append("/" + files_in_tmp_dir.first()) : tmp_dir_path;
qDebug() << "Final imported tmp path dir is " << dir_import_path;
qDebug() << "Removing destination";
this->m_dir_out.removeRecursively();
qDebug() << "Moving zip content to destination";
QDir dir;
qDebug() << dir_import_path << " to " << this->m_dir_out;
auto ret = dir.rename(dir_import_path, this->m_dir_out.absolutePath());
tmp_dir.removeRecursively();;
emit resultReady(!ret);
}

View File

@ -0,0 +1,49 @@
#ifndef RMJOB_H
#define RMJOB_H
#include "qurl.h"
#include <QThread>
#include <QDir>
/**
* @class RmJob
* @brief A class to handle removing recursively a path in a separate thread.
*
*/
class UnzipJob : public QThread
{
Q_OBJECT
/**
* @brief The main function that performs the unzip operation.
*
* Handles the process of unziping a archive to a target directory.
*/
void run() override;
signals:
/**
* @brief Signal emitted when the unzip operation is complete.
*
* @param err A boolean indicating whether an error occurred during unzipping.
* `true` if an error occurred, `false` if the clone was successful.
*/
void resultReady(const bool err);
private:
QUrl m_zip_url; ///< The url of the archive.
QDir m_dir_out; ///< The directory where the content of the archive will be unzip.
public:
/**
* @brief Constructor for the UnzipJob class.
*
* Initializes the UnzipJob with the specified target path to be removed.
*
* @param zip_url Url of the archive to be unzip.
* @param dir_out Target directory where the content of the archive must be extracted.
*/
UnzipJob(QUrl zip_url, QDir dir_out);
};
#endif // RMJOB_H

View File

@ -1,51 +1,49 @@
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <quazip5/JlCompress.h>
#include <QSemaphore>
#include "jobs/unzipjob.h"
#include "utils.h"
Utils::Utils():
m_sem(std::unique_ptr<QSemaphore>(new QSemaphore(1)))
{}
bool Utils::unzip(QUrl zip_url, QString dir_out_path)
{
auto tmp_dir_path = QStandardPaths::writableLocation(
QStandardPaths::CacheLocation).append("/unzip");
QDir tmp_dir(tmp_dir_path);
tmp_dir.removeRecursively();
tmp_dir.mkpath(".");
qDebug() << "Temp dir path is " << tmp_dir_path;
auto status = !JlCompress::extractDir(
zip_url.toLocalFile(),
tmp_dir_path
).isEmpty();
if (!status) {
tmp_dir.removeRecursively();
if (!this->m_sem->tryAcquire(1, 500)) {
return false;
}
qInfo() << "Unzip path " << zip_url << " to " << dir_out_path;
auto job = new UnzipJob(zip_url, QDir(dir_out_path));
connect(job, &UnzipJob::resultReady, this, &Utils::unzipResult);
connect(job, &UnzipJob::finished, job, &QObject::deleteLater);
job->start();
return true;
}
qDebug() << "Guessing if it should remove a single root folder";
QStringList files_in_tmp_dir = tmp_dir.entryList(QDir::AllEntries | QDir::Hidden |
QDir::NoDotAndDotDot);
void Utils::unzipResult(bool err)
{
auto dir_import_path =
files_in_tmp_dir.length() == 1 ?
tmp_dir_path.append("/" + files_in_tmp_dir.first()) : tmp_dir_path;
qDebug() << "Final imported tmp path dir is " << dir_import_path;
qDebug() << "Unzip Result";
if (err) {
qInfo() << "Unzip Failed";
emit unzipFailed();
qDebug() << "Removing destination";
QDir dir_out(dir_out_path);
dir_out.removeRecursively();
} else {
qInfo() << "Unzip Succeed";
emit unzipSucceed();
}
this->m_sem->release(1);
}
qDebug() << "Moving zip content to destination";
QDir dir;
qDebug() << dir_import_path << " to " << dir_out_path;
auto ret = dir.rename(dir_import_path, dir_out_path);
tmp_dir.removeRecursively();;
return ret;
QString Utils::manifestPath()
{
auto path = QDir(QDir::currentPath()).filePath("manifest_.json");
qInfo() << "Manifest path : " << path;
return path;
}
bool Utils::rmFile(QUrl file_url)
@ -58,10 +56,3 @@ bool Utils::rmDir(QUrl dir_url)
QDir dir(dir_url.toLocalFile());
return dir.removeRecursively();
}
QString Utils::manifestPath()
{
auto path = QDir(QDir::currentPath()).filePath("manifest_.json");
qDebug() << "Manifest path : " << path;
return path;
}

View File

@ -4,6 +4,8 @@
#include <QObject>
#include <QUrl>
#include <QQuickWindow>
#include <memory>
#include <QSemaphore>
/**
* @class Utils
@ -16,50 +18,43 @@ class Utils : public QObject
{
Q_OBJECT
private slots:
/**
* @brief Slot to handle the result of a unzip operation.
* @param err True if an error occurred during the operation.
*/
void unzipResult(bool err);
signals:
/**
* @brief Emitted when the archive is successfully extracted.
*/
void unzipSucceed();
/**
* @brief Emitted when the unzipping operation fails.
*/
void unzipFailed();
private:
std::unique_ptr<QSemaphore> m_sem; /**< Semaphore for managing concurrent operations. */
public:
/**
* @brief Default constructor for the Utils class.
* @brief Constructor for the Utils class.
*/
Utils() = default;
Utils();
/**
* @brief Default destructor for the Utils class.
*/
~Utils() override = default;
/**
* @brief Unzips a ZIP file to the specified output directory.
*
* This method extracts the contents of a ZIP file from the specified URL and saves them to the provided
* output directory path.
* @brief Start a job to unzips a ZIP file to the specified output directory.
*
* @param zip_url The URL of the ZIP file to unzip.
* @param dir_out The output directory where the contents of the ZIP file should be extracted.
* @return `true` if the unzipping operation was successful, `false` otherwise.
* @return `true` if the unzipping job is started successfullly, `false` otherwise.
*/
Q_INVOKABLE bool unzip(QUrl zip_url, QString dir_out);
/**
* @brief Removes a file at the specified URL.
*
* This method deletes a file at the specified URL.
*
* @param file_url The URL of the file to delete.
* @return `true` if the file was successfully removed, `false` otherwise.
*/
Q_INVOKABLE bool rmFile(QUrl file_url);
/**
* @brief Removes a directory at the specified URL.
*
* This method deletes a directory at the specified URL, along with its contents.
*
* @param dir_url The URL of the directory to remove.
* @return `true` if the directory was successfully removed, `false` otherwise.
*/
Q_INVOKABLE bool rmDir(QUrl dir_url);
/**
* @brief Retrieves the path to the manifest data.
*
@ -69,6 +64,22 @@ public:
*/
Q_INVOKABLE QString manifestPath();
/**
* @brief Removes a file located at the specified URL.
*
* @param file_url The URL of the file to remove.
* @return `true` if the file was successfully removed; `false` otherwise.
*/
Q_INVOKABLE bool rmFile(QUrl file_url);
/**
* @brief Removes a directory located at the specified URL.
*
* @param dir_url The URL of the directory to remove.
* @return `true` if the directory was successfully removed; `false` otherwise.
*/
Q_INVOKABLE bool rmDir(QUrl dir_url);
};
#endif

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: utpass.qrouland\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-20 15:00+0100\n"
"POT-Creation-Date: 2025-02-04 17:49+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -38,12 +38,12 @@ msgid "passphrase"
msgstr ""
#: ../qml/dialogs/PassphraseDialog.qml:29
#: ../qml/dialogs/SimpleValidationDialog.qml:9
#: ../qml/dialogs/SimpleValidationDialog.qml:8
msgid "Ok"
msgstr ""
#: ../qml/dialogs/PassphraseDialog.qml:41
#: ../qml/dialogs/SimpleValidationDialog.qml:34
#: ../qml/dialogs/SimpleValidationDialog.qml:28
msgid "Cancel"
msgstr ""
@ -79,27 +79,27 @@ msgstr ""
msgid "Info"
msgstr ""
#: ../qml/pages/PasswordList.qml:44
#: ../qml/pages/PasswordList.qml:45
msgid "No password found"
msgstr ""
#: ../qml/pages/PasswordList.qml:55
#: ../qml/pages/PasswordList.qml:58
msgid "You can import a password store by cloning or"
msgstr ""
#: ../qml/pages/PasswordList.qml:61
#: ../qml/pages/PasswordList.qml:65
msgid "importing a password store zip in the settings"
msgstr ""
#: ../qml/pages/PasswordList.qml:95
#: ../qml/pages/PasswordList.qml:100
msgid "Decryption failed !"
msgstr ""
#: ../qml/pages/PasswordList.qml:109
#: ../qml/pages/PasswordList.qml:114
msgid "Back"
msgstr ""
#: ../qml/pages/PasswordList.qml:116 ../qml/pages/headers/MainHeader.qml:9
#: ../qml/pages/PasswordList.qml:121 ../qml/pages/headers/MainHeader.qml:9
#: ../qml/pages/headers/StackHeader.qml:9 UTPass.desktop.in.h:1
msgid "UTPass"
msgstr ""
@ -112,90 +112,94 @@ msgstr ""
msgid "Search"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:41
#: ../qml/pages/settings/DeleteRepo.qml:42
#: ../qml/pages/settings/Settings.qml:58
msgid "Delete Password Store"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:54
#: ../qml/pages/settings/DeleteRepo.qml:55
msgid "You're are about to delete<br>the current Password Store.<br>Continue ?"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:55
#: ../qml/pages/settings/ImportZip.qml:62
#: ../qml/pages/settings/InfoKeys.qml:140
#: ../qml/pages/settings/git/ImportGitClone.qml:55
#: ../qml/pages/settings/DeleteRepo.qml:56
#: ../qml/pages/settings/ImportZip.qml:66
#: ../qml/pages/settings/InfoKeys.qml:174
#: ../qml/pages/settings/git/ImportGitClone.qml:56
msgid "Yes"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:68
#: ../qml/pages/settings/DeleteRepo.qml:69
msgid "Password Store removal failed !"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:77
#: ../qml/pages/settings/DeleteRepo.qml:78
msgid "Password Store deleted !"
msgstr ""
#: ../qml/pages/settings/DeleteRepo.qml:89
#: ../qml/pages/settings/InfoKeys.qml:182
#: ../qml/pages/settings/DeleteRepo.qml:90
#: ../qml/pages/settings/InfoKeys.qml:216
msgid "Info Keys"
msgstr ""
#: ../qml/pages/settings/ImportKeyFile.qml:57
#: ../qml/pages/settings/ImportKeyFile.qml:61
msgid "Key import failed !"
msgstr ""
#: ../qml/pages/settings/ImportKeyFile.qml:66
#: ../qml/pages/settings/ImportKeyFile.qml:70
msgid "Key successfully imported !"
msgstr ""
#: ../qml/pages/settings/ImportKeyFile.qml:77
#: ../qml/pages/settings/ImportKeyFile.qml:81
msgid "GPG Key Import"
msgstr ""
#: ../qml/pages/settings/ImportZip.qml:61
#: ../qml/pages/settings/ImportZip.qml:65
msgid ""
"Importing a new zip will delete<br>any existing password store!<br>Continue ?"
msgstr ""
#: ../qml/pages/settings/ImportZip.qml:75
#: ../qml/pages/settings/ImportZip.qml:79
msgid "Password store import failed !"
msgstr ""
#: ../qml/pages/settings/ImportZip.qml:84
#: ../qml/pages/settings/git/ImportGitClone.qml:77
#: ../qml/pages/settings/ImportZip.qml:88
#: ../qml/pages/settings/git/ImportGitClone.qml:78
msgid "Password store sucessfully imported !"
msgstr ""
#: ../qml/pages/settings/ImportZip.qml:96
#: ../qml/pages/settings/ImportZip.qml:100
msgid "Zip Password Store Import"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:56
#: ../qml/pages/settings/InfoKeys.qml:47
msgid "No key found"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:83
msgid "Key ID :"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:90
msgid "Users IDs : "
#: ../qml/pages/settings/InfoKeys.qml:124
msgid "User IDs : "
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:117
#: ../qml/pages/settings/InfoKeys.qml:151
msgid "Delete this key"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:139
#: ../qml/pages/settings/InfoKeys.qml:173
msgid "You're are about to delete<br>%1.<br>Continue ?"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:153
#: ../qml/pages/settings/InfoKeys.qml:187
msgid "Key removal failed !"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:162
#: ../qml/pages/settings/InfoKeys.qml:196
msgid "Key successfully deleted !"
msgstr ""
#: ../qml/pages/settings/InfoKeys.qml:174
#: ../qml/pages/settings/InfoKeys.qml:208
msgid "An Error occured getting GPG keys !"
msgstr ""
@ -232,25 +236,26 @@ msgstr ""
msgid "Repo Url"
msgstr ""
#: ../qml/pages/settings/git/GitCloneHttp.qml:32
#: ../qml/pages/settings/git/GitCloneHttpAuth.qml:50
#: ../qml/pages/settings/git/GitCloneHttp.qml:40
#: ../qml/pages/settings/git/GitCloneHttpAuth.qml:60
msgid "Clone"
msgstr ""
#: ../qml/pages/settings/git/GitCloneHttpAuth.qml:33
#: ../qml/pages/settings/git/GitCloneHttpAuth.qml:35
#: ../qml/pages/settings/git/GitCloneHttpAuth.qml:46
msgid "Password"
msgstr ""
#: ../qml/pages/settings/git/ImportGitClone.qml:54
#: ../qml/pages/settings/git/ImportGitClone.qml:55
msgid ""
"Importing a git repo will delete<br>any existing password store!"
"<br>Continue ?"
msgstr ""
#: ../qml/pages/settings/git/ImportGitClone.qml:68
#: ../qml/pages/settings/git/ImportGitClone.qml:69
msgid "An error occured during git clone !"
msgstr ""
#: ../qml/pages/settings/git/ImportGitClone.qml:89
#: ../qml/pages/settings/git/ImportGitClone.qml:90
msgid "Git Clone Import"
msgstr ""

View File

@ -19,12 +19,14 @@ Item {
Rectangle {
anchors.fill: parent
color: theme.palette.normal.background
Text {
text: copyText.text
anchors.left: parent.left
anchors.leftMargin: units.gu(2)
anchors.verticalCenter: parent.verticalCenter
color: theme.palette.normal.backgroundText
}
Icon {

View File

@ -14,7 +14,7 @@ Component {
color: theme.palette.normal.background
Text {
text: fileBaseName
text: fileIsDir ? fileName : fileName.slice(0, -4) // remove .gpg if it's a file
anchors.left: parent.left
anchors.leftMargin: units.gu(2)
anchors.verticalCenter: parent.verticalCenter

View File

@ -5,18 +5,12 @@ import QtQuick 2.4
Dialog {
id: dialog
property string text
property string continueText: i18n.tr("Ok")
property color continueColor: theme.palette.normal.positive
signal validated()
signal canceled()
Text {
horizontalAlignment: Text.AlignHCenter
text: dialog.text
}
Button {
id: continueButton

View File

@ -26,6 +26,7 @@ Page {
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.left: parent.left
color: theme.palette.normal.background
Flow {
id: container

View File

@ -38,29 +38,34 @@ Page {
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Text {
text: i18n.tr("No password found")
width: parent.width
horizontalAlignment: Text.AlignHCenter
color: theme.palette.normal.backgroundText
}
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Text {
text: i18n.tr("You can import a password store by cloning or")
width: parent.width
horizontalAlignment: Text.AlignHCenter
color: theme.palette.normal.backgroundText
}
Text {
text: i18n.tr("importing a password store zip in the settings")
width: parent.width
horizontalAlignment: Text.AlignHCenter
color: theme.palette.normal.backgroundText
}
}

View File

@ -32,6 +32,7 @@ Page {
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Button {
@ -76,8 +77,8 @@ Page {
SuccessDialog {
textSuccess: i18n.tr("Password Store deleted !")
onDialogClosed: {
pageStack.pop();
pageStack.pop();
pageStack.clear();
pageStack.push(Qt.resolvedUrl("../PasswordList.qml"));
}
}

View File

@ -29,12 +29,16 @@ Page {
console.log("Charged");
console.log(importKeyFilePage.activeTransfer.items[0].url);
var status = Pass.importGPGKey(importKeyFilePage.activeTransfer.items[0].url);
Pass.importGPGKeySucceed.connect(function() {
Utils.rmFile(importKeyFilePage.activeTransfer.items[0].url);
if (status)
PopupUtils.open(dialogImportKeyPageSucess);
else
PopupUtils.open(dialogImportKeyPageError);
importKeyFilePage.activeTransfer = null;
PopupUtils.open(dialogImportKeyPageSucess);
});
Pass.importGPGKeyFailed.connect(function(message) {
Utils.rmFile(importKeyFilePage.activeTransfer.items[0].url);
importKeyFilePage.activeTransfer = null;
PopupUtils.open(dialogImportKeyPageError);
});
}
});
}

View File

@ -32,13 +32,17 @@ Page {
if (importZipPage.activeTransfer.state === ContentTransfer.Charged) {
console.log("Charged");
console.log(importZipPage.activeTransfer.items[0].url);
var status = Utils.unzip(importZipPage.activeTransfer.items[0].url, Pass.getPasswordStore());
var status = Utils.unzip(importZipPage.activeTransfer.items[0].url, Pass.password_store);
Utils.unzipSucceed.connect(function() {
Utils.rmFile(importZipPage.activeTransfer.items[0].url);
if (status)
PopupUtils.open(dialogImportZipPageSuccess);
else
PopupUtils.open(dialogImportZipPageError);
importZipPage.activeTransfer = null;
PopupUtils.open(dialogImportZipPageSuccess);
});
Utils.unzipFailed.connect(function() {
Utils.rmFile(importZipPage.activeTransfer.items[0].url);
importZipPage.activeTransfer = null;
PopupUtils.open(dialogImportZipPageError);
});
}
});
}
@ -83,8 +87,8 @@ Page {
SuccessDialog {
textSuccess: i18n.tr("Password store sucessfully imported !")
onDialogClosed: {
pageStack.pop();
pageStack.pop();
pageStack.clear();
pageStack.push(Qt.resolvedUrl("../PasswordList.qml"));
}
}

View File

@ -9,11 +9,12 @@ import QtQuick 2.4
Page {
id: infoKeysPage
property QtObject currentKey
property list<QtObject> __keys
property QtObject __currentKey
Component.onCompleted: {
Pass.getAllGPGKeysSucceed.connect(function(keys_info) {
infoKeysListView.model = keys_info;
infoKeysPage.__keys = keys_info.keys;
});
Pass.getAllGPGKeysFailed.connect(function(message) {
PopupUtils.open(infoKeysPageGetAllError);
@ -27,6 +28,30 @@ Page {
Pass.getAllGPGKeys();
}
Column {
anchors.top: infoKeysHeader.bottom
anchors.bottom: parent.bottom
anchors.right: parent.right
anchors.left: parent.left
anchors.leftMargin: units.gu(2)
anchors.rightMargin: units.gu(2)
visible: infoKeysPage.__keys.length === 0
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Text {
text: i18n.tr("No key found")
width: parent.width
horizontalAlignment: Text.AlignHCenter
color: theme.palette.normal.backgroundText
}
}
ListView {
id: infoKeysListView
@ -36,6 +61,8 @@ Page {
anchors.left: parent.left
anchors.leftMargin: units.gu(2)
anchors.rightMargin: units.gu(2)
visible: infoKeysPage.__keys.length !== 0
model: infoKeysPage.__keys
delegate: Grid {
columns: 1
@ -61,7 +88,12 @@ Page {
width: parent.width
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: model.modelData.uid
text: {
if (!model.modelData)
"";
else
model.modelData.keyid;
}
color: theme.palette.normal.backgroundText
}
@ -75,19 +107,21 @@ Page {
id: userIdsModel
Component.onCompleted: {
for (var i = 0; i < model.modelData.userIds.length; ++i) {
if (model.modelData) {
for (var i = 0; i < model.modelData.userids.length; ++i) {
userIdsModel.append({
"model": model.modelData.userIds[i]
"model": model.modelData.userids[i]
});
}
}
}
}
Text {
width: parent.width
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: i18n.tr('Users IDs : ')
text: i18n.tr('User IDs : ')
color: theme.palette.normal.backgroundText
}
@ -98,7 +132,7 @@ Page {
width: parent.width
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
text: modelData.uid
text: modelData
color: theme.palette.normal.backgroundText
}
@ -117,7 +151,7 @@ Page {
text: i18n.tr("Delete this key")
color: theme.palette.normal.negative
onClicked: {
infoKeysPage.currentKey = model.modelData;
infoKeysPage.__currentKey = model.modelData;
PopupUtils.open(infoKeysPageDeleteValidation, infoKeysPage);
}
}
@ -136,11 +170,11 @@ Page {
id: infoKeysPageDeleteValidation
SimpleValidationDialog {
text: i18n.tr("You're are about to delete<br>%1.<br>Continue ?").arg(infoKeysPage.currentKey.uid)
text: i18n.tr("You're are about to delete<br>%1.<br>Continue ?").arg(infoKeysPage.__currentKey.keyid)
continueText: i18n.tr("Yes")
continueColor: theme.palette.normal.negative
onValidated: {
var status = Pass.deleteGPGKey(infoKeysPage.currentKey);
var status = Pass.deleteGPGKey(infoKeysPage.__currentKey);
}
}

View File

@ -63,7 +63,7 @@ Page {
verticalAlignment: Text.AlignVCenter
width: parent.width
height: units.gu(4)
color: LomiriColors.red
color: theme.palette.normal.negative
text: i18n.tr('Warning: importing delete any exiting Password Store')
}

View File

@ -14,6 +14,7 @@ Column {
verticalAlignment: Text.AlignVCenter
width: parent.width
text: i18n.tr('Repo Url')
color: theme.palette.normal.backgroundText
}
TextField {
@ -22,6 +23,13 @@ Column {
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
width: parent.width
placeholderText: "http(s)://<hostname>"
}
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Button {

View File

@ -14,6 +14,7 @@ Column {
verticalAlignment: Text.AlignVCenter
width: parent.width
text: i18n.tr('Repo Url')
color: theme.palette.normal.backgroundText
}
TextField {
@ -22,6 +23,7 @@ Column {
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
width: parent.width
placeholderText: "http(s)://<username>@<hostname>"
}
Text {
@ -31,6 +33,7 @@ Column {
verticalAlignment: Text.AlignVCenter
width: parent.width
text: i18n.tr('Password')
color: theme.palette.normal.backgroundText
}
TextField {
@ -40,6 +43,13 @@ Column {
verticalAlignment: Text.AlignVCenter
width: parent.width
echoMode: TextInput.Password
placeholderText: i18n.tr('Password')
}
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
Button {

View File

@ -33,6 +33,7 @@ Page {
Rectangle {
width: parent.width
height: units.gu(1)
color: theme.palette.normal.background
}
GitModeOptionSelector {
@ -76,8 +77,8 @@ Page {
SuccessDialog {
textSuccess: i18n.tr("Password store sucessfully imported !")
onDialogClosed: {
pageStack.pop();
pageStack.pop();
pageStack.clear();
pageStack.push(Qt.resolvedUrl("../../PasswordList.qml"));
}
}

20
tests.in.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <QGuiApplication>
#include <QCoreApplication>
#include <QUrl>
#include <QString>
#include <QQuickView>
#include <QtQml>
#include <QtQuickTest/quicktest.h>
int main(int argc, char *argv[])
{
qDebug() << "Starting app from tests.cpp";
new QGuiApplication(argc, argv);
QGuiApplication::setApplicationName("utpass.qrouland");
return quick_test_main(argc, argv, @TESTS_PATH@, @TESTS_PATH@);
}

View File

@ -0,0 +1 @@
SomePassword

View File

@ -0,0 +1 @@
<EFBFBD>^<03><><EFBFBD><EFBFBD><16>8m@<40><>z<>,fL<66><><D58E>j<EFBFBD><1C><>'<12><>!<21>[Pfi0<69>p<EFBFBD><70>:<3A><02>0C<30>9e;<13><><EFBFBD>/<2F><><EFBFBD><04>c<EFBFBD>;Y<>\<5C><><EFBFBD><EFBFBD><EFBFBD>L<EFBFBD>Gw<>H<EFBFBD><48>` <10>Pt.E8<45><38><EFBFBD>y<EFBFBD><79>[z[m<><6D><EFBFBD><EFBFBD><EFBFBD> <20><03>O<EFBFBD><4F><EFBFBD>XkL<6B><4C>c<EFBFBD><63><EFBFBD><1C>pC<70>+<2B><>$<24><>I<EFBFBD>I{<7B>H<EFBFBD>v<EFBFBD><76><EFBFBD>0<10>]<5D>r<EFBFBD><72>H<EFBFBD><1A><08>l/<2F>!l<><03><><EFBFBD><EFBFBD><EFBFBD>0

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -5,6 +5,7 @@ set(
SRC
plugin.cpp
utils.cpp
passphraseprovider.h
)
set(CMAKE_AUTOMOC ON)
@ -22,6 +23,31 @@ endif()
add_library(${PLUGIN} MODULE ${SRC})
set_target_properties(${PLUGIN} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGIN})
qt5_use_modules(${PLUGIN} Qml Quick DBus)
set(RNP_BUILD_DIR "${CMAKE_SOURCE_DIR}/build/${ARCH_TRIPLET}/rnp/install")
INCLUDE_DIRECTORIES(${RNP_BUILD_DIR}/include)
add_library(rnp STATIC IMPORTED)
set_property(TARGET rnp PROPERTY IMPORTED_LOCATION "${RNP_BUILD_DIR}/lib/librnp.a")
add_library(gpgerror SHARED IMPORTED)
set_property(TARGET gpgerror PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpg-error.so.0.28.0")
add_library(libassuan SHARED IMPORTED)
set_property(TARGET libassuan PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libassuan.so")
add_library(libgpgme SHARED IMPORTED)
set_property(TARGET libgpgme PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpgme.so")
add_library(libgpgmepp SHARED IMPORTED)
set_property(TARGET libgpgmepp PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libgpgmepp.so")
add_library(libqgpgme SHARED IMPORTED)
set_property(TARGET libqgpgme PROPERTY IMPORTED_LOCATION "/usr/lib/${ARCH_TRIPLET}/libqgpgme.so")
target_link_libraries(${PLUGIN} rnp gpgerror libassuan libgpgme libgpgmepp libqgpgme)
set(QT_IMPORTS_DIR "/lib/${ARCH_TRIPLET}")

View File

@ -0,0 +1,41 @@
#ifndef UTPASSPHRASEPROVIDER_H
#define UTPASSPHRASEPROVIDER_H
#include <QObject>
#include <gpg-error.h>
extern "C" {
#include <rnp/rnp.h>
}
class TesTPassphraseProvider : public QObject
{
Q_OBJECT
private:
explicit TesTPassphraseProvider(QObject * parent = nullptr)
{}
public:
~TesTPassphraseProvider() = default;
static TesTPassphraseProvider &instance()
{
static TesTPassphraseProvider instance;
return instance;
}
TesTPassphraseProvider(TesTPassphraseProvider const &) = delete;
void operator=(TesTPassphraseProvider const &) = delete;
static bool
example_pass_provider(rnp_ffi_t ffi,
void *app_ctx,
rnp_key_handle_t key,
const char *pgp_context,
char buf[],
size_t buf_len)
{
strncpy(buf, "utpasspassphrase", buf_len);
return true;
}
};
#endif

View File

@ -6,5 +6,5 @@
void TestsUtilsPlugin::registerTypes(const char *uri)
{
//@uri TestUtils
qmlRegisterSingletonType<TestsUtilsPlugin>(uri, 1, 0, "TestUtils", [](QQmlEngine *, QJSEngine *) -> QObject * { return new TestsUtils; });
qmlRegisterSingletonType<TestsUtilsPlugin>(uri, 1, 0, "TestsUtils", [](QQmlEngine *, QJSEngine *) -> QObject * { return new TestsUtils; });
}

View File

@ -1,2 +1,2 @@
module TestUtils
plugin TestUtils
module TestsUtils
plugin TestsUtils

View File

@ -3,17 +3,21 @@
#include <QUrl>
#include <QUuid>
#include <QtCore/QStandardPaths>
#include <memory>
#include <quazip5/JlCompress.h>
#include "passphraseprovider.h"
#include "utils.h"
TestsUtils::TestsUtils():
m_passphrase_povider(std::unique_ptr<TesTPassphraseProvider>(new TesTPassphraseProvider()))
{}
QString TestsUtils::getTempPath()
{
qFatal("yp");
// Get the system's temporary directory
QString tempDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
qDebug() << "TempDir : " << tempDir;
QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
// Generate a unique UUID
QString uuid = QUuid::createUuid().toString(QUuid::WithoutBraces);
@ -22,17 +26,51 @@ QString TestsUtils::getTempPath()
QString newTempDir = tempDir + "/" + uuid;
QDir dir;
if (!dir.exists(newTempDir)) {
// Create the directory
if (dir.mkpath(newTempDir)) {
return newTempDir; // Return the path if successful
} else {
return "Failed to create directory"; // Return an error message
dir.mkpath(newTempDir);
qDebug() << "[TestUtils] TempDir : " << newTempDir;
return newTempDir;
}
} else {
return newTempDir; // If the directory already exists, return its path
bool TestsUtils::fileExists(QUrl path)
{
QString p = path.toLocalFile();
auto ret = QFileInfo::exists(p) && QFileInfo(p).isFile();
qDebug() << "[TestUtils]" << p << "is existing file :" << ret;
return ret;
}
void TestsUtils::copyFolder(QUrl sourceFolderUrl, QUrl destFolderUrl)
{
auto sourceFolder = sourceFolderUrl.toLocalFile();
auto destFolder = destFolderUrl.toLocalFile();
QDir sourceDir(sourceFolder);
if (!sourceDir.exists())
return;
QDir destDir(destFolder);
if (!destDir.exists()) {
destDir.mkdir(destFolder);
}
qDebug() << "[TestUtils]" << "Copy files from" << sourceFolder << "to" << destFolder;
QStringList files = sourceDir.entryList(QDir::Files);
for (int i = 0; i < files.count(); i++) {
QString srcName = sourceFolder + "/" + files[i];
QString destName = destFolder + "/" + files[i];
QFile::copy(srcName, destName);
qDebug() << "[TestUtils]" << "Copy file from" << srcName << "to" << destName;
}
files.clear();
files = sourceDir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
for (int i = 0; i < files.count(); i++) {
QString srcName = sourceFolder + "/" + files[i];
QString destName = destFolder + "/" + files[i];
this->copyFolder(srcName, destName);
}
}
QObject *TestsUtils::getTestPassphraseProvider()
{
return &TesTPassphraseProvider::instance();
}

View File

@ -1,19 +1,25 @@
#ifndef TESTSUTILS_H
#define TESTSUTILS_H
#include "passphraseprovider.h"
#include <QObject>
#include <QUrl>
#include <QQuickWindow>
#include <memory>
class TestsUtils : public QObject
{
Q_OBJECT
public:
TestsUtils() = default;
TestsUtils();
~TestsUtils() override = default;
Q_INVOKABLE QString getTempPath();
Q_INVOKABLE bool fileExists(QUrl path);
Q_INVOKABLE void copyFolder(QUrl sourceFolder, QUrl destFolder);
Q_INVOKABLE QObject *getTestPassphraseProvider();
};
#endif

View File

@ -1,11 +0,0 @@
import Git 1.0
import QtQuick 2.9
import QtTest 1.2
TestCase {
function test_git_clone() {
verify(Git.clone("", ""));
}
name: "git"
}

View File

@ -1,11 +0,0 @@
import Git 1.0
import QtQuick 2.9
import QtTest 1.2
TestCase {
function test_git_clone() {
verify(Git.clone("", ""));
}
name: "git"
}

View File

@ -0,0 +1,19 @@
import Pass 1.0
import QtQuick 2.9
import QtTest 1.2
import TestsUtils 1.0
TestCase {
property string password_store
property string gpg_home
function init() {
Pass.initialize(null);
gpg_home = TestsUtils.getTempPath();
Pass.gpg_home = gpg_home;
password_store = TestsUtils.getTempPath();
Pass.password_store = password_store;
Pass.passphrase_provider = TestsUtils.getTestPassphraseProvider();
}
}

View File

@ -0,0 +1,63 @@
import Pass 1.0
import QtQuick 2.9
import QtTest 1.2
import TestsUtils 1.0
PassTestCase {
//TODO some additionanl error test
function init_data() {
return [{
"spy": getAllGPGKeysSucceed,
"err_msg": null,
"add_home_gpg_data": false,
"keys": []
}, {
"spy": getAllGPGKeysSucceed,
"err_msg": null,
"add_home_gpg_data": true,
"keys": [{
"fingerprint": "F97476B6FA58A84B004E4616D4BAF1FDB7BA9ECC",
"keyid": "D4BAF1FDB7BA9ECC",
"userids": "UTPass Test <utpass@test.org>",
"hasSecret": true
}]
}];
}
function test_get_keys(data) {
if (data.add_home_gpg_data === true)
TestsUtils.copyFolder(Qt.resolvedUrl("../../assets/gpghome"), Qt.resolvedUrl(gpg_home));
var keys;
Pass.getAllGPGKeysSucceed.connect(function(keys_info) {
keys = keys_info;
});
Pass.getAllGPGKeys();
data.spy.wait();
verify(keys.length === data.keys.length, "Nb keys %1 but was excepted %2".arg(keys.length).arg(data.nb_keys));
for (var i = 0; i < keys.length; i++) {
console.info(keys.keys[i]);
console.info(keys.keys[i].keyid);
verify(keys.keys[i].fingerprint === data.keys[i].fingerprint, "fingerprint is %1 but was excepted %2".arg(keys.keys[i].fingerprint).arg(data.keys[i].fingerprint));
verify(keys.keys[i].keyid === data.keys[i].keyid, "keyid is %1 but was excepted %2".arg(keys.keys[i].keyid).arg(data.keys[i].keyid));
verify(keys.keys[i].userids[0] === data.keys[i].userids, "userids is %1 but was excepted %2".arg(keys.keys[i].userids[0]).arg(data.keys[i].userids));
verify(keys.keys[i].hasSecret === data.keys[i].hasSecret, "hasSecret is %1 but was excepted %2".arg(keys.keys[i].hasSecret).arg(data.keys[i].hasSecret));
}
}
SignalSpy {
id: getAllGPGKeysSucceed
target: Pass
signalName: "getAllGPGKeysSucceed"
}
SignalSpy {
id: getAllGPGKeysFailed
target: Pass
signalName: "getAllGPGKeysFailed"
}
}

View File

@ -0,0 +1,53 @@
import Pass 1.0
import QtQuick 2.9
import QtTest 1.2
import TestsUtils 1.0
PassTestCase {
function init_data() {
return [{
"file": Qt.resolvedUrl("../../assets/gpg/test_key.gpg"),
"spy": importGPGKeySucceed,
"err_msg": null
}, {
"file": Qt.resolvedUrl("../../assets/gpg/test_key_do_not_exist.gpg"),
"spy": importGPGKeyFailed,
"err_msg": "Error reading file"
}, {
"file": Qt.resolvedUrl("../../assets/gpg/test_key_invalid.gpg"),
"spy": importGPGKeyFailed,
"err_msg": "Bad state"
}];
}
function test_import_key(data) {
var err_msg;
Pass.importGPGKeyFailed.connect(function(message) {
err_msg = message;
});
Pass.importGPGKey(data.file);
data.spy.wait();
if (data.err_msg) {
verify(err_msg === data.err_msg, "Should return %1 but return %2".arg(data.err_msg).arg(err_msg));
} else {
console.info(Qt.resolvedUrl("%1/pubkeyring.pgp".arg(gpg_home)));
verify(TestsUtils.fileExists(Qt.resolvedUrl("%1/pubring.pgp".arg(gpg_home))), "%1/pubring.pgp should be created".arg(gpg_home));
verify(TestsUtils.fileExists(Qt.resolvedUrl("%1/secring.pgp".arg(gpg_home))), "%1/secring.pgp should be created".arg(gpg_home));
}
}
SignalSpy {
id: importGPGKeySucceed
target: Pass
signalName: "importGPGKeySucceed"
}
SignalSpy {
id: importGPGKeyFailed
target: Pass
signalName: "importGPGKeyFailed"
}
}

View File

@ -0,0 +1,58 @@
import Pass 1.0
import QtQuick 2.9
import QtTest 1.2
import TestsUtils 1.0
PassTestCase {
//TODO some additionanl error test
function init_data() {
return [{
"spy": showFailed,
"err_msg": "Bad password",
"add_home_gpg_data": true,
"file": "../../assets/gpg/clear_text.txt.gpg"
}, {
"spy": showFailed,
"err_msg": "No suitable key",
"add_home_gpg_data": false,
"file": "../../assets/gpg/clear_text.txt.gpg"
}];
}
function test_pass_show(data) {
if (data.add_home_gpg_data === true)
TestsUtils.copyFolder(Qt.resolvedUrl("../../assets/gpghome"), Qt.resolvedUrl(gpg_home));
var fname, ctext;
Pass.showSucceed.connect(function(file_name, clear_text) {
fname = file_name;
ctext = clear_text;
});
var err_msg;
Pass.showFailed.connect(function(err) {
err_msg = err;
});
Pass.show(Qt.resolvedUrl(data.file));
data.spy.wait();
if (data.err_msg)
verify(err_msg === data.err_msg, "Should return %1 but return %2".arg(data.err_msg).arg(err_msg));
else
verify(false);
}
SignalSpy {
id: showSucceed
target: Pass
signalName: "showSucceed"
}
SignalSpy {
id: showFailed
target: Pass
signalName: "showFailed"
}
}

13
tests/units/tst_git.qml Normal file
View File

@ -0,0 +1,13 @@
import Pass 1.0
import QtQuick 2.9
import QtTest 1.2
TestCase {
function test_import_key() {
var homedir = TestUtils.getTempPath();
Pass;
verify(false);
}
name: "git"
}

View File

@ -1,6 +1,6 @@
import QtQuick 2.9
import QtTest 1.2
import TestUtils 1.0
import TestsUtils 1.0
import Utils 1.0
TestCase {