main
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<String>,
#[sea_orm(column_type = "String(StringLen::N(64))")]
pub secret_key_id: Option<String>,
pub last_totp_step: Option<i64>,
pub mfa_last_used_at: Option<DateTimeUtc>,
#[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<DateTimeUtc>,
pub revoked_at: Option<DateTimeUtc>,
#[sea_orm(belongs_to, from = "user_id", to = "id", on_delete = "Cascade")]
pub user: BelongsTo<super::users::Entity>,
#[sea_orm(has_one)]
pub webauthn_credential: HasOne<super::user_webauthn_credentials::Entity>,
}
#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
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)
}
}