-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.rs
139 lines (134 loc) · 4.16 KB
/
auth.rs
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
138
139
use crate::openapi::apis::configuration::Configuration;
use crate::openapi::apis::{authenticate_api, transactions_api};
use crate::Error;
pub struct Auth {
pub(crate) configuration: Configuration,
}
impl Auth {
/// Creates a new instance of the `Auth` struct.
pub fn new(configuration: Configuration) -> Self {
Self { configuration }
}
/// Creates a transaction to start a user's registration process.
///
/// # Arguments
///
/// * `external_id` - A unique, immutable string that represents the user.
/// * `passkey_display_name` - The label for the user's passkey that they will see when logging in.
///
/// # Returns
///
/// A `Result` containing the transaction ID as a string or an `Error`.
///
/// # Examples
///
/// ```ignore
/// use passage_flex::PassageFlex;
///
/// let passage_flex = PassageFlex::new(
/// std::env::var("PASSAGE_APP_ID").unwrap(),
/// std::env::var("PASSAGE_API_KEY").unwrap(),
/// );
///
/// let transaction = passage_flex
/// .create_register_transaction(
/// "00000000-0000-0000-0000-000000000001".to_string(),
/// "[email protected]".to_string(),
/// )
/// .await
/// .unwrap();
/// ```
pub async fn create_register_transaction(
&self,
external_id: String,
passkey_display_name: String,
) -> Result<String, Error> {
transactions_api::create_register_transaction(
&self.configuration,
crate::openapi::models::CreateTransactionRegisterRequest {
external_id,
passkey_display_name,
},
)
.await
.map(|response| response.transaction_id)
.map_err(Into::into)
}
/// Creates a transaction to start a user's authentication process.
///
/// # Arguments
///
/// * `external_id` - A unique, immutable string that represents the user.
///
/// # Returns
///
/// A `Result` containing the transaction ID as a string or an `Error`.
///
/// # Examples
///
/// ```ignore
/// use passage_flex::PassageFlex;
///
/// let passage_flex = PassageFlex::new(
/// std::env::var("PASSAGE_APP_ID").unwrap(),
/// std::env::var("PASSAGE_API_KEY").unwrap(),
/// );
///
/// let transaction = passage_flex
/// .create_authenticate_transaction(
/// "00000000-0000-0000-0000-000000000001".to_string(),
/// )
/// .await
/// .unwrap();
/// ```
pub async fn create_authenticate_transaction(
&self,
external_id: String,
) -> Result<String, Error> {
transactions_api::create_authenticate_transaction(
&self.configuration,
crate::openapi::models::CreateTransactionAuthenticateRequest { external_id },
)
.await
.map(|response| response.transaction_id)
.map_err(Into::into)
}
/// Verifies the nonce received from a WebAuthn registration or authentication ceremony.
///
/// # Arguments
///
/// * `nonce` - The nonce string to be verified.
///
/// # Returns
///
/// A `Result` containing the external ID as a string or an `Error`.
///
/// # Examples
///
/// ```ignore
/// use passage_flex::PassageFlex;
///
/// let passage_flex = PassageFlex::new(
/// std::env::var("PASSAGE_APP_ID").unwrap(),
/// std::env::var("PASSAGE_API_KEY").unwrap(),
/// );
///
/// match passage_flex.verify_nonce("01234567890123456789".to_string()).await {
/// Ok(external_id) => {
/// // use external_id to do things like generate and send your own auth token
/// }
/// Err(err) => {
/// // nonce was invalid or unable to be verified
/// }
/// }
/// ```
pub async fn verify_nonce(&self, nonce: String) -> Result<String, Error> {
authenticate_api::authenticate_verify_nonce(
&self.configuration,
crate::openapi::models::Nonce { nonce },
)
.await
.map(|response| response.external_id)
.map_err(Into::into)
}
}