1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use async_trait::async_trait;
use serde::Serialize;
use thiserror::Error;
use crate::user_management::{MagicAuth, UserManagement};
use crate::{ResponseExt, WorkOsError, WorkOsResult};
/// The parameters for [`CreateMagicAuth`].
#[derive(Debug, Serialize)]
pub struct CreateMagicAuthParams<'a> {
/// The email address of the user.
pub email: &'a str,
/// The token of an invitation.
pub invitation_token: Option<&'a str>,
}
/// An error returned from [`CreateMagicAuth`].
#[derive(Debug, Error)]
pub enum CreateMagicAuthError {}
impl From<CreateMagicAuthError> for WorkOsError<CreateMagicAuthError> {
fn from(err: CreateMagicAuthError) -> Self {
Self::Operation(err)
}
}
/// [WorkOS Docs: Create a Magic Auth code](https://workos.com/docs/reference/user-management/magic-auth/create)
#[async_trait]
pub trait CreateMagicAuth {
/// Creates a one-time authentication code that can be sent to the user's email address.
///
/// [WorkOS Docs: Create a Magic Auth code](https://workos.com/docs/reference/user-management/magic-auth/create)
///
/// # Examples
///
/// ```
/// # use workos::WorkOsResult;
/// # use workos::user_management::*;
/// use workos::{ApiKey, WorkOs};
///
/// # async fn run() -> WorkOsResult<(), CreateMagicAuthError> {
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
///
/// let magic_auth = workos
/// .user_management()
/// .create_magic_auth(&CreateMagicAuthParams {
/// email: "marcelina@example.com",
/// invitation_token: None,
/// })
/// .await?;
/// # Ok(())
/// # }
/// ```
async fn create_magic_auth(
&self,
params: &CreateMagicAuthParams<'_>,
) -> WorkOsResult<MagicAuth, CreateMagicAuthError>;
}
#[async_trait]
impl CreateMagicAuth for UserManagement<'_> {
async fn create_magic_auth(
&self,
params: &CreateMagicAuthParams<'_>,
) -> WorkOsResult<MagicAuth, CreateMagicAuthError> {
let url = self.workos.base_url().join("/user_management/magic_auth")?;
let magic_auth = self
.workos
.client()
.post(url)
.bearer_auth(self.workos.key())
.json(¶ms)
.send()
.await?
.handle_unauthorized_or_generic_error()
.await?
.json::<MagicAuth>()
.await?;
Ok(magic_auth)
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use tokio;
use crate::user_management::MagicAuthId;
use crate::{ApiKey, WorkOs};
use super::*;
#[tokio::test]
async fn it_calls_the_create_magic_auth_endpoint() {
let mut server = mockito::Server::new_async().await;
let workos = WorkOs::builder(&ApiKey::from("sk_example_123456789"))
.base_url(&server.url())
.unwrap()
.build();
server
.mock("POST", "/user_management/magic_auth")
.match_header("Authorization", "Bearer sk_example_123456789")
.with_status(201)
.with_body(
json!({
"id": "magic_auth_01E4ZCR3C56J083X43JQXF3JK5",
"user_id": "user_01HWWYEH2NPT48X82ZT23K5AX4",
"email": "marcelina.davis@example.com",
"expires_at": "2021-07-01T19:07:33.155Z",
"code": "123456",
"created_at": "2021-06-25T19:07:33.155Z",
"updated_at": "2021-06-25T19:07:33.155Z"
})
.to_string(),
)
.create_async()
.await;
let magic_auth = workos
.user_management()
.create_magic_auth(&CreateMagicAuthParams {
email: "marcelina@example.com",
invitation_token: None,
})
.await
.unwrap();
assert_eq!(
magic_auth.id,
MagicAuthId::from("magic_auth_01E4ZCR3C56J083X43JQXF3JK5")
)
}
}