forked from letsgetrusty/live-bootcamp-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zainen Suzuki
committed
Nov 4, 2024
1 parent
d78bb16
commit 863f9fe
Showing
10 changed files
with
236 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::domain::{ | ||
data_stores::{LoginAttemptId, TwoFACode, TwoFACodeStore, TwoFACodeStoreError}, | ||
email::{self, Email} | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct HashmapTwoFACodeStore { | ||
pub codes: HashMap<Email, (LoginAttemptId, TwoFACode)>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl TwoFACodeStore for HashmapTwoFACodeStore { | ||
async fn add_code(&mut self, email: Email, login_attempt_id: LoginAttemptId, code: TwoFACode) -> Result<(), TwoFACodeStoreError> { | ||
if self.get_code(&email).await.is_ok() { | ||
return Err(TwoFACodeStoreError::CodeAlreadyExists) | ||
} | ||
self.codes.insert(email.clone(), (login_attempt_id, code)); | ||
|
||
if self.get_code(&email).await.is_err() { | ||
return Err(TwoFACodeStoreError::UnexpectedError) | ||
} | ||
Ok(()) | ||
} | ||
async fn remove_code(&mut self, email: &Email) -> Result<(), TwoFACodeStoreError> { | ||
match self.codes.remove(&email) { | ||
Some(_) => Ok(()), | ||
None => Err(TwoFACodeStoreError::LoginAttemptIdNotFound) | ||
} | ||
} | ||
async fn get_code(&self, email: &Email) -> Result<(LoginAttemptId, TwoFACode), TwoFACodeStoreError> { | ||
match self.codes.get(email) { | ||
Some((login_attempt_id, code)) => Ok((login_attempt_id.clone(), code.clone())), | ||
None => Err(TwoFACodeStoreError::LoginAttemptIdNotFound) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::domain::Email; | ||
use super::*; | ||
use uuid::Uuid; | ||
|
||
#[tokio::test] | ||
async fn test_add_remove_get_methods() { | ||
let email = Email::parse("email@email.com".to_owned()).expect("Failed to create email"); | ||
let mut store = HashmapTwoFACodeStore { | ||
codes: HashMap::new() | ||
}; | ||
let login_attempt_id = LoginAttemptId::parse(Uuid::new_v4().to_string()).expect("Failed to parse uuid"); | ||
let two_fa_code = TwoFACode::parse("123456".to_owned()).expect("Failed to parse code"); | ||
|
||
let inserted = store.add_code(email.clone(), login_attempt_id.clone(), two_fa_code.clone()).await; | ||
assert!(inserted.is_ok()); | ||
assert_eq!(store.codes.is_empty(), false); | ||
|
||
let code = store.get_code(&email).await; | ||
assert!(code.is_ok()); | ||
let (id, code) = code.unwrap(); | ||
assert_eq!(id, login_attempt_id); | ||
assert_eq!(code, two_fa_code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod hashmap_banned_token_store; | ||
mod hashmap_user_store; | ||
mod hashmap_two_fa_code_store; | ||
|
||
pub use hashmap_banned_token_store::*; | ||
pub use hashmap_user_store::*; | ||
pub use hashmap_two_fa_code_store::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.