| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
|
| 5 |
use super::{DbUserSession, UserId, UserSessionId}; |
| 6 |
use crate::error::Result; |
| 7 |
|
| 8 |
|
| 9 |
#[tracing::instrument(skip_all)] |
| 10 |
pub async fn create_user_session( |
| 11 |
pool: &PgPool, |
| 12 |
user_id: UserId, |
| 13 |
user_agent: Option<&str>, |
| 14 |
ip_address: Option<&str>, |
| 15 |
) -> Result<UserSessionId> { |
| 16 |
let row = sqlx::query_scalar!( |
| 17 |
r#"INSERT INTO user_sessions (user_id, user_agent, ip_address) VALUES ($1, $2, $3) RETURNING id AS "id: UserSessionId""#, |
| 18 |
user_id as UserId, |
| 19 |
user_agent, |
| 20 |
ip_address, |
| 21 |
) |
| 22 |
.fetch_one(pool) |
| 23 |
.await?; |
| 24 |
|
| 25 |
Ok(row) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#[tracing::instrument(skip_all)] |
| 40 |
pub async fn prune_user_sessions_over_cap(pool: &PgPool, user_id: UserId, max: i64) -> Result<u64> { |
| 41 |
let result = sqlx::query!( |
| 42 |
r#" |
| 43 |
DELETE FROM user_sessions |
| 44 |
WHERE user_id = $1 AND kind = 'active' |
| 45 |
AND id NOT IN ( |
| 46 |
SELECT id FROM user_sessions |
| 47 |
WHERE user_id = $1 AND kind = 'active' |
| 48 |
ORDER BY last_active_at DESC, id DESC |
| 49 |
LIMIT $2 |
| 50 |
) |
| 51 |
"#, |
| 52 |
user_id as UserId, |
| 53 |
max, |
| 54 |
) |
| 55 |
.execute(pool) |
| 56 |
.await?; |
| 57 |
|
| 58 |
Ok(result.rows_affected()) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#[tracing::instrument(skip_all)] |
| 66 |
pub async fn create_pending_2fa_session( |
| 67 |
pool: &PgPool, |
| 68 |
user_id: UserId, |
| 69 |
user_agent: Option<&str>, |
| 70 |
ip_address: Option<&str>, |
| 71 |
) -> Result<UserSessionId> { |
| 72 |
let row = sqlx::query_scalar!( |
| 73 |
r#"INSERT INTO user_sessions (user_id, user_agent, ip_address, kind) |
| 74 |
VALUES ($1, $2, $3, 'pending_2fa') RETURNING id AS "id: UserSessionId""#, |
| 75 |
user_id as UserId, |
| 76 |
user_agent, |
| 77 |
ip_address, |
| 78 |
) |
| 79 |
.fetch_one(pool) |
| 80 |
.await?; |
| 81 |
|
| 82 |
Ok(row) |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[tracing::instrument(skip_all)] |
| 88 |
pub async fn pending_2fa_session_exists( |
| 89 |
pool: &PgPool, |
| 90 |
id: UserSessionId, |
| 91 |
user_id: UserId, |
| 92 |
) -> Result<bool> { |
| 93 |
let exists = sqlx::query_scalar!( |
| 94 |
r#"SELECT EXISTS(SELECT 1 FROM user_sessions WHERE id = $1 AND user_id = $2 AND kind = 'pending_2fa') AS "exists!""#, |
| 95 |
id as UserSessionId, |
| 96 |
user_id as UserId, |
| 97 |
) |
| 98 |
.fetch_one(pool) |
| 99 |
.await?; |
| 100 |
Ok(exists) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[tracing::instrument(skip_all)] |
| 110 |
pub async fn delete_pending_2fa_session( |
| 111 |
pool: &PgPool, |
| 112 |
id: UserSessionId, |
| 113 |
user_id: UserId, |
| 114 |
) -> Result<()> { |
| 115 |
sqlx::query!( |
| 116 |
"DELETE FROM user_sessions WHERE id = $1 AND user_id = $2 AND kind = 'pending_2fa'", |
| 117 |
id as UserSessionId, |
| 118 |
user_id as UserId, |
| 119 |
) |
| 120 |
.execute(pool) |
| 121 |
.await?; |
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
pub struct TouchResult { |
| 128 |
|
| 129 |
pub valid: bool, |
| 130 |
|
| 131 |
|
| 132 |
pub suspended: bool, |
| 133 |
|
| 134 |
|
| 135 |
pub can_create_projects: bool, |
| 136 |
|
| 137 |
pub is_fan_plus: bool, |
| 138 |
|
| 139 |
pub creator_tier: Option<String>, |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[tracing::instrument(skip_all)] |
| 148 |
pub async fn touch_session(pool: &PgPool, session_id: UserSessionId) -> Result<TouchResult> { |
| 149 |
|
| 150 |
|
| 151 |
let row = sqlx::query!( |
| 152 |
r#" |
| 153 |
UPDATE user_sessions us |
| 154 |
SET last_active_at = NOW() |
| 155 |
FROM users u |
| 156 |
WHERE us.id = $1 AND u.id = us.user_id |
| 157 |
RETURNING |
| 158 |
u.suspended_at IS NOT NULL AS "suspended!", |
| 159 |
u.can_create_projects AS "can_create_projects!", |
| 160 |
EXISTS(SELECT 1 FROM fan_plus_subscriptions fps WHERE fps.user_id = u.id AND fps.status = 'active') AS "is_fan_plus!", |
| 161 |
(SELECT cs.tier FROM creator_subscriptions cs WHERE cs.user_id = u.id AND cs.status = 'active') AS "creator_tier" |
| 162 |
"#, |
| 163 |
session_id as UserSessionId, |
| 164 |
) |
| 165 |
.map(|r| (r.suspended, r.can_create_projects, r.is_fan_plus, r.creator_tier)) |
| 166 |
.fetch_optional(pool) |
| 167 |
.await?; |
| 168 |
|
| 169 |
match row { |
| 170 |
Some((suspended, can_create_projects, is_fan_plus, creator_tier)) => Ok(TouchResult { |
| 171 |
valid: true, |
| 172 |
suspended, |
| 173 |
can_create_projects, |
| 174 |
is_fan_plus, |
| 175 |
creator_tier, |
| 176 |
}), |
| 177 |
None => Ok(TouchResult { |
| 178 |
valid: false, |
| 179 |
suspended: false, |
| 180 |
can_create_projects: false, |
| 181 |
is_fan_plus: false, |
| 182 |
creator_tier: None, |
| 183 |
}), |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
#[tracing::instrument(skip_all)] |
| 189 |
pub async fn get_user_sessions(pool: &PgPool, user_id: UserId) -> Result<Vec<DbUserSession>> { |
| 190 |
let sessions = sqlx::query_as!( |
| 191 |
DbUserSession, |
| 192 |
r#"SELECT id AS "id: UserSessionId", user_id AS "user_id: UserId", |
| 193 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", |
| 194 |
last_active_at AS "last_active_at: chrono::DateTime<chrono::Utc>", |
| 195 |
user_agent, ip_address |
| 196 |
FROM user_sessions |
| 197 |
WHERE user_id = $1 |
| 198 |
ORDER BY last_active_at DESC |
| 199 |
LIMIT 100"#, |
| 200 |
user_id as UserId, |
| 201 |
) |
| 202 |
.fetch_all(pool) |
| 203 |
.await?; |
| 204 |
|
| 205 |
Ok(sessions) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
#[tracing::instrument(skip_all)] |
| 210 |
pub async fn count_user_sessions(pool: &PgPool, user_id: UserId) -> Result<i64> { |
| 211 |
let count = sqlx::query_scalar!( |
| 212 |
r#"SELECT COUNT(*) AS "count!" FROM user_sessions WHERE user_id = $1"#, |
| 213 |
user_id as UserId, |
| 214 |
) |
| 215 |
.fetch_one(pool) |
| 216 |
.await?; |
| 217 |
|
| 218 |
Ok(count) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
#[tracing::instrument(skip_all)] |
| 223 |
pub async fn delete_user_session( |
| 224 |
pool: &PgPool, |
| 225 |
session_id: UserSessionId, |
| 226 |
user_id: UserId, |
| 227 |
) -> Result<bool> { |
| 228 |
let rows = sqlx::query!( |
| 229 |
"DELETE FROM user_sessions WHERE id = $1 AND user_id = $2", |
| 230 |
session_id as UserSessionId, |
| 231 |
user_id as UserId, |
| 232 |
) |
| 233 |
.execute(pool) |
| 234 |
.await?; |
| 235 |
|
| 236 |
Ok(rows.rows_affected() > 0) |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
#[tracing::instrument(skip_all)] |
| 248 |
pub async fn delete_session_by_id( |
| 249 |
pool: &PgPool, |
| 250 |
session_id: UserSessionId, |
| 251 |
user_id: UserId, |
| 252 |
) -> Result<bool> { |
| 253 |
let rows = sqlx::query!( |
| 254 |
"DELETE FROM user_sessions WHERE id = $1 AND user_id = $2", |
| 255 |
session_id as UserSessionId, |
| 256 |
user_id as UserId, |
| 257 |
) |
| 258 |
.execute(pool) |
| 259 |
.await?; |
| 260 |
|
| 261 |
Ok(rows.rows_affected() > 0) |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
#[tracing::instrument(skip_all)] |
| 267 |
pub async fn prune_expired_sessions( |
| 268 |
pool: &PgPool, |
| 269 |
stale_threshold: chrono::DateTime<chrono::Utc>, |
| 270 |
) -> Result<u64> { |
| 271 |
|
| 272 |
let result = sqlx::query("DELETE FROM user_sessions WHERE last_active_at < $1") |
| 273 |
.bind(stale_threshold) |
| 274 |
.execute(pool) |
| 275 |
.await?; |
| 276 |
|
| 277 |
Ok(result.rows_affected()) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
#[tracing::instrument(skip_all)] |
| 282 |
pub async fn delete_other_sessions( |
| 283 |
pool: &PgPool, |
| 284 |
current_session_id: UserSessionId, |
| 285 |
user_id: UserId, |
| 286 |
) -> Result<Vec<UserSessionId>> { |
| 287 |
let ids = sqlx::query_scalar!( |
| 288 |
r#"DELETE FROM user_sessions WHERE user_id = $1 AND id != $2 RETURNING id AS "id: UserSessionId""#, |
| 289 |
user_id as UserId, |
| 290 |
current_session_id as UserSessionId, |
| 291 |
) |
| 292 |
.fetch_all(pool) |
| 293 |
.await?; |
| 294 |
|
| 295 |
Ok(ids) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
#[tracing::instrument(skip_all)] |
| 305 |
pub async fn delete_all_sessions_for_user( |
| 306 |
pool: &PgPool, |
| 307 |
user_id: UserId, |
| 308 |
) -> Result<Vec<UserSessionId>> { |
| 309 |
let mut tx = pool.begin().await?; |
| 310 |
|
| 311 |
let ids = sqlx::query_scalar!( |
| 312 |
r#"DELETE FROM user_sessions WHERE user_id = $1 RETURNING id AS "id: UserSessionId""#, |
| 313 |
user_id as UserId, |
| 314 |
) |
| 315 |
.fetch_all(&mut *tx) |
| 316 |
.await?; |
| 317 |
|
| 318 |
sqlx::query!( |
| 319 |
"UPDATE users SET jwt_invalidated_at = NOW() WHERE id = $1", |
| 320 |
user_id as UserId, |
| 321 |
) |
| 322 |
.execute(&mut *tx) |
| 323 |
.await?; |
| 324 |
|
| 325 |
tx.commit().await?; |
| 326 |
|
| 327 |
Ok(ids) |
| 328 |
} |
| 329 |
|