KD SOAP API Documentation: KDSoapAuthentication.cpp Source File

| 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****************************************************************************/
10#include "KDSoapAuthentication.h"
11#include "KDSoapNamespaceManager.h"
12#include "KDSoapNamespacePrefixes_p.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
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
33KDSoapAuthentication::KDSoapAuthentication()
34 : d(new Private)
35{
36 d->usePasswordDigest = true;
37}
38
39 KDSoapAuthentication::KDSoapAuthentication(const KDSoapAuthentication &other)
40 : d(new Private)
41{
42 *d = *other.d;
43}
44
45 KDSoapAuthentication &KDSoapAuthentication::operator=(const KDSoapAuthentication &other)
46{
47 *d = *other.d;
48 return *this;
49}
50
51KDSoapAuthentication::~KDSoapAuthentication()
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) // clazy:exclude=function-args-by-ref
77{
78 d->overrideWSUsernameCreatedTime = std::move(overrideWSUsernameCreatedTime);
79}
80
81 void KDSoapAuthentication::setOverrideWSUsernameNonce(QByteArray overrideWSUsernameNonce) // clazy:exclude=function-args-by-ref
82{
83 d->overrideWSUsernameNonce = std::move(overrideWSUsernameNonce);
84}
85
86 QString KDSoapAuthentication::user() const
87{
88 return d->user;
89}
90
91 QString KDSoapAuthentication::password() const
92{
93 return d->password;
94}
95
96 bool KDSoapAuthentication::usePasswordDigest() const
97{
98 return d->usePasswordDigest;
99}
100
101 bool KDSoapAuthentication::useWSUsernameToken() const
102{
103 return d->useWSUsernameToken;
104}
105
106 QDateTime KDSoapAuthentication::overrideWSUsernameCreatedTime() const
107{
108 return d->overrideWSUsernameCreatedTime;
109}
110
111 QByteArray KDSoapAuthentication::overrideWSUsernameNonce() const
112{
113 return d->overrideWSUsernameNonce;
114}
115
116 bool KDSoapAuthentication::hasAuth() const
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 }
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}
KDSoapAuthentication.h
KDSoapNamespaceManager.h
KDSoapNamespacePrefixes_p.h
KDSoapAuthentication
Definition KDSoapAuthentication.h:31
KDSoapAuthentication::useWSUsernameToken
bool useWSUsernameToken() const
Definition KDSoapAuthentication.cpp:101
KDSoapAuthentication::setPassword
void setPassword(const QString &password)
Definition KDSoapAuthentication.cpp:61
KDSoapAuthentication::setUseWSUsernameToken
void setUseWSUsernameToken(bool useWSUsernameToken)
Definition KDSoapAuthentication.cpp:71
KDSoapAuthentication::hasAuth
bool hasAuth() const
Definition KDSoapAuthentication.cpp:116
KDSoapAuthentication::setUsePasswordDigest
void setUsePasswordDigest(const bool usePasswordDigest)
Definition KDSoapAuthentication.cpp:66
KDSoapAuthentication::KDSoapAuthentication
KDSoapAuthentication()
Definition KDSoapAuthentication.cpp:33
KDSoapAuthentication::setOverrideWSUsernameCreatedTime
void setOverrideWSUsernameCreatedTime(QDateTime overrideWSUsernameCreatedTime)
Definition KDSoapAuthentication.cpp:76
KDSoapAuthentication::user
QString user() const
Definition KDSoapAuthentication.cpp:86
KDSoapAuthentication::setOverrideWSUsernameNonce
void setOverrideWSUsernameNonce(QByteArray overrideWSUsernameNonce)
Definition KDSoapAuthentication.cpp:81
KDSoapAuthentication::overrideWSUsernameCreatedTime
QDateTime overrideWSUsernameCreatedTime() const
Definition KDSoapAuthentication.cpp:106
KDSoapAuthentication::password
QString password() const
Definition KDSoapAuthentication.cpp:91
KDSoapAuthentication::operator=
KDSoapAuthentication & operator=(const KDSoapAuthentication &other)
Definition KDSoapAuthentication.cpp:45
KDSoapAuthentication::~KDSoapAuthentication
~KDSoapAuthentication()
Definition KDSoapAuthentication.cpp:51
KDSoapAuthentication::usePasswordDigest
bool usePasswordDigest() const
Definition KDSoapAuthentication.cpp:96
KDSoapAuthentication::overrideWSUsernameNonce
QByteArray overrideWSUsernameNonce() const
Definition KDSoapAuthentication.cpp:111
KDSoapAuthentication::setUser
void setUser(const QString &user)
Definition KDSoapAuthentication.cpp:56
KDSoapNamespaceManager::soapSecurityUtility
static QString soapSecurityUtility()
Definition KDSoapNamespaceManager.cpp:66
KDSoapNamespaceManager::soapSecurityExtention
static QString soapSecurityExtention()
Definition KDSoapNamespaceManager.cpp:61
QAuthenticator
QAuthenticator::setPassword
void setPassword(const QString &password)
QAuthenticator::setUser
void setUser(const QString &user)
QByteArray
QByteArray::constData
const char * constData() const const
QByteArray::number
QByteArray number(int n, int base)
QByteArray::toBase64
QByteArray toBase64(QByteArray::Base64Options options) const const
QCryptographicHash::Sha1
Sha1
QCryptographicHash::hash
QByteArray hash(const QByteArray &data, QCryptographicHash::Algorithm method)
QDateTime
QDateTime::currentDateTimeUtc
QDateTime currentDateTimeUtc()
QDateTime::toString
QString toString(Qt::DateFormat format) const const
QLatin1String
QNetworkReply
QObject::property
QVariant property(const char *name) const const
QObject::setProperty
bool setProperty(const char *name, const QVariant &value)
QRandomGenerator
QRandomGenerator::generate64
quint64 generate64()
QString
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QString::toUtf8
QByteArray toUtf8() const const
QVariant::toBool
bool toBool() const const
QXmlStreamWriter
QXmlStreamWriter::writeAttribute
void writeAttribute(const QString &qualifiedName, const QString &value)
QXmlStreamWriter::writeCharacters
void writeCharacters(const QString &text)
QXmlStreamWriter::writeEndElement
void writeEndElement()
QXmlStreamWriter::writeStartElement
void writeStartElement(const QString &qualifiedName)

© 2010-2024 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 Sun Jan 7 2024 11:47:36 for KD SOAP API Documentation by doxygen 1.9.8