blob: 33326b7f411adee81da5a7582153ca41b73c0502 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// SPDX-FileCopyrightText: 2011 Jeremy Lainé <jeremy.laine@m4x.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppPasswordChecker.h"
#include <QCryptographicHash>
#include <QString>
#include <QTimer>
/// Returns the requested domain.
QString QXmppPasswordRequest::domain() const
{
return m_domain;
}
/// Sets the requested \a domain.
///
/// \param domain
void QXmppPasswordRequest::setDomain(const QString &domain)
{
m_domain = domain;
}
/// Returns the given password.
QString QXmppPasswordRequest::password() const
{
return m_password;
}
/// Sets the given \a password.
void QXmppPasswordRequest::setPassword(const QString &password)
{
m_password = password;
}
/// Returns the requested username.
QString QXmppPasswordRequest::username() const
{
return m_username;
}
/// Sets the requested \a username.
///
/// \param username
void QXmppPasswordRequest::setUsername(const QString &username)
{
m_username = username;
}
/// Constructs a new QXmppPasswordReply.
///
/// \param parent
QXmppPasswordReply::QXmppPasswordReply(QObject *parent)
: QObject(parent),
m_error(QXmppPasswordReply::NoError),
m_isFinished(false)
{
}
/// Returns the received MD5 digest.
QByteArray QXmppPasswordReply::digest() const
{
return m_digest;
}
/// Sets the received MD5 digest.
///
/// \param digest
void QXmppPasswordReply::setDigest(const QByteArray &digest)
{
m_digest = digest;
}
/// Returns the error that was found during the processing of this request.
///
/// If no error was found, returns NoError.
QXmppPasswordReply::Error QXmppPasswordReply::error() const
{
return m_error;
}
/// Returns the error that was found during the processing of this request.
///
void QXmppPasswordReply::setError(QXmppPasswordReply::Error error)
{
m_error = error;
}
/// Mark reply as finished.
void QXmppPasswordReply::finish()
{
m_isFinished = true;
Q_EMIT finished();
}
/// Delay marking reply as finished.
void QXmppPasswordReply::finishLater()
{
QTimer::singleShot(0, this, &QXmppPasswordReply::finish);
}
/// Returns true when the reply has finished.
bool QXmppPasswordReply::isFinished() const
{
return m_isFinished;
}
/// Returns the received password.
QString QXmppPasswordReply::password() const
{
return m_password;
}
/// Sets the received password.
///
/// \param password
void QXmppPasswordReply::setPassword(const QString &password)
{
m_password = password;
}
/// Checks that the given credentials are valid.
///
/// The base implementation requires that you reimplement getPassword().
///
/// \param request
QXmppPasswordReply *QXmppPasswordChecker::checkPassword(const QXmppPasswordRequest &request)
{
auto *reply = new QXmppPasswordReply;
QString secret;
QXmppPasswordReply::Error error = getPassword(request, secret);
if (error == QXmppPasswordReply::NoError) {
if (request.password() != secret) {
reply->setError(QXmppPasswordReply::AuthorizationError);
}
} else {
reply->setError(error);
}
// reply is finished
reply->finishLater();
return reply;
}
/// Retrieves the MD5 digest for the given username.
///
/// Reimplement this method if your backend natively supports
/// retrieving MD5 digests.
///
/// \param request
QXmppPasswordReply *QXmppPasswordChecker::getDigest(const QXmppPasswordRequest &request)
{
auto *reply = new QXmppPasswordReply;
QString secret;
QXmppPasswordReply::Error error = getPassword(request, secret);
if (error == QXmppPasswordReply::NoError) {
reply->setDigest(QCryptographicHash::hash(
(request.username() + ":" + request.domain() + ":" + secret).toUtf8(),
QCryptographicHash::Md5));
} else {
reply->setError(error);
}
// reply is finished
reply->finishLater();
return reply;
}
/// Retrieves the password for the given username.
///
/// The simplest way to write a password checker is to reimplement this method.
///
/// \param request
/// \param password
QXmppPasswordReply::Error QXmppPasswordChecker::getPassword(const QXmppPasswordRequest &request, QString &password)
{
Q_UNUSED(request);
Q_UNUSED(password);
return QXmppPasswordReply::TemporaryError;
}
/// Returns true if the getPassword() method is implemented.
///
bool QXmppPasswordChecker::hasGetPassword() const
{
return false;
}
|