2026-05-06 14:00:41 -04:00
|
|
|
use std::collections::HashMap;
|
2024-05-03 21:42:47 -04:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2026-04-23 20:02:48 +01:00
|
|
|
use axum_client_ip::ClientIp;
|
2024-12-15 00:05:47 -05:00
|
|
|
use conduwuit::{
|
2026-05-06 14:00:41 -04:00
|
|
|
Err, Result, debug_info, info,
|
2026-03-21 21:33:23 -04:00
|
|
|
utils::{self},
|
2024-12-15 00:05:47 -05:00
|
|
|
};
|
2025-04-04 03:30:13 +00:00
|
|
|
use conduwuit_service::Services;
|
2026-05-06 14:00:41 -04:00
|
|
|
use futures::StreamExt;
|
2026-03-22 20:51:14 -04:00
|
|
|
use lettre::{Address, message::Mailbox};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
|
|
|
|
api::client::{
|
2026-03-22 20:51:14 -04:00
|
|
|
account::{
|
2026-05-05 13:35:35 -04:00
|
|
|
register::{self, LoginType, RegistrationKind},
|
2026-03-22 20:51:14 -04:00
|
|
|
request_registration_token_via_email,
|
|
|
|
|
},
|
2026-03-21 20:59:41 -04:00
|
|
|
uiaa::{AuthFlow, AuthType},
|
2025-07-02 19:32:50 +01:00
|
|
|
},
|
2026-04-07 12:57:36 -04:00
|
|
|
assign,
|
2020-07-30 18:14:47 +02:00
|
|
|
};
|
2026-03-21 20:59:41 -04:00
|
|
|
use serde_json::value::RawValue;
|
2026-05-05 09:09:38 -04:00
|
|
|
use service::{mailer::messages, users::HashedPassword};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2026-05-05 14:18:32 -04:00
|
|
|
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
|
2024-07-22 07:43:51 +00:00
|
|
|
use crate::Ruma;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-02-15 20:47:12 -05:00
|
|
|
/// # `POST /_matrix/client/v3/register`
|
2020-07-31 14:40:28 +02:00
|
|
|
///
|
|
|
|
|
/// Register an account on this homeserver.
|
|
|
|
|
///
|
2024-02-15 20:47:12 -05:00
|
|
|
/// You can use [`GET
|
|
|
|
|
/// /_matrix/client/v3/register/available`](fn.get_register_available_route.
|
2021-08-31 19:14:37 +02:00
|
|
|
/// html) to check if the user id is valid and available.
|
2024-03-22 22:44:31 -04:00
|
|
|
#[allow(clippy::doc_markdown)]
|
2026-01-04 03:04:37 +00:00
|
|
|
#[tracing::instrument(skip_all, fields(%client), name = "register", level = "info")]
|
2024-06-10 21:04:51 -04:00
|
|
|
pub(crate) async fn register_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
2026-04-23 20:02:48 +01:00
|
|
|
ClientIp(client): ClientIp,
|
2024-12-15 00:05:47 -05:00
|
|
|
body: Ruma<register::v3::Request>,
|
2024-06-10 21:04:51 -04:00
|
|
|
) -> Result<register::v3::Response> {
|
2026-05-05 13:35:35 -04:00
|
|
|
if body.kind != RegistrationKind::User {
|
|
|
|
|
return Err!(Request(GuestAccessForbidden("Guests may not register on this server.")));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 18:24:24 -05:00
|
|
|
// Allow registration if it's enabled in the config file or if this is the first
|
|
|
|
|
// run (so the first user account can be created)
|
|
|
|
|
let allow_registration =
|
|
|
|
|
services.config.allow_registration || services.firstrun.is_first_run();
|
|
|
|
|
|
2026-05-26 13:28:23 -04:00
|
|
|
if !allow_registration && body.identity.is_none() {
|
2024-01-24 22:29:30 -05:00
|
|
|
info!(
|
2026-05-05 09:09:38 -04:00
|
|
|
?body.username,
|
|
|
|
|
?body.initial_device_display_name,
|
|
|
|
|
"Rejecting registration attempt as registration is disabled"
|
2024-01-24 22:29:30 -05:00
|
|
|
);
|
2023-12-21 20:44:58 -05:00
|
|
|
|
2026-01-06 11:51:12 -05:00
|
|
|
return Err!(Request(Forbidden(
|
|
|
|
|
"This server is not accepting registrations at this time."
|
|
|
|
|
)));
|
2023-12-21 21:16:56 -05:00
|
|
|
}
|
|
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
let user_id = if body.body.login_type == Some(LoginType::ApplicationService) {
|
|
|
|
|
let Some(appservice_info) = &body.identity else {
|
|
|
|
|
return Err!(Request(Forbidden(
|
|
|
|
|
"Only appservices can use the appservice login type."
|
|
|
|
|
)));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let user_id = services
|
|
|
|
|
.users
|
|
|
|
|
.determine_registration_user_id(body.username.clone(), None, Some(appservice_info))
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
services.users.create(&user_id, None)?;
|
|
|
|
|
|
|
|
|
|
user_id
|
2026-03-26 13:42:51 -04:00
|
|
|
} else {
|
|
|
|
|
// Perform UIAA to determine the user's identity
|
|
|
|
|
let (flows, params) = create_registration_uiaa_session(&services).await?;
|
|
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
let identity = services
|
|
|
|
|
.uiaa
|
|
|
|
|
.authenticate(&body.auth, flows, params, None)
|
2026-05-05 09:09:38 -04:00
|
|
|
.await?;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
let password = if let Some(password) = &body.password {
|
|
|
|
|
HashedPassword::new(password)?
|
|
|
|
|
} else {
|
|
|
|
|
return Err!(Request(InvalidParam("A password must be provided.")));
|
|
|
|
|
};
|
2026-03-21 20:59:41 -04:00
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
let user_id = services
|
|
|
|
|
.users
|
|
|
|
|
.determine_registration_user_id(body.username.clone(), identity.email.as_ref(), None)
|
|
|
|
|
.await?;
|
2024-04-16 22:39:49 -04:00
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
services
|
|
|
|
|
.users
|
|
|
|
|
.create_local_account(&user_id, password, identity.email)
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
user_id
|
2026-05-05 09:09:38 -04:00
|
|
|
};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2026-03-29 12:25:42 -04:00
|
|
|
let (token, device) = if !body.inhibit_login {
|
2026-05-21 11:36:06 -04:00
|
|
|
// If UIAA is disabled, we can't create a device. In that case only appservices
|
|
|
|
|
// can reach this point in the first place, so we return an error for them.
|
|
|
|
|
if !services.config.oauth.compatibility_mode.uiaa_available() {
|
|
|
|
|
return Err!(Request(AppserviceLoginUnsupported(
|
|
|
|
|
"User-interactive appservice registration is not available on this server."
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
// Generate new device id if the user didn't specify one
|
2026-05-05 09:09:38 -04:00
|
|
|
let device_id = body
|
|
|
|
|
.device_id
|
|
|
|
|
.clone()
|
2025-09-11 17:21:20 +01:00
|
|
|
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
|
2020-07-30 20:49:29 +02:00
|
|
|
|
2025-09-11 17:21:20 +01:00
|
|
|
// Generate new token for the device
|
|
|
|
|
let new_token = utils::random_string(TOKEN_LENGTH);
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2025-09-11 17:21:20 +01:00
|
|
|
// Create device for this account
|
|
|
|
|
services
|
|
|
|
|
.users
|
|
|
|
|
.create_device(
|
|
|
|
|
&user_id,
|
|
|
|
|
&device_id,
|
|
|
|
|
&new_token,
|
2026-04-30 08:54:55 -04:00
|
|
|
None,
|
2025-09-11 17:21:20 +01:00
|
|
|
body.initial_device_display_name.clone(),
|
|
|
|
|
Some(client.to_string()),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
(Some(new_token), Some(device_id))
|
|
|
|
|
} else {
|
2026-03-29 12:25:42 -04:00
|
|
|
// Don't create a device for inhibited logins
|
2025-09-11 17:21:20 +01:00
|
|
|
(None, None)
|
|
|
|
|
};
|
2026-04-07 12:57:36 -04:00
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
debug_info!(%user_id, ?device, "New account created via legacy registration");
|
2024-03-20 11:19:41 -04:00
|
|
|
|
2026-04-07 12:57:36 -04:00
|
|
|
Ok(assign!(register::v3::Response::new(user_id), {
|
2025-09-11 17:21:20 +01:00
|
|
|
access_token: token,
|
|
|
|
|
device_id: device,
|
2022-10-09 17:25:06 +02:00
|
|
|
refresh_token: None,
|
|
|
|
|
expires_in: None,
|
2026-04-07 12:57:36 -04:00
|
|
|
}))
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 20:59:41 -04:00
|
|
|
/// Determine which flows and parameters should be presented when
|
|
|
|
|
/// registering a new account.
|
|
|
|
|
async fn create_registration_uiaa_session(
|
|
|
|
|
services: &Services,
|
|
|
|
|
) -> Result<(Vec<AuthFlow>, Box<RawValue>)> {
|
|
|
|
|
let mut params = HashMap::<String, serde_json::Value>::new();
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
let flows = if services.firstrun.is_first_run() {
|
2026-03-21 20:59:41 -04:00
|
|
|
// Registration token forced while in first-run mode
|
2026-03-22 20:51:14 -04:00
|
|
|
vec![AuthFlow::new(vec![AuthType::RegistrationToken])]
|
2026-03-21 20:59:41 -04:00
|
|
|
} else {
|
2026-03-22 20:51:14 -04:00
|
|
|
let mut flows = vec![];
|
|
|
|
|
|
2026-03-21 20:59:41 -04:00
|
|
|
if services
|
|
|
|
|
.registration_tokens
|
|
|
|
|
.iterate_tokens()
|
|
|
|
|
.next()
|
|
|
|
|
.await
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
2026-03-22 20:51:14 -04:00
|
|
|
// Trusted registration flow with a token is available
|
|
|
|
|
let mut token_flow = AuthFlow::new(vec![AuthType::RegistrationToken]);
|
|
|
|
|
|
|
|
|
|
if let Some(smtp) = &services.config.smtp
|
|
|
|
|
&& smtp.require_email_for_token_registration
|
|
|
|
|
{
|
|
|
|
|
// Email is required for token registrations
|
|
|
|
|
token_flow.stages.push(AuthType::EmailIdentity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flows.push(token_flow);
|
2026-03-21 20:59:41 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
let mut untrusted_flow = AuthFlow::default();
|
|
|
|
|
|
2026-03-21 20:59:41 -04:00
|
|
|
if services.config.recaptcha_private_site_key.is_some() {
|
|
|
|
|
if let Some(pubkey) = &services.config.recaptcha_site_key {
|
2026-03-22 20:51:14 -04:00
|
|
|
// ReCaptcha is configured for untrusted registrations
|
|
|
|
|
untrusted_flow.stages.push(AuthType::ReCaptcha);
|
2026-03-21 20:59:41 -04:00
|
|
|
|
|
|
|
|
params.insert(
|
|
|
|
|
AuthType::ReCaptcha.as_str().to_owned(),
|
|
|
|
|
serde_json::json!({
|
|
|
|
|
"public_key": pubkey,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
if let Some(smtp) = &services.config.smtp
|
|
|
|
|
&& smtp.require_email_for_registration
|
2026-03-21 20:59:41 -04:00
|
|
|
{
|
2026-03-22 20:51:14 -04:00
|
|
|
// Email is required for untrusted registrations
|
|
|
|
|
untrusted_flow.stages.push(AuthType::EmailIdentity);
|
2026-03-21 20:59:41 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
if !untrusted_flow.stages.is_empty() {
|
|
|
|
|
flows.push(untrusted_flow);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:19:08 -04:00
|
|
|
// Require all users to agree to the terms and conditions, if configured
|
|
|
|
|
let terms = &services.config.registration_terms;
|
2026-05-06 14:00:41 -04:00
|
|
|
if !terms.documents.is_empty() {
|
|
|
|
|
let mut terms_map = HashMap::new();
|
2026-04-06 12:19:08 -04:00
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
for (id, document) in &terms.documents {
|
|
|
|
|
terms_map.insert(id.to_owned(), serde_json::json!({
|
|
|
|
|
terms.language.clone(): serde_json::to_value(document).expect("should be able to serialize document")
|
|
|
|
|
}));
|
2026-04-06 12:19:08 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-06 14:00:41 -04:00
|
|
|
terms_map.insert("version".to_owned(), "latest".into());
|
|
|
|
|
|
2026-04-06 12:19:08 -04:00
|
|
|
params.insert(
|
|
|
|
|
AuthType::Terms.as_str().to_owned(),
|
|
|
|
|
serde_json::json!({
|
2026-05-06 14:00:41 -04:00
|
|
|
"policies": terms_map,
|
2026-04-06 12:19:08 -04:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for flow in &mut flows {
|
|
|
|
|
flow.stages.insert(0, AuthType::Terms);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
if flows.is_empty() {
|
|
|
|
|
// No flows are configured. Bail out by default
|
|
|
|
|
// unless open registration was explicitly enabled.
|
|
|
|
|
if !services
|
|
|
|
|
.config
|
|
|
|
|
.yes_i_am_very_very_sure_i_want_an_open_registration_server_prone_to_abuse
|
|
|
|
|
{
|
|
|
|
|
return Err!(Request(Forbidden(
|
|
|
|
|
"This server is not accepting registrations at this time."
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We have open registration enabled (😧), provide a dummy flow
|
|
|
|
|
flows.push(AuthFlow::new(vec![AuthType::Dummy]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flows
|
|
|
|
|
};
|
2026-03-21 20:59:41 -04:00
|
|
|
|
|
|
|
|
let params = serde_json::value::to_raw_value(¶ms).expect("params should be valid JSON");
|
|
|
|
|
|
|
|
|
|
Ok((flows, params))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 20:51:14 -04:00
|
|
|
/// # `POST /_matrix/client/v3/register/email/requestToken`
|
|
|
|
|
///
|
2026-03-23 09:21:33 -04:00
|
|
|
/// Requests a validation email for the purpose of registering a new account.
|
|
|
|
|
pub(crate) async fn request_registration_token_via_email_route(
|
2026-03-22 20:51:14 -04:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
|
body: Ruma<request_registration_token_via_email::v3::Request>,
|
|
|
|
|
) -> Result<request_registration_token_via_email::v3::Response> {
|
|
|
|
|
let Ok(email) = Address::try_from(body.email.clone()) else {
|
2026-03-23 09:21:33 -04:00
|
|
|
return Err!(Request(InvalidParam("Invalid email address.")));
|
2026-03-22 20:51:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if services
|
|
|
|
|
.threepid
|
|
|
|
|
.get_localpart_for_email(&email)
|
|
|
|
|
.await
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
2026-03-23 09:21:33 -04:00
|
|
|
return Err!(Request(ThreepidInUse("This email address is already in use.")));
|
2026-03-22 20:51:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let session = services
|
|
|
|
|
.threepid
|
|
|
|
|
.send_validation_email(
|
|
|
|
|
Mailbox::new(None, email),
|
|
|
|
|
|verification_link| messages::NewAccount {
|
|
|
|
|
server_name: services.config.server_name.as_ref(),
|
|
|
|
|
verification_link,
|
|
|
|
|
},
|
|
|
|
|
&body.client_secret,
|
|
|
|
|
body.send_attempt.try_into().unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(request_registration_token_via_email::v3::Response::new(session))
|
|
|
|
|
}
|