Skip to main content

better_auth_core/config/
scalars.rs

1use super::{
2    AccountConfig, AccountLinkingConfig, AdvancedDatabaseConfig, ApiErrorConfig, AuthConfig,
3    CrossSubDomainConfig, IpAddressConfig, SessionConfig, TrustedValues, VerificationConfig,
4};
5use chrono::Duration;
6
7impl SessionConfig {
8    /// Read the effective session lifetime. Omission and zero use seven days.
9    pub fn expires_in(&self) -> Duration {
10        self.expires_in
11            .filter(|age| !age.is_zero())
12            .unwrap_or_else(|| Duration::days(7))
13    }
14
15    /// Read the effective refresh interval. Omission uses one day; zero refreshes on every read.
16    pub fn update_age(&self) -> Duration {
17        self.update_age.unwrap_or_else(|| Duration::days(1))
18    }
19
20    /// Read the effective refresh policy. Omission permits refresh.
21    pub fn disable_session_refresh(&self) -> bool {
22        self.disable_session_refresh.unwrap_or(false)
23    }
24
25    /// Read the database persistence policy used with secondary storage.
26    pub fn store_session_in_database(&self) -> bool {
27        self.store_session_in_database.unwrap_or(false)
28    }
29
30    /// Read the ended-session retention policy. Omission permits deletion.
31    pub fn preserve_session_in_database(&self) -> bool {
32        self.preserve_session_in_database.unwrap_or(false)
33    }
34}
35
36impl AccountConfig {
37    /// Read the token refresh policy. Omission updates stored tokens on sign-in.
38    pub fn update_account_on_sign_in(&self) -> bool {
39        self.update_account_on_sign_in.unwrap_or(true)
40    }
41
42    /// Read the token encryption policy. Omission keeps plaintext storage.
43    pub fn encrypt_oauth_tokens(&self) -> bool {
44        self.encrypt_oauth_tokens.unwrap_or(false)
45    }
46}
47
48impl AccountLinkingConfig {
49    /// Read the effective account-linking setting. Omission enables linking.
50    pub fn enabled(&self) -> bool {
51        self.enabled.unwrap_or(true)
52    }
53
54    /// Read the last-account removal policy. Omission keeps one linked account.
55    pub fn allow_unlinking_all(&self) -> bool {
56        self.allow_unlinking_all.unwrap_or(false)
57    }
58
59    /// Read the profile update policy. Omission preserves existing user information.
60    pub fn update_user_info_on_link(&self) -> bool {
61        self.update_user_info_on_link.unwrap_or(false)
62    }
63}
64
65impl VerificationConfig {
66    /// Read the expired-record cleanup policy. Omission permits cleanup.
67    pub fn disable_cleanup(&self) -> bool {
68        self.disable_cleanup.unwrap_or(false)
69    }
70}
71
72impl ApiErrorConfig {
73    /// Read the HTTP failure policy. Omission returns an ordinary error response.
74    pub fn throw_errors(&self) -> bool {
75        self.throw_errors.unwrap_or(false)
76    }
77}
78
79impl IpAddressConfig {
80    /// Read the configured header order. Omission uses `x-forwarded-for`; an empty list stays empty.
81    pub fn headers(&self) -> impl Iterator<Item = &str> {
82        self.headers
83            .iter()
84            .flatten()
85            .map(String::as_str)
86            .chain(self.headers.is_none().then_some("x-forwarded-for"))
87    }
88
89    /// Read the IP tracking policy. Omission keeps tracking enabled.
90    pub fn disable_ip_tracking(&self) -> bool {
91        self.disable_ip_tracking.unwrap_or(false)
92    }
93}
94
95impl CrossSubDomainConfig {
96    /// Read the cross-subdomain policy. Omission disables sharing.
97    pub fn enabled(&self) -> bool {
98        self.enabled.unwrap_or(false)
99    }
100}
101
102impl AdvancedDatabaseConfig {
103    /// Read the effective ID policy. Omission uses random alphanumeric IDs.
104    pub fn generate_id(&self) -> &crate::id::IdGeneration {
105        self.generate_id
106            .as_ref()
107            .unwrap_or(&crate::id::IdGeneration::Random)
108    }
109}
110
111static EMPTY_TRUSTED_VALUES: TrustedValues = TrustedValues::Static(Vec::new());
112
113impl AuthConfig {
114    pub(crate) fn trusted_origin_values(&self) -> &TrustedValues {
115        self.trusted_origins
116            .as_ref()
117            .unwrap_or(&EMPTY_TRUSTED_VALUES)
118    }
119}
120
121impl AccountLinkingConfig {
122    pub(crate) fn trusted_provider_values(&self) -> &TrustedValues {
123        self.trusted_providers
124            .as_ref()
125            .unwrap_or(&EMPTY_TRUSTED_VALUES)
126    }
127}