Skip to main content

rustauth_plugins/oauth_proxy/
options.rs

1use serde::Serialize;
2use time::Duration;
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
5pub struct OAuthProxyOptions {
6    #[serde(skip_serializing_if = "Option::is_none", rename = "currentURL")]
7    pub current_url: Option<String>,
8    #[serde(skip_serializing_if = "Option::is_none", rename = "productionURL")]
9    pub production_url: Option<String>,
10    #[serde(rename = "maxAge")]
11    pub max_age: Duration,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub secret: Option<String>,
14}
15
16impl Default for OAuthProxyOptions {
17    fn default() -> Self {
18        Self {
19            current_url: None,
20            production_url: None,
21            max_age: Duration::minutes(1),
22            secret: None,
23        }
24    }
25}
26
27impl OAuthProxyOptions {
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    #[must_use]
33    pub fn builder() -> OAuthProxyOptionsBuilder {
34        OAuthProxyOptionsBuilder::default()
35    }
36
37    #[must_use]
38    pub fn current_url(mut self, current_url: impl Into<String>) -> Self {
39        self.current_url = Some(current_url.into());
40        self
41    }
42
43    #[must_use]
44    pub fn production_url(mut self, production_url: impl Into<String>) -> Self {
45        self.production_url = Some(production_url.into());
46        self
47    }
48
49    #[must_use]
50    pub fn max_age(mut self, max_age: Duration) -> Self {
51        self.max_age = max_age;
52        self
53    }
54
55    #[must_use]
56    pub fn secret(mut self, secret: impl Into<String>) -> Self {
57        self.secret = Some(secret.into());
58        self
59    }
60
61    pub(crate) fn to_value(&self) -> serde_json::Value {
62        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
63    }
64}
65
66#[derive(Debug, Clone, Default)]
67pub struct OAuthProxyOptionsBuilder {
68    current_url: Option<Option<String>>,
69    production_url: Option<Option<String>>,
70    max_age: Option<Duration>,
71    secret: Option<Option<String>>,
72}
73
74impl OAuthProxyOptionsBuilder {
75    #[must_use]
76    pub fn current_url(mut self, current_url: impl Into<String>) -> Self {
77        self.current_url = Some(Some(current_url.into()));
78        self
79    }
80
81    #[must_use]
82    pub fn production_url(mut self, production_url: impl Into<String>) -> Self {
83        self.production_url = Some(Some(production_url.into()));
84        self
85    }
86
87    #[must_use]
88    pub fn max_age(mut self, max_age: Duration) -> Self {
89        self.max_age = Some(max_age);
90        self
91    }
92
93    #[must_use]
94    pub fn secret(mut self, secret: impl Into<String>) -> Self {
95        self.secret = Some(Some(secret.into()));
96        self
97    }
98
99    #[must_use]
100    pub fn build(self) -> OAuthProxyOptions {
101        let defaults = OAuthProxyOptions::default();
102        OAuthProxyOptions {
103            current_url: self.current_url.unwrap_or(defaults.current_url),
104            production_url: self.production_url.unwrap_or(defaults.production_url),
105            max_age: self.max_age.unwrap_or(defaults.max_age),
106            secret: self.secret.unwrap_or(defaults.secret),
107        }
108    }
109}