libQuotient
A Qt library for building matrix clients
registration.h
Go to the documentation of this file.
1 // THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
2 
3 #pragma once
4 
5 #include <Quotient/csapi/definitions/auth_data.h>
6 #include <Quotient/csapi/definitions/request_email_validation.h>
7 #include <Quotient/csapi/definitions/request_msisdn_validation.h>
8 #include <Quotient/csapi/definitions/request_token_response.h>
9 
10 #include <Quotient/jobs/basejob.h>
11 
12 namespace Quotient {
13 
14 //! \brief Register for an account on this homeserver.
15 //!
16 //! This API endpoint uses the [User-Interactive Authentication
17 //! API](/client-server-api/#user-interactive-authentication-api), except in the cases where a guest
18 //! account is being registered.
19 //!
20 //! Register for an account on this homeserver.
21 //!
22 //! There are two kinds of user account:
23 //!
24 //! - `user` accounts. These accounts may use the full API described in this specification.
25 //!
26 //! - `guest` accounts. These accounts may have limited permissions and may not be supported by all
27 //! servers.
28 //!
29 //! If registration is successful, this endpoint will issue an access token
30 //! the client can use to authorize itself in subsequent requests.
31 //!
32 //! If the client does not supply a `device_id`, the server must
33 //! auto-generate one.
34 //!
35 //! The server SHOULD register an account with a User ID based on the
36 //! `username` provided, if any. Note that the grammar of Matrix User ID
37 //! localparts is restricted, so the server MUST either map the provided
38 //! `username` onto a `user_id` in a logical manner, or reject any
39 //! `username` which does not comply to the grammar with
40 //! `M_INVALID_USERNAME`.
41 //!
42 //! Matrix clients MUST NOT assume that localpart of the registered
43 //! `user_id` matches the provided `username`.
44 //!
45 //! The returned access token must be associated with the `device_id`
46 //! supplied by the client or generated by the server. The server may
47 //! invalidate any access token previously associated with that device. See
48 //! [Relationship between access tokens and
49 //! devices](/client-server-api/#relationship-between-access-tokens-and-devices).
50 //!
51 //! When registering a guest account, all parameters in the request body
52 //! with the exception of `initial_device_display_name` MUST BE ignored
53 //! by the server. The server MUST pick a `device_id` for the account
54 //! regardless of input.
55 //!
56 //! Any user ID returned by this API must conform to the grammar given in the
57 //! [Matrix specification](/appendices/#user-identifiers).
58 class QUOTIENT_API RegisterJob : public BaseJob {
59 public:
60  //! \param kind
61  //! The kind of account to register. Defaults to `user`.
62  //!
63  //! \param auth
64  //! Additional authentication information for the
65  //! user-interactive authentication API. Note that this
66  //! information is *not* used to define how the registered user
67  //! should be authenticated, but is instead used to
68  //! authenticate the `register` call itself.
69  //!
70  //! \param username
71  //! The basis for the localpart of the desired Matrix ID. If omitted,
72  //! the homeserver MUST generate a Matrix ID local part.
73  //!
74  //! \param password
75  //! The desired password for the account.
76  //!
77  //! \param deviceId
78  //! ID of the client device. If this does not correspond to a
79  //! known client device, a new device will be created. The server
80  //! will auto-generate a device_id if this is not specified.
81  //!
82  //! \param initialDeviceDisplayName
83  //! A display name to assign to the newly-created device. Ignored
84  //! if `device_id` corresponds to a known device.
85  //!
86  //! \param inhibitLogin
87  //! If true, an `access_token` and `device_id` should not be
88  //! returned from this call, therefore preventing an automatic
89  //! login. Defaults to false.
90  //!
91  //! \param refreshToken
92  //! If true, the client supports refresh tokens.
93  explicit RegisterJob(const QString& kind = u"user"_s,
94  const std::optional<AuthenticationData>& auth = std::nullopt,
95  const QString& username = {}, const QString& password = {},
96  const QString& deviceId = {}, const QString& initialDeviceDisplayName = {},
97  std::optional<bool> inhibitLogin = std::nullopt,
98  std::optional<bool> refreshToken = std::nullopt);
99 
100  // Result properties
101 
102  //! The fully-qualified Matrix user ID (MXID) that has been registered.
103  //!
104  //! Any user ID returned by this API must conform to the grammar given in the
105  //! [Matrix specification](/appendices/#user-identifiers).
106  QString userId() const { return loadFromJson<QString>("user_id"_L1); }
107 
108  //! An access token for the account.
109  //! This access token can then be used to authorize other requests.
110  //! Required if the `inhibit_login` option is false.
111  QString accessToken() const { return loadFromJson<QString>("access_token"_L1); }
112 
113  //! A refresh token for the account. This token can be used to
114  //! obtain a new access token when it expires by calling the
115  //! `/refresh` endpoint.
116  //!
117  //! Omitted if the `inhibit_login` option is true.
118  QString refreshToken() const { return loadFromJson<QString>("refresh_token"_L1); }
119 
120  //! The lifetime of the access token, in milliseconds. Once
121  //! the access token has expired a new access token can be
122  //! obtained by using the provided refresh token. If no
123  //! refresh token is provided, the client will need to re-log in
124  //! to obtain a new access token. If not given, the client can
125  //! assume that the access token will not expire.
126  //!
127  //! Omitted if the `inhibit_login` option is true.
128  std::optional<int> expiresInMs() const
129  {
130  return loadFromJson<std::optional<int>>("expires_in_ms"_L1);
131  }
132 
133  //! ID of the registered device. Will be the same as the
134  //! corresponding parameter in the request, if one was specified.
135  //! Required if the `inhibit_login` option is false.
136  QString deviceId() const { return loadFromJson<QString>("device_id"_L1); }
137 
138  struct Response {
139  //! The fully-qualified Matrix user ID (MXID) that has been registered.
140  //!
141  //! Any user ID returned by this API must conform to the grammar given in the
142  //! [Matrix specification](/appendices/#user-identifiers).
143  QString userId{};
144 
145  //! An access token for the account.
146  //! This access token can then be used to authorize other requests.
147  //! Required if the `inhibit_login` option is false.
148  QString accessToken{};
149 
150  //! A refresh token for the account. This token can be used to
151  //! obtain a new access token when it expires by calling the
152  //! `/refresh` endpoint.
153  //!
154  //! Omitted if the `inhibit_login` option is true.
155  QString refreshToken{};
156 
157  //! The lifetime of the access token, in milliseconds. Once
158  //! the access token has expired a new access token can be
159  //! obtained by using the provided refresh token. If no
160  //! refresh token is provided, the client will need to re-log in
161  //! to obtain a new access token. If not given, the client can
162  //! assume that the access token will not expire.
163  //!
164  //! Omitted if the `inhibit_login` option is true.
165  std::optional<int> expiresInMs{};
166 
167  //! ID of the registered device. Will be the same as the
168  //! corresponding parameter in the request, if one was specified.
169  //! Required if the `inhibit_login` option is false.
170  QString deviceId{};
171  };
172 };
173 
174 template <std::derived_from<RegisterJob> JobT>
175 constexpr inline auto doCollectResponse<JobT> = [](JobT* j) -> RegisterJob::Response {
176  return { j->userId(), j->accessToken(), j->refreshToken(), j->expiresInMs(), j->deviceId() };
177 };
178 
179 //! \brief Begins the validation process for an email to be used during registration.
180 //!
181 //! The homeserver must check that the given email address is **not**
182 //! already associated with an account on this homeserver. The homeserver
183 //! should validate the email itself, either by sending a validation email
184 //! itself or by using a service it has control over.
185 class QUOTIENT_API RequestTokenToRegisterEmailJob : public BaseJob {
186 public:
187  explicit RequestTokenToRegisterEmailJob(const EmailValidationData& data);
188 
189  // Result properties
190 
191  //! An email has been sent to the specified address. Note that this
192  //! may be an email containing the validation token or it may be
193  //! informing the user of an error.
194  RequestTokenResponse response() const { return fromJson<RequestTokenResponse>(jsonData()); }
195 };
196 
197 inline auto collectResponse(const RequestTokenToRegisterEmailJob* job) { return job->response(); }
198 
199 //! \brief Requests a validation token be sent to the given phone number for the purpose of
200 //! registering an account
201 //!
202 //! The homeserver must check that the given phone number is **not**
203 //! already associated with an account on this homeserver. The homeserver
204 //! should validate the phone number itself, either by sending a validation
205 //! message itself or by using a service it has control over.
206 class QUOTIENT_API RequestTokenToRegisterMSISDNJob : public BaseJob {
207 public:
208  explicit RequestTokenToRegisterMSISDNJob(const MsisdnValidationData& data);
209 
210  // Result properties
211 
212  //! An SMS message has been sent to the specified phone number. Note
213  //! that this may be an SMS message containing the validation token or
214  //! it may be informing the user of an error.
215  RequestTokenResponse response() const { return fromJson<RequestTokenResponse>(jsonData()); }
216 };
217 
218 inline auto collectResponse(const RequestTokenToRegisterMSISDNJob* job) { return job->response(); }
219 
220 //! \brief Changes a user's password.
221 //!
222 //! Changes the password for an account on this homeserver.
223 //!
224 //! This API endpoint uses the [User-Interactive Authentication
225 //! API](/client-server-api/#user-interactive-authentication-api) to ensure the user changing the
226 //! password is actually the owner of the account.
227 //!
228 //! An access token should be submitted to this endpoint if the client has
229 //! an active session.
230 //!
231 //! The homeserver may change the flows available depending on whether a
232 //! valid access token is provided. The homeserver SHOULD NOT revoke the
233 //! access token provided in the request. Whether other access tokens for
234 //! the user are revoked depends on the request parameters.
235 class QUOTIENT_API ChangePasswordJob : public BaseJob {
236 public:
237  //! \param newPassword
238  //! The new password for the account.
239  //!
240  //! \param logoutDevices
241  //! Whether the user's other access tokens, and their associated devices, should be
242  //! revoked if the request succeeds.
243  //!
244  //! When `false`, the server can still take advantage of the [soft logout
245  //! method](/client-server-api/#soft-logout) for the user's remaining devices.
246  //!
247  //! \param auth
248  //! Additional authentication information for the user-interactive authentication API.
249  explicit ChangePasswordJob(const QString& newPassword, bool logoutDevices = true,
250  const std::optional<AuthenticationData>& auth = std::nullopt);
251 };
252 
253 //! \brief Requests a validation token be sent to the given email address for the purpose of
254 //! resetting a user's password
255 //!
256 //! The homeserver must check that the given email address **is
257 //! associated** with an account on this homeserver. This API should be
258 //! used to request validation tokens when authenticating for the
259 //! `/account/password` endpoint.
260 //!
261 //! This API's parameters and response are identical to that of the
262 //! [`/register/email/requestToken`](/client-server-api/#post_matrixclientv3registeremailrequesttoken)
263 //! endpoint, except that
264 //! `M_THREEPID_NOT_FOUND` may be returned if no account matching the
265 //! given email address could be found. The server may instead send an
266 //! email to the given address prompting the user to create an account.
267 //! `M_THREEPID_IN_USE` may not be returned.
268 //!
269 //! The homeserver should validate the email itself, either by sending a
270 //! validation email itself or by using a service it has control over.
271 class QUOTIENT_API RequestTokenToResetPasswordEmailJob : public BaseJob {
272 public:
273  explicit RequestTokenToResetPasswordEmailJob(const EmailValidationData& data);
274 
275  // Result properties
276 
277  //! An email was sent to the given address.
278  RequestTokenResponse response() const { return fromJson<RequestTokenResponse>(jsonData()); }
279 };
280 
281 inline auto collectResponse(const RequestTokenToResetPasswordEmailJob* job)
282 {
283  return job->response();
284 }
285 
286 //! \brief Requests a validation token be sent to the given phone number for the purpose of
287 //! resetting a user's password.
288 //!
289 //! The homeserver must check that the given phone number **is
290 //! associated** with an account on this homeserver. This API should be
291 //! used to request validation tokens when authenticating for the
292 //! `/account/password` endpoint.
293 //!
294 //! This API's parameters and response are identical to that of the
295 //! [`/register/msisdn/requestToken`](/client-server-api/#post_matrixclientv3registermsisdnrequesttoken)
296 //! endpoint, except that
297 //! `M_THREEPID_NOT_FOUND` may be returned if no account matching the
298 //! given phone number could be found. The server may instead send the SMS
299 //! to the given phone number prompting the user to create an account.
300 //! `M_THREEPID_IN_USE` may not be returned.
301 //!
302 //! The homeserver should validate the phone number itself, either by sending a
303 //! validation message itself or by using a service it has control over.
304 class QUOTIENT_API RequestTokenToResetPasswordMSISDNJob : public BaseJob {
305 public:
306  explicit RequestTokenToResetPasswordMSISDNJob(const MsisdnValidationData& data);
307 
308  // Result properties
309 
310  //! An SMS message was sent to the given phone number.
311  RequestTokenResponse response() const { return fromJson<RequestTokenResponse>(jsonData()); }
312 };
313 
314 inline auto collectResponse(const RequestTokenToResetPasswordMSISDNJob* job)
315 {
316  return job->response();
317 }
318 
319 //! \brief Deactivate a user's account.
320 //!
321 //! Deactivate the user's account, removing all ability for the user to
322 //! login again.
323 //!
324 //! This API endpoint uses the [User-Interactive Authentication
325 //! API](/client-server-api/#user-interactive-authentication-api).
326 //!
327 //! An access token should be submitted to this endpoint if the client has
328 //! an active session.
329 //!
330 //! The homeserver may change the flows available depending on whether a
331 //! valid access token is provided.
332 //!
333 //! Unlike other endpoints, this endpoint does not take an `id_access_token`
334 //! parameter because the homeserver is expected to sign the request to the
335 //! identity server instead.
336 class QUOTIENT_API DeactivateAccountJob : public BaseJob {
337 public:
338  //! \param auth
339  //! Additional authentication information for the user-interactive authentication API.
340  //!
341  //! \param idServer
342  //! The identity server to unbind all of the user's 3PIDs from.
343  //! If not provided, the homeserver MUST use the `id_server`
344  //! that was originally use to bind each identifier. If the
345  //! homeserver does not know which `id_server` that was,
346  //! it must return an `id_server_unbind_result` of
347  //! `no-support`.
348  //!
349  //! \param erase
350  //! Whether the user would like their content to be erased as
351  //! much as possible from the server.
352  //!
353  //! Erasure means that any users (or servers) which join the
354  //! room after the erasure request are served redacted copies of
355  //! the events sent by this account. Users which had visibility
356  //! on those events prior to the erasure are still able to see
357  //! unredacted copies. No redactions are sent and the erasure
358  //! request is not shared over federation, so other servers
359  //! might still serve unredacted copies.
360  //!
361  //! The server should additionally erase any non-event data
362  //! associated with the user, such as [account data](/client-server-api/#client-config)
363  //! and [contact
364  //! 3PIDs](/client-server-api/#adding-account-administrative-contact-information).
365  //!
366  //! Defaults to `false` if not present.
367  explicit DeactivateAccountJob(const std::optional<AuthenticationData>& auth = std::nullopt,
368  const QString& idServer = {},
369  std::optional<bool> erase = std::nullopt);
370 
371  // Result properties
372 
373  //! An indicator as to whether or not the homeserver was able to unbind
374  //! the user's 3PIDs from the identity server(s). `success` indicates
375  //! that all identifiers have been unbound from the identity server while
376  //! `no-support` indicates that one or more identifiers failed to unbind
377  //! due to the identity server refusing the request or the homeserver
378  //! being unable to determine an identity server to unbind from. This
379  //! must be `success` if the homeserver has no identifiers to unbind
380  //! for the user.
381  QString idServerUnbindResult() const
382  {
383  return loadFromJson<QString>("id_server_unbind_result"_L1);
384  }
385 };
386 
387 inline auto collectResponse(const DeactivateAccountJob* job) { return job->idServerUnbindResult(); }
388 
389 //! \brief Checks to see if a username is available on the server.
390 //!
391 //! Checks to see if a username is available, and valid, for the server.
392 //!
393 //! The server should check to ensure that, at the time of the request, the
394 //! username requested is available for use. This includes verifying that an
395 //! application service has not claimed the username and that the username
396 //! fits the server's desired requirements (for example, a server could dictate
397 //! that it does not permit usernames with underscores).
398 //!
399 //! Matrix clients may wish to use this API prior to attempting registration,
400 //! however the clients must also be aware that using this API does not normally
401 //! reserve the username. This can mean that the username becomes unavailable
402 //! between checking its availability and attempting to register it.
403 class QUOTIENT_API CheckUsernameAvailabilityJob : public BaseJob {
404 public:
405  //! \param username
406  //! The username to check the availability of.
407  explicit CheckUsernameAvailabilityJob(const QString& username);
408 
409  //! \brief Construct a URL without creating a full-fledged job object
410  //!
411  //! This function can be used when a URL for CheckUsernameAvailabilityJob
412  //! is necessary but the job itself isn't.
413  static QUrl makeRequestUrl(const HomeserverData& hsData, const QString& username);
414 
415  // Result properties
416 
417  //! A flag to indicate that the username is available. This should always
418  //! be `true` when the server replies with 200 OK.
419  std::optional<bool> available() const
420  {
421  return loadFromJson<std::optional<bool>>("available"_L1);
422  }
423 };
424 
425 inline auto collectResponse(const CheckUsernameAvailabilityJob* job) { return job->available(); }
426 
427 } // namespace Quotient