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
17class KPasswdServerClientPrivate
18{
19public:
20 KPasswdServerClientPrivate()
21 : seqNr(0)
22 {
23 }
24
25 qlonglong seqNr;
26 QString lastHost;
27};
28
29KPasswdServerClient::KPasswdServerClient()
30 : m_interface(
31 new OrgKdeKPasswdServerInterface(QStringLiteral("org.kde.kpasswdserver6"), QStringLiteral("/modules/kpasswdserver"), QDBusConnection::sessionBus()))
32 , d(new KPasswdServerClientPrivate)
33{
34}
35
36KPasswdServerClient::~KPasswdServerClient()
37{
38 delete m_interface;
39}
40
41bool 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)!";
57 // qDebug() << reply.error().name() << reply.error().message();
58 return false;
59 }
60
61 if (!loop.waitForResult(requestId: reply.value())) {
62 qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
63 return false;
64 }
65
66 if (loop.authInfo().isModified()) {
67 // qDebug() << "username=" << info.username << "password=[hidden]";
68 *info = loop.authInfo();
69 return true;
70 }
71
72 return false;
73}
74
75int KPasswdServerClient::queryAuthInfo(KIO::AuthInfo *info, const QString &errorMsg, qlonglong windowId, qlonglong usertime)
76{
77 if (info->url.host() != d->lastHost) { // see kpasswdserver/DESIGN
78 d->lastHost = info->url.host();
79 d->seqNr = 0;
80 }
81
82 // qDebug() << "window-id=" << windowId;
83
84 if (!QCoreApplication::instance()) {
85 qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for queryAuthInfo.";
86 return KIO::ERR_PASSWD_SERVER;
87 }
88
89 // create the loop for waiting for a result before sending the request
90 KPasswdServerLoop loop;
91 QObject::connect(sender: m_interface, signal: &OrgKdeKPasswdServerInterface::queryAuthInfoAsyncResult, context: &loop, slot: &KPasswdServerLoop::slotQueryResult);
92
93 QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(info: *info, errorMsg, windowId, seqNr: d->seqNr, usertime);
94 if (!reply.isValid()) {
95 qCWarning(KIO_CORE) << "Can't communicate with kiod_kpasswdserver (for queryAuthInfo)!";
96 // qDebug() << reply.error().name() << reply.error().message();
97 return KIO::ERR_PASSWD_SERVER;
98 }
99
100 if (!loop.waitForResult(requestId: reply.value())) {
101 qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
102 return KIO::ERR_PASSWD_SERVER;
103 }
104
105 *info = loop.authInfo();
106
107 // qDebug() << "username=" << info->username << "password=[hidden]";
108
109 const qlonglong newSeqNr = loop.seqNr();
110
111 if (newSeqNr > 0) {
112 d->seqNr = newSeqNr;
113 if (info->isModified()) {
114 return KJob::NoError;
115 }
116 }
117
118 return KIO::ERR_USER_CANCELED;
119}
120
121void KPasswdServerClient::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
122{
123 m_interface->addAuthInfo(info, windowId);
124}
125
126void KPasswdServerClient::removeAuthInfo(const QString &host, const QString &protocol, const QString &user)
127{
128 m_interface->removeAuthInfo(host, protocol, user);
129}
130

source code of kio/src/core/kpasswdserverclient.cpp