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)!" << reply.error().message();
57 return false;
58 }
59
60 if (!loop.waitForResult(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
74int 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
84 qCWarning(KIO_CORE) << "KIO worker is not a QCoreApplication! This is required for queryAuthInfo.";
86 }
87
88 // create the loop for waiting for a result before sending the request
89 KPasswdServerLoop loop;
90 QObject::connect(m_interface, &OrgKdeKPasswdServerInterface::queryAuthInfoAsyncResult, &loop, &KPasswdServerLoop::slotQueryResult);
91
92 QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(*info, errorMsg, windowId, 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();
97 }
98
99 if (!loop.waitForResult(reply.value())) {
100 qCWarning(KIO_CORE) << "kiod_kpasswdserver died while waiting for reply!";
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
120void KPasswdServerClient::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
121{
122 m_interface->addAuthInfo(info, windowId);
123}
124
125void KPasswdServerClient::removeAuthInfo(const QString &host, const QString &protocol, const QString &user)
126{
127 m_interface->removeAuthInfo(host, protocol, user);
128}
This class is intended to make it easier to prompt for, cache and retrieve authorization information.
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()
QString message() const const
const QDBusError & error()
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 Sat Dec 21 2024 16:58:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.