-
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.
Merge pull request #15 from EleisonC/Add-redis
Implement Redis
- Loading branch information
Showing
11 changed files
with
232 additions
and
10 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
55 changes: 55 additions & 0 deletions
55
auth-service/src/services/data_stores/redis_banned_token_store.rs
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,55 @@ | ||
use std::sync::Arc; | ||
|
||
use redis::{Commands, Connection}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::{ | ||
domain::data_stores::{ | ||
BannedTokenStore, BannedTokenStoreError}, | ||
utils::auth::TOKEN_TTL_SECONDS, | ||
}; | ||
|
||
pub struct RedisBannedTokenStore { | ||
conn: Arc<RwLock<Connection>> | ||
} | ||
|
||
impl RedisBannedTokenStore { | ||
pub fn new(conn: Arc<RwLock<Connection>>) -> Self { | ||
Self{ conn } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl BannedTokenStore for RedisBannedTokenStore { | ||
async fn store_banned_token(&mut self, token: String) -> Result<(), BannedTokenStoreError> { | ||
let redis_token_key = get_key(&token); | ||
let mut store_conn = self.conn.write().await; | ||
|
||
let ttl = TOKEN_TTL_SECONDS as u64; | ||
let result = store_conn.set_ex::<String, bool, ()>(redis_token_key, true, ttl); | ||
|
||
match result { | ||
Ok(_) => Ok(()), | ||
Err(_) => Err(BannedTokenStoreError::UnexpectedError) | ||
} | ||
} | ||
|
||
async fn check_banned_token(&self, token: String) -> Result<String, BannedTokenStoreError> { | ||
let redis_token_key = get_key(&token); | ||
let mut store_conn = self.conn.write().await; | ||
|
||
let result = store_conn.exists(redis_token_key); | ||
|
||
match result { | ||
Ok(true) => Ok(format!("Token {} is banned", token)), | ||
Ok(false) => Err(BannedTokenStoreError::TokenNotFound), | ||
Err(_) => Err(BannedTokenStoreError::UnexpectedError) | ||
} | ||
} | ||
} | ||
|
||
const BANNED_TOKEN_KEY_PREFIX: &str = "banned_token:"; | ||
|
||
fn get_key(token: &str) -> String { | ||
return format!("{}{}", BANNED_TOKEN_KEY_PREFIX, token); | ||
} |
81 changes: 81 additions & 0 deletions
81
auth-service/src/services/data_stores/redis_two_fa_code_store.rs
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,81 @@ | ||
use std::sync::Arc; | ||
|
||
use redis::{Commands, Connection}; | ||
use serde::{Deserialize, Serialize}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::domain::{ | ||
data_stores::{ TwoFACodeStore, TwoFACodeStoreError}, | ||
Email, LoginAttemptId, TwoFACode, | ||
}; | ||
|
||
pub struct RedisTwoFACodeStore { | ||
conn: Arc<RwLock<Connection>> | ||
} | ||
|
||
impl RedisTwoFACodeStore { | ||
pub fn new(conn: Arc<RwLock<Connection>>) -> Self { | ||
Self{ conn } | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl TwoFACodeStore for RedisTwoFACodeStore { | ||
async fn add_code(&mut self, | ||
email: Email, | ||
login_attempt_id: LoginAttemptId, | ||
code: TwoFACode | ||
) -> Result<(), TwoFACodeStoreError> { | ||
let mut conn = self.conn.write().await; | ||
let key = get_key(&email); | ||
|
||
let two_fa_tuple = TwoFATuple(login_attempt_id.as_ref().to_owned(), code.as_ref().to_owned()); | ||
|
||
let serialized_data = serde_json::to_string(&two_fa_tuple).map_err(|_| TwoFACodeStoreError::UnexpectedError)?; | ||
|
||
let _:() = conn | ||
.set_ex(&key, serialized_data, TEN_MINUTES_IN_SECONDS) | ||
.map_err(|_| TwoFACodeStoreError::UnexpectedError)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn remove_code(&mut self, email: &Email) -> Result<(), TwoFACodeStoreError> { | ||
let mut conn = self.conn.write().await; | ||
let key = get_key(email); | ||
|
||
let _:() = conn | ||
.del(&key) | ||
.map_err(|_| TwoFACodeStoreError::UnexpectedError)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn get_code( | ||
&self, | ||
email: &Email, | ||
) -> Result<(LoginAttemptId, TwoFACode), TwoFACodeStoreError> { | ||
let mut conn = self.conn.write().await; | ||
let key = get_key(email); | ||
|
||
match conn.get::<_, String>(&key) { | ||
Ok(data) => { | ||
let two_fa_tuple: TwoFATuple = serde_json::from_str(&data) | ||
.map_err(|_| TwoFACodeStoreError::UnexpectedError)?; | ||
|
||
Ok((LoginAttemptId::parse(two_fa_tuple.0).unwrap(), TwoFACode::parse(two_fa_tuple.1).unwrap())) | ||
}, | ||
Err(_) => Err(TwoFACodeStoreError::LoginAttemptIdNotFound) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct TwoFATuple(pub String, pub String); | ||
|
||
const TEN_MINUTES_IN_SECONDS: u64 = 600; | ||
const TWO_FA_CODE_PREFIX: &str = "two_fa_code:"; | ||
|
||
fn get_key(email: &Email) -> String { | ||
format!("{}{}", TWO_FA_CODE_PREFIX, email.as_ref()) | ||
} |
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