KD SOAP API Documentation  2.1
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-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 #include "KDSoapAuthentication.h"
11 #include "KDSoapNamespaceManager.h"
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 
22 class KDSoapAuthentication::Private
23 {
24 public:
25  QString user;
26  QString password;
27  bool usePasswordDigest = false;
28  bool useWSUsernameToken = false;
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 
52 {
53  delete d;
54 }
55 
56 void KDSoapAuthentication::setUser(const QString &user)
57 {
58  d->user = user;
59 }
60 
61 void KDSoapAuthentication::setPassword(const QString &password)
62 {
63  d->password = password;
64 }
65 
66 void KDSoapAuthentication::setUsePasswordDigest(const bool usePasswordDigest)
67 {
68  d->usePasswordDigest = usePasswordDigest;
69 }
70 
71 void KDSoapAuthentication::setUseWSUsernameToken(bool useWSUsernameToken)
72 {
73  d->useWSUsernameToken = useWSUsernameToken;
74 }
75 
76 void KDSoapAuthentication::setOverrideWSUsernameCreatedTime(QDateTime overrideWSUsernameCreatedTime)
77 {
78  d->overrideWSUsernameCreatedTime = overrideWSUsernameCreatedTime;
79 }
80 
81 void KDSoapAuthentication::setOverrideWSUsernameNonce(QByteArray overrideWSUsernameNonce)
82 {
83  d->overrideWSUsernameNonce = 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 
121 void 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 
134 bool KDSoapAuthentication::hasWSUsernameTokenHeader() const
135 {
136  return hasAuth() && d->useWSUsernameToken;
137 }
138 
139 void 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  }
156  QDateTime time = QDateTime::currentDateTimeUtc();
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"));
166  writer.writeCharacters(QString::fromLatin1(nonce.toBase64().constData()));
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)
static QString soapSecurityUtility()
static QString soapSecurityExtention()

© 2010-2022 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 Jun 13 2023 12:18:34 for KD SOAP API Documentation by doxygen 1.9.1