fastauth_plugin_two_factor/
model.rs1use fastauth_core::plugin_prelude::*;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct TwoFactorUser {
9 pub two_factor_enabled: bool,
12}
13
14impl TwoFactorUser {
15 pub const TWO_FACTOR_ENABLED: Col<TwoFactorUser, bool> = Col::new("two_factor_enabled");
16}
17
18impl OnTable for TwoFactorUser {
19 type Table = User;
20}
21
22impl Fields for TwoFactorUser {
23 fn columns() -> &'static [ColumnDef] {
24 const COLUMNS: &[ColumnDef] = &[ColumnDef::new("two_factor_enabled", ColumnType::Bool)
25 .default(DefaultValue::Bool(false))];
26 COLUMNS
27 }
28
29 fn decode(row: &Row) -> Result<Self, DecodeError> {
30 Ok(Self {
31 two_factor_enabled: row.get("two_factor_enabled")?,
32 })
33 }
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
38pub struct TwoFactorRow {
39 pub id: Id,
40 pub user_id: Id,
41 pub secret: Vec<u8>,
43 pub backup_codes: Vec<u8>,
45 pub last_used_step: Option<i64>,
47}
48
49impl TwoFactorRow {
50 pub const ID: Col<TwoFactorRow, Id> = Col::new("id");
51 pub const USER_ID: Col<TwoFactorRow, Id> = Col::new("user_id");
52 pub const BACKUP_CODES: Col<TwoFactorRow, Vec<u8>> = Col::new("backup_codes");
53 pub const LAST_USED_STEP: Col<TwoFactorRow, Option<i64>> = Col::new("last_used_step");
54}
55
56impl OnTable for TwoFactorRow {
57 type Table = TwoFactorRow;
58}
59
60impl Model for TwoFactorRow {
61 const TABLE: &'static str = "two_factor";
62
63 fn columns() -> &'static [ColumnDef] {
64 const COLUMNS: &[ColumnDef] = &[
65 ColumnDef::new("id", ColumnType::Id).primary_key(),
66 ColumnDef::new("user_id", ColumnType::Id)
67 .references(User::TABLE, "id", OnDelete::Cascade)
68 .index(),
69 ColumnDef::new("secret", ColumnType::Bytes).hidden(),
70 ColumnDef::new("backup_codes", ColumnType::Bytes).hidden(),
71 ColumnDef::new("last_used_step", ColumnType::Int)
72 .nullable()
73 .hidden(),
74 ];
75 COLUMNS
76 }
77
78 fn decode(row: &Row) -> Result<Self, DecodeError> {
79 Ok(Self {
80 id: row.get("id")?,
81 user_id: row.get("user_id")?,
82 secret: row.get("secret")?,
83 backup_codes: row.get("backup_codes")?,
84 last_used_step: row.get("last_used_step")?,
85 })
86 }
87
88 fn encode(&self) -> Vec<(&'static str, Value)> {
89 vec![
90 ("id", self.id.clone().into_value()),
91 ("user_id", self.user_id.clone().into_value()),
92 ("secret", self.secret.clone().into_value()),
93 ("backup_codes", self.backup_codes.clone().into_value()),
94 ("last_used_step", self.last_used_step.into_value()),
95 ]
96 }
97}