ruma_client_api/account/
register.rs

1//! `POST /_matrix/client/*/register`
2//!
3//! Register an account on this homeserver.
4
5use serde::{Deserialize, Serialize};
6
7pub mod v3 {
8    //! `/v3/` ([spec])
9    //!
10    //! [spec]: https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3register
11
12    use std::time::Duration;
13
14    use ruma_common::{
15        api::{request, response, Metadata},
16        metadata, OwnedDeviceId, OwnedUserId,
17    };
18
19    use super::{LoginType, RegistrationKind};
20    use crate::uiaa::{AuthData, UiaaResponse};
21
22    const METADATA: Metadata = metadata! {
23        method: POST,
24        rate_limited: true,
25        authentication: AppserviceTokenOptional,
26        history: {
27            1.0 => "/_matrix/client/r0/register",
28            1.1 => "/_matrix/client/v3/register",
29        }
30    };
31
32    /// Request type for the `register` endpoint.
33    #[request(error = UiaaResponse)]
34    #[derive(Default)]
35    pub struct Request {
36        /// The desired password for the account.
37        ///
38        /// May be empty for accounts that should not be able to log in again
39        /// with a password, e.g., for guest or application service accounts.
40        #[serde(skip_serializing_if = "Option::is_none")]
41        pub password: Option<String>,
42
43        /// Localpart of the desired Matrix ID.
44        ///
45        /// If omitted, the homeserver MUST generate a Matrix ID local part.
46        #[serde(skip_serializing_if = "Option::is_none")]
47        pub username: Option<String>,
48
49        /// ID of the client device.
50        ///
51        /// If this does not correspond to a known client device, a new device will be created.
52        /// The server will auto-generate a device_id if this is not specified.
53        #[serde(skip_serializing_if = "Option::is_none")]
54        pub device_id: Option<OwnedDeviceId>,
55
56        /// A display name to assign to the newly-created device.
57        ///
58        /// Ignored if `device_id` corresponds to a known device.
59        #[serde(skip_serializing_if = "Option::is_none")]
60        pub initial_device_display_name: Option<String>,
61
62        /// Additional authentication information for the user-interactive authentication API.
63        ///
64        /// Note that this information is not used to define how the registered user should be
65        /// authenticated, but is instead used to authenticate the register call itself.
66        /// It should be left empty, or omitted, unless an earlier call returned an response
67        /// with status code 401.
68        #[serde(skip_serializing_if = "Option::is_none")]
69        pub auth: Option<AuthData>,
70
71        /// Kind of account to register
72        ///
73        /// Defaults to `User` if omitted.
74        #[ruma_api(query)]
75        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
76        pub kind: RegistrationKind,
77
78        /// If `true`, an `access_token` and `device_id` should not be returned
79        /// from this call, therefore preventing an automatic login.
80        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
81        pub inhibit_login: bool,
82
83        /// Login `type` used by Appservices.
84        ///
85        /// Appservices can [bypass the registration flows][admin] entirely by providing their
86        /// token in the header and setting this login `type` to `m.login.application_service`.
87        ///
88        /// [admin]: https://spec.matrix.org/latest/application-service-api/#server-admin-style-permissions
89        #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
90        pub login_type: Option<LoginType>,
91
92        /// If set to `true`, the client supports [refresh tokens].
93        ///
94        /// [refresh tokens]: https://spec.matrix.org/latest/client-server-api/#refreshing-access-tokens
95        #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
96        pub refresh_token: bool,
97
98        /// A [guest user]'s access token, to upgrade to a regular account.
99        ///
100        /// If this is set, the `username` field is also required.
101        ///
102        /// [guest user]: https://spec.matrix.org/latest/client-server-api/#guest-access
103        #[serde(skip_serializing_if = "Option::is_none")]
104        pub guest_access_token: Option<String>,
105    }
106
107    /// Response type for the `register` endpoint.
108    #[response(error = UiaaResponse)]
109    pub struct Response {
110        /// An access token for the account.
111        ///
112        /// This access token can then be used to authorize other requests.
113        ///
114        /// Required if the request's `inhibit_login` was set to `false`.
115        #[serde(skip_serializing_if = "Option::is_none")]
116        pub access_token: Option<String>,
117
118        /// The fully-qualified Matrix ID that has been registered.
119        pub user_id: OwnedUserId,
120
121        /// ID of the registered device.
122        ///
123        /// Will be the same as the corresponding parameter in the request, if one was specified.
124        ///
125        /// Required if the request's `inhibit_login` was set to `false`.
126        pub device_id: Option<OwnedDeviceId>,
127
128        /// A [refresh token] for the account.
129        ///
130        /// This token can be used to obtain a new access token when it expires by calling the
131        /// [`refresh_token`] endpoint.
132        ///
133        /// Omitted if the request's `inhibit_login` was set to `true`.
134        ///
135        /// [refresh token]: https://spec.matrix.org/latest/client-server-api/#refreshing-access-tokens
136        /// [`refresh_token`]: crate::session::refresh_token
137        #[serde(skip_serializing_if = "Option::is_none")]
138        pub refresh_token: Option<String>,
139
140        /// The lifetime of the access token, in milliseconds.
141        ///
142        /// Once the access token has expired, a new access token can be obtained by using the
143        /// provided refresh token. If no refresh token is provided, the client will need to
144        /// re-login to obtain a new access token.
145        ///
146        /// If this is `None`, the client can assume that the access token will not expire.
147        ///
148        /// Omitted if the request's `inhibit_login` was set to `true`.
149        #[serde(
150            with = "ruma_common::serde::duration::opt_ms",
151            default,
152            skip_serializing_if = "Option::is_none",
153            rename = "expires_in_ms"
154        )]
155        pub expires_in: Option<Duration>,
156    }
157
158    impl Request {
159        /// Creates a new `Request` with all parameters defaulted.
160        pub fn new() -> Self {
161            Default::default()
162        }
163    }
164
165    impl Response {
166        /// Creates a new `Response` with the given user ID.
167        pub fn new(user_id: OwnedUserId) -> Self {
168            Self {
169                access_token: None,
170                user_id,
171                device_id: None,
172                refresh_token: None,
173                expires_in: None,
174            }
175        }
176    }
177}
178
179/// The kind of account being registered.
180#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
181#[serde(rename_all = "snake_case")]
182#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
183pub enum RegistrationKind {
184    /// A guest account
185    ///
186    /// These accounts may have limited permissions and may not be supported by all servers.
187    Guest,
188
189    /// A regular user account
190    #[default]
191    User,
192}
193
194/// The login type.
195#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
196#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
197pub enum LoginType {
198    /// An appservice-specific login type
199    #[serde(rename = "m.login.application_service")]
200    ApplicationService,
201}