| 0024ccb1 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::{now_ms, Pool}; |
| 4 |
use sqlx::FromRow; |
| 5 |
|
| 6 |
|
| 7 |
pub const IDLE_MS: i64 = 14 * 24 * 60 * 60_000; |
| 8 |
|
| 9 |
pub const ABSOLUTE_MS: i64 = 30 * 24 * 60 * 60_000; |
| 10 |
|
| 11 |
const TOUCH_EVERY_MS: i64 = 60_000; |
| 12 |
|
| 13 |
#[derive(Clone, Debug, FromRow)] |
| 14 |
pub struct Session { |
| 15 |
pub id: i64, |
| 16 |
pub account_id: i64, |
| 17 |
pub salt: String, |
| 18 |
pub created_ms: i64, |
| 19 |
pub seen_ms: i64, |
| 20 |
pub address: String, |
| 21 |
pub agent: String, |
| 22 |
} |
| 23 |
|
| 24 |
impl Session { |
| 25 |
|
| 26 |
pub fn live(&self, salt: &str, now: i64) -> bool { |
| 27 |
self.salt == salt && now - self.seen_ms < IDLE_MS && now - self.created_ms < ABSOLUTE_MS |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
const COLUMNS: &str = "id, account_id, salt, created_ms, seen_ms, address, agent"; |
| 32 |
|
| 33 |
pub async fn create(pool: &Pool, account_id: i64, hash: &str, salt: &str) -> sqlx::Result<i64> { |
| 34 |
let now = now_ms(); |
| 97b18869 Andy McCall ↶ |
35 |
sqlx::query_scalar("INSERT INTO sessions (account_id, hash, salt, created_ms, seen_ms) VALUES ($1, $2, $3, $4, $5) RETURNING id") |
| 0024ccb1 Andy McCall ↶ |
36 |
.bind(account_id) |
| 37 |
.bind(hash) |
| 38 |
.bind(salt) |
| 39 |
.bind(now) |
| 40 |
.bind(now) |
| 97b18869 Andy McCall ↶ |
41 |
.fetch_one(pool) |
| 42 |
.await |
| 0024ccb1 Andy McCall ↶ |
43 |
} |
| 44 |
|
| 45 |
pub async fn by_hash(pool: &Pool, hash: &str) -> sqlx::Result<Option<Session>> { |
| 5d90e0e0 Andy McCall |
46 |
sqlx::query_as(&format!("SELECT {COLUMNS} FROM sessions WHERE hash = $1")).bind(hash).fetch_optional(pool).await |
| 0024ccb1 Andy McCall ↶ |
47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
pub async fn touch(pool: &Pool, s: &Session, address: &str, agent: &str, now: i64) -> sqlx::Result<()> { |
| 52 |
if s.agent.is_empty() && s.address.is_empty() && (!agent.is_empty() || !address.is_empty()) { |
| 5d90e0e0 Andy McCall |
53 |
sqlx::query("UPDATE sessions SET seen_ms = $1, address = $2, agent = $3 WHERE id = $4") |
| 0024ccb1 Andy McCall ↶ |
54 |
.bind(now) |
| 55 |
.bind(address) |
| 56 |
.bind(agent) |
| 57 |
.bind(s.id) |
| 58 |
.execute(pool) |
| 59 |
.await?; |
| 60 |
} else if now - s.seen_ms >= TOUCH_EVERY_MS { |
| 5d90e0e0 Andy McCall |
61 |
sqlx::query("UPDATE sessions SET seen_ms = $1 WHERE id = $2").bind(now).bind(s.id).execute(pool).await?; |
| 0024ccb1 Andy McCall ↶ |
62 |
} |
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
pub async fn of_account(pool: &Pool, account_id: i64, salt: &str) -> sqlx::Result<Vec<Session>> { |
| 68 |
let now = now_ms(); |
| 5d90e0e0 Andy McCall |
69 |
let all: Vec<Session> = sqlx::query_as(&format!("SELECT {COLUMNS} FROM sessions WHERE account_id = $1 ORDER BY seen_ms DESC")) |
| 0024ccb1 Andy McCall ↶ |
70 |
.bind(account_id) |
| 71 |
.fetch_all(pool) |
| 72 |
.await?; |
| 73 |
Ok(all.into_iter().filter(|s| s.live(salt, now)).collect()) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
pub async fn end(pool: &Pool, account_id: i64, id: i64) -> sqlx::Result<bool> { |
| 5d90e0e0 Andy McCall |
78 |
let r = sqlx::query("DELETE FROM sessions WHERE id = $1 AND account_id = $2").bind(id).bind(account_id).execute(pool).await?; |
| 0024ccb1 Andy McCall ↶ |
79 |
Ok(r.rows_affected() > 0) |
| 80 |
} |
| 81 |
|
| 82 |
pub async fn end_hash(pool: &Pool, hash: &str) -> sqlx::Result<()> { |
| 5d90e0e0 Andy McCall |
83 |
sqlx::query("DELETE FROM sessions WHERE hash = $1").bind(hash).execute(pool).await?; |
| 0024ccb1 Andy McCall ↶ |
84 |
Ok(()) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
pub async fn end_others(pool: &Pool, account_id: i64, keep: i64) -> sqlx::Result<()> { |
| 5d90e0e0 Andy McCall |
89 |
sqlx::query("DELETE FROM sessions WHERE account_id = $1 AND id != $2").bind(account_id).bind(keep).execute(pool).await?; |
| 0024ccb1 Andy McCall ↶ |
90 |
Ok(()) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
pub async fn sweep(pool: &Pool) -> sqlx::Result<u64> { |
| 95 |
let now = now_ms(); |
| 96 |
let r = sqlx::query( |
| 5d90e0e0 Andy McCall |
97 |
"DELETE FROM sessions WHERE seen_ms < $1 OR created_ms < $2 \ |
| 0024ccb1 Andy McCall ↶ |
98 |
OR salt != (SELECT session_salt FROM accounts WHERE accounts.id = sessions.account_id)", |
| 99 |
) |
| 100 |
.bind(now - IDLE_MS) |
| 101 |
.bind(now - ABSOLUTE_MS) |
| 102 |
.execute(pool) |
| 103 |
.await?; |
| 104 |
Ok(r.rows_affected()) |
| 105 |
} |
| 106 |
|
| 107 |
#[cfg(test)] |
| 108 |
mod tests { |
| 109 |
use super::*; |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn a_session_ends_on_a_new_salt_when_idle_or_when_old() { |
| 113 |
let t0 = 1_000_000_000_000; |
| 114 |
let s = |
| 115 |
Session { id: 1, account_id: 1, salt: "a".into(), created_ms: t0, seen_ms: t0, address: String::new(), agent: String::new() }; |
| 116 |
assert!(s.live("a", t0 + 1000)); |
| 117 |
assert!(!s.live("b", t0 + 1000), "a new salt: signed out everywhere"); |
| 118 |
assert!(!s.live("a", t0 + IDLE_MS), "unused for 14 days"); |
| 119 |
let busy = Session { seen_ms: t0 + ABSOLUTE_MS - 1000, ..s.clone() }; |
| 120 |
assert!(busy.live("a", t0 + ABSOLUTE_MS - 1)); |
| 121 |
assert!(!busy.live("a", t0 + ABSOLUTE_MS), "30 days, however busy"); |
| 122 |
} |
| 123 |
} |