File indexing completed on 2024-11-10 12:23:15

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2009 Michael Leupold <lemma@confuego.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kpasswdserverclient.h"
0009 #include "kiocoredebug.h"
0010 
0011 #include <kio/authinfo.h>
0012 #include <kio/global.h>
0013 
0014 #include "kpasswdserver_interface.h"
0015 #include "kpasswdserverloop_p.h"
0016 
0017 class KPasswdServerClientPrivate
0018 {
0019 public:
0020     KPasswdServerClientPrivate()
0021         : seqNr(0)
0022     {
0023     }
0024 
0025     qlonglong seqNr;
0026     QString lastHost;
0027 };
0028 
0029 KPasswdServerClient::KPasswdServerClient()
0030     : m_interface(
0031         new OrgKdeKPasswdServerInterface(QStringLiteral("org.kde.kpasswdserver"), QStringLiteral("/modules/kpasswdserver"), QDBusConnection::sessionBus()))
0032     , d(new KPasswdServerClientPrivate)
0033 {
0034 }
0035 
0036 KPasswdServerClient::~KPasswdServerClient()
0037 {
0038     delete m_interface;
0039 }
0040 
0041 bool KPasswdServerClient::checkAuthInfo(KIO::AuthInfo *info, qlonglong windowId, qlonglong usertime)
0042 {
0043     // qDebug() << "window-id=" << windowId << "url=" << info.url;
0044 
0045     if (!QCoreApplication::instance()) {
0046         qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for checkAuthInfo.";
0047         return false;
0048     }
0049 
0050     // create the loop for waiting for a result before sending the request
0051     KPasswdServerLoop loop;
0052     QObject::connect(m_interface, &OrgKdeKPasswdServerInterface::checkAuthInfoAsyncResult, &loop, &KPasswdServerLoop::slotQueryResult);
0053 
0054     QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(*info, windowId, usertime);
0055     if (!reply.isValid()) {
0056         qCWarning(KIO_CORE) << "Can't communicate with kiod_kpasswdserver (for checkAuthInfo)!";
0057         // qDebug() << reply.error().name() << reply.error().message();
0058         return false;
0059     }
0060 
0061     if (!loop.waitForResult(reply.value())) {
0062         qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
0063         return false;
0064     }
0065 
0066     if (loop.authInfo().isModified()) {
0067         // qDebug() << "username=" << info.username << "password=[hidden]";
0068         *info = loop.authInfo();
0069         return true;
0070     }
0071 
0072     return false;
0073 }
0074 
0075 int KPasswdServerClient::queryAuthInfo(KIO::AuthInfo *info, const QString &errorMsg, qlonglong windowId, qlonglong usertime)
0076 {
0077     if (info->url.host() != d->lastHost) { // see kpasswdserver/DESIGN
0078         d->lastHost = info->url.host();
0079         d->seqNr = 0;
0080     }
0081 
0082     // qDebug() << "window-id=" << windowId;
0083 
0084     if (!QCoreApplication::instance()) {
0085         qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for queryAuthInfo.";
0086         return KIO::ERR_PASSWD_SERVER;
0087     }
0088 
0089     // create the loop for waiting for a result before sending the request
0090     KPasswdServerLoop loop;
0091     QObject::connect(m_interface, &OrgKdeKPasswdServerInterface::queryAuthInfoAsyncResult, &loop, &KPasswdServerLoop::slotQueryResult);
0092 
0093     QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(*info, errorMsg, windowId, d->seqNr, usertime);
0094     if (!reply.isValid()) {
0095         qCWarning(KIO_CORE) << "Can't communicate with kiod_kpasswdserver (for queryAuthInfo)!";
0096         // qDebug() << reply.error().name() << reply.error().message();
0097         return KIO::ERR_PASSWD_SERVER;
0098     }
0099 
0100     if (!loop.waitForResult(reply.value())) {
0101         qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
0102         return KIO::ERR_PASSWD_SERVER;
0103     }
0104 
0105     *info = loop.authInfo();
0106 
0107     // qDebug() << "username=" << info->username << "password=[hidden]";
0108 
0109     const qlonglong newSeqNr = loop.seqNr();
0110 
0111     if (newSeqNr > 0) {
0112         d->seqNr = newSeqNr;
0113         if (info->isModified()) {
0114             return KJob::NoError;
0115         }
0116     }
0117 
0118     return KIO::ERR_USER_CANCELED;
0119 }
0120 
0121 void KPasswdServerClient::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
0122 {
0123     m_interface->addAuthInfo(info, windowId);
0124 }
0125 
0126 void KPasswdServerClient::removeAuthInfo(const QString &host, const QString &protocol, const QString &user)
0127 {
0128     m_interface->removeAuthInfo(host, protocol, user);
0129 }