From 130669793dfb77b286a26a373378b4d1f2be837c Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Sat, 26 Oct 2024 02:20:32 -0400 Subject: Very basic rocket db state and session lookup (with hardcoded session id) --- src/db/mod.rs | 1 + src/db/models/auth.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/db/models/mod.rs | 1 + 3 files changed, 86 insertions(+) create mode 100644 src/db/mod.rs create mode 100644 src/db/models/auth.rs create mode 100644 src/db/models/mod.rs (limited to 'src/db') diff --git a/src/db/mod.rs b/src/db/mod.rs new file mode 100644 index 0000000..c446ac8 --- /dev/null +++ b/src/db/mod.rs @@ -0,0 +1 @@ +pub mod models; diff --git a/src/db/models/auth.rs b/src/db/models/auth.rs new file mode 100644 index 0000000..b4fa070 --- /dev/null +++ b/src/db/models/auth.rs @@ -0,0 +1,84 @@ +use rocket::serde::{self, Serialize}; +use uuid::Uuid; +use time::OffsetDateTime; +use sqlx::{postgres::types::PgInterval, Pool, Postgres}; + +#[derive(Serialize, Debug)] +#[serde(crate = "rocket::serde")] +pub struct User { + uuid: Uuid, + email: String, + username: String, + display_name: Option, + password_hash: Option, + #[serde(with="time::serde::rfc3339")] + time_created: OffsetDateTime, + #[serde(with="time::serde::rfc3339::option")] + time_password_changed: Option, + #[serde(with="time::serde::rfc3339::option")] + time_email_confirmed: Option +} + +#[derive(Serialize, Debug)] +#[serde(crate = "rocket::serde")] +pub struct LoginSession { + uuid: Uuid, + #[serde(with="time::serde::rfc3339")] + time_created: OffsetDateTime, + #[serde(with="time::serde::rfc3339")] + time_last_active: OffsetDateTime, + #[serde(skip_serializing)] + duration: PgInterval, + #[serde(with="time::serde::rfc3339::option")] + time_logged_out: Option, + ip_address: String, + user_agent: String, + referer: String, + user: User +} + +impl LoginSession { + pub async fn lookup(connection: &Pool::, uuid: Uuid) -> Option { + joinrs::query_parsed!( + connection = connection, + query = r#" + select + login_session.*, + user_uuid, + user_display_name, + user_password_hash, + user_time_created, + user_time_password_changed, + user_time_email_confirmed, + user_username::text as username, + user_email::text as email + from phtx.login_session + left join phtx."user" + on login_session_user_uuid = user_uuid + where login_session_time_last_active + login_session_duration >= now() + and login_session_uuid = $1 + "#, + params = [uuid], + return_type = LoginSession { + uuid: login_session_uuid, + time_created: login_session_time_created, + time_last_active: login_session_time_last_active, + duration: login_session_duration, + time_logged_out: login_session_time_logged_out, + ip_address: login_session_ip_address, + user_agent: login_session_user_agent, + referer: login_session_referer, + user: User { + uuid: user_uuid, + email: email, + username: username, + display_name: user_display_name, + password_hash: user_password_hash, + time_created: user_time_created, + time_password_changed: user_time_password_changed, + time_email_confirmed: user_time_email_confirmed + } + } + ).ok() + } +} diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs new file mode 100644 index 0000000..0e4a05d --- /dev/null +++ b/src/db/models/mod.rs @@ -0,0 +1 @@ +pub mod auth; -- cgit 1.4.1