KIO

kpasswdserverclient.cpp
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
30 : m_interface(
31 new OrgKdeKPasswdServerInterface(QStringLiteral("org.kde.kpasswdserver6"), QStringLiteral("/modules/kpasswdserver"), QDBusConnection::sessionBus()))
32 , d(new KPasswdServerClientPrivate)
33{
34}
35
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
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(m_interface, &OrgKdeKPasswdServerInterface::checkAuthInfoAsyncResult, &loop, &KPasswdServerLoop::slotQueryResult);
53
54 QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(*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(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
85 qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for queryAuthInfo.";
87 }
88
89 // create the loop for waiting for a result before sending the request
90 KPasswdServerLoop loop;
91 QObject::connect(m_interface, &OrgKdeKPasswdServerInterface::queryAuthInfoAsyncResult, &loop, &KPasswdServerLoop::slotQueryResult);
92
93 QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(*info, errorMsg, windowId, 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();
98 }
99
100 if (!loop.waitForResult(reply.value())) {
101 qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
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}
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
Definition authinfo.h:50
bool isModified() const
Use this method to check if the object was modified.
Definition authinfo.cpp:142
QUrl url
The URL for which authentication is to be stored.
Definition authinfo.h:100
void addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
Manually add authentication information to kpasswdserver's cache.
KPasswdServerClient()
Creates a client instance for kpasswdserver.
bool checkAuthInfo(KIO::AuthInfo *info, qlonglong windowId, qlonglong usertime)
Check if kpasswdserver has cached authentication information regarding an AuthInfo object.
void removeAuthInfo(const QString &host, const QString &protocol, const QString &user)
Manually remove authentication information from kpasswdserver's cache.
int queryAuthInfo(KIO::AuthInfo *info, const QString &errorMsg, qlonglong windowId, qlonglong usertime)
Let kpasswdserver ask the user for authentication information.
@ ERR_PASSWD_SERVER
returned by WorkerBase::openPasswordDialog,
Definition global.h:196
QCoreApplication * instance()
bool isValid() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString host(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:51 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.