refactor: session entity structure

This commit is contained in:
Sebastian Pravda 2022-10-29 12:13:17 +02:00
parent 59e028b0ed
commit 5e36b7b3c8
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
4 changed files with 12 additions and 15 deletions

View file

@ -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::Model, DbErr> {
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

View file

@ -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)
};

View file

@ -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)]

View file

@ -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,
}