| 1 |
|
| 2 |
|
| 3 |
use sqlx::PgPool; |
| 4 |
|
| 5 |
use super::UserId; |
| 6 |
use crate::error::Result; |
| 7 |
|
| 8 |
|
| 9 |
#[tracing::instrument(skip_all)] |
| 10 |
pub async fn get_totp_secret(pool: &PgPool, user_id: UserId) -> Result<Option<String>> { |
| 11 |
let secret: Option<String> = |
| 12 |
sqlx::query_scalar("SELECT totp_secret FROM users WHERE id = $1") |
| 13 |
.bind(user_id) |
| 14 |
.fetch_one(pool) |
| 15 |
.await?; |
| 16 |
|
| 17 |
Ok(secret) |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
#[tracing::instrument(skip_all)] |
| 22 |
pub async fn set_totp_secret(pool: &PgPool, user_id: UserId, secret: &str) -> Result<()> { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
sqlx::query("UPDATE users SET totp_secret = $2, totp_last_used_step = NULL WHERE id = $1") |
| 28 |
.bind(user_id) |
| 29 |
.bind(secret) |
| 30 |
.execute(pool) |
| 31 |
.await?; |
| 32 |
|
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
#[tracing::instrument(skip_all)] |
| 38 |
pub async fn enable_totp(pool: &PgPool, user_id: UserId) -> Result<()> { |
| 39 |
sqlx::query("UPDATE users SET totp_enabled = true WHERE id = $1") |
| 40 |
.bind(user_id) |
| 41 |
.execute(pool) |
| 42 |
.await?; |
| 43 |
|
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
#[tracing::instrument(skip_all)] |
| 49 |
pub async fn disable_totp(pool: &PgPool, user_id: UserId) -> Result<()> { |
| 50 |
sqlx::query("UPDATE users SET totp_secret = NULL, totp_enabled = false, totp_last_used_step = NULL WHERE id = $1") |
| 51 |
.bind(user_id) |
| 52 |
.execute(pool) |
| 53 |
.await?; |
| 54 |
|
| 55 |
sqlx::query("DELETE FROM backup_codes WHERE user_id = $1") |
| 56 |
.bind(user_id) |
| 57 |
.execute(pool) |
| 58 |
.await?; |
| 59 |
|
| 60 |
Ok(()) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
#[tracing::instrument(skip_all)] |
| 65 |
pub async fn get_totp_last_used_step(pool: &PgPool, user_id: UserId) -> Result<Option<i64>> { |
| 66 |
let step: Option<i64> = |
| 67 |
sqlx::query_scalar("SELECT totp_last_used_step FROM users WHERE id = $1") |
| 68 |
.bind(user_id) |
| 69 |
.fetch_one(pool) |
| 70 |
.await?; |
| 71 |
|
| 72 |
Ok(step) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#[tracing::instrument(skip_all)] |
| 77 |
pub async fn set_totp_last_used_step(pool: &PgPool, user_id: UserId, step: i64) -> Result<()> { |
| 78 |
sqlx::query("UPDATE users SET totp_last_used_step = $2 WHERE id = $1") |
| 79 |
.bind(user_id) |
| 80 |
.bind(step) |
| 81 |
.execute(pool) |
| 82 |
.await?; |
| 83 |
|
| 84 |
Ok(()) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
#[tracing::instrument(skip_all)] |
| 89 |
pub async fn is_totp_enabled(pool: &PgPool, user_id: UserId) -> Result<bool> { |
| 90 |
let enabled: bool = |
| 91 |
sqlx::query_scalar("SELECT totp_enabled FROM users WHERE id = $1") |
| 92 |
.bind(user_id) |
| 93 |
.fetch_one(pool) |
| 94 |
.await?; |
| 95 |
|
| 96 |
Ok(enabled) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
#[tracing::instrument(skip_all)] |
| 101 |
pub async fn create_backup_codes( |
| 102 |
pool: &PgPool, |
| 103 |
user_id: UserId, |
| 104 |
code_hashes: &[String], |
| 105 |
) -> Result<()> { |
| 106 |
let mut tx = pool.begin().await?; |
| 107 |
|
| 108 |
|
| 109 |
sqlx::query("DELETE FROM backup_codes WHERE user_id = $1") |
| 110 |
.bind(user_id) |
| 111 |
.execute(&mut *tx) |
| 112 |
.await?; |
| 113 |
|
| 114 |
|
| 115 |
sqlx::query( |
| 116 |
"INSERT INTO backup_codes (user_id, code_hash) SELECT $1, UNNEST($2::text[])", |
| 117 |
) |
| 118 |
.bind(user_id) |
| 119 |
.bind(code_hashes) |
| 120 |
.execute(&mut *tx) |
| 121 |
.await?; |
| 122 |
|
| 123 |
tx.commit().await?; |
| 124 |
Ok(()) |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#[tracing::instrument(skip_all)] |
| 139 |
pub async fn verify_and_consume_backup_code( |
| 140 |
pool: &PgPool, |
| 141 |
user_id: UserId, |
| 142 |
code: &str, |
| 143 |
legacy_hmac: &str, |
| 144 |
) -> Result<bool> { |
| 145 |
use argon2::{password_hash::PasswordVerifier, Argon2, PasswordHash}; |
| 146 |
|
| 147 |
let rows: Vec<(uuid::Uuid, String)> = sqlx::query_as( |
| 148 |
"SELECT id, code_hash FROM backup_codes WHERE user_id = $1 AND used_at IS NULL", |
| 149 |
) |
| 150 |
.bind(user_id) |
| 151 |
.fetch_all(pool) |
| 152 |
.await?; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
let mut matched_id: Option<uuid::Uuid> = None; |
| 164 |
for (id, stored) in &rows { |
| 165 |
let is_match = if stored.starts_with("$argon2") { |
| 166 |
match PasswordHash::new(stored) { |
| 167 |
Ok(parsed) => Argon2::default() |
| 168 |
.verify_password(code.as_bytes(), &parsed) |
| 169 |
.is_ok(), |
| 170 |
Err(e) => { |
| 171 |
tracing::warn!(error = %e, "malformed argon2 backup code hash in DB; skipping"); |
| 172 |
false |
| 173 |
} |
| 174 |
} |
| 175 |
} else { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
crate::crypto::constant_time_compare(stored, legacy_hmac) |
| 180 |
}; |
| 181 |
if is_match { |
| 182 |
matched_id = Some(*id); |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
let Some(id) = matched_id else { |
| 188 |
return Ok(false); |
| 189 |
}; |
| 190 |
|
| 191 |
let result = sqlx::query( |
| 192 |
"UPDATE backup_codes SET used_at = NOW() WHERE id = $1 AND used_at IS NULL", |
| 193 |
) |
| 194 |
.bind(id) |
| 195 |
.execute(pool) |
| 196 |
.await?; |
| 197 |
|
| 198 |
Ok(result.rows_affected() > 0) |
| 199 |
} |
| 200 |
|