• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KIO

  • sources
  • kde-4.14
  • kdelibs
  • kio
  • kio
kpasswdserver.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2009 Michael Leupold <lemma@confuego.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3, or any
9  * later version accepted by the membership of KDE e.V. (or its
10  * successor approved by the membership of KDE e.V.), which shall
11  * act as a proxy defined in Section 6 of version 3 of the license.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "kpasswdserver_p.h"
23 
24 #include <kio/authinfo.h>
25 #include <QtCore/QByteArray>
26 #include <QtCore/QEventLoop>
27 #include <kdebug.h>
28 
29 #include "kpasswdserverloop_p.h"
30 #include "kpasswdserver_interface.h"
31 
32 namespace KIO
33 {
34 
35 KPasswdServer::KPasswdServer()
36  : m_interface(new OrgKdeKPasswdServerInterface("org.kde.kded",
37  "/modules/kpasswdserver",
38  QDBusConnection::sessionBus()))
39 {
40 }
41 
42 KPasswdServer::~KPasswdServer()
43 {
44  delete m_interface;
45 }
46 
47 bool KPasswdServer::checkAuthInfo(KIO::AuthInfo &info, qlonglong windowId,
48  qlonglong usertime)
49 {
50  kDebug(7019) << "window-id=" << windowId << "url=" << info.url;
51 
52  // special handling for kioslaves which aren't QCoreApplications
53  if (!QCoreApplication::instance()) {
54  kWarning(7019) << "kioslave is not a QCoreApplication!";
55  return legacyCheckAuthInfo(info, windowId, usertime);
56  }
57 
58  // create the loop for waiting for a result before sending the request
59  KPasswdServerLoop loop;
60  QObject::connect(m_interface, SIGNAL(checkAuthInfoAsyncResult(qlonglong,qlonglong,KIO::AuthInfo)),
61  &loop, SLOT(slotQueryResult(qlonglong,qlonglong,KIO::AuthInfo)));
62 
63  QDBusReply<qlonglong> reply = m_interface->checkAuthInfoAsync(info, windowId,
64  usertime);
65  if (!reply.isValid()) {
66  if (reply.error().type() == QDBusError::UnknownMethod) {
67  if (legacyCheckAuthInfo(info, windowId, usertime)) {
68  return true;
69  }
70  }
71 
72  kWarning(7019) << "Can't communicate with kded_kpasswdserver (for checkAuthInfo)!";
73  kDebug(7019) << reply.error().name() << reply.error().message();
74  return false;
75  }
76 
77  if (!loop.waitForResult(reply.value())) {
78  kWarning(7019) << "kded_kpasswdserver died while waiting for reply!";
79  return false;
80  }
81 
82  if (loop.authInfo().isModified()) {
83  kDebug(7019) << "username=" << info.username << "password=[hidden]";
84  info = loop.authInfo();
85  return true;
86  }
87 
88  return false;
89 }
90 
91 bool KPasswdServer::legacyCheckAuthInfo(KIO::AuthInfo &info, qlonglong windowId,
92  qlonglong usertime)
93 {
94  kWarning(7019) << "Querying old kded_kpasswdserver.";
95 
96  QByteArray params;
97  QDataStream stream(&params, QIODevice::WriteOnly);
98  stream << info;
99  QDBusReply<QByteArray> reply = m_interface->checkAuthInfo(params, windowId,
100  usertime);
101  if (reply.isValid()) {
102  AuthInfo authResult;
103  QDataStream stream2(reply.value());
104  stream2 >> authResult;
105  if (authResult.isModified()) {
106  info = authResult;
107  return true;
108  }
109  }
110  return false;
111 }
112 
113 qlonglong KPasswdServer::queryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg,
114  qlonglong windowId, qlonglong seqNr,
115  qlonglong usertime)
116 {
117  kDebug(7019) << "window-id=" << windowId;
118 
119  // special handling for kioslaves which aren't QCoreApplications
120  if (!QCoreApplication::instance()) {
121  kWarning(7019) << "kioslave is not a QCoreApplication!";
122  return legacyQueryAuthInfo(info, errorMsg, windowId, seqNr, usertime);
123  }
124 
125  // create the loop for waiting for a result before sending the request
126  KPasswdServerLoop loop;
127  QObject::connect(m_interface, SIGNAL(queryAuthInfoAsyncResult(qlonglong,qlonglong,KIO::AuthInfo)),
128  &loop, SLOT(slotQueryResult(qlonglong,qlonglong,KIO::AuthInfo)));
129 
130  QDBusReply<qlonglong> reply = m_interface->queryAuthInfoAsync(info, errorMsg,
131  windowId, seqNr,
132  usertime);
133  if (!reply.isValid()) {
134  // backwards compatibility for old kpasswdserver
135  if (reply.error().type() == QDBusError::UnknownMethod) {
136  qlonglong res = legacyQueryAuthInfo(info, errorMsg, windowId, seqNr,
137  usertime);
138  if (res > 0) {
139  return res;
140  }
141  }
142 
143  kWarning(7019) << "Can't communicate with kded_kpasswdserver (for queryAuthInfo)!";
144  kDebug(7019) << reply.error().name() << reply.error().message();
145  return -1;
146  }
147 
148  if (!loop.waitForResult(reply.value())) {
149  kWarning(7019) << "kded_kpasswdserver died while waiting for reply!";
150  return -1;
151  }
152 
153  info = loop.authInfo();
154 
155  kDebug(7019) << "username=" << info.username << "password=[hidden]";
156 
157  return loop.seqNr();
158 }
159 
160 qlonglong KPasswdServer::legacyQueryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg,
161  qlonglong windowId, qlonglong seqNr,
162  qlonglong usertime)
163 {
164  kWarning(7019) << "Querying old kded_kpasswdserver.";
165 
166  QByteArray params;
167  QDataStream stream(&params, QIODevice::WriteOnly);
168  stream << info;
169  QDBusPendingReply<QByteArray, qlonglong> reply = m_interface->queryAuthInfo(params, errorMsg,
170  windowId, seqNr,
171  usertime);
172  reply.waitForFinished();
173  if (reply.isValid()) {
174  AuthInfo authResult;
175  QDataStream stream2(reply.argumentAt<0>());
176  stream2 >> authResult;
177  if (authResult.isModified()) {
178  info = authResult;
179  }
180  return reply.argumentAt<1>();
181  }
182  return -1;
183 }
184 
185 void KPasswdServer::addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
186 {
187  QDBusReply<void> reply = m_interface->addAuthInfo(info, windowId);
188  if (!reply.isValid() && reply.error().type() == QDBusError::UnknownMethod) {
189  legacyAddAuthInfo(info, windowId);
190  }
191 }
192 
193 void KPasswdServer::legacyAddAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
194 {
195  kWarning(7019) << "Querying old kded_kpasswdserver.";
196 
197  QByteArray params;
198  QDataStream stream(&params, QIODevice::WriteOnly);
199  stream << info;
200  m_interface->addAuthInfo(params, windowId);
201 }
202 
203 void KPasswdServer::removeAuthInfo(const QString &host, const QString &protocol,
204  const QString &user)
205 {
206  m_interface->removeAuthInfo(host, protocol, user);
207 }
208 
209 }
KIO::KPasswdServer::~KPasswdServer
~KPasswdServer()
Definition: kpasswdserver.cpp:42
KIO::KPasswdServerLoop::waitForResult
bool waitForResult(qlonglong requestId)
Definition: kpasswdserverloop.cpp:42
KIO::AuthInfo::url
KUrl url
The URL for which authentication is to be stored.
Definition: authinfo.h:110
kdebug.h
QByteArray
QDBusReply
QDataStream
KIO::KPasswdServer::checkAuthInfo
bool checkAuthInfo(KIO::AuthInfo &info, qlonglong windowId, qlonglong usertime)
Check if kpasswdserver has cached authentication information regarding an AuthInfo object...
Definition: kpasswdserver.cpp:47
QDBusError::type
ErrorType type() const
QDBusError::name
QString name() const
KIO::AuthInfo
This class is intended to make it easier to prompt for, cache and retrieve authorization information...
Definition: authinfo.h:57
QDBusError::message
QString message() const
QDBusConnection
QDBusReply::isValid
bool isValid() const
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KIO::KPasswdServerLoop::authInfo
const AuthInfo & authInfo() const
Definition: kpasswdserverloop.cpp:55
kpasswdserver_p.h
KIO::AuthInfo::isModified
bool isModified() const
Use this method to check if the object was modified.
Definition: authinfo.cpp:165
KIO::KPasswdServer::removeAuthInfo
void removeAuthInfo(const QString &host, const QString &protocol, const QString &user)
Manually remove authentication information from kpasswdserver's cache.
Definition: kpasswdserver.cpp:203
KIO::KPasswdServerLoop::seqNr
qlonglong seqNr() const
Definition: kpasswdserverloop.cpp:50
QDBusPendingReply
QDBusReply::value
Type value() const
QCoreApplication::instance
QCoreApplication * instance()
QString
KIO::KPasswdServer::queryAuthInfo
qlonglong queryAuthInfo(KIO::AuthInfo &info, const QString &errorMsg, qlonglong windowId, qlonglong seqNr, qlonglong usertime)
Let kpasswdserver ask the user for authentication information.
Definition: kpasswdserver.cpp:113
authinfo.h
QDBusPendingReply::waitForFinished
void waitForFinished()
QDBusPendingReply::argumentAt
QVariant argumentAt(int index) const
KIO::KPasswdServerLoop
Definition: kpasswdserverloop_p.h:34
QDBusPendingReply::isValid
bool isValid() const
KIO::AuthInfo::username
QString username
This is required for caching.
Definition: authinfo.h:115
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KIO::KPasswdServer::KPasswdServer
KPasswdServer()
Definition: kpasswdserver.cpp:35
KIO::KPasswdServer::addAuthInfo
void addAuthInfo(const KIO::AuthInfo &info, qlonglong windowId)
Manually add authentication information to kpasswdserver's cache.
Definition: kpasswdserver.cpp:185
kpasswdserverloop_p.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QDBusReply::error
const QDBusError & error()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal