Skip to main content

openstack_sdk_identity/v4/federation/mapping/
create.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//
15// WARNING: This file is automatically generated from OpenAPI schema using
16// `openstack-codegenerator`.
17
18use derive_builder::Builder;
19use http::{HeaderMap, HeaderName, HeaderValue};
20
21use openstack_sdk_core::api::rest_endpoint_prelude::*;
22
23use serde::Deserialize;
24use serde::Serialize;
25use serde_json::Value;
26use std::borrow::Cow;
27use std::collections::BTreeMap;
28
29#[derive(Debug, Deserialize, Clone, Serialize)]
30pub enum Type {
31    #[serde(rename = "jwt")]
32    Jwt,
33    #[serde(rename = "oidc")]
34    Oidc,
35}
36
37/// OIDC/JWT attribute mapping create data.
38#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
39#[builder(setter(strip_option))]
40pub struct Mapping<'a> {
41    /// List of allowed redirect urls (only for `oidc` type).
42    #[serde(skip_serializing_if = "Option::is_none")]
43    #[builder(default, setter(into))]
44    pub(crate) allowed_redirect_uris: Option<Vec<Cow<'a, str>>>,
45
46    /// List of audiences that must be present in the token.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[builder(default, setter(into))]
49    pub(crate) bound_audiences: Option<Vec<Cow<'a, str>>>,
50
51    /// Additional claims that must be present in the token.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    #[builder(default, private, setter(into, name = "_bound_claims"))]
54    pub(crate) bound_claims: Option<BTreeMap<Cow<'a, str>, Value>>,
55
56    /// Token subject value that must be set in the token.
57    #[serde(skip_serializing_if = "Option::is_none")]
58    #[builder(default, setter(into))]
59    pub(crate) bound_subject: Option<Cow<'a, str>>,
60
61    /// `domain_id` owning the attribute mapping.
62    ///
63    /// Unset `domain_id` means the attribute mapping is shared and can be used
64    /// by different domains. This requires `domain_id_claim` to be present.
65    /// Attribute mapping can be only shared when the referred identity
66    /// provider is also shared (does not set the `domain_id` attribute).
67    #[serde(skip_serializing_if = "Option::is_none")]
68    #[builder(default, setter(into))]
69    pub(crate) domain_id: Option<Cow<'a, str>>,
70
71    /// `domain_id` claim name.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    #[builder(default, setter(into))]
74    pub(crate) domain_id_claim: Option<Cow<'a, str>>,
75
76    /// Mapping enabled property. Inactive mappings can not be used for login.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    #[builder(default, setter(into))]
79    pub(crate) enabled: Option<bool>,
80
81    /// `groups` claim name.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    #[builder(default, setter(into))]
84    pub(crate) groups_claim: Option<Cow<'a, str>>,
85
86    /// Attribute mapping ID for federated logins.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    #[builder(default, setter(into))]
89    pub(crate) id: Option<Option<Cow<'a, str>>>,
90
91    /// ID of the federated identity provider for which this attribute mapping
92    /// can be used.
93    #[serde()]
94    #[builder(setter(into))]
95    pub(crate) idp_id: Cow<'a, str>,
96
97    /// Attribute mapping name for federated logins.
98    #[serde()]
99    #[builder(setter(into))]
100    pub(crate) name: Cow<'a, str>,
101
102    /// List of OIDC scopes.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    #[builder(default, setter(into))]
105    pub(crate) oidc_scopes: Option<Vec<Cow<'a, str>>>,
106
107    /// Fixed project_id for the token.
108    #[serde(skip_serializing_if = "Option::is_none")]
109    #[builder(default, setter(into))]
110    pub(crate) token_project_id: Option<Cow<'a, str>>,
111
112    /// Token restrictions to be applied to the granted token.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    #[builder(default, setter(into))]
115    pub(crate) token_restriction_id: Option<Option<Cow<'a, str>>>,
116
117    /// Attribute mapping type.
118    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
119    #[builder(default)]
120    pub(crate) _type: Option<Type>,
121
122    /// `user_id` claim name.
123    #[serde()]
124    #[builder(setter(into))]
125    pub(crate) user_id_claim: Cow<'a, str>,
126
127    /// `user_name` claim name.
128    #[serde()]
129    #[builder(setter(into))]
130    pub(crate) user_name_claim: Cow<'a, str>,
131}
132
133impl<'a> MappingBuilder<'a> {
134    /// Additional claims that must be present in the token.
135    pub fn bound_claims<I, K, V>(&mut self, iter: I) -> &mut Self
136    where
137        I: Iterator<Item = (K, V)>,
138        K: Into<Cow<'a, str>>,
139        V: Into<Value>,
140    {
141        self.bound_claims
142            .get_or_insert(None)
143            .get_or_insert_with(BTreeMap::new)
144            .extend(iter.map(|(k, v)| (k.into(), v.into())));
145        self
146    }
147}
148
149#[derive(Builder, Debug, Clone)]
150#[builder(setter(strip_option))]
151pub struct Request<'a> {
152    /// OIDC/JWT attribute mapping create data.
153    #[builder(setter(into))]
154    pub(crate) mapping: Mapping<'a>,
155
156    #[builder(setter(name = "_headers"), default, private)]
157    _headers: Option<HeaderMap>,
158}
159impl<'a> Request<'a> {
160    /// Create a builder for the endpoint.
161    pub fn builder() -> RequestBuilder<'a> {
162        RequestBuilder::default()
163    }
164}
165
166impl<'a> RequestBuilder<'a> {
167    /// Add a single header to the Mapping.
168    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
169    where
170        K: Into<HeaderName>,
171        V: Into<HeaderValue>,
172    {
173        self._headers
174            .get_or_insert(None)
175            .get_or_insert_with(HeaderMap::new)
176            .insert(header_name.into(), header_value.into());
177        self
178    }
179
180    /// Add multiple headers.
181    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
182    where
183        I: Iterator<Item = T>,
184        T: Into<(Option<HeaderName>, HeaderValue)>,
185    {
186        self._headers
187            .get_or_insert(None)
188            .get_or_insert_with(HeaderMap::new)
189            .extend(iter.map(Into::into));
190        self
191    }
192}
193
194impl RestEndpoint for Request<'_> {
195    fn method(&self) -> http::Method {
196        http::Method::POST
197    }
198
199    fn endpoint(&self) -> Cow<'static, str> {
200        "federation/mappings".to_string().into()
201    }
202
203    fn parameters(&self) -> QueryParams<'_> {
204        QueryParams::default()
205    }
206
207    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
208        let mut params = JsonBodyParams::default();
209
210        params.push("mapping", serde_json::to_value(&self.mapping)?);
211
212        params.into_body()
213    }
214
215    fn service_type(&self) -> ServiceType {
216        ServiceType::Identity
217    }
218
219    fn response_key(&self) -> Option<Cow<'static, str>> {
220        Some("mapping".into())
221    }
222
223    /// Returns headers to be set into the request
224    fn request_headers(&self) -> Option<&HeaderMap> {
225        self._headers.as_ref()
226    }
227
228    /// Returns required API version
229    fn api_version(&self) -> Option<ApiVersion> {
230        Some(ApiVersion::new(4, 0))
231    }
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237    use http::{HeaderName, HeaderValue};
238    use httpmock::MockServer;
239    #[cfg(feature = "sync")]
240    use openstack_sdk_core::api::Query;
241    use openstack_sdk_core::test::client::FakeOpenStackClient;
242    use openstack_sdk_core::types::ServiceType;
243    use serde_json::json;
244
245    #[test]
246    fn test_service_type() {
247        assert_eq!(
248            Request::builder()
249                .mapping(
250                    MappingBuilder::default()
251                        .idp_id("foo")
252                        .name("foo")
253                        .user_id_claim("foo")
254                        .user_name_claim("foo")
255                        .build()
256                        .unwrap()
257                )
258                .build()
259                .unwrap()
260                .service_type(),
261            ServiceType::Identity
262        );
263    }
264
265    #[test]
266    fn test_response_key() {
267        assert_eq!(
268            Request::builder()
269                .mapping(
270                    MappingBuilder::default()
271                        .idp_id("foo")
272                        .name("foo")
273                        .user_id_claim("foo")
274                        .user_name_claim("foo")
275                        .build()
276                        .unwrap()
277                )
278                .build()
279                .unwrap()
280                .response_key()
281                .unwrap(),
282            "mapping"
283        );
284    }
285
286    #[cfg(feature = "sync")]
287    #[test]
288    fn endpoint() {
289        let server = MockServer::start();
290        let client = FakeOpenStackClient::new(server.base_url());
291        let mock = server.mock(|when, then| {
292            when.method(httpmock::Method::POST)
293                .path("/federation/mappings".to_string());
294
295            then.status(200)
296                .header("content-type", "application/json")
297                .json_body(json!({ "mapping": {} }));
298        });
299
300        let endpoint = Request::builder()
301            .mapping(
302                MappingBuilder::default()
303                    .idp_id("foo")
304                    .name("foo")
305                    .user_id_claim("foo")
306                    .user_name_claim("foo")
307                    .build()
308                    .unwrap(),
309            )
310            .build()
311            .unwrap();
312        let _: serde_json::Value = endpoint.query(&client).unwrap();
313        mock.assert();
314    }
315
316    #[cfg(feature = "sync")]
317    #[test]
318    fn endpoint_headers() {
319        let server = MockServer::start();
320        let client = FakeOpenStackClient::new(server.base_url());
321        let mock = server.mock(|when, then| {
322            when.method(httpmock::Method::POST)
323                .path("/federation/mappings".to_string())
324                .header("foo", "bar")
325                .header("not_foo", "not_bar");
326            then.status(200)
327                .header("content-type", "application/json")
328                .json_body(json!({ "mapping": {} }));
329        });
330
331        let endpoint = Request::builder()
332            .mapping(
333                MappingBuilder::default()
334                    .idp_id("foo")
335                    .name("foo")
336                    .user_id_claim("foo")
337                    .user_name_claim("foo")
338                    .build()
339                    .unwrap(),
340            )
341            .headers(
342                [(
343                    Some(HeaderName::from_static("foo")),
344                    HeaderValue::from_static("bar"),
345                )]
346                .into_iter(),
347            )
348            .header(
349                HeaderName::from_static("not_foo"),
350                HeaderValue::from_static("not_bar"),
351            )
352            .build()
353            .unwrap();
354        let _: serde_json::Value = endpoint.query(&client).unwrap();
355        mock.assert();
356    }
357}