diff --git a/src/identity/application/services/login/command.rs b/src/identity/application/services/login/command.rs index 8b13789..0b2540e 100644 --- a/src/identity/application/services/login/command.rs +++ b/src/identity/application/services/login/command.rs @@ -1 +1,39 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; + +use super::*; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct LoginCommand { + success: bool, +} + +impl LoginCommand { + pub fn new(_username: String, supplied_password: String, actual_password_hash: &str) -> IdentityCommandResult { + let success = + argon2_creds::Config::verify(actual_password_hash, &supplied_password)?; + Ok(Self { success }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cmd() { + let config = argon2_creds::Config::default(); + let password = "adsfasdfasd"; + let hashed_password = config.password(password).unwrap(); + assert!( + LoginCommand::new("realaravinth".into(), password.into(), &hashed_password,).unwrap().success + ); + assert!( + !LoginCommand::new("realaravinth".into(), "password".into(), &hashed_password,).unwrap().success + ); + } +} diff --git a/src/identity/application/services/login/error.rs b/src/identity/application/services/login/error.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/identity/application/services/login/error.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/identity/application/services/login/events.rs b/src/identity/application/services/login/events.rs new file mode 100644 index 0000000..84b0b25 --- /dev/null +++ b/src/identity/application/services/login/events.rs @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct LoginEvent { + success: bool, +} + +impl LoginEvent { + pub fn new(success: bool) -> Self { + Self { success } + } +} diff --git a/src/identity/application/services/login/mod.rs b/src/identity/application/services/login/mod.rs index 0d0d0f7..20dc2ba 100644 --- a/src/identity/application/services/login/mod.rs +++ b/src/identity/application/services/login/mod.rs @@ -1,3 +1,18 @@ -mod command; -mod error; -mod service; +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +pub mod command; +pub mod events; +pub mod service; + +use super::errors::*; + +#[async_trait::async_trait] +pub trait LoginUseCase { + async fn login( + &self, + cmd: command::LoginCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ) -> events::LoginEvent; +} diff --git a/src/identity/application/services/login/service.rs b/src/identity/application/services/login/service.rs index 8b13789..091183a 100644 --- a/src/identity/application/services/login/service.rs +++ b/src/identity/application/services/login/service.rs @@ -1 +1,48 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later +use super::*; + +pub struct LoginService; + +#[async_trait::async_trait] +impl LoginUseCase for LoginService { + async fn login( + &self, + cmd: command::LoginCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ) -> events::LoginEvent { + events::LoginEvent::new(cmd.success().to_owned()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[actix_rt::test] + async fn test_service() { + let config = argon2_creds::Config::default(); + let username = "realaravinth"; + let password = "password"; + let hashed_password = config.password(password).unwrap(); + + let s = LoginService; + + { + let cmd = + command::LoginCommand::new(username.into(), password.into(), &hashed_password).unwrap(); + let res = s.login(cmd.clone()).await; + assert_eq!(res.success(), cmd.success()); + } + + { + // success=false: + let cmd = + command::LoginCommand::new(username.into(), "asdfasdf".into(), &hashed_password).unwrap(); + let res = s.login(cmd.clone()).await; + assert_eq!(res.success(), cmd.success()); + } + } +}