improvement: guests are more spec-conform

next
Timo 2020-08-22 21:19:14 +02:00
parent 64789537f5
commit df936e8e7e
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
1 changed files with 24 additions and 10 deletions

View File

@ -15,6 +15,7 @@ use ruma::{
UserId, UserId,
}; };
use register::RegistrationKind;
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::{get, post}; use rocket::{get, post};
@ -83,12 +84,18 @@ pub fn register_route(
)); ));
} }
let is_guest = matches!(body.kind, Some(RegistrationKind::Guest));
// Validate user id // Validate user id
let user_id = UserId::parse_with_server_name( let user_id = UserId::parse_with_server_name(
body.username if is_guest {
.clone() utils::random_string(GUEST_NAME_LENGTH)
.unwrap_or_else(|| utils::random_string(GUEST_NAME_LENGTH)) } else {
.to_lowercase(), body.username.clone().ok_or_else(|| {
Error::BadRequest(ErrorKind::MissingParam, "Missing username field.")
})?
}
.to_lowercase(),
db.globals.server_name(), db.globals.server_name(),
) )
.ok() .ok()
@ -131,7 +138,12 @@ pub fn register_route(
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
let password = body.password.clone().unwrap_or_default(); let password = if is_guest {
None
} else {
body.password.clone()
}
.unwrap_or_default();
// Create user // Create user
db.users.create(&user_id, &password)?; db.users.create(&user_id, &password)?;
@ -149,7 +161,7 @@ pub fn register_route(
&db.globals, &db.globals,
)?; )?;
if body.inhibit_login { if !is_guest && body.inhibit_login {
return Ok(register::Response { return Ok(register::Response {
access_token: None, access_token: None,
user_id, user_id,
@ -159,10 +171,12 @@ pub fn register_route(
} }
// Generate new device id if the user didn't specify one // Generate new device id if the user didn't specify one
let device_id = body let device_id = if is_guest {
.device_id None
.clone() } else {
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); body.device_id.clone()
}
.unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into());
// Generate new token for the device // Generate new token for the device
let token = utils::random_string(TOKEN_LENGTH); let token = utils::random_string(TOKEN_LENGTH);