summary refs log tree commit diff
path: root/src/db/models/auth.rs
blob: b4fa070c3fea508d9dfa02f7e3390992b41e52a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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()
    }
}