clerk_fapi_rs/apis/
passkeys_api.rs

1/*
2 * Clerk Frontend API
3 *
4 * The Clerk REST Frontend API, meant to be accessed from a browser or native environment.  This is a Form Based API and all the data must be sent and formatted according to the `application/x-www-form-urlencoded` content type.  ### Versions  When the API changes in a way that isn't compatible with older versions, a new version is released. Each version is identified by its release date, e.g. `2021-02-05`. For more information, please see [Clerk API Versions](https://clerk.com/docs/backend-requests/versioning/overview).  ### Using the Try It Console  The `Try It` feature of the docs only works for **Development Instances** when using the `DevBrowser` security scheme. To use it, first generate a dev instance token from the `/v1/dev_browser` endpoint.  Please see https://clerk.com/docs for more information.
5 *
6 * The version of the OpenAPI document: v1
7 * Contact: support@clerk.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16/// struct for typed errors of method [`attempt_passkey_verification`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum AttemptPasskeyVerificationError {
20    Status400(models::ClerkErrors),
21    Status401(models::ClerkErrors),
22    Status403(models::ClerkErrors),
23    Status404(models::ClerkErrors),
24    Status422(models::ClerkErrors),
25    UnknownValue(serde_json::Value),
26}
27
28/// struct for typed errors of method [`delete_passkey`]
29#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum DeletePasskeyError {
32    Status403(models::ClerkErrors),
33    Status404(models::ClerkErrors),
34    UnknownValue(serde_json::Value),
35}
36
37/// struct for typed errors of method [`patch_passkey`]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum PatchPasskeyError {
41    Status404(models::ClerkErrors),
42    Status422(models::ClerkErrors),
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`post_passkey`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum PostPasskeyError {
50    Status403(models::ClerkErrors),
51    UnknownValue(serde_json::Value),
52}
53
54/// struct for typed errors of method [`read_passkey`]
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(untagged)]
57pub enum ReadPasskeyError {
58    Status404(models::ClerkErrors),
59    Status422(models::ClerkErrors),
60    UnknownValue(serde_json::Value),
61}
62
63/// Attempts to verify the provided passkey.
64pub async fn attempt_passkey_verification(
65    configuration: &configuration::Configuration,
66    passkey_id: &str,
67    origin: Option<&str>,
68    strategy: Option<&str>,
69    public_key_credential: Option<&str>,
70) -> Result<models::ClientClientWrappedPasskey, Error<AttemptPasskeyVerificationError>> {
71    // add a prefix to parameters to efficiently prevent name collisions
72    let p_path_passkey_id = passkey_id;
73    let p_header_origin = origin;
74    let p_form_strategy = strategy;
75    let p_form_public_key_credential = public_key_credential;
76
77    let uri_str = format!(
78        "{}/v1/me/passkeys/{passkey_id}/attempt_verification",
79        configuration.base_path,
80        passkey_id = crate::apis::urlencode(p_path_passkey_id)
81    );
82    let mut req_builder = configuration
83        .client
84        .request(reqwest::Method::POST, &uri_str);
85
86    if let Some(ref apikey) = configuration.api_key {
87        let key = apikey.key.clone();
88        let value = match apikey.prefix {
89            Some(ref prefix) => format!("{prefix} {key}"),
90            None => key,
91        };
92        req_builder = req_builder.query(&[("__dev_session", value)]);
93    }
94    if let Some(ref user_agent) = configuration.user_agent {
95        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
96    }
97    if let Some(param_value) = p_header_origin {
98        req_builder = req_builder.header("Origin", param_value.to_string());
99    }
100    if let Some(ref apikey) = configuration.api_key {
101        let key = apikey.key.clone();
102        let value = match apikey.prefix {
103            Some(ref prefix) => format!("{prefix} {key}"),
104            None => key,
105        };
106        req_builder = req_builder.header("__session", value);
107    };
108    let mut multipart_form_params = std::collections::HashMap::new();
109    if let Some(param_value) = p_form_strategy {
110        multipart_form_params.insert("strategy", param_value.to_string());
111    }
112    if let Some(param_value) = p_form_public_key_credential {
113        multipart_form_params.insert("public_key_credential", param_value.to_string());
114    }
115    req_builder = req_builder.form(&multipart_form_params);
116
117    let req = req_builder.build()?;
118    let resp = configuration.client.execute(req).await?;
119
120    let status = resp.status();
121    let content_type = resp
122        .headers()
123        .get("content-type")
124        .and_then(|v| v.to_str().ok())
125        .unwrap_or("application/octet-stream");
126    let content_type = super::ContentType::from(content_type);
127
128    if !status.is_client_error() && !status.is_server_error() {
129        let content = resp.text().await?;
130        match content_type {
131            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
132            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedPasskey`"))),
133            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedPasskey`")))),
134        }
135    } else {
136        let content = resp.text().await?;
137        let entity: Option<AttemptPasskeyVerificationError> = serde_json::from_str(&content).ok();
138        Err(Error::ResponseError(ResponseContent {
139            status,
140            content,
141            entity,
142        }))
143    }
144}
145
146/// Delete a passkey by id.
147pub async fn delete_passkey(
148    configuration: &configuration::Configuration,
149    passkey_id: &str,
150) -> Result<models::ClientClientWrappedDeletedObject, Error<DeletePasskeyError>> {
151    // add a prefix to parameters to efficiently prevent name collisions
152    let p_path_passkey_id = passkey_id;
153
154    let uri_str = format!(
155        "{}/v1/me/passkeys/{passkey_id}",
156        configuration.base_path,
157        passkey_id = crate::apis::urlencode(p_path_passkey_id)
158    );
159    let mut req_builder = configuration
160        .client
161        .request(reqwest::Method::DELETE, &uri_str);
162
163    if let Some(ref apikey) = configuration.api_key {
164        let key = apikey.key.clone();
165        let value = match apikey.prefix {
166            Some(ref prefix) => format!("{prefix} {key}"),
167            None => key,
168        };
169        req_builder = req_builder.query(&[("__dev_session", value)]);
170    }
171    if let Some(ref user_agent) = configuration.user_agent {
172        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
173    }
174    if let Some(ref apikey) = configuration.api_key {
175        let key = apikey.key.clone();
176        let value = match apikey.prefix {
177            Some(ref prefix) => format!("{prefix} {key}"),
178            None => key,
179        };
180        req_builder = req_builder.header("__session", value);
181    };
182
183    let req = req_builder.build()?;
184    let resp = configuration.client.execute(req).await?;
185
186    let status = resp.status();
187    let content_type = resp
188        .headers()
189        .get("content-type")
190        .and_then(|v| v.to_str().ok())
191        .unwrap_or("application/octet-stream");
192    let content_type = super::ContentType::from(content_type);
193
194    if !status.is_client_error() && !status.is_server_error() {
195        let content = resp.text().await?;
196        match content_type {
197            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
198            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedDeletedObject`"))),
199            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedDeletedObject`")))),
200        }
201    } else {
202        let content = resp.text().await?;
203        let entity: Option<DeletePasskeyError> = serde_json::from_str(&content).ok();
204        Err(Error::ResponseError(ResponseContent {
205            status,
206            content,
207            entity,
208        }))
209    }
210}
211
212/// Update properties of a specific passkey.
213pub async fn patch_passkey(
214    configuration: &configuration::Configuration,
215    passkey_id: &str,
216    name: Option<&str>,
217) -> Result<models::ClientClientWrappedPasskey, Error<PatchPasskeyError>> {
218    // add a prefix to parameters to efficiently prevent name collisions
219    let p_path_passkey_id = passkey_id;
220    let p_form_name = name;
221
222    let uri_str = format!(
223        "{}/v1/me/passkeys/{passkey_id}",
224        configuration.base_path,
225        passkey_id = crate::apis::urlencode(p_path_passkey_id)
226    );
227    let mut req_builder = configuration
228        .client
229        .request(reqwest::Method::PATCH, &uri_str);
230
231    if let Some(ref apikey) = configuration.api_key {
232        let key = apikey.key.clone();
233        let value = match apikey.prefix {
234            Some(ref prefix) => format!("{prefix} {key}"),
235            None => key,
236        };
237        req_builder = req_builder.query(&[("__dev_session", value)]);
238    }
239    if let Some(ref user_agent) = configuration.user_agent {
240        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
241    }
242    if let Some(ref apikey) = configuration.api_key {
243        let key = apikey.key.clone();
244        let value = match apikey.prefix {
245            Some(ref prefix) => format!("{prefix} {key}"),
246            None => key,
247        };
248        req_builder = req_builder.header("__session", value);
249    };
250    let mut multipart_form_params = std::collections::HashMap::new();
251    if let Some(param_value) = p_form_name {
252        multipart_form_params.insert("name", param_value.to_string());
253    }
254    req_builder = req_builder.form(&multipart_form_params);
255
256    let req = req_builder.build()?;
257    let resp = configuration.client.execute(req).await?;
258
259    let status = resp.status();
260    let content_type = resp
261        .headers()
262        .get("content-type")
263        .and_then(|v| v.to_str().ok())
264        .unwrap_or("application/octet-stream");
265    let content_type = super::ContentType::from(content_type);
266
267    if !status.is_client_error() && !status.is_server_error() {
268        let content = resp.text().await?;
269        match content_type {
270            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
271            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedPasskey`"))),
272            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedPasskey`")))),
273        }
274    } else {
275        let content = resp.text().await?;
276        let entity: Option<PatchPasskeyError> = serde_json::from_str(&content).ok();
277        Err(Error::ResponseError(ResponseContent {
278            status,
279            content,
280            entity,
281        }))
282    }
283}
284
285/// Create a new passkey.
286pub async fn post_passkey(
287    configuration: &configuration::Configuration,
288    _clerk_session_id: Option<&str>,
289    origin: Option<&str>,
290    x_original_host: Option<&str>,
291) -> Result<models::ClientClientWrappedPasskey, Error<PostPasskeyError>> {
292    // add a prefix to parameters to efficiently prevent name collisions
293    let p_query_clerk_session_id = _clerk_session_id;
294    let p_header_origin = origin;
295    let p_header_x_original_host = x_original_host;
296
297    let uri_str = format!("{}/v1/me/passkeys", configuration.base_path);
298    let mut req_builder = configuration
299        .client
300        .request(reqwest::Method::POST, &uri_str);
301
302    if let Some(ref param_value) = p_query_clerk_session_id {
303        req_builder = req_builder.query(&[("_clerk_session_id", &param_value.to_string())]);
304    }
305    if let Some(ref apikey) = configuration.api_key {
306        let key = apikey.key.clone();
307        let value = match apikey.prefix {
308            Some(ref prefix) => format!("{prefix} {key}"),
309            None => key,
310        };
311        req_builder = req_builder.query(&[("__dev_session", value)]);
312    }
313    if let Some(ref user_agent) = configuration.user_agent {
314        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
315    }
316    if let Some(param_value) = p_header_origin {
317        req_builder = req_builder.header("Origin", param_value.to_string());
318    }
319    if let Some(param_value) = p_header_x_original_host {
320        req_builder = req_builder.header("X-Original-Host", param_value.to_string());
321    }
322    if let Some(ref apikey) = configuration.api_key {
323        let key = apikey.key.clone();
324        let value = match apikey.prefix {
325            Some(ref prefix) => format!("{prefix} {key}"),
326            None => key,
327        };
328        req_builder = req_builder.header("__session", value);
329    };
330
331    let req = req_builder.build()?;
332    let resp = configuration.client.execute(req).await?;
333
334    let status = resp.status();
335    let content_type = resp
336        .headers()
337        .get("content-type")
338        .and_then(|v| v.to_str().ok())
339        .unwrap_or("application/octet-stream");
340    let content_type = super::ContentType::from(content_type);
341
342    if !status.is_client_error() && !status.is_server_error() {
343        let content = resp.text().await?;
344        match content_type {
345            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
346            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedPasskey`"))),
347            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedPasskey`")))),
348        }
349    } else {
350        let content = resp.text().await?;
351        let entity: Option<PostPasskeyError> = serde_json::from_str(&content).ok();
352        Err(Error::ResponseError(ResponseContent {
353            status,
354            content,
355            entity,
356        }))
357    }
358}
359
360/// Retrieve all properties associated a specific passkey.
361pub async fn read_passkey(
362    configuration: &configuration::Configuration,
363    passkey_id: &str,
364) -> Result<models::ClientClientWrappedPasskey, Error<ReadPasskeyError>> {
365    // add a prefix to parameters to efficiently prevent name collisions
366    let p_path_passkey_id = passkey_id;
367
368    let uri_str = format!(
369        "{}/v1/me/passkeys/{passkey_id}",
370        configuration.base_path,
371        passkey_id = crate::apis::urlencode(p_path_passkey_id)
372    );
373    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
374
375    if let Some(ref apikey) = configuration.api_key {
376        let key = apikey.key.clone();
377        let value = match apikey.prefix {
378            Some(ref prefix) => format!("{prefix} {key}"),
379            None => key,
380        };
381        req_builder = req_builder.query(&[("__dev_session", value)]);
382    }
383    if let Some(ref user_agent) = configuration.user_agent {
384        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
385    }
386    if let Some(ref apikey) = configuration.api_key {
387        let key = apikey.key.clone();
388        let value = match apikey.prefix {
389            Some(ref prefix) => format!("{prefix} {key}"),
390            None => key,
391        };
392        req_builder = req_builder.header("__session", value);
393    };
394
395    let req = req_builder.build()?;
396    let resp = configuration.client.execute(req).await?;
397
398    let status = resp.status();
399    let content_type = resp
400        .headers()
401        .get("content-type")
402        .and_then(|v| v.to_str().ok())
403        .unwrap_or("application/octet-stream");
404    let content_type = super::ContentType::from(content_type);
405
406    if !status.is_client_error() && !status.is_server_error() {
407        let content = resp.text().await?;
408        match content_type {
409            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
410            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedPasskey`"))),
411            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedPasskey`")))),
412        }
413    } else {
414        let content = resp.text().await?;
415        let entity: Option<ReadPasskeyError> = serde_json::from_str(&content).ok();
416        Err(Error::ResponseError(ResponseContent {
417            status,
418            content,
419            entity,
420        }))
421    }
422}