KWallet

kwalletportalsecrets.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kwalletportalsecrets.h"
9
10#include "kwalletportalsecretsadaptor.h"
11
12#include <QRandomGenerator>
13
14KWalletPortalSecrets::KWalletPortalSecrets(KWalletD *parent)
15 : QObject(parent)
16 , m_kwalletd(parent)
17{
18 (void)new KWalletPortalSecretsAdaptor(this);
19
20 QDBusConnection::sessionBus().registerService(QStringLiteral("org.freedesktop.impl.portal.desktop.kwallet"));
21 QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/freedesktop/portal/desktop"), this, QDBusConnection::ExportAdaptors);
22
23 connect(m_kwalletd, &KWalletD::walletAsyncOpened, this, &KWalletPortalSecrets::walletOpened);
24}
25
26uint KWalletPortalSecrets::RetrieveSecret(const QDBusObjectPath &handle,
27 const QString &app_id,
29 const QVariantMap &options,
30 QVariantMap &results)
31{
32 Q_UNUSED(handle);
33 Q_UNUSED(options);
34 Q_UNUSED(results);
35
36 setDelayedReply(true);
37
38 const QString wallet = m_kwalletd->networkWallet();
39
40 int transactionId = m_kwalletd->openAsync(wallet, 0, "xdg-desktop-portal", false, connection(), message());
41 Request request{message(), fd.fileDescriptor(), app_id};
42 m_pendingRequests.insert(transactionId, request);
43
44 return 0;
45}
46
47void KWalletPortalSecrets::walletOpened(int transactionId, int walletHandle)
48{
49 if (!m_pendingRequests.contains(transactionId)) {
50 // wallet open request was not from us
51 return;
52 }
53
54 const Request request = m_pendingRequests.take(transactionId);
55
56 bool exists = m_kwalletd->hasEntry(walletHandle, "xdg-desktop-portal", request.appId, "xdg-desktop-portal");
57
58 QByteArray secret;
59
60 if (exists) {
61 secret = m_kwalletd->readEntry(walletHandle, "xdg-desktop-portal", request.appId, "xdg-desktop-portal");
62 } else {
63 secret = generateSecret();
64 m_kwalletd->writeEntry(walletHandle, "xdg-desktop-portal", request.appId, secret, "xdg-desktop-portal");
65 m_kwalletd->sync(walletHandle, "xdg-desktop-portal");
66 }
67
68 QFile outFile;
69 outFile.open(request.fd, QIODevice::ReadWrite);
70
71 outFile.write(secret);
72
73 const auto replyList = QVariantList{{(uint)0}, {{QVariantMap{}}}};
74 auto reply = request.message.createReply(replyList);
76}
77
78QByteArray KWalletPortalSecrets::generateSecret() const
79{
80 const int secretSize = 64;
81 QByteArray secret;
82 secret.resize(secretSize);
84
85 return secret;
86}
iterator begin()
iterator end()
void resize(qsizetype newSize, char c)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
bool registerService(const QString &serviceName)
bool send(const QDBusMessage &message) const const
QDBusConnection sessionBus()
QDBusConnection connection() const const
const QDBusMessage & message() const const
void setDelayedReply(bool enable) const const
int fileDescriptor() const const
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
T take(const Key &key)
qint64 write(const QByteArray &data)
quint32 generate()
QRandomGenerator securelySeeded()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.