summaryrefslogtreecommitdiffstats
path: root/src/network/access
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@qt.io>2023-08-24 10:12:27 +0300
committerJuha Vuolle <juha.vuolle@qt.io>2023-12-08 15:53:35 +0200
commit98b240d00a8e68c1d4f159d7ebb5f7802c442a8a (patch)
treebfd51203bea1f0b8db9d1f363c10665a2f631d7d /src/network/access
parentb72701a69070917e743f10d08fa5327862e174d6 (diff)
Add username/password support to QNetworkRequestFactory
Task-number: QTBUG-114717 Change-Id: I8d6beb6f81668dcba59cbaee6044606fb874bad2 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/network/access')
-rw-r--r--src/network/access/qnetworkrequestfactory.cpp88
-rw-r--r--src/network/access/qnetworkrequestfactory.h8
-rw-r--r--src/network/access/qnetworkrequestfactory_p.h2
3 files changed, 98 insertions, 0 deletions
diff --git a/src/network/access/qnetworkrequestfactory.cpp b/src/network/access/qnetworkrequestfactory.cpp
index 2e136c4334..108ac85794 100644
--- a/src/network/access/qnetworkrequestfactory.cpp
+++ b/src/network/access/qnetworkrequestfactory.cpp
@@ -350,6 +350,86 @@ void QNetworkRequestFactory::clearBearerToken()
}
/*!
+ Returns the username set to this factory.
+
+ \sa setUserName(), clearUserName(), password()
+*/
+QString QNetworkRequestFactory::userName() const
+{
+ return d->userName;
+}
+
+/*!
+ Sets the username of this factory to \a userName.
+
+ The username is set in the request URL when \l request() is called.
+ The QRestAccessManager / QNetworkAccessManager will attempt to use
+ these credentials when the server indicates that authentication
+ is required.
+
+ \sa userName(), clearUserName(), password()
+*/
+void QNetworkRequestFactory::setUserName(const QString &userName)
+{
+ if (d->userName == userName)
+ return;
+ d.detach();
+ d->userName = userName;
+}
+
+/*!
+ Clears the username set to this factory.
+*/
+void QNetworkRequestFactory::clearUserName()
+{
+ if (d->userName.isEmpty())
+ return;
+ d.detach();
+ d->userName.clear();
+}
+
+/*!
+ Returns the password set to this factory.
+
+ \sa password(), clearPassword(), userName()
+*/
+QString QNetworkRequestFactory::password() const
+{
+ return d->password;
+}
+
+/*!
+ Sets the password of this factory to \a password.
+
+ The password is set in the request URL when \l request() is called.
+ The QRestAccessManager / QNetworkAccessManager will attempt to use
+ these credentials when the server indicates that authentication
+ is required.
+
+ \sa password(), clearPassword(), userName()
+*/
+void QNetworkRequestFactory::setPassword(const QString &password)
+{
+ if (d->password == password)
+ return;
+ d.detach();
+ d->password = password;
+}
+
+/*!
+ Clears the password set to this factory.
+
+ \sa password(), setPassword(), userName()
+*/
+void QNetworkRequestFactory::clearPassword()
+{
+ if (d->password.isEmpty())
+ return;
+ d.detach();
+ d->password.clear();
+}
+
+/*!
Sets \a timeout used for transfers.
\sa transferTimeout(), QNetworkRequest::setTransferTimeout(),
@@ -471,6 +551,10 @@ QUrl QNetworkRequestFactoryPrivate::requestUrl(const QString *path,
QUrl resultUrl = baseUrl;
QUrlQuery resultQuery(providedQuery);
QString basePath = baseUrl.path();
+
+ resultUrl.setUserName(userName);
+ resultUrl.setPassword(password);
+
// Separate the path and query parameters components on the application-provided path
const QString requestPath{providedPath.path()};
const QUrlQuery pathQueryItems{providedPath};
@@ -518,6 +602,8 @@ bool QNetworkRequestFactoryPrivate::equals(
#endif
baseUrl == other.baseUrl &&
bearerToken == other.bearerToken &&
+ userName == other.userName &&
+ password == other.password &&
headers.equals(other.headers) &&
queryParameters == other.queryParameters;
}
@@ -541,6 +627,8 @@ QDebug operator<<(QDebug debug, const QNetworkRequestFactory &factory)
<< ", queryParameters = " << factory.queryParameters().queryItems()
<< ", bearerToken = " << (factory.bearerToken().isEmpty() ? "(empty)" : "(is set)")
<< ", transferTimeout = " << factory.transferTimeout()
+ << ", userName = " << (factory.userName().isEmpty() ? "(empty)" : "(is set)")
+ << ", password = " << (factory.password().isEmpty() ? "(empty)" : "(is set)")
#if QT_CONFIG(ssl)
<< ", SSL configuration"
<< (factory.sslConfiguration().isNull() ? " is not set (default)" : " is set")
diff --git a/src/network/access/qnetworkrequestfactory.h b/src/network/access/qnetworkrequestfactory.h
index 0c08ce42a6..324e811ddc 100644
--- a/src/network/access/qnetworkrequestfactory.h
+++ b/src/network/access/qnetworkrequestfactory.h
@@ -59,6 +59,14 @@ public:
Q_NETWORK_EXPORT void setBearerToken(const QByteArray &token);
Q_NETWORK_EXPORT void clearBearerToken();
+ Q_NETWORK_EXPORT QString userName() const;
+ Q_NETWORK_EXPORT void setUserName(const QString &userName);
+ Q_NETWORK_EXPORT void clearUserName();
+
+ Q_NETWORK_EXPORT QString password() const;
+ Q_NETWORK_EXPORT void setPassword(const QString &password);
+ Q_NETWORK_EXPORT void clearPassword();
+
Q_NETWORK_EXPORT void setTransferTimeout(std::chrono::milliseconds timeout);
Q_NETWORK_EXPORT std::chrono::milliseconds transferTimeout() const;
diff --git a/src/network/access/qnetworkrequestfactory_p.h b/src/network/access/qnetworkrequestfactory_p.h
index 8378669ce9..c4fb33e8b2 100644
--- a/src/network/access/qnetworkrequestfactory_p.h
+++ b/src/network/access/qnetworkrequestfactory_p.h
@@ -42,6 +42,8 @@ public:
QUrl baseUrl;
QHttpHeaders headers;
QByteArray bearerToken;
+ QString userName;
+ QString password;
QUrlQuery queryParameters;
std::chrono::milliseconds transferTimeout{0};
};