From 86782d58ffd0b08cb3dbeed4b09fa4d7556bf809 Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 27 Jul 2024 21:07:36 +0000 Subject: [PATCH] Cleanup --- engine/src/auth/mod.rs | 4 -- engine/src/auth/session.rs | 32 ++++++++++++++++ engine/src/database/mod.rs | 19 --------- engine/src/main.rs | 1 - engine/src/permissions/default_rules.rs | 51 ------------------------- engine/src/permissions/mod.rs | 2 - engine/src/permissions/rule.rs | 18 --------- engine/src/routes/auth.rs | 12 ++---- 8 files changed, 35 insertions(+), 104 deletions(-) delete mode 100644 engine/src/permissions/default_rules.rs delete mode 100644 engine/src/permissions/mod.rs delete mode 100644 engine/src/permissions/rule.rs diff --git a/engine/src/auth/mod.rs b/engine/src/auth/mod.rs index 8a75945..4d8c803 100644 --- a/engine/src/auth/mod.rs +++ b/engine/src/auth/mod.rs @@ -1,6 +1,2 @@ pub mod oauth; pub mod session; - -pub trait AuthenticationProvider { - async fn isValidAuthToken(&self, authToken: &str) -> bool; -} diff --git a/engine/src/auth/session.rs b/engine/src/auth/session.rs index dba10ff..60f54d5 100644 --- a/engine/src/auth/session.rs +++ b/engine/src/auth/session.rs @@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize}; use sqlx::types::chrono; use uuid::Uuid; +use crate::database::Database; + #[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize)] pub struct SessionState { pub id: Uuid, @@ -10,3 +12,33 @@ pub struct SessionState { pub last_access: chrono::DateTime, pub valid: bool, } + +impl SessionState { + pub async fn new( + user_id: i32, + user_agent: &str, + user_ip: &str, + database: &Database, + ) -> Result { + let session = sqlx::query_as::<_, SessionState>( + "INSERT INTO sessions (user_id, user_agent, user_ip) VALUES ($1, $2, $3) RETURNING *", + ) + .bind(user_id) + .bind(user_agent) + .bind(user_ip) + .fetch_one(&database.pool) + .await?; + Ok(session) + } + + pub async fn get_by_id(id: Uuid, database: &Database) -> Result { + let session = sqlx::query_as::<_, SessionState>( + "SELECT * FROM sessions WHERE id = $1 AND valid = TRUE", + ) + .bind(id) + .fetch_one(&database.pool) + .await?; + + Ok(session) + } +} diff --git a/engine/src/database/mod.rs b/engine/src/database/mod.rs index 994c5e7..36f5df3 100644 --- a/engine/src/database/mod.rs +++ b/engine/src/database/mod.rs @@ -62,23 +62,4 @@ impl Database { Ok(user) } - - pub async fn create_session(&self, user_id: i32, user_agent: &str, user_ip: &str) -> Result { - let session = sqlx::query_as::<_, SessionState>("INSERT INTO sessions (user_id, user_agent, user_ip) VALUES ($1, $2, $3) RETURNING *") - .bind(user_id) - .bind(user_agent) - .bind(user_ip) - .fetch_one(&self.pool) - .await?; - Ok(session) - } - - pub async fn get_session_by_id(&self, id: Uuid) -> Result { - let session = sqlx::query_as::<_, SessionState>("SELECT * FROM sessions WHERE id = $1 AND valid = TRUE") - .bind(id) - .fetch_one(&self.pool) - .await?; - - Ok(session) - } } diff --git a/engine/src/main.rs b/engine/src/main.rs index c0d6f33..4609962 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -6,7 +6,6 @@ mod state; mod models; mod routes; mod database; -mod permissions; #[async_std::main] async fn main() { diff --git a/engine/src/permissions/default_rules.rs b/engine/src/permissions/default_rules.rs deleted file mode 100644 index ef77de2..0000000 --- a/engine/src/permissions/default_rules.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::permissions::rule::{PermissionedEntity, Rule, RuleOutput}; - -/** - * - * Examples rules for permissions (impl Rule): - * - byOrg(ruleOutput, org) - * - byUser(ruleOutput, user) - * - byGroup(ruleOutput, group) - * - */ - -#[derive(Debug, Clone)] -pub struct ByOrgRule { - pub rule_output: RuleOutput, - pub org: String, -} - -impl Rule for ByOrgRule { - fn check(&self, entity: PermissionedEntity) -> RuleOutput { - match entity { - PermissionedEntity::Organization(org) => { - if org == self.org { - self.rule_output - } - else { - RuleOutput::PASSTHROUGH - } - }, - _ => RuleOutput::PASSTHROUGH, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_by_org_rule() { - let rule = ByOrgRule { - rule_output: RuleOutput::READ, - org: "test".to_string(), - }; - - let entity = PermissionedEntity::Organization("test".to_string()); - assert_eq!(rule.check(entity), RuleOutput::READ); - - let entity = PermissionedEntity::Organization("other".to_string()); - assert_eq!(rule.check(entity), RuleOutput::PASSTHROUGH); - } -} diff --git a/engine/src/permissions/mod.rs b/engine/src/permissions/mod.rs deleted file mode 100644 index a8afe3a..0000000 --- a/engine/src/permissions/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod rule; -mod default_rules; diff --git a/engine/src/permissions/rule.rs b/engine/src/permissions/rule.rs deleted file mode 100644 index 118c431..0000000 --- a/engine/src/permissions/rule.rs +++ /dev/null @@ -1,18 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum RuleOutput { - DISSALOW, - READ, - WRITE, - PASSTHROUGH, -} - -#[derive(Debug, Clone)] -pub enum PermissionedEntity { - Organization(String), - User(String), - Group(String), -} - -pub trait Rule { - fn check(&self, entity: PermissionedEntity) -> RuleOutput; -} diff --git a/engine/src/routes/auth.rs b/engine/src/routes/auth.rs index e269450..9bd27bd 100644 --- a/engine/src/routes/auth.rs +++ b/engine/src/routes/auth.rs @@ -1,4 +1,4 @@ -use crate::state::AppState; +use crate::{auth::session::SessionState, state::AppState}; use openid::{Options, Token}; use poem::{ handler, @@ -81,16 +81,10 @@ pub async fn callback( let user_agent = headers.get("user-agent").unwrap().to_str().unwrap(); let user_ip = ip.0.unwrap().to_string(); - let session = state - .database - .create_session(user.id, user_agent, &user_ip) + let session = SessionState::new(user.id, user_agent, &user_ip, &state.database) .await .unwrap(); - // let session = state.database.get_session_by_id(&user.id).await.unwrap(); - - // TODO: return a token - let token = session.id; Redirect::temporary("http://localhost:3000/me") @@ -102,7 +96,7 @@ pub async fn me(state: Data<&Arc>, cookies: &CookieJar) -> impl IntoRe let token = cookies.get("property.v3x.token").unwrap(); let token = Uuid::parse_str(token.value_str()).unwrap(); - let session = state.database.get_session_by_id(token).await.unwrap(); + let session = SessionState::get_by_id(token, &state.database).await.unwrap(); let user = state .database