From 5e36b7b3c800889987c076746d648990d07c7490 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Sat, 29 Oct 2022 12:13:17 +0200 Subject: [PATCH] refactor: session entity structure --- core/src/mutation.rs | 7 +++---- core/src/services/candidate_service.rs | 4 +--- entity/src/session.rs | 5 +++-- migration/src/m20221025_154422_create_session.rs | 11 +++++------ 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/mutation.rs b/core/src/mutation.rs index e1d6bd3..9b8c071 100644 --- a/core/src/mutation.rs +++ b/core/src/mutation.rs @@ -1,4 +1,4 @@ -use chrono::Utc; +use chrono::{Utc, Duration}; use ::entity::{candidate, session}; use sea_orm::{*, prelude::Uuid}; use crate::crypto::hash_password; @@ -31,14 +31,13 @@ impl Mutation { db: &DbConn, user_id: i32, random_uuid: Uuid, - hashed_jwt: String ) -> Result { session::ActiveModel { id: Set(random_uuid), - hashed_token: Set(hashed_jwt), user_id: Set(user_id), + ip_address: Set("127.0.0.1".to_string()), created_at: Set(Utc::now().naive_local()), - updated_at: Set(Utc::now().naive_local()), + expires_at: Set(Utc::now().naive_local().checked_add_signed(Duration::days(1)).unwrap()), } .insert(db) .await diff --git a/core/src/services/candidate_service.rs b/core/src/services/candidate_service.rs index 0f64e96..dc0771f 100644 --- a/core/src/services/candidate_service.rs +++ b/core/src/services/candidate_service.rs @@ -53,9 +53,7 @@ impl CandidateService { // user is authenticated, generate a session let random_uuid: Uuid = Uuid::new_v4(); - let jwt = generate_candidate_token(candidate); - - let session = match Mutation::insert_session(db, user_id, random_uuid, hash_sha256(jwt)).await { + let session = match Mutation::insert_session(db, user_id, random_uuid).await { Ok(session) => session, Err(_) => return Err(DB_ERROR) }; diff --git a/entity/src/session.rs b/entity/src/session.rs index 4f0be16..f7322c4 100644 --- a/entity/src/session.rs +++ b/entity/src/session.rs @@ -7,10 +7,11 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: Uuid, - pub hashed_token: String, pub user_id: i32, + #[sea_orm(column_type = "Custom(\"inet\".to_owned())")] + pub ip_address: String, pub created_at: DateTime, - pub updated_at: DateTime, + pub expires_at: DateTime, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/migration/src/m20221025_154422_create_session.rs b/migration/src/m20221025_154422_create_session.rs index 9327626..a6003dd 100644 --- a/migration/src/m20221025_154422_create_session.rs +++ b/migration/src/m20221025_154422_create_session.rs @@ -18,10 +18,9 @@ impl MigrationTrait for Migration { .unique_key() .primary_key(), ) - .col(ColumnDef::new(Session::HashedToken).string().not_null()) .col(ColumnDef::new(Session::UserId).integer().not_null()) .col(ColumnDef::new(Session::CreatedAt).date_time().not_null()) - .col(ColumnDef::new(Session::UpdatedAt).date_time().not_null()) + .col(ColumnDef::new(Session::ExpiresAt).date_time().not_null()) .to_owned(); manager @@ -36,10 +35,10 @@ impl MigrationTrait for Migration { .unique_key() .primary_key(), ) - .col(ColumnDef::new(Session::HashedToken).string().not_null()) .col(ColumnDef::new(Session::UserId).integer().not_null()) + .col(ColumnDef::new(Session::IpAddress).inet().not_null()) .col(ColumnDef::new(Session::CreatedAt).date_time().not_null()) - .col(ColumnDef::new(Session::UpdatedAt).date_time().not_null()) + .col(ColumnDef::new(Session::ExpiresAt).date_time().not_null()) .to_owned(), ) .await @@ -57,8 +56,8 @@ impl MigrationTrait for Migration { pub enum Session { Table, Id, - HashedToken, UserId, + IpAddress, CreatedAt, - UpdatedAt + ExpiresAt, }