Skip to content

Commit

Permalink
Check uniqueness username earlier #489
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Sep 23, 2024
1 parent 1702819 commit 7d3c7e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/src/plugins/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ struct MailConfirmation {
pub name: String,
}

#[derive(Debug, Clone)]
struct UserName {
pub name: String,
}

impl UserName {
/// Throws error if email address is already taken
pub fn check_used(name: &str, store: &impl Storelike) -> AtomicResult<Self> {
let mut drive_url = store
.get_self_url()
.ok_or("No self url, cant check name")?
.clone();
drive_url.set_subdomain(Some(name))?;

match store.get_resource(&drive_url.to_string()) {
Ok(_) => Err("Name already used".into()),
Err(_) => Ok(Self { name: name.into() }),
}
}
}

impl std::fmt::Display for UserName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}

#[tracing::instrument]
pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult<Resource> {
let HandleGetContext {
Expand All @@ -57,7 +84,7 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
let store = context.store;
for (k, v) in subject.query_pairs() {
match k.as_ref() {
"name" | urls::NAME => name_option = Some(v.to_string()),
"name" | urls::NAME => name_option = Some(UserName::check_used(&v, store)?),
"email" => email_option = Some(EmailAddress::new(v.to_string())?),
_ => {}
}
Expand All @@ -68,13 +95,14 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
};

let name = name_option.ok_or("No name provided")?;
let _validate_name = Value::new(&name.to_string(), &crate::datatype::DataType::Slug)?;
let email = email_option.ok_or("No email provided")?.check_used(store)?;

// send the user an e-mail to confirm sign up
let store_clone = store.clone();
let confirmation_token_struct = MailConfirmation {
email: email.clone(),
name: name.clone(),
name: name.to_string(),
};
let token = crate::token::sign_claim(store, confirmation_token_struct)?;
let mut confirm_url = store
Expand Down
1 change: 1 addition & 0 deletions server/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn origin(url: &str) -> String {
)
}

/// Checks if the origin in the Cookie matches the requested subject.
pub fn get_auth_from_cookie(
headers: &HeaderMap,
requested_subject: &str,
Expand Down

0 comments on commit 7d3c7e4

Please sign in to comment.