-
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 #4 from JKearnsl/jk-1604
update 0.1.4
- Loading branch information
Showing
57 changed files
with
625 additions
and
1,452 deletions.
There are no files selected for viewing
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,6 +1,6 @@ | ||
[package] | ||
name = "tobox" | ||
version = "0.1.3" | ||
version = "0.1.4" | ||
authors = ["JKearnsl <[email protected]>"] | ||
edition = "2021" | ||
|
||
|
@@ -43,3 +43,4 @@ sqlx = { version = "^0.8", features = [ | |
"sqlx-sqlite", | ||
"chrono" | ||
] } | ||
rand = "0.8.5" |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
pub mod header; | ||
pub mod token; |
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,91 @@ | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use crate::application::common::id_provider::IdProvider; | ||
use crate::domain::models::permission::PermissionTag; | ||
use crate::domain::models::session::Session; | ||
use crate::domain::models::user::UserId; | ||
|
||
pub struct IdTokenProvider { | ||
token: Option<String>, | ||
user_id: Option<UserId>, | ||
permissions: Vec<PermissionTag>, | ||
is_auth: bool | ||
} | ||
|
||
|
||
impl IdTokenProvider { | ||
pub fn new( | ||
token: Option<String>, | ||
token_processor: &TokenProcessor, | ||
) -> Result<Self, String> { | ||
match token { | ||
Some(token) => { | ||
let session = token_processor.get_token_session(&token)?; | ||
Ok(Self { | ||
token: Some(token), | ||
user_id: Option::from(session.user_id), | ||
permissions: session.permissions, | ||
is_auth: true | ||
}) | ||
} | ||
None => { | ||
Ok(Self { | ||
token, | ||
user_id: None, | ||
permissions: PermissionTag::guest_tags(), | ||
is_auth: false | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl IdProvider for IdTokenProvider { | ||
fn token(&self) -> Option<&String> { | ||
self.token.as_ref() | ||
} | ||
fn user_id(&self) -> Option<&UserId> { | ||
self.user_id.as_ref() | ||
} | ||
fn permissions(&self) -> &Vec<PermissionTag> { | ||
&self.permissions | ||
} | ||
fn is_auth(&self) -> &bool { | ||
&self.is_auth | ||
} | ||
} | ||
|
||
|
||
pub struct TokenProcessor { | ||
data: Arc<RwLock<HashMap<String, Session>>>, | ||
} | ||
|
||
impl TokenProcessor { | ||
pub fn new() -> Self { | ||
Self { | ||
data: Arc::new(RwLock::new(HashMap::new())), | ||
} | ||
} | ||
|
||
pub fn set_token_session(&self, session: &Session) -> String { | ||
let token = session.token.clone(); | ||
let mut data = self.data.write().unwrap(); | ||
data.insert(token.clone(), session.clone()); | ||
token | ||
} | ||
|
||
pub fn get_token_session(&self, token: &str) -> Result<Session, String> { | ||
let data = self.data.read().unwrap(); | ||
match data.get(token) { | ||
Some(session) => { | ||
if session.expires_at < chrono::Utc::now() { | ||
Ok(session.clone()) | ||
} else { | ||
Err("Token expired".to_string()) | ||
} | ||
} | ||
None => Err("Token not found".to_string()) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.