Allow registration without username

This commit is contained in:
Radek Stępień 2022-06-18 11:13:37 +00:00 committed by Timo Kösters
parent 6e106b5732
commit 84ec057f6e

View File

@ -16,8 +16,10 @@ use ruma::{
uiaa::{AuthFlow, AuthType, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
events::{ events::{
room::member::{MembershipState, RoomMemberEventContent}, room::{
room::message::RoomMessageEventContent, member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
},
GlobalAccountDataEventType, RoomEventType, GlobalAccountDataEventType, RoomEventType,
}, },
push, UserId, push, UserId,
@ -27,7 +29,7 @@ use tracing::{info, warn};
use register::RegistrationKind; use register::RegistrationKind;
const GUEST_NAME_LENGTH: usize = 10; const RANDOM_USER_ID_LENGTH: usize = 10;
/// # `GET /_matrix/client/r0/register/available` /// # `GET /_matrix/client/r0/register/available`
/// ///
@ -95,38 +97,38 @@ pub async fn register_route(
let is_guest = body.kind == RegistrationKind::Guest; let is_guest = body.kind == RegistrationKind::Guest;
let mut missing_username = false; let user_id = match (&body.username, is_guest) {
(Some(username), false) => {
// Validate user id let proposed_user_id =
let user_id = UserId::parse_with_server_name( UserId::parse_with_server_name(username.to_lowercase(), db.globals.server_name())
if is_guest { .ok()
utils::random_string(GUEST_NAME_LENGTH) .filter(|user_id| {
} else { !user_id.is_historical()
body.username.clone().unwrap_or_else(|| { && user_id.server_name() == db.globals.server_name()
// If the user didn't send a username field, that means the client is just trying })
// the get an UIAA error to see available flows .ok_or(Error::BadRequest(
missing_username = true; ErrorKind::InvalidUsername,
// Just give the user a random name. He won't be able to register with it anyway. "Username is invalid.",
utils::random_string(GUEST_NAME_LENGTH) ))?;
}) if db.users.exists(&proposed_user_id)? {
return Err(Error::BadRequest(
ErrorKind::UserInUse,
"Desired user ID is already taken.",
));
}
proposed_user_id
} }
.to_lowercase(), _ => loop {
db.globals.server_name(), let proposed_user_id = UserId::parse_with_server_name(
) utils::random_string(RANDOM_USER_ID_LENGTH).to_lowercase(),
.ok() db.globals.server_name(),
.filter(|user_id| !user_id.is_historical() && user_id.server_name() == db.globals.server_name()) )
.ok_or(Error::BadRequest( .unwrap();
ErrorKind::InvalidUsername, if !db.users.exists(&proposed_user_id)? {
"Username is invalid.", break proposed_user_id;
))?; }
},
// Check if username is creative enough };
if db.users.exists(&user_id)? {
return Err(Error::BadRequest(
ErrorKind::UserInUse,
"Desired user ID is already taken.",
));
}
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
@ -169,13 +171,6 @@ pub async fn register_route(
} }
} }
if missing_username {
return Err(Error::BadRequest(
ErrorKind::MissingParam,
"Missing username field.",
));
}
let password = if is_guest { let password = if is_guest {
None None
} else { } else {