feat: identity: login service

This commit is contained in:
Aravinth Manivannan 2024-05-18 00:02:08 +05:30
parent 2d1fb4fd1c
commit 33022dc4ea
Signed by: realaravinth
GPG key ID: F8F50389936984FF
5 changed files with 120 additions and 4 deletions

View file

@ -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
);
}
}

View 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 }
}
}

View file

@ -1,3 +1,18 @@
mod command;
mod error;
mod service;
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// 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;
}

View file

@ -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());
}
}
}