QXmpp Version: 1.10.0
|
The QXmppRegistrationManager class manages in-band registration and account management tasks like changing the password as defined in XEP-0077: In-Band Registration. More...
#include <QXmppRegistrationManager.h>
Signals | |
void | supportedByServerChanged () |
void | passwordChanged (const QString &newPassword) |
void | passwordChangeFailed (QXmppStanza::Error error) |
void | registrationFormReceived (const QXmppRegisterIq &iq) |
void | accountDeleted () |
void | accountDeletionFailed (QXmppStanza::Error error) |
void | registrationSucceeded () |
void | registrationFailed (const QXmppStanza::Error &error) |
Signals inherited from QXmppLoggable | |
void | setGauge (const QString &gauge, double value) |
Sets the given gauge to value. | |
void | logMessage (QXmppLogger::MessageType type, const QString &msg) |
This signal is emitted to send logging messages. | |
void | updateCounter (const QString &counter, qint64 amount=1) |
Updates the given counter by amount. | |
Public Member Functions | |
QXmppRegistrationManager () | |
QStringList | discoveryFeatures () const override |
void | changePassword (const QString &newPassword) |
void | deleteAccount () |
bool | supportedByServer () const |
void | requestRegistrationForm (const QString &service={}) |
void | setRegistrationFormToSend (const QXmppRegisterIq &iq) |
void | setRegistrationFormToSend (const QXmppDataForm &dataForm) |
void | sendCachedRegistrationForm () |
bool | registerOnConnectEnabled () const |
void | setRegisterOnConnectEnabled (bool enabled) |
Public Member Functions inherited from QXmppClientExtension | |
QXmppClientExtension () | |
virtual QStringList | discoveryFeatures () const |
virtual QList< QXmppDiscoveryIq::Identity > | discoveryIdentities () const |
virtual bool | handleStanza (const QDomElement &stanza) |
You need to implement this method to process incoming XMPP stanzas. More... | |
virtual bool | handleStanza (const QDomElement &stanza, const std::optional< QXmppE2eeMetadata > &e2eeMetadata) |
You need to implement this method to process incoming XMPP stanzas. More... | |
Public Member Functions inherited from QXmppLoggable | |
QXmppLoggable (QObject *parent=nullptr) | |
Protected Member Functions | |
void | onRegistered (QXmppClient *client) override |
void | onUnregistered (QXmppClient *client) override |
Protected Member Functions inherited from QXmppClientExtension | |
QXmppClient * | client () const |
virtual void | setClient (QXmppClient *client) |
virtual void | onRegistered (QXmppClient *client) |
virtual void | onUnregistered (QXmppClient *client) |
void | injectIq (const QDomElement &element, const std::optional< QXmppE2eeMetadata > &e2eeMetadata) |
bool | injectMessage (QXmppMessage &&message) |
Protected Member Functions inherited from QXmppLoggable | |
void | debug (const QString &message) |
void | info (const QString &message) |
void | warning (const QString &message) |
void | logReceived (const QString &message) |
void | logSent (const QString &message) |
Properties | |
bool | supportedByServer |
Whether support of XEP-0077: In-band Registration has been discovered on the server. | |
The QXmppRegistrationManager class manages in-band registration and account management tasks like changing the password as defined in XEP-0077: In-Band Registration.
To make use of this manager, you need to instantiate it and load it into the QXmppClient instance as follows:
This manager automatically recognizes whether the local server supports XEP-0077 (see supportedByServer()). You just need to request the service discovery information from the server on connect as below:
As soon as the result is retrieved, the supportedByServer() property should be correct and could be used to display the user whether account management tasks can be performed on this server.
However, this is not relevant if you only want to register a new account on a server.
To change the password of the current account changePassword() can be used. Upon that either passwordChanged() or passwordChangeFailed() is emitted.
If changing the password was successful, the new password is automatically set in the QXmppClient::configuration(), so reconnecting works properly.
Example:
If you want to delete your account on the server, you can do that using deleteAccount(). When the result is received either accountDeleted() or accountDeletionFailed() is emitted. In case it was successful, the manager automatically disconnects from the client. If the server takes too much time to confirm the account deletion, you may disconnect manually after a reasonable timeout.
QXmpp periodically sends pings to the server. If the server does not respond to it within QXmppConfiguration::keepAliveInterval(), QXmpp disconnects from the server. Make sure to handle that case if it happens during account deletion. E.g., you could try to connect to the server again with the same account and check whether QXmpp::AuthenticationError::NotAuthorized occurs. In that case, the account can be considered as deleted.
Registering with a server consists of multiple steps:
First of all, you need to enable the registration process in the registration manager, which of course needs to be activated in the client.
After that you can start to connect to the server you want to register with. No JID is set in the QXmppConfiguration for the client and instead only the server is set.
Alternatively, you can also provide a domain-only JID and no password to connectToServer():
Now as soon as (START)TLS was handled, the registration manager interrupts the normal connection process. The manager checks whether the server supports in-band registration and whether the server advertises this as a stream feature.
If the server does not support in-band registration, the manager will abort the connection at this point and emit the registrationFailed() signal with a fixed QXmppStanza::Error of type QXmppStanza::Error::Cancel and with a condition of QXmppStanza::Error::FeatureNotImplemented.
The manager will now request the registration form. This will either result in an error (reported by registrationFailed()) or, if everything went well, the registration form is reported by registrationFormReceived().
To handle everything correctly, you need to connect to both Q_SIGNALS:
Now you need to fill out the registration form. The server can close the connection during that time. It is due to some servers kicking unauthorized clients after some time when the clients are inactive. That is often the case when user interaction is required before the completed form is submitted to the server. In order to support account creation for both servers closing the connection and servers keeping it open, you need to handle those cases appropriately.
If the returned IQ contains a data form, that can be displayed to a user or can be filled out in another way.
If the server does not support data forms, you can check the standard fields of the QXmppRegisterIq. You need to search the fields for empty (non-null) strings. All fields that contain an empty string are required and can be filled out. You can just set values for those fields and send the form as described in the next step.
Option A: If the connection is still open once the form is filled out, set the form using setRegistrationFormToSend() and then trigger the form to be directly sent using sendCachedRegistrationForm().
Option B: If the connection is closed before the form is filled out, set the form using setRegistrationFormToSend() and connect to the server again. The registration manager will automatically send the set form once connected.
The form is now sent to the server. As soon as the result is received, either registrationSucceeded() or registrationFailed() is emitted.
In case there was a conflict or another error, you should request a new form and restart the process. This is especially important, if the form can only be used once as with most CAPTCHA implementations.
You need to disconnect now. The user can then enter their credentials and connect as usual.
It is also possible to extract username and password from the sent form, but that does not work always. There might also be forms that have no clear username or password fields.
QXmppRegistrationManager::QXmppRegistrationManager | ( | ) |
Default constructor.
|
signal |
Emitted, when the account was deleted successfully.
|
signal |
Emitted, when the account could not be deleted.
void QXmppRegistrationManager::changePassword | ( | const QString & | newPassword | ) |
Changes the password of the user's account.
newPassword | The new password to be set. This must not be empty. |
void QXmppRegistrationManager::deleteAccount | ( | ) |
Cancels an existing registration on the server.
|
overridevirtual |
This adds the jabber:iq:register
namespace to the features.
Reimplemented from QXmppClientExtension.
|
overrideprotectedvirtual |
Called after the extension has been added to a QXmppClient.
client |
Reimplemented from QXmppClientExtension.
|
overrideprotectedvirtual |
Called after the extension has been removed from a QXmppClient.
client |
Reimplemented from QXmppClientExtension.
|
signal |
Emitted, when the password of the account was changed successfully.
The new password is automatically set in QXmppClient::configuration().
newPassword | The new password that was set on the server. |
|
signal |
Emitted, when changing the password did not succeed.
error | Error returned from the service. |
bool QXmppRegistrationManager::registerOnConnectEnabled | ( | ) | const |
Returns whether to only request the registration form and not to connect with username/password.
|
signal |
Emitted, when the registration failed.
error | The returned error from the service. The reported errors might be different from server to server, but the common ones are the following:
|
|
signal |
Emitted, when a registration form has been received.
iq | The received form. If it does not contain a valid data form (see QXmppRegisterIq::form()), the required fields should be marked by empty (but not null) strings in the QXmppRegisterIq (i.e. QXmppRegisterIq::password().isNull() => false). |
|
signal |
Emitted, when the registration with a service completed successfully.
To connect with the account you still need to set the correct credentials in QXmppClient::configuration() and reconnect.
void QXmppRegistrationManager::requestRegistrationForm | ( | const QString & | service = {} | ) |
Requests the registration form for registering.
service | The service which the registration form should be requested from. If left empty, this will default to the local server. |
void QXmppRegistrationManager::sendCachedRegistrationForm | ( | ) |
Sends a completed registration form that was previously set using setRegistrationFormToSend().
You usually only need to set the form and the manager will automatically send it when connected. More details can be found in the documentation of the QXmppRegistrationManager.
void QXmppRegistrationManager::setRegisterOnConnectEnabled | ( | bool | enabled | ) |
Sets whether to only request the registration form and not to connect with username/password.
enabled | true to register, false to connect normally. |
void QXmppRegistrationManager::setRegistrationFormToSend | ( | const QXmppDataForm & | dataForm | ) |
Sets a registration form to be sent on the next connect with the server.
dataForm | The completed data form for registration. |
void QXmppRegistrationManager::setRegistrationFormToSend | ( | const QXmppRegisterIq & | iq | ) |
Sets a registration form to be sent on the next connect with the server.
iq | The completed registration form. |
bool QXmppRegistrationManager::supportedByServer | ( | ) | const |
Returns whether the server supports registration.
By default this is set to false and only changes, if you request the service discovery info of the connected server using QXmppDiscoveryManager::requestInfo().
This is only relevant to actions that happen after authentication.
|
signal |
Emitted, when registrationSupported() changed.
This can happen after the service discovery info of the server was retrieved using QXmppDiscoveryManager::requestInfo() or on disconnect.