summary refs log tree commit diff
path: root/src/db/models/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/models/auth.rs')
-rw-r--r--src/db/models/auth.rs84
1 files changed, 84 insertions, 0 deletions
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<String>,
+    password_hash: Option<String>,
+    #[serde(with="time::serde::rfc3339")]
+    time_created: OffsetDateTime,
+    #[serde(with="time::serde::rfc3339::option")]
+    time_password_changed: Option<OffsetDateTime>,
+    #[serde(with="time::serde::rfc3339::option")]
+    time_email_confirmed: Option<OffsetDateTime>
+}
+
+#[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<OffsetDateTime>,
+    ip_address: String,
+    user_agent: String,
+    referer: String,
+    user: User
+}
+
+impl LoginSession {
+    pub async fn lookup(connection: &Pool::<Postgres>, uuid: Uuid) -> Option<LoginSession> {
+        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()
+    }
+}