KD SOAP API Documentation 2.2
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 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
76void KDSoapAuthentication::setOverrideWSUsernameCreatedTime(QDateTime overrideWSUsernameCreatedTime) // clazy:exclude=function-args-by-ref
77{
78 d->overrideWSUsernameCreatedTime = std::move(overrideWSUsernameCreatedTime);
79}
80
81void KDSoapAuthentication::setOverrideWSUsernameNonce(QByteArray overrideWSUsernameNonce) // clazy:exclude=function-args-by-ref
82{
83 d->overrideWSUsernameNonce = std::move(overrideWSUsernameNonce);
84}
85
87{
88 return d->user;
89}
90
92{
93 return d->password;
94}
95
97{
98 return d->usePasswordDigest;
99}
100
102{
103 return d->useWSUsernameToken;
104}
105
107{
108 return d->overrideWSUsernameCreatedTime;
109}
110
112{
113 return d->overrideWSUsernameNonce;
114}
115
117{
118 return !d->user.isEmpty() || !d->password.isEmpty();
119}
120
121void KDSoapAuthentication::handleAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
122{
123 // qDebug() << "handleAuthenticationRequired" << reply << reply->url() << "realm=" << authenticator->realm();
124 // Only proceed if
125 // 1) we have some authentication to offer
126 // 2) we didn't try once already (unittest: BuiltinHttpTest::testAsyncCallRefusedAuth)
127 if (hasAuth() && !reply->property("authAdded").toBool()) {
128 authenticator->setUser(d->user);
129 authenticator->setPassword(d->password);
130 reply->setProperty("authAdded", true);
131 }
132}
133
134bool KDSoapAuthentication::hasWSUsernameTokenHeader() const
135{
136 return hasAuth() && d->useWSUsernameToken;
137}
138
139void KDSoapAuthentication::writeWSUsernameTokenHeader(QXmlStreamWriter &writer) const
140{
141 if (!hasAuth()) {
142 return;
143 }
144
145 const QString securityExtentionNS = KDSoapNamespaceManager::soapSecurityExtention();
146 const QString securityUtilityNS = KDSoapNamespaceManager::soapSecurityUtility();
147#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
148 static QRandomGenerator generator;
149 QByteArray nonce = "kdsoap" + QByteArray::number(generator.generate64());
150#else
151 QByteArray nonce = "kdsoap" + QByteArray::number(qrand());
152#endif
153 if (!d->overrideWSUsernameNonce.isEmpty()) {
154 nonce = d->overrideWSUsernameNonce;
155 }
157 if (d->overrideWSUsernameCreatedTime.isValid()) {
158 time = d->overrideWSUsernameCreatedTime;
159 }
160 QString timestamp = time.toString(QLatin1String("yyyy-MM-ddTHH:mm:ssZ"));
161
162 writer.writeStartElement(securityExtentionNS, QLatin1String("Security"));
163 writer.writeStartElement(securityExtentionNS, QLatin1String("UsernameToken"));
164
165 writer.writeStartElement(securityExtentionNS, QLatin1String("Nonce"));
167 writer.writeEndElement();
168
169 writer.writeStartElement(securityUtilityNS, QLatin1String("Created"));
170 writer.writeCharacters(timestamp);
171 writer.writeEndElement();
172
173 writer.writeStartElement(securityExtentionNS, QLatin1String("Password"));
174 if (d->usePasswordDigest) {
175 writer.writeAttribute(QLatin1String("Type"),
176 QLatin1String("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"));
177 QByteArray passwordConcat = nonce + timestamp.toUtf8() + d->password.toUtf8();
178 QByteArray passwordHash = QCryptographicHash::hash(passwordConcat, QCryptographicHash::Sha1);
179 writer.writeCharacters(QString::fromLatin1(passwordHash.toBase64().constData()));
180 } else {
181 writer.writeAttribute(QLatin1String("Type"),
182 QLatin1String("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"));
183 writer.writeCharacters(d->password);
184 }
185 writer.writeEndElement();
186
187 writer.writeStartElement(securityExtentionNS, QLatin1String("Username"));
188 writer.writeCharacters(d->user);
189 writer.writeEndElement();
190
191 writer.writeEndElement();
192 writer.writeEndElement();
193}
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)

© 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 Sat Apr 20 2024 00:04:25 for KD SOAP API Documentation by doxygen 1.9.8