Skip to main content

ruma_client_api/account/
register.rs

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