Skip to main content

openstack_sdk_identity/v4/user/passkey/
register_start.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
18//! Generate a challenge that the user must sign with the passkey or security
19//! device. Signed challenge must be sent to the
20//! `/v4/users/{user_id}/passkey/register_finish` endpoint.
21//!
22use derive_builder::Builder;
23use http::{HeaderMap, HeaderName, HeaderValue};
24
25use openstack_sdk_core::api::rest_endpoint_prelude::*;
26
27use serde::Deserialize;
28use serde::Serialize;
29use std::borrow::Cow;
30
31/// Passkey information.
32#[derive(Builder, Debug, Deserialize, Clone, Serialize)]
33#[builder(setter(strip_option))]
34pub struct Passkey<'a> {
35    /// Passkey description.
36    #[serde(skip_serializing_if = "Option::is_none")]
37    #[builder(default, setter(into))]
38    pub(crate) description: Option<Cow<'a, str>>,
39}
40
41#[derive(Builder, Debug, Clone)]
42#[builder(setter(strip_option))]
43pub struct Request<'a> {
44    /// Passkey information.
45    #[builder(setter(into))]
46    pub(crate) passkey: Passkey<'a>,
47
48    /// The ID of the user.
49    #[builder(default, setter(into))]
50    user_id: Cow<'a, str>,
51
52    #[builder(setter(name = "_headers"), default, private)]
53    _headers: Option<HeaderMap>,
54}
55impl<'a> Request<'a> {
56    /// Create a builder for the endpoint.
57    pub fn builder() -> RequestBuilder<'a> {
58        RequestBuilder::default()
59    }
60}
61
62impl<'a> RequestBuilder<'a> {
63    /// Add a single header to the Register_Start.
64    pub fn header<K, V>(&mut self, header_name: K, header_value: V) -> &mut Self
65    where
66        K: Into<HeaderName>,
67        V: Into<HeaderValue>,
68    {
69        self._headers
70            .get_or_insert(None)
71            .get_or_insert_with(HeaderMap::new)
72            .insert(header_name.into(), header_value.into());
73        self
74    }
75
76    /// Add multiple headers.
77    pub fn headers<I, T>(&mut self, iter: I) -> &mut Self
78    where
79        I: Iterator<Item = T>,
80        T: Into<(Option<HeaderName>, HeaderValue)>,
81    {
82        self._headers
83            .get_or_insert(None)
84            .get_or_insert_with(HeaderMap::new)
85            .extend(iter.map(Into::into));
86        self
87    }
88}
89
90impl RestEndpoint for Request<'_> {
91    fn method(&self) -> http::Method {
92        http::Method::POST
93    }
94
95    fn endpoint(&self) -> Cow<'static, str> {
96        format!(
97            "users/{user_id}/passkeys/register_start",
98            user_id = self.user_id.as_ref(),
99        )
100        .into()
101    }
102
103    fn parameters(&self) -> QueryParams<'_> {
104        QueryParams::default()
105    }
106
107    fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, BodyError> {
108        let mut params = JsonBodyParams::default();
109
110        params.push("passkey", serde_json::to_value(&self.passkey)?);
111
112        params.into_body()
113    }
114
115    fn service_type(&self) -> ServiceType {
116        ServiceType::Identity
117    }
118
119    fn response_key(&self) -> Option<Cow<'static, str>> {
120        Some("public_key".into())
121    }
122
123    /// Returns headers to be set into the request
124    fn request_headers(&self) -> Option<&HeaderMap> {
125        self._headers.as_ref()
126    }
127
128    /// Returns required API version
129    fn api_version(&self) -> Option<ApiVersion> {
130        Some(ApiVersion::new(4, 0))
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137    use http::{HeaderName, HeaderValue};
138    use httpmock::MockServer;
139    #[cfg(feature = "sync")]
140    use openstack_sdk_core::api::Query;
141    use openstack_sdk_core::test::client::FakeOpenStackClient;
142    use openstack_sdk_core::types::ServiceType;
143    use serde_json::json;
144
145    #[test]
146    fn test_service_type() {
147        assert_eq!(
148            Request::builder()
149                .passkey(PasskeyBuilder::default().build().unwrap())
150                .build()
151                .unwrap()
152                .service_type(),
153            ServiceType::Identity
154        );
155    }
156
157    #[test]
158    fn test_response_key() {
159        assert_eq!(
160            Request::builder()
161                .passkey(PasskeyBuilder::default().build().unwrap())
162                .build()
163                .unwrap()
164                .response_key()
165                .unwrap(),
166            "public_key"
167        );
168    }
169
170    #[cfg(feature = "sync")]
171    #[test]
172    fn endpoint() {
173        let server = MockServer::start();
174        let client = FakeOpenStackClient::new(server.base_url());
175        let mock = server.mock(|when, then| {
176            when.method(httpmock::Method::POST).path(format!(
177                "/users/{user_id}/passkeys/register_start",
178                user_id = "user_id",
179            ));
180
181            then.status(200)
182                .header("content-type", "application/json")
183                .json_body(json!({ "public_key": {} }));
184        });
185
186        let endpoint = Request::builder()
187            .user_id("user_id")
188            .passkey(PasskeyBuilder::default().build().unwrap())
189            .build()
190            .unwrap();
191        let _: serde_json::Value = endpoint.query(&client).unwrap();
192        mock.assert();
193    }
194
195    #[cfg(feature = "sync")]
196    #[test]
197    fn endpoint_headers() {
198        let server = MockServer::start();
199        let client = FakeOpenStackClient::new(server.base_url());
200        let mock = server.mock(|when, then| {
201            when.method(httpmock::Method::POST)
202                .path(format!(
203                    "/users/{user_id}/passkeys/register_start",
204                    user_id = "user_id",
205                ))
206                .header("foo", "bar")
207                .header("not_foo", "not_bar");
208            then.status(200)
209                .header("content-type", "application/json")
210                .json_body(json!({ "public_key": {} }));
211        });
212
213        let endpoint = Request::builder()
214            .user_id("user_id")
215            .passkey(PasskeyBuilder::default().build().unwrap())
216            .headers(
217                [(
218                    Some(HeaderName::from_static("foo")),
219                    HeaderValue::from_static("bar"),
220                )]
221                .into_iter(),
222            )
223            .header(
224                HeaderName::from_static("not_foo"),
225                HeaderValue::from_static("not_bar"),
226            )
227            .build()
228            .unwrap();
229        let _: serde_json::Value = endpoint.query(&client).unwrap();
230        mock.assert();
231    }
232}