1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2009 Michael Leupold <lemma@confuego.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #include "kpasswdserverclient.h" |
9 | #include "kiocoredebug.h" |
10 | |
11 | #include <kio/authinfo.h> |
12 | #include <kio/global.h> |
13 | |
14 | #include "kpasswdserver_interface.h" |
15 | #include "kpasswdserverloop_p.h" |
16 | |
17 | class KPasswdServerClientPrivate |
18 | { |
19 | public: |
20 | KPasswdServerClientPrivate() |
21 | : seqNr(0) |
22 | { |
23 | } |
24 | |
25 | qlonglong seqNr; |
26 | QString lastHost; |
27 | }; |
28 | |
29 | KPasswdServerClient::KPasswdServerClient() |
30 | : m_interface( |
31 | new OrgKdeKPasswdServerInterface(QStringLiteral("org.kde.kpasswdserver6" ), QStringLiteral("/modules/kpasswdserver" ), QDBusConnection::sessionBus())) |
32 | , d(new KPasswdServerClientPrivate) |
33 | { |
34 | } |
35 | |
36 | KPasswdServerClient::~KPasswdServerClient() |
37 | { |
38 | delete m_interface; |
39 | } |
40 | |
41 | bool KPasswdServerClient::checkAuthInfo(KIO::AuthInfo *info, qlonglong windowId, qlonglong usertime) |
42 | { |
43 | // qDebug() << "window-id=" << windowId << "url=" << info.url; |
44 | |
45 | if (!QCoreApplication::instance()) { |
46 | qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for checkAuthInfo." ; |
47 | return false; |
48 | } |
49 | |
50 | // create the loop for waiting for a result before sending the request |
51 | KPasswdServerLoop loop; |
52 | QObject::connect(sender: m_interface, signal: &OrgKdeKPasswdServerInterface::checkAuthInfoAsyncResult, context: &loop, slot: &KPasswdServerLoop::slotQueryResult); |
53 | |
54 | QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(info: *info, windowId, usertime); |
55 | if (!reply.isValid()) { |
56 | qCWarning(KIO_CORE) << "Can't communicate with kiod_kpasswdserver (for checkAuthInfo)!" << reply.error().message(); |
57 | return false; |
58 | } |
59 | |
60 | if (!loop.waitForResult(requestId: reply.value())) { |
61 | qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!" ; |
62 | return false; |
63 | } |
64 | |
65 | if (loop.authInfo().isModified()) { |
66 | // qDebug() << "username=" << info.username << "password=[hidden]"; |
67 | *info = loop.authInfo(); |
68 | return true; |
69 | } |
70 | |
71 | return false; |
72 | } |
73 | |
74 | int KPasswdServerClient::queryAuthInfo(KIO::AuthInfo *info, const QString &errorMsg, qlonglong windowId, qlonglong usertime) |
75 | { |
76 | if (info->url.host() != d->lastHost) { // see kpasswdserver/DESIGN |
77 | d->lastHost = info->url.host(); |
78 | d->seqNr = 0; |
79 | } |
80 | |
81 | // qDebug() << "window-id=" << windowId; |
82 | |
83 | if (!QCoreApplication::instance()) { |
84 | qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for queryAuthInfo." ; |
85 | return KIO::ERR_PASSWD_SERVER; |
86 | } |
87 | |
88 | // create the loop for waiting for a result before sending the request |
89 | KPasswdServerLoop loop; |
90 | QObject::connect(sender: m_interface, signal: &OrgKdeKPasswdServerInterface::queryAuthInfoAsyncResult, context: &loop, slot: &KPasswdServerLoop::slotQueryResult); |
91 | |
92 | QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(info: *info, errorMsg, windowId, seqNr: d->seqNr, usertime); |
93 | if (!reply.isValid()) { |
94 | qCWarning(KIO_CORE) << "Can't communicate with kiod_kpasswdserver (for queryAuthInfo)!" ; |
95 | // qDebug() << reply.error().name() << reply.error().message(); |
96 | return KIO::ERR_PASSWD_SERVER; |
97 | } |
98 | |
99 | if (!loop.waitForResult(requestId: reply.value())) { |
100 | qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!" ; |
101 | return KIO::ERR_PASSWD_SERVER; |
102 | } |
103 | |
104 | *info = loop.authInfo(); |
105 | |
106 | // qDebug() << "username=" << info->username << "password=[hidden]"; |
107 | |
108 | const qlonglong newSeqNr = loop.seqNr(); |
109 | |
110 | if (newSeqNr > 0) { |
111 | d->seqNr = newSeqNr; |
112 | if (info->isModified()) { |
113 | return KJob::NoError; |
114 | } |
115 | } |
116 | |
117 | return KIO::ERR_USER_CANCELED; |
118 | } |
119 | |
120 | void KPasswdServerClient::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId) |
121 | { |
122 | m_interface->addAuthInfo(info, windowId); |
123 | } |
124 | |
125 | void KPasswdServerClient::removeAuthInfo(const QString &host, const QString &protocol, const QString &user) |
126 | { |
127 | m_interface->removeAuthInfo(host, protocol, user); |
128 | } |
129 | |