Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 27, 2024
1 parent d2b0950 commit 86782d5
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 104 deletions.
4 changes: 0 additions & 4 deletions engine/src/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
pub mod oauth;
pub mod session;

pub trait AuthenticationProvider {
async fn isValidAuthToken(&self, authToken: &str) -> bool;
}
32 changes: 32 additions & 0 deletions engine/src/auth/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -10,3 +12,33 @@ pub struct SessionState {
pub last_access: chrono::DateTime<chrono::Utc>,
pub valid: bool,
}

impl SessionState {
pub async fn new(
user_id: i32,
user_agent: &str,
user_ip: &str,
database: &Database,
) -> Result<Self, sqlx::Error> {
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<Self, sqlx::Error> {
let session = sqlx::query_as::<_, SessionState>(
"SELECT * FROM sessions WHERE id = $1 AND valid = TRUE",
)
.bind(id)
.fetch_one(&database.pool)
.await?;

Ok(session)
}
}
19 changes: 0 additions & 19 deletions engine/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,4 @@ impl Database {

Ok(user)
}

pub async fn create_session(&self, user_id: i32, user_agent: &str, user_ip: &str) -> Result<SessionState, sqlx::Error> {
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<SessionState, sqlx::Error> {
let session = sqlx::query_as::<_, SessionState>("SELECT * FROM sessions WHERE id = $1 AND valid = TRUE")
.bind(id)
.fetch_one(&self.pool)
.await?;

Ok(session)
}
}
1 change: 0 additions & 1 deletion engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod state;
mod models;
mod routes;
mod database;
mod permissions;

#[async_std::main]
async fn main() {
Expand Down
51 changes: 0 additions & 51 deletions engine/src/permissions/default_rules.rs

This file was deleted.

2 changes: 0 additions & 2 deletions engine/src/permissions/mod.rs

This file was deleted.

18 changes: 0 additions & 18 deletions engine/src/permissions/rule.rs

This file was deleted.

12 changes: 3 additions & 9 deletions engine/src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::state::AppState;
use crate::{auth::session::SessionState, state::AppState};
use openid::{Options, Token};
use poem::{
handler,
Expand Down Expand Up @@ -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")
Expand All @@ -102,7 +96,7 @@ pub async fn me(state: Data<&Arc<AppState>>, 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
Expand Down

0 comments on commit 86782d5

Please sign in to comment.