Skip to content

Commit

Permalink
Check with the homeserver the username is still available before regi…
Browse files Browse the repository at this point in the history
…stering
  • Loading branch information
sandhose committed Jan 15, 2025
1 parent 02db622 commit 24999ef
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/handlers/src/views/register/steps/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use axum_extra::TypedHeader;
use chrono::Duration;
use mas_axum_utils::{cookies::CookieJar, FancyError, SessionInfoExt as _};
use mas_data_model::UserAgent;
use mas_matrix::BoxHomeserverConnection;
use mas_router::{PostAuthAction, UrlBuilder};
use mas_storage::{
queue::{ProvisionUserJob, QueueJobRepositoryExt as _},
Expand All @@ -36,6 +37,7 @@ pub(crate) async fn get(
activity_tracker: BoundActivityTracker,
user_agent: Option<TypedHeader<headers::UserAgent>>,
State(url_builder): State<UrlBuilder>,
homeserver: BoxHomeserverConnection,
cookie_jar: CookieJar,
Path(id): Path<Ulid>,
) -> Result<impl IntoResponse, FancyError> {
Expand Down Expand Up @@ -72,6 +74,7 @@ pub(crate) async fn get(
// Check that this registration belongs to this browser
let registrations = UserRegistrationSessions::load(&cookie_jar);
if !registrations.contains(&registration) {
// XXX: we should have a better error screen here
return Err(FancyError::from(anyhow::anyhow!(
"Could not find the registration in the browser cookies"
)));
Expand All @@ -82,12 +85,21 @@ pub(crate) async fn get(
// address

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

// TODO: query the homeserver
if !homeserver
.is_localpart_available(&registration.username)
.await?
{
return Err(FancyError::from(anyhow::anyhow!(
"Username is not available"
)));
}

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

0 comments on commit 24999ef

Please sign in to comment.