use sea_orm::entity::Set; use sea_orm::entity::prelude::*; use uuid::Uuid; #[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)] #[sea_orm( rs_type = "String", db_type = "String(StringLen::N(16))", rename_all = "snake_case" )] pub enum MfaType { Totp, Webauthn, Sms, Email, } #[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)] #[sea_orm( rs_type = "String", db_type = "String(StringLen::N(16))", rename_all = "snake_case" )] pub enum MfaStatus { Enabled, Disabled, Pending, Revoked, } #[sea_orm::model] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[sea_orm(table_name = "user_mfa")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: Uuid, #[sea_orm(indexed)] pub user_id: i64, pub mfa_type: MfaType, #[sea_orm(default_value = "pending")] pub mfa_status: MfaStatus, #[sea_orm(column_type = "String(StringLen::N(64))")] pub name: String, #[sea_orm(column_type = "Text")] pub secret_ciphertext: Option, #[sea_orm(column_type = "String(StringLen::N(64))")] pub secret_key_id: Option, pub last_totp_step: Option, pub mfa_last_used_at: Option, #[sea_orm(default_expr = "Expr::current_timestamp()")] pub created_at: DateTimeUtc, #[sea_orm(default_expr = "Expr::current_timestamp()")] pub updated_at: DateTimeUtc, #[sea_orm(default_value = 1)] pub version: i32, pub verified_at: Option, pub revoked_at: Option, #[sea_orm(belongs_to, from = "user_id", to = "id", on_delete = "Cascade")] pub user: BelongsTo, #[sea_orm(has_one)] pub webauthn_credential: HasOne, } #[async_trait::async_trait] impl ActiveModelBehavior for ActiveModel { async fn before_save(mut self, _db: &C, insert: bool) -> Result where C: ConnectionTrait, { if insert && self.id.is_not_set() { self.id = Set(Uuid::now_v7()); } self.updated_at = Set(chrono::Utc::now()); Ok(self) } }