Add a clear method to QAuthenticator

Provide an alternative for
*auth = QAuthenticator();

[ChangeLog][QtNetwork][QAuthenticator] Add a new method to clear all
credentials.

Fixes: QTBUG-139124
Change-Id: Ia33b5dcb8eb6961f5daa5bc78addd8c78dd10e9a
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Mate Barany 2025-08-21 16:23:37 +02:00
parent 6dfef00403
commit 745d968361
4 changed files with 62 additions and 0 deletions

View File

@ -358,6 +358,19 @@ bool QAuthenticator::isNull() const
return !d;
}
/*!
\since 6.11
Clears all credentials and resets the object to its default uninitialized
state.
*/
void QAuthenticator::clear() noexcept
{
*d = QAuthenticatorPrivate();
d->phase = QAuthenticatorPrivate::Done;
}
#if QT_CONFIG(sspi) // SSPI
class QSSPIWindowsHandles
{

View File

@ -43,6 +43,8 @@ public:
bool isNull() const;
void detach();
void clear() noexcept;
private:
friend class QAuthenticatorPrivate;
QAuthenticatorPrivate *d;

View File

@ -40,6 +40,30 @@ class Q_NETWORK_EXPORT QAuthenticatorPrivate
public:
enum Method { None, Basic, Negotiate, Ntlm, DigestMd5, };
QAuthenticatorPrivate();
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QAuthenticatorPrivate)
void swap(QAuthenticatorPrivate &other) noexcept
{
user.swap(other.user);
extractedUser.swap(other.extractedUser);
password.swap(other.password);
options.swap(other.options);
std::swap(method, other.method);
realm.swap(other.realm);
challenge.swap(other.challenge);
#if QT_CONFIG(sspi) // SSPI
sspiWindowsHandles.swap(other.sspiWindowsHandles);
#elif QT_CONFIG(gssapi) // GSSAPI
gssApiHandles.swap(other.gssApiHandles);
#endif
std::swap(hasFailed, other.hasFailed);
std::swap(phase, other.phase);
cnonce.swap(other.cnonce);
std::swap(nonceCount, other.nonceCount);
workstation.swap(other.workstation);
userDomain.swap(other.userDomain);
}
~QAuthenticatorPrivate();
QString user;

View File

@ -29,6 +29,8 @@ private Q_SLOTS:
void equalityOperators();
void isMethodSupported();
void testClear();
};
tst_QAuthenticator::tst_QAuthenticator()
@ -188,6 +190,27 @@ void tst_QAuthenticator::isMethodSupported()
QVERIFY(!QAuthenticatorPrivate::isMethodSupported("Bearer"));
}
void tst_QAuthenticator::testClear()
{
QAuthenticator qauth;
QVERIFY(qauth.isNull());
qauth.setUser("User");
qauth.setPassword("Password");
qauth.setRealm("Nether");
QVERIFY(!qauth.isNull());
QCOMPARE(qauth.user(), "User");
QCOMPARE(qauth.password(), "Password");
QCOMPARE(qauth.realm(), "Nether");
qauth.clear();
QVERIFY(!qauth.isNull());
QCOMPARE(qauth.user(), QString());
QCOMPARE(qauth.password(), QString());
QCOMPARE(qauth.realm(), QString());
}
QTEST_MAIN(tst_QAuthenticator);
#include "tst_qauthenticator.moc"