KD SOAP API Documentation 2.1
Loading...
Searching...
No Matches
KDSoapAuthentication.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of the KD Soap project.
4**
5** SPDX-FileCopyrightText: 2010-2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6**
7** SPDX-License-Identifier: MIT
8**
9****************************************************************************/
13#include <QAuthenticator>
14#include <QCryptographicHash>
15#include <QDateTime>
16#include <QDebug>
17#include <QNetworkReply>
18#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
19#include <QRandomGenerator>
20#endif
21
22class KDSoapAuthentication::Private
23{
24public:
25 QString user;
26 QString password;
27 bool usePasswordDigest = false;
28 bool useWSUsernameToken = false;
29 QDateTime overrideWSUsernameCreatedTime;
30 QByteArray overrideWSUsernameNonce;
31};
32
34 : d(new Private)
35{
36 d->usePasswordDigest = true;
37}
38
40 : d(new Private)
41{
42 *d = *other.d;
43}
44
46{
47 *d = *other.d;
48 return *this;
49}
50
55
57{
58 d->user = user;
59}
60
62{
63 d->password = password;
64}
65
66void KDSoapAuthentication::setUsePasswordDigest(const bool usePasswordDigest)
67{
68 d->usePasswordDigest = usePasswordDigest;
69}
70
72{
73 d->useWSUsernameToken = useWSUsernameToken;
74}
75
77{
78 d->overrideWSUsernameCreatedTime = overrideWSUsernameCreatedTime;
79}
80
81// cppcheck-suppress passedByValue (cppcheck is correct, but changing it would be BIC)
83{
84 d->overrideWSUsernameNonce = overrideWSUsernameNonce;
85}
86
88{
89 return d->user;
90}
91
93{
94 return d->password;
95}
96
98{
99 return d->usePasswordDigest;
100}
101
103{
104 return d->useWSUsernameToken;
105}
106
108{
109 return d->overrideWSUsernameCreatedTime;
110}
111
113{
114 return d->overrideWSUsernameNonce;
115}
116
118{
119 return !d->user.isEmpty() || !d->password.isEmpty();
120}
121
122void KDSoapAuthentication::handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
123{
124 // qDebug() << "handleAuthenticationRequired" << reply << reply->url() << "realm=" << authenticator->realm();
125 // Only proceed if
126 // 1) we have some authentication to offer
127 // 2) we didn't try once already (unittest: BuiltinHttpTest::testAsyncCallRefusedAuth)
128 if (hasAuth() && !reply->property("authAdded").toBool()) {
129 authenticator->setUser(d->user);
130 authenticator->setPassword(d->password);
131 reply->setProperty("authAdded", true);
132 }
133}
134
135bool KDSoapAuthentication::hasWSUsernameTokenHeader() const
136{
137 return hasAuth() && d->useWSUsernameToken;
138}
139
140void KDSoapAuthentication::writeWSUsernameTokenHeader(QXmlStreamWriter &writer) const
141{
142 if (!hasAuth()) {
143 return;
144 }
145
146 const QString securityExtentionNS = KDSoapNamespaceManager::soapSecurityExtention();
147 const QString securityUtilityNS = KDSoapNamespaceManager::soapSecurityUtility();
148#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
149 static QRandomGenerator generator;
150 QByteArray nonce = "kdsoap" + QByteArray::number(generator.generate64());
151#else
152 QByteArray nonce = "kdsoap" + QByteArray::number(qrand());
153#endif
154 if (!d->overrideWSUsernameNonce.isEmpty()) {
155 nonce = d->overrideWSUsernameNonce;
156 }
158 if (d->overrideWSUsernameCreatedTime.isValid()) {
159 time = d->overrideWSUsernameCreatedTime;
160 }
161 QString timestamp = time.toString(QLatin1String("yyyy-MM-ddTHH:mm:ssZ"));
162
163 writer.writeStartElement(securityExtentionNS, QLatin1String("Security"));
164 writer.writeStartElement(securityExtentionNS, QLatin1String("UsernameToken"));
165
166 writer.writeStartElement(securityExtentionNS, QLatin1String("Nonce"));
168 writer.writeEndElement();
169
170 writer.writeStartElement(securityUtilityNS, QLatin1String("Created"));
171 writer.writeCharacters(timestamp);
172 writer.writeEndElement();
173
174 writer.writeStartElement(securityExtentionNS, QLatin1String("Password"));
175 if (d->usePasswordDigest) {
176 writer.writeAttribute(QLatin1String("Type"),
177 QLatin1String("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"));
178 QByteArray passwordConcat = nonce + timestamp.toUtf8() + d->password.toUtf8();
179 QByteArray passwordHash = QCryptographicHash::hash(passwordConcat, QCryptographicHash::Sha1);
180 writer.writeCharacters(QString::fromLatin1(passwordHash.toBase64().constData()));
181 } else {
182 writer.writeAttribute(QLatin1String("Type"),
183 QLatin1String("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"));
184 writer.writeCharacters(d->password);
185 }
186 writer.writeEndElement();
187
188 writer.writeStartElement(securityExtentionNS, QLatin1String("Username"));
189 writer.writeCharacters(d->user);
190 writer.writeEndElement();
191
192 writer.writeEndElement();
193 writer.writeEndElement();
194}
void setPassword(const QString &password)
void setUseWSUsernameToken(bool useWSUsernameToken)
void setUsePasswordDigest(const bool usePasswordDigest)
void setOverrideWSUsernameCreatedTime(QDateTime overrideWSUsernameCreatedTime)
void setOverrideWSUsernameNonce(QByteArray overrideWSUsernameNonce)
QDateTime overrideWSUsernameCreatedTime() const
KDSoapAuthentication & operator=(const KDSoapAuthentication &other)
QByteArray overrideWSUsernameNonce() const
void setUser(const QString &user)
void setPassword(const QString &password)
void setUser(const QString &user)
const char * constData() const const
QByteArray number(int n, int base)
QByteArray toBase64(QByteArray::Base64Options options) const const
QByteArray hash(const QByteArray &data, QCryptographicHash::Algorithm method)
QDateTime currentDateTimeUtc()
QString toString(Qt::DateFormat format) const const
QVariant property(const char *name) const const
bool setProperty(const char *name, const QVariant &value)
quint64 generate64()
QString fromLatin1(const char *str, int size)
QByteArray toUtf8() const const
bool toBool() const const
void writeAttribute(const QString &qualifiedName, const QString &value)
void writeCharacters(const QString &text)
void writeEndElement()
void writeStartElement(const QString &qualifiedName)

© 2010-2023 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-soap/
Generated on Tue Dec 26 2023 00:00:25 for KD SOAP API Documentation by doxygen 1.9.8