feat: identity: login service
This commit is contained in:
parent
2d1fb4fd1c
commit
33022dc4ea
5 changed files with 120 additions and 4 deletions
|
@ -1 +1,39 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// 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<Self> {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
17
src/identity/application/services/login/events.rs
Normal file
17
src/identity/application/services/login/events.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// 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 }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,18 @@
|
||||||
mod command;
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
mod error;
|
//
|
||||||
mod service;
|
// 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<String>;
|
||||||
|
) -> events::LoginEvent;
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,48 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
//
|
||||||
|
// 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<String>;
|
||||||
|
) -> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue