| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
|
| 5 |
use crate::db::{SyncAppId, UserId}; |
| 6 |
use crate::error::Result; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
pub mod sync_security_event { |
| 11 |
|
| 12 |
|
| 13 |
pub const AUTH_FAILURE: &str = "auth_failure"; |
| 14 |
|
| 15 |
pub const DEVICE_REMOVED: &str = "device_removed"; |
| 16 |
|
| 17 |
pub const KEY_ROTATION_COMPLETED: &str = "key_rotation_completed"; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[tracing::instrument(skip_all)] |
| 24 |
pub async fn record_security_event( |
| 25 |
pool: &PgPool, |
| 26 |
app_id: SyncAppId, |
| 27 |
user_id: Option<UserId>, |
| 28 |
event_type: &str, |
| 29 |
detail: Option<serde_json::Value>, |
| 30 |
ip: Option<&str>, |
| 31 |
) -> Result<()> { |
| 32 |
sqlx::query( |
| 33 |
"INSERT INTO sync_security_events (app_id, user_id, event_type, detail, ip) |
| 34 |
VALUES ($1, $2, $3, $4, $5)", |
| 35 |
) |
| 36 |
.bind(app_id) |
| 37 |
.bind(user_id) |
| 38 |
.bind(event_type) |
| 39 |
.bind(detail) |
| 40 |
.bind(ip) |
| 41 |
.execute(pool) |
| 42 |
.await?; |
| 43 |
Ok(()) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#[tracing::instrument(skip_all)] |
| 58 |
pub async fn invalidate_user_sync_tokens(pool: &PgPool, user_id: UserId) -> Result<()> { |
| 59 |
sqlx::query("UPDATE users SET sync_jwt_invalidated_at = NOW() WHERE id = $1") |
| 60 |
.bind(user_id) |
| 61 |
.execute(pool) |
| 62 |
.await?; |
| 63 |
Ok(()) |
| 64 |
} |
| 65 |
|