Skip to content

Commit d885104

Browse files
committed
Check uniqueness username earlier #489
1 parent 37d5942 commit d885104

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/src/plugins/register.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ struct MailConfirmation {
4545
pub name: String,
4646
}
4747

48+
#[derive(Debug, Clone)]
49+
struct UserName {
50+
pub name: String,
51+
}
52+
53+
impl UserName {
54+
/// Throws error if email address is already taken
55+
pub fn check_used(name: &str, store: &impl Storelike) -> AtomicResult<Self> {
56+
let mut drive_url = store
57+
.get_self_url()
58+
.ok_or("No self url, cant check name")?
59+
.clone();
60+
drive_url.set_subdomain(Some(name))?;
61+
62+
match store.get_resource(&drive_url.to_string()) {
63+
Ok(_) => Err("Name already used".into()),
64+
Err(_) => Ok(Self { name: name.into() }),
65+
}
66+
}
67+
}
68+
69+
impl std::fmt::Display for UserName {
70+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
write!(f, "{}", self.name)
72+
}
73+
}
74+
4875
#[tracing::instrument]
4976
pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult<Resource> {
5077
let HandleGetContext {
@@ -57,7 +84,7 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
5784
let store = context.store;
5885
for (k, v) in subject.query_pairs() {
5986
match k.as_ref() {
60-
"name" | urls::NAME => name_option = Some(v.to_string()),
87+
"name" | urls::NAME => name_option = Some(UserName::check_used(&v, store)?),
6188
"email" => email_option = Some(EmailAddress::new(v.to_string())?),
6289
_ => {}
6390
}
@@ -68,13 +95,14 @@ pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult
6895
};
6996

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

73101
// send the user an e-mail to confirm sign up
74102
let store_clone = store.clone();
75103
let confirmation_token_struct = MailConfirmation {
76104
email: email.clone(),
77-
name: name.clone(),
105+
name: name.to_string(),
78106
};
79107
let token = crate::token::sign_claim(store, confirmation_token_struct)?;
80108
let mut confirm_url = store

server/src/helpers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn origin(url: &str) -> String {
6060
)
6161
}
6262

63+
/// Checks if the origin in the Cookie matches the requested subject.
6364
pub fn get_auth_from_cookie(
6465
headers: &HeaderMap,
6566
requested_subject: &str,

0 commit comments

Comments
 (0)