1/*
2 This file is part of the KDE Password Server
3 SPDX-FileCopyrightText: 2002 Waldo Bastian (bastian@kde.org)
4 SPDX-FileCopyrightText: 2012 Dawit Alemayehu (adawit@kde.org)
5
6 SPDX-License-Identifier: GPL-2.0-only
7*/
8
9// KDE Password Server
10
11#ifndef KPASSWDSERVER_H
12#define KPASSWDSERVER_H
13
14#include <QDBusContext>
15#include <QDBusMessage>
16#include <QHash>
17#include <QList>
18#include <QWidget>
19
20#include <KDEDModule>
21#include <kio/authinfo.h>
22
23class KMessageDialog;
24class KPasswordDialog;
25
26namespace KWallet
27{
28class Wallet;
29}
30
31class KPasswdServer : public KDEDModule, protected QDBusContext
32{
33 Q_OBJECT
34
35public:
36 explicit KPasswdServer(QObject *parent, const QList<QVariant> & = QList<QVariant>());
37 ~KPasswdServer() override;
38
39 // Called by the unit test
40 void setWalletDisabled(bool d)
41 {
42 m_walletDisabled = d;
43 }
44
45public Q_SLOTS:
46 qlonglong checkAuthInfoAsync(KIO::AuthInfo, qlonglong, qlonglong);
47 qlonglong queryAuthInfoAsync(const KIO::AuthInfo &, const QString &, qlonglong, qlonglong, qlonglong);
48 void addAuthInfo(const KIO::AuthInfo &, qlonglong);
49 void removeAuthInfo(const QString &host, const QString &protocol, const QString &user);
50
51 // legacy methods provided for compatibility with old clients
52 QByteArray checkAuthInfo(const QByteArray &, qlonglong, qlonglong);
53 QByteArray queryAuthInfo(const QByteArray &, const QString &, qlonglong, qlonglong, qlonglong);
54 void addAuthInfo(const QByteArray &, qlonglong);
55
56 void processRequest();
57 // Remove all authentication info associated with windowId
58 void removeAuthForWindowId(qlonglong windowId);
59
60Q_SIGNALS:
61 void checkAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
62 void queryAuthInfoAsyncResult(qlonglong requestId, qlonglong seqNr, const KIO::AuthInfo &);
63
64private Q_SLOTS:
65 void passwordDialogDone(int result, KPasswordDialog *sender);
66 void retryDialogDone(int result, KMessageDialog *sender);
67 void windowRemoved(WId);
68
69private:
70 struct AuthInfoContainer {
71 AuthInfoContainer()
72 {
73 }
74
75 KIO::AuthInfo info;
76 QString directory;
77
78 enum { expNever, expWindowClose, expTime } expire;
79 QList<qlonglong> windowList;
80 qulonglong expireTime = expNever;
81 qlonglong seqNr = 0;
82
83 bool isCanceled = false;
84
85 struct Sorter {
86 bool operator()(const AuthInfoContainer &n1, const AuthInfoContainer &n2) const;
87 };
88 };
89
90 struct Request {
91 bool isAsync; // true for async requests
92 qlonglong requestId; // set for async requests only
93 QDBusMessage transaction; // set for sync requests only
94 QString key;
95 KIO::AuthInfo info;
96 QString errorMsg;
97 qlonglong windowId;
98 qlonglong seqNr;
99 bool prompt;
100 };
101
102 QString createCacheKey(const KIO::AuthInfo &info);
103 const AuthInfoContainer *findAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
104 void removeAuthInfoItem(const QString &key, const KIO::AuthInfo &info);
105 void addAuthInfoItem(const QString &key, const KIO::AuthInfo &info, qlonglong windowId, qlonglong seqNr, bool canceled);
106 void copyAuthInfo(const AuthInfoContainer *, KIO::AuthInfo &);
107 void updateAuthExpire(const QString &key, const AuthInfoContainer *, qlonglong windowId, bool keep);
108
109#ifdef HAVE_KF6WALLET
110 bool openWallet(qlonglong windowId);
111#endif
112
113 bool hasPendingQuery(const QString &key, const KIO::AuthInfo &info);
114 void sendResponse(Request *request);
115 void showPasswordDialog(Request *request);
116 void updateCachedRequestKey(QList<Request *> &, const QString &oldKey, const QString &newKey);
117
118 using AuthInfoContainerList = QList<AuthInfoContainer>;
119 QHash<QString, AuthInfoContainerList *> m_authDict;
120
121 QList<Request *> m_authPending;
122 QList<Request *> m_authWait;
123 QHash<int, QStringList> mWindowIdList;
124 QHash<QObject *, Request *> m_authInProgress;
125 QHash<QObject *, Request *> m_authRetryInProgress;
126 QStringList m_authPrompted;
127 KWallet::Wallet *m_wallet;
128 bool m_walletDisabled;
129 qlonglong m_seqNr;
130};
131
132#endif
133

source code of kio/src/kpasswdserver/kpasswdserver.h