Skip to main content

swf_builders/services/
authentication.rs

1use swf_core::models::authentication::*;
2
3/// Represents a service used to build AuthenticationPolicyDefinitions
4#[derive(Default)]
5pub struct AuthenticationPolicyDefinitionBuilder {
6    reference: Option<String>,
7    builder: Option<AuthenticationSchemeBuilder>,
8}
9
10impl AuthenticationPolicyDefinitionBuilder {
11    /// Initializes a new AuthenticationPolicyDefinition
12    pub fn new() -> Self {
13        Self::default()
14    }
15
16    /// Sets the name of the top-level authentication policy to use
17    pub fn use_(&mut self, reference: &str) -> &mut Self {
18        self.reference = Some(reference.to_string());
19        self
20    }
21
22    /// Configures the policy to use 'Basic' authentication
23    pub fn basic(&mut self) -> &mut BasicAuthenticationSchemeDefinitionBuilder {
24        let builder = BasicAuthenticationSchemeDefinitionBuilder::new();
25        self.builder = Some(AuthenticationSchemeBuilder::Basic(builder));
26        if let Some(AuthenticationSchemeBuilder::Basic(ref mut builder)) = self.builder {
27            builder
28        } else {
29            unreachable!("Builder should always be set to Basic");
30        }
31    }
32
33    /// Configures the policy to use 'Bearer' authentication
34    pub fn bearer(&mut self) -> &mut BearerAuthenticationSchemeDefinitionBuilder {
35        let builder = BearerAuthenticationSchemeDefinitionBuilder::new();
36        self.builder = Some(AuthenticationSchemeBuilder::Bearer(builder));
37        if let Some(AuthenticationSchemeBuilder::Bearer(ref mut builder)) = self.builder {
38            builder
39        } else {
40            unreachable!("Builder should always be set to Bearer");
41        }
42    }
43
44    /// Configures the policy to use 'Digest' authentication
45    pub fn digest(&mut self) -> &mut DigestAuthenticationSchemeDefinitionBuilder {
46        let builder = DigestAuthenticationSchemeDefinitionBuilder::new();
47        self.builder = Some(AuthenticationSchemeBuilder::Digest(builder));
48        if let Some(AuthenticationSchemeBuilder::Digest(ref mut builder)) = self.builder {
49            builder
50        } else {
51            unreachable!("Builder should always be set to Digest");
52        }
53    }
54
55    /// Configures the policy to use 'OAuth2' authentication
56    pub fn oauth2(&mut self) -> &mut OAuth2AuthenticationSchemeDefinitionBuilder {
57        let builder = OAuth2AuthenticationSchemeDefinitionBuilder::new();
58        self.builder = Some(AuthenticationSchemeBuilder::OAuth2(builder));
59        if let Some(AuthenticationSchemeBuilder::OAuth2(ref mut builder)) = self.builder {
60            builder
61        } else {
62            unreachable!("Builder should always be set to OAuth2");
63        }
64    }
65
66    /// Configures the policy to use 'Oidc' authentication
67    pub fn oidc(&mut self) -> &mut OidcAuthenticationSchemeDefinitionBuilder {
68        let builder = OidcAuthenticationSchemeDefinitionBuilder::new();
69        self.builder = Some(AuthenticationSchemeBuilder::Oidc(builder));
70        if let Some(AuthenticationSchemeBuilder::Oidc(ref mut builder)) = self.builder {
71            builder
72        } else {
73            unreachable!("Builder should always be set to Oidc");
74        }
75    }
76
77    /// Builds the configured AuthenticationPolicyDefinition
78    pub fn build(self) -> AuthenticationPolicyDefinition {
79        if let Some(reference) = self.reference {
80            AuthenticationPolicyDefinition {
81                use_: Some(reference),
82                ..Default::default()
83            }
84        } else {
85            if let Some(builder) = self.builder {
86                match builder {
87                    AuthenticationSchemeBuilder::Basic(builder) => builder.build(),
88                    AuthenticationSchemeBuilder::Bearer(builder) => builder.build(),
89                    AuthenticationSchemeBuilder::Digest(builder) => builder.build(),
90                    AuthenticationSchemeBuilder::OAuth2(builder) => builder.build(),
91                    AuthenticationSchemeBuilder::Oidc(builder) => builder.build(),
92                }
93            } else {
94                panic!("The authentication policy must be configured");
95            }
96        }
97    }
98}
99
100/// Enumerates all supported authentication scheme builders
101pub(crate) enum AuthenticationSchemeBuilder {
102    Basic(BasicAuthenticationSchemeDefinitionBuilder),
103    Bearer(BearerAuthenticationSchemeDefinitionBuilder),
104    Digest(DigestAuthenticationSchemeDefinitionBuilder),
105    OAuth2(OAuth2AuthenticationSchemeDefinitionBuilder),
106    Oidc(OidcAuthenticationSchemeDefinitionBuilder),
107}
108
109/// Represents the service used to build BasicAuthenticationSchemeDefinitions
110#[derive(Default)]
111pub struct BasicAuthenticationSchemeDefinitionBuilder {
112    scheme: BasicAuthenticationSchemeDefinition,
113}
114
115impl BasicAuthenticationSchemeDefinitionBuilder {
116    /// Initializes a new BasicAuthenticationSchemeDefinitionBuilder
117    pub fn new() -> Self {
118        Self::default()
119    }
120
121    /// Configures the authentication scheme to load from the specified secret
122    pub fn use_secret(&mut self, secret: &str) -> &mut Self {
123        self.scheme.use_ = Some(secret.to_string());
124        self
125    }
126
127    /// Sets the username to use
128    pub fn with_username(&mut self, username: &str) -> &mut Self {
129        self.scheme.username = Some(username.to_string());
130        self
131    }
132
133    /// Sets the password to use
134    pub fn with_password(&mut self, password: &str) -> &mut Self {
135        self.scheme.password = Some(password.to_string());
136        self
137    }
138
139    /// Builds the configured AuthenticationPolicyDefinition
140    pub fn build(self) -> AuthenticationPolicyDefinition {
141        AuthenticationPolicyDefinition {
142            basic: Some(self.scheme),
143            ..Default::default()
144        }
145    }
146}
147
148/// Represents the service used to build BearerAuthenticationSchemeDefinitions
149#[derive(Default)]
150pub struct BearerAuthenticationSchemeDefinitionBuilder {
151    scheme: BearerAuthenticationSchemeDefinition,
152}
153
154impl BearerAuthenticationSchemeDefinitionBuilder {
155    /// Initializes a new BearerAuthenticationSchemeDefinitionBuilder
156    pub fn new() -> Self {
157        Self::default()
158    }
159
160    /// Configures the authentication scheme to load from the specified secret
161    pub fn use_secret(&mut self, secret: &str) -> &mut Self {
162        self.scheme.use_ = Some(secret.to_string());
163        self
164    }
165
166    /// Sets the bearer token to use
167    pub fn with_token(&mut self, token: &str) -> &mut Self {
168        self.scheme.token = Some(token.to_string());
169        self
170    }
171
172    /// Builds the configured AuthenticationPolicyDefinition
173    pub fn build(self) -> AuthenticationPolicyDefinition {
174        AuthenticationPolicyDefinition {
175            bearer: Some(self.scheme),
176            ..Default::default()
177        }
178    }
179}
180
181/// Represents the service used to build DigestAuthenticationSchemeDefinitions
182#[derive(Default)]
183pub struct DigestAuthenticationSchemeDefinitionBuilder {
184    scheme: DigestAuthenticationSchemeDefinition,
185}
186
187impl DigestAuthenticationSchemeDefinitionBuilder {
188    /// Initializes a new DigestAuthenticationSchemeDefinitionBuilder
189    pub fn new() -> Self {
190        Self::default()
191    }
192
193    /// Configures the authentication scheme to load from the specified secret
194    pub fn use_secret(&mut self, secret: &str) -> &mut Self {
195        self.scheme.use_ = Some(secret.to_string());
196        self
197    }
198
199    /// Sets the username to use
200    pub fn with_username(&mut self, username: &str) -> &mut Self {
201        self.scheme.username = Some(username.to_string());
202        self
203    }
204
205    /// Sets the password to use
206    pub fn with_password(&mut self, password: &str) -> &mut Self {
207        self.scheme.password = Some(password.to_string());
208        self
209    }
210
211    /// Builds the configured AuthenticationPolicyDefinition
212    pub fn build(self) -> AuthenticationPolicyDefinition {
213        AuthenticationPolicyDefinition {
214            digest: Some(self.scheme),
215            ..Default::default()
216        }
217    }
218}
219
220/// Represents the service used to build OAuth2AuthenticationSchemeDefinitions
221#[derive(Default)]
222pub struct OAuth2AuthenticationSchemeDefinitionBuilder {
223    scheme: OAuth2AuthenticationSchemeDefinition,
224}
225
226impl OAuth2AuthenticationSchemeDefinitionBuilder {
227    /// Initializes a new OAuth2AuthenticationSchemeDefinitionBuilder
228    pub fn new() -> Self {
229        Self::default()
230    }
231
232    /// Configures the authentication scheme to load from the specified secret
233    pub fn use_secret(&mut self, secret: &str) -> &mut Self {
234        self.scheme.use_ = Some(secret.to_string());
235        self
236    }
237
238    /// Sets the OAuth2 authority URL
239    pub fn with_authority(&mut self, authority: &str) -> &mut Self {
240        self.scheme.authority = Some(authority.to_string());
241        self
242    }
243
244    /// Sets the OAuth2 grant type
245    pub fn with_grant(&mut self, grant: &str) -> &mut Self {
246        self.scheme.grant = Some(grant.to_string());
247        self
248    }
249
250    /// Sets the OAuth2 client id
251    pub fn with_client_id(&mut self, id: &str) -> &mut Self {
252        self.scheme.client.get_or_insert_with(Default::default).id = Some(id.to_string());
253        self
254    }
255
256    /// Sets the OAuth2 client secret
257    pub fn with_client_secret(&mut self, secret: &str) -> &mut Self {
258        self.scheme
259            .client
260            .get_or_insert_with(Default::default)
261            .secret = Some(secret.to_string());
262        self
263    }
264
265    /// Sets the OAuth2 scopes
266    pub fn with_scopes(&mut self, scopes: Vec<String>) -> &mut Self {
267        self.scheme.scopes = Some(scopes);
268        self
269    }
270
271    /// Builds the configured AuthenticationPolicyDefinition
272    pub fn build(self) -> AuthenticationPolicyDefinition {
273        AuthenticationPolicyDefinition {
274            oauth2: Some(self.scheme),
275            ..Default::default()
276        }
277    }
278}
279
280/// Represents the service used to build OpenIDConnectSchemeDefinitions
281#[derive(Default)]
282pub struct OidcAuthenticationSchemeDefinitionBuilder {
283    scheme: OpenIDConnectSchemeDefinition,
284}
285
286impl OidcAuthenticationSchemeDefinitionBuilder {
287    /// Initializes a new OidcAuthenticationSchemeDefinitionBuilder
288    pub fn new() -> Self {
289        Self::default()
290    }
291
292    /// Configures the authentication scheme to load from the specified secret
293    pub fn use_secret(&mut self, secret: &str) -> &mut Self {
294        self.scheme.use_ = Some(secret.to_string());
295        self
296    }
297
298    /// Sets the OIDC authority URL (full token endpoint URL)
299    pub fn with_authority(&mut self, authority: &str) -> &mut Self {
300        self.scheme.authority = Some(authority.to_string());
301        self
302    }
303
304    /// Sets the OIDC grant type
305    pub fn with_grant(&mut self, grant: &str) -> &mut Self {
306        self.scheme.grant = Some(grant.to_string());
307        self
308    }
309
310    /// Sets the OIDC client id
311    pub fn with_client_id(&mut self, id: &str) -> &mut Self {
312        self.scheme.client.get_or_insert_with(Default::default).id = Some(id.to_string());
313        self
314    }
315
316    /// Sets the OIDC client secret
317    pub fn with_client_secret(&mut self, secret: &str) -> &mut Self {
318        self.scheme
319            .client
320            .get_or_insert_with(Default::default)
321            .secret = Some(secret.to_string());
322        self
323    }
324
325    /// Sets the OIDC scopes
326    pub fn with_scopes(&mut self, scopes: Vec<String>) -> &mut Self {
327        self.scheme.scopes = Some(scopes);
328        self
329    }
330
331    /// Builds the configured AuthenticationPolicyDefinition
332    pub fn build(self) -> AuthenticationPolicyDefinition {
333        AuthenticationPolicyDefinition {
334            oidc: Some(self.scheme),
335            ..Default::default()
336        }
337    }
338}