Skip to main content

openauth_core/options/
account.rs

1/// Account and OAuth account behavior.
2#[derive(Debug, Clone, PartialEq, Eq)]
3pub struct AccountOptions {
4    pub update_account_on_sign_in: bool,
5    pub encrypt_oauth_tokens: bool,
6    pub store_account_cookie: bool,
7    pub store_state_strategy: OAuthStateStoreStrategy,
8    pub account_linking: AccountLinkingOptions,
9}
10
11impl Default for AccountOptions {
12    fn default() -> Self {
13        Self {
14            update_account_on_sign_in: true,
15            encrypt_oauth_tokens: false,
16            store_account_cookie: false,
17            store_state_strategy: OAuthStateStoreStrategy::Cookie,
18            account_linking: AccountLinkingOptions::default(),
19        }
20    }
21}
22
23impl AccountOptions {
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    pub fn builder() -> Self {
29        Self::new()
30    }
31
32    #[must_use]
33    pub fn update_account_on_sign_in(mut self, enabled: bool) -> Self {
34        self.update_account_on_sign_in = enabled;
35        self
36    }
37
38    #[must_use]
39    pub fn encrypt_oauth_tokens(mut self, enabled: bool) -> Self {
40        self.encrypt_oauth_tokens = enabled;
41        self
42    }
43
44    #[must_use]
45    pub fn store_account_cookie(mut self, enabled: bool) -> Self {
46        self.store_account_cookie = enabled;
47        self
48    }
49
50    #[must_use]
51    pub fn store_state_strategy(mut self, strategy: OAuthStateStoreStrategy) -> Self {
52        self.store_state_strategy = strategy;
53        self
54    }
55
56    #[must_use]
57    pub fn account_linking(mut self, account_linking: AccountLinkingOptions) -> Self {
58        self.account_linking = account_linking;
59        self
60    }
61}
62
63#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
64pub enum OAuthStateStoreStrategy {
65    #[default]
66    Cookie,
67    Database,
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct AccountLinkingOptions {
72    pub enabled: bool,
73    pub disable_implicit_linking: bool,
74    pub trusted_providers: Vec<String>,
75    pub allow_different_emails: bool,
76    pub allow_unlinking_all: bool,
77    pub update_user_info_on_link: bool,
78}
79
80impl Default for AccountLinkingOptions {
81    fn default() -> Self {
82        Self {
83            enabled: true,
84            disable_implicit_linking: false,
85            trusted_providers: Vec::new(),
86            allow_different_emails: false,
87            allow_unlinking_all: false,
88            update_user_info_on_link: false,
89        }
90    }
91}
92
93impl AccountLinkingOptions {
94    pub fn new() -> Self {
95        Self::default()
96    }
97
98    pub fn builder() -> Self {
99        Self::new()
100    }
101
102    #[must_use]
103    pub fn enabled(mut self, enabled: bool) -> Self {
104        self.enabled = enabled;
105        self
106    }
107
108    #[must_use]
109    pub fn disable_implicit_linking(mut self, enabled: bool) -> Self {
110        self.disable_implicit_linking = enabled;
111        self
112    }
113
114    #[must_use]
115    pub fn trusted_provider(mut self, provider: impl Into<String>) -> Self {
116        self.trusted_providers.push(provider.into());
117        self
118    }
119
120    #[must_use]
121    pub fn trusted_providers<I, S>(mut self, providers: I) -> Self
122    where
123        I: IntoIterator<Item = S>,
124        S: Into<String>,
125    {
126        self.trusted_providers
127            .extend(providers.into_iter().map(Into::into));
128        self
129    }
130
131    #[must_use]
132    pub fn allow_different_emails(mut self, enabled: bool) -> Self {
133        self.allow_different_emails = enabled;
134        self
135    }
136
137    #[must_use]
138    pub fn allow_unlinking_all(mut self, enabled: bool) -> Self {
139        self.allow_unlinking_all = enabled;
140        self
141    }
142
143    #[must_use]
144    pub fn update_user_info_on_link(mut self, enabled: bool) -> Self {
145        self.update_user_info_on_link = enabled;
146        self
147    }
148}