rustauth_core/options/
verification.rs1use std::collections::BTreeMap;
2use std::fmt;
3use std::future::Future;
4use std::pin::Pin;
5use std::sync::Arc;
6
7use crate::error::RustAuthError;
8
9use super::model_schema::ModelSchemaOptions;
10
11pub type StoreIdentifierHashFuture =
12 Pin<Box<dyn Future<Output = Result<String, RustAuthError>> + Send>>;
13pub type StoreIdentifierHashFn = Arc<dyn Fn(String) -> StoreIdentifierHashFuture + Send + Sync>;
14
15#[derive(Clone, Default)]
17pub enum StoreIdentifierOption {
18 #[default]
19 Plain,
20 Hashed,
21 Custom(StoreIdentifierHashFn),
22}
23
24impl fmt::Debug for StoreIdentifierOption {
25 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26 match self {
27 Self::Plain => formatter.write_str("Plain"),
28 Self::Hashed => formatter.write_str("Hashed"),
29 Self::Custom(_) => formatter.write_str("Custom(<hash>)"),
30 }
31 }
32}
33
34#[derive(Clone, Debug, Default)]
36pub enum VerificationStoreIdentifierConfig {
37 #[default]
38 Plain,
39 Single(StoreIdentifierOption),
40 WithOverrides {
41 default: StoreIdentifierOption,
42 overrides: BTreeMap<String, StoreIdentifierOption>,
43 },
44}
45
46impl VerificationStoreIdentifierConfig {
47 pub fn hashed() -> Self {
48 Self::Single(StoreIdentifierOption::Hashed)
49 }
50
51 pub fn resolve(&self, identifier: &str) -> StoreIdentifierOption {
52 match self {
53 Self::Plain => StoreIdentifierOption::Plain,
54 Self::Single(option) => option.clone(),
55 Self::WithOverrides { default, overrides } => {
56 for (prefix, option) in overrides {
57 if identifier.starts_with(prefix) {
58 return option.clone();
59 }
60 }
61 default.clone()
62 }
63 }
64 }
65}
66
67#[derive(Clone, Debug, Default)]
69pub struct VerificationOptions {
70 pub schema: ModelSchemaOptions,
71 pub store_identifier: VerificationStoreIdentifierConfig,
72 pub disable_cleanup: bool,
73}
74
75impl VerificationOptions {
76 pub fn new() -> Self {
77 Self::default()
78 }
79
80 #[must_use]
81 pub fn schema(mut self, schema: ModelSchemaOptions) -> Self {
82 self.schema = schema;
83 self
84 }
85
86 #[must_use]
87 pub fn store_identifier_hashed(mut self) -> Self {
88 self.store_identifier = VerificationStoreIdentifierConfig::hashed();
89 self
90 }
91
92 #[must_use]
93 pub fn store_identifier(mut self, config: VerificationStoreIdentifierConfig) -> Self {
94 self.store_identifier = config;
95 self
96 }
97
98 #[must_use]
99 pub fn disable_cleanup(mut self, disabled: bool) -> Self {
100 self.disable_cleanup = disabled;
101 self
102 }
103}