Skip to content

Commit

Permalink
Merge pull request #4 from JKearnsl/jk-1604
Browse files Browse the repository at this point in the history
update 0.1.4
  • Loading branch information
JKearnsl authored Aug 5, 2024
2 parents 7ee0926 + 2f5b1b1 commit b0d5cc5
Show file tree
Hide file tree
Showing 57 changed files with 625 additions and 1,452 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
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"

Expand Down Expand Up @@ -43,3 +43,4 @@ sqlx = { version = "^0.8", features = [
"sqlx-sqlite",
"chrono"
] }
rand = "0.8.5"
162 changes: 0 additions & 162 deletions src/tobox/adapters/auth/header.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/tobox/adapters/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod header;
pub mod token;
91 changes: 91 additions & 0 deletions src/tobox/adapters/auth/token.rs
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())
}
}
}
8 changes: 2 additions & 6 deletions src/tobox/application/box/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ impl Interactor<(), CreateBoxResultDTO> for CreateBox<'_> {
Ok(_) => (),
Err(error) => return match error {
DomainError::AccessDenied => Err(
ApplicationError::Forbidden(
ErrorContent::Message(error.to_string())
)
ApplicationError::Forbidden(ErrorContent::from(error))
),
DomainError::AuthorizationRequired => Err(
ApplicationError::Unauthorized(
ErrorContent::Message(error.to_string())
)
ApplicationError::Unauthorized(ErrorContent::from(error))
)
}
};
Expand Down
14 changes: 4 additions & 10 deletions src/tobox/application/box/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,18 @@ impl Interactor<DeleteBoxDTO, ()> for DeleteBox<'_> {
Ok(_) => (),
Err(error) => return match error {
DomainError::AccessDenied => Err(
ApplicationError::Forbidden(
ErrorContent::Message(error.to_string())
)
ApplicationError::Forbidden(ErrorContent::from(error))
),
DomainError::AuthorizationRequired => Err(
ApplicationError::Unauthorized(
ErrorContent::Message(error.to_string())
)
ApplicationError::Unauthorized(ErrorContent::from(error))
)
}
};

match self.box_gateway.get_box_by_id(&data.id).await {
match self.box_gateway.get_box(&data.id).await {
Some(_) => (),
None => return Err(
ApplicationError::InvalidData(
ErrorContent::Message("Box not found".to_string())
)
ApplicationError::InvalidData(ErrorContent::from("Box not found"))
)
}

Expand Down
Loading

0 comments on commit b0d5cc5

Please sign in to comment.