mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2025-11-25 22:22:33 +02:00
* Use Diesels MultiConnections Derive With this PR we remove almost all custom macro's to create the multiple database type code. This is now handled by Diesel it self. This removed the need of the following functions/macro's: - `db_object!` - `::to_db` - `.from_db()` It is also possible to just use one schema instead of multiple per type. Also done: - Refactored the SQLite backup function - Some formatting of queries so every call is one a separate line, this looks a bit better - Declare `conn` as mut inside each `db_run!` instead of having to declare it as `mut` in functions or calls - Added an `ACTIVE_DB_TYPE` static which holds the currently active database type - Removed `diesel_logger` crate and use Diesel's `set_default_instrumentation()` If you want debug queries you can now simply change the log level of `vaultwarden::db::query_logger` - Use PostgreSQL v17 in the Alpine images to match the Debian Trixie version - Optimized the Workflows since `diesel_logger` isn't needed anymore And on the extra plus-side, this lowers the compile-time and binary size too. Signed-off-by: BlackDex <black.dex@gmail.com> * Adjust query_logger and some other small items Signed-off-by: BlackDex <black.dex@gmail.com> * Remove macro, replaced with an function Signed-off-by: BlackDex <black.dex@gmail.com> * Implement custom connection manager Signed-off-by: BlackDex <black.dex@gmail.com> * Updated some crates to keep up2date Signed-off-by: BlackDex <black.dex@gmail.com> * Small adjustment Signed-off-by: BlackDex <black.dex@gmail.com> * crate updates Signed-off-by: BlackDex <black.dex@gmail.com> * Update crates Signed-off-by: BlackDex <black.dex@gmail.com> --------- Signed-off-by: BlackDex <black.dex@gmail.com>
75 lines
2.4 KiB
Rust
75 lines
2.4 KiB
Rust
use chrono::Utc;
|
|
|
|
use crate::db::schema::twofactor_duo_ctx;
|
|
use crate::{api::EmptyResult, db::DbConn, error::MapResult};
|
|
use diesel::prelude::*;
|
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
|
#[diesel(table_name = twofactor_duo_ctx)]
|
|
#[diesel(primary_key(state))]
|
|
pub struct TwoFactorDuoContext {
|
|
pub state: String,
|
|
pub user_email: String,
|
|
pub nonce: String,
|
|
pub exp: i64,
|
|
}
|
|
|
|
impl TwoFactorDuoContext {
|
|
pub async fn find_by_state(state: &str, conn: &DbConn) -> Option<Self> {
|
|
db_run! { conn: {
|
|
twofactor_duo_ctx::table
|
|
.filter(twofactor_duo_ctx::state.eq(state))
|
|
.first::<Self>(conn)
|
|
.ok()
|
|
}}
|
|
}
|
|
|
|
pub async fn save(state: &str, user_email: &str, nonce: &str, ttl: i64, conn: &DbConn) -> EmptyResult {
|
|
// A saved context should never be changed, only created or deleted.
|
|
let exists = Self::find_by_state(state, conn).await;
|
|
if exists.is_some() {
|
|
return Ok(());
|
|
};
|
|
|
|
let exp = Utc::now().timestamp() + ttl;
|
|
|
|
db_run! { conn: {
|
|
diesel::insert_into(twofactor_duo_ctx::table)
|
|
.values((
|
|
twofactor_duo_ctx::state.eq(state),
|
|
twofactor_duo_ctx::user_email.eq(user_email),
|
|
twofactor_duo_ctx::nonce.eq(nonce),
|
|
twofactor_duo_ctx::exp.eq(exp)
|
|
))
|
|
.execute(conn)
|
|
.map_res("Error saving context to twofactor_duo_ctx")
|
|
}}
|
|
}
|
|
|
|
pub async fn find_expired(conn: &DbConn) -> Vec<Self> {
|
|
let now = Utc::now().timestamp();
|
|
db_run! { conn: {
|
|
twofactor_duo_ctx::table
|
|
.filter(twofactor_duo_ctx::exp.lt(now))
|
|
.load::<Self>(conn)
|
|
.expect("Error finding expired contexts in twofactor_duo_ctx")
|
|
}}
|
|
}
|
|
|
|
pub async fn delete(&self, conn: &DbConn) -> EmptyResult {
|
|
db_run! { conn: {
|
|
diesel::delete(
|
|
twofactor_duo_ctx::table
|
|
.filter(twofactor_duo_ctx::state.eq(&self.state)))
|
|
.execute(conn)
|
|
.map_res("Error deleting from twofactor_duo_ctx")
|
|
}}
|
|
}
|
|
|
|
pub async fn purge_expired_duo_contexts(conn: &DbConn) {
|
|
for context in Self::find_expired(conn).await {
|
|
context.delete(conn).await.ok();
|
|
}
|
|
}
|
|
}
|