feat: identity: register_user command
This commit is contained in:
parent
828a53b60d
commit
8c136cb46c
2 changed files with 72 additions and 28 deletions
|
@ -2,6 +2,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use super::error::*;
|
||||||
use derive_getters::Getters;
|
use derive_getters::Getters;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
@ -19,20 +20,20 @@ impl RegisterUserCommand {
|
||||||
password: String,
|
password: String,
|
||||||
confirm_password: String,
|
confirm_password: String,
|
||||||
config: &argon2_creds::Config,
|
config: &argon2_creds::Config,
|
||||||
) -> Self {
|
) -> RegisterUserCommandResult<Self> {
|
||||||
let username = config.username(&username).unwrap();
|
let username = config.username(&username)?;
|
||||||
config.email(&email).unwrap();
|
config.email(&email)?;
|
||||||
|
|
||||||
if password != confirm_password {
|
if password != confirm_password {
|
||||||
todo!("Passwords Don't match");
|
return Err(RegisterUserCommandError::PasswordsDontMatch);
|
||||||
}
|
}
|
||||||
let hashed_password: String = config.password(&password).unwrap();
|
let hashed_password: String = config.password(&password)?;
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
username,
|
username,
|
||||||
email,
|
email,
|
||||||
hashed_password,
|
hashed_password,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,28 +50,43 @@ mod tests {
|
||||||
"asdfasdfasdfasdf".into(),
|
"asdfasdfasdfasdf".into(),
|
||||||
"asdfasdfasdfasdf".into(),
|
"asdfasdfasdfasdf".into(),
|
||||||
&config,
|
&config,
|
||||||
);
|
|
||||||
let result = std::panic::catch_unwind(||
|
|
||||||
RegisterUserCommand::new(
|
|
||||||
"username".into(),
|
|
||||||
"username@example.com".into(),
|
|
||||||
"password".into(),
|
|
||||||
"password".into(),
|
|
||||||
&config,
|
|
||||||
)
|
)
|
||||||
); // TODO: check for error, and not panic when err handling is implemented
|
.unwrap();
|
||||||
assert!(result.is_err()); // Blacklist error
|
|
||||||
|
|
||||||
let result = std::panic::catch_unwind(||
|
assert_eq!(
|
||||||
RegisterUserCommand::new(
|
RegisterUserCommand::new(
|
||||||
"realaravinth".into(),
|
"realaravinth".into(),
|
||||||
"realaravinth@example.com".into(),
|
"username".into(),
|
||||||
"password".into(),
|
"password".into(),
|
||||||
"mismatch_password".into(),
|
"password".into(),
|
||||||
&config,
|
&config,
|
||||||
)
|
)
|
||||||
); // TODO: check for error, and not panic when err handling is implemented
|
.err(),
|
||||||
assert!(result.is_err());
|
Some(RegisterUserCommandError::BadEmail)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(matches!(
|
||||||
|
RegisterUserCommand::new(
|
||||||
|
"username".into(),
|
||||||
|
"username@example.com".into(),
|
||||||
|
"password".into(),
|
||||||
|
"password".into(),
|
||||||
|
&config,
|
||||||
|
)
|
||||||
|
.err(),
|
||||||
|
Some(RegisterUserCommandError::BadUsername(_))
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
RegisterUserCommand::new(
|
||||||
|
"realaravinth".into(),
|
||||||
|
"realaravinth@example.com".into(),
|
||||||
|
"password".into(),
|
||||||
|
"mismatch_password".into(),
|
||||||
|
&config,
|
||||||
|
)
|
||||||
|
.err(),
|
||||||
|
Some(RegisterUserCommandError::PasswordsDontMatch)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,31 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
use argon2_creds::CredsError;
|
||||||
|
use derive_more::Display;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
pub type RegisterUserCommandResult<V> = Result<V, RegisterUserCommandError>;
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub enum RegisterUserCommandError {
|
||||||
|
BadUsername(String),
|
||||||
|
BadEmail,
|
||||||
|
BadPassowrd(String),
|
||||||
|
PasswordsDontMatch,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CredsError> for RegisterUserCommandError {
|
||||||
|
fn from(v: CredsError) -> Self {
|
||||||
|
match v {
|
||||||
|
CredsError::ProfainityError => Self::BadUsername(v.to_string()),
|
||||||
|
CredsError::UsernameCaseMappedError => Self::BadUsername(v.to_string()),
|
||||||
|
CredsError::BlacklistError => Self::BadUsername(v.to_string()),
|
||||||
|
CredsError::NotAnEmail => Self::BadEmail,
|
||||||
|
CredsError::PasswordTooShort => Self::BadPassowrd(v.to_string()),
|
||||||
|
CredsError::PasswordTooLong => Self::BadPassowrd(v.to_string()),
|
||||||
|
CredsError::Argon2Error(e) => Self::BadUsername(e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue