Skip to content

Commit 24999ef

Browse files
committed
Check with the homeserver the username is still available before registering
1 parent 02db622 commit 24999ef

File tree

1 file changed

+15
-1
lines changed
  • crates/handlers/src/views/register/steps

1 file changed

+15
-1
lines changed

crates/handlers/src/views/register/steps/finish.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use axum_extra::TypedHeader;
1212
use chrono::Duration;
1313
use mas_axum_utils::{cookies::CookieJar, FancyError, SessionInfoExt as _};
1414
use mas_data_model::UserAgent;
15+
use mas_matrix::BoxHomeserverConnection;
1516
use mas_router::{PostAuthAction, UrlBuilder};
1617
use mas_storage::{
1718
queue::{ProvisionUserJob, QueueJobRepositoryExt as _},
@@ -36,6 +37,7 @@ pub(crate) async fn get(
3637
activity_tracker: BoundActivityTracker,
3738
user_agent: Option<TypedHeader<headers::UserAgent>>,
3839
State(url_builder): State<UrlBuilder>,
40+
homeserver: BoxHomeserverConnection,
3941
cookie_jar: CookieJar,
4042
Path(id): Path<Ulid>,
4143
) -> Result<impl IntoResponse, FancyError> {
@@ -72,6 +74,7 @@ pub(crate) async fn get(
7274
// Check that this registration belongs to this browser
7375
let registrations = UserRegistrationSessions::load(&cookie_jar);
7476
if !registrations.contains(&registration) {
77+
// XXX: we should have a better error screen here
7578
return Err(FancyError::from(anyhow::anyhow!(
7679
"Could not find the registration in the browser cookies"
7780
)));
@@ -82,12 +85,21 @@ pub(crate) async fn get(
8285
// address
8386

8487
if repo.user().exists(&registration.username).await? {
88+
// XXX: this could have a better error message, but as this is unlikely to
89+
// happen, we're fine with a vague message for now
8590
return Err(FancyError::from(anyhow::anyhow!(
8691
"Username is already taken"
8792
)));
8893
}
8994

90-
// TODO: query the homeserver
95+
if !homeserver
96+
.is_localpart_available(&registration.username)
97+
.await?
98+
{
99+
return Err(FancyError::from(anyhow::anyhow!(
100+
"Username is not available"
101+
)));
102+
}
91103

92104
// For now, we require an email address on the registration, but this might
93105
// change in the future
@@ -115,6 +127,8 @@ pub(crate) async fn get(
115127
.await?
116128
> 0
117129
{
130+
// XXX: this could have a better error message, but as this is unlikely to
131+
// happen, we're fine with a vague message for now
118132
return Err(FancyError::from(anyhow::anyhow!(
119133
"Email address is already used"
120134
)));

0 commit comments

Comments
 (0)