Skip to content

Commit 7661fde

Browse files
committed
feat: moves auth operations to a nested Auth struct
1 parent 085762c commit 7661fde

File tree

4 files changed

+151
-265
lines changed

4 files changed

+151
-265
lines changed

src/auth.rs

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
use crate::openapi::apis::configuration::Configuration;
2+
use crate::openapi::apis::{authenticate_api, transactions_api};
3+
use crate::Error;
4+
5+
pub struct Auth {
6+
pub(crate) configuration: Configuration,
7+
}
8+
9+
impl Auth {
10+
/// Creates a new instance of the `Auth` struct.
11+
pub fn new(configuration: Configuration) -> Self {
12+
Self { configuration }
13+
}
14+
15+
/// Creates a transaction to start a user's registration process.
16+
///
17+
/// # Arguments
18+
///
19+
/// * `external_id` - A unique, immutable string that represents the user.
20+
/// * `passkey_display_name` - The label for the user's passkey that they will see when logging in.
21+
///
22+
/// # Returns
23+
///
24+
/// A `Result` containing the transaction ID as a string or an `Error`.
25+
///
26+
/// # Examples
27+
///
28+
/// ```ignore
29+
/// use passage_flex::PassageFlex;
30+
///
31+
/// let passage_flex = PassageFlex::new(
32+
/// std::env::var("PASSAGE_APP_ID").unwrap(),
33+
/// std::env::var("PASSAGE_API_KEY").unwrap(),
34+
/// );
35+
///
36+
/// let transaction = passage_flex
37+
/// .create_register_transaction(
38+
/// "00000000-0000-0000-0000-000000000001".to_string(),
39+
/// "[email protected]".to_string(),
40+
/// )
41+
/// .await
42+
/// .unwrap();
43+
/// ```
44+
pub async fn create_register_transaction(
45+
&self,
46+
external_id: String,
47+
passkey_display_name: String,
48+
) -> Result<String, Error> {
49+
transactions_api::create_register_transaction(
50+
&self.configuration,
51+
crate::openapi::models::CreateTransactionRegisterRequest {
52+
external_id,
53+
passkey_display_name,
54+
},
55+
)
56+
.await
57+
.map(|response| response.transaction_id)
58+
.map_err(Into::into)
59+
}
60+
61+
/// Creates a transaction to start a user's authentication process.
62+
///
63+
/// # Arguments
64+
///
65+
/// * `external_id` - A unique, immutable string that represents the user.
66+
///
67+
/// # Returns
68+
///
69+
/// A `Result` containing the transaction ID as a string or an `Error`.
70+
///
71+
/// # Examples
72+
///
73+
/// ```ignore
74+
/// use passage_flex::PassageFlex;
75+
///
76+
/// let passage_flex = PassageFlex::new(
77+
/// std::env::var("PASSAGE_APP_ID").unwrap(),
78+
/// std::env::var("PASSAGE_API_KEY").unwrap(),
79+
/// );
80+
///
81+
/// let transaction = passage_flex
82+
/// .create_authenticate_transaction(
83+
/// "00000000-0000-0000-0000-000000000001".to_string(),
84+
/// )
85+
/// .await
86+
/// .unwrap();
87+
/// ```
88+
pub async fn create_authenticate_transaction(
89+
&self,
90+
external_id: String,
91+
) -> Result<String, Error> {
92+
transactions_api::create_authenticate_transaction(
93+
&self.configuration,
94+
crate::openapi::models::CreateTransactionAuthenticateRequest { external_id },
95+
)
96+
.await
97+
.map(|response| response.transaction_id)
98+
.map_err(Into::into)
99+
}
100+
101+
/// Verifies the nonce received from a WebAuthn registration or authentication ceremony.
102+
///
103+
/// # Arguments
104+
///
105+
/// * `nonce` - The nonce string to be verified.
106+
///
107+
/// # Returns
108+
///
109+
/// A `Result` containing the external ID as a string or an `Error`.
110+
///
111+
/// # Examples
112+
///
113+
/// ```ignore
114+
/// use passage_flex::PassageFlex;
115+
///
116+
/// let passage_flex = PassageFlex::new(
117+
/// std::env::var("PASSAGE_APP_ID").unwrap(),
118+
/// std::env::var("PASSAGE_API_KEY").unwrap(),
119+
/// );
120+
///
121+
/// match passage_flex.verify_nonce("01234567890123456789".to_string()).await {
122+
/// Ok(external_id) => {
123+
/// // use external_id to do things like generate and send your own auth token
124+
/// }
125+
/// Err(err) => {
126+
/// // nonce was invalid or unable to be verified
127+
/// }
128+
/// }
129+
/// ```
130+
pub async fn verify_nonce(&self, nonce: String) -> Result<String, Error> {
131+
authenticate_api::authenticate_verify_nonce(
132+
&self.configuration,
133+
crate::openapi::models::Nonce { nonce },
134+
)
135+
.await
136+
.map(|response| response.external_id)
137+
.map_err(Into::into)
138+
}
139+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub mod models;
6262
#[rustfmt::skip]
6363
pub mod openapi;
6464

65+
pub mod auth;
6566
pub mod passage_flex;
6667
pub mod user;
6768
pub use passage_flex::PassageFlex;

src/passage_flex.rs

+5-167
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use crate::models::AppInfo;
1+
use crate::auth::Auth;
22
use crate::openapi::apis::configuration::Configuration;
3-
use crate::openapi::apis::{apps_api, authenticate_api, transactions_api};
43
use crate::user::User;
5-
use crate::Error;
64

75
pub struct PassageFlex {
86
app_id: String,
9-
configuration: Configuration,
7+
pub auth: Auth,
108
pub user: User,
119
}
1210

@@ -47,13 +45,10 @@ impl PassageFlex {
4745
.build()
4846
.expect("Failed to create reqwest client for Passage");
4947

48+
let auth = Auth::new(configuration.clone());
5049
let user = User::new(configuration.clone());
5150

52-
let mut client = Self {
53-
app_id,
54-
configuration,
55-
user,
56-
};
51+
let mut client = Self { app_id, auth, user };
5752
// Set the default server URL
5853
client.set_server_url(SERVER_URL.to_string());
5954

@@ -62,164 +57,7 @@ impl PassageFlex {
6257

6358
fn set_server_url(&mut self, server_url: String) {
6459
self.user.configuration.base_path = format!("{}/v1/apps/{}", server_url, self.app_id);
65-
self.configuration.base_path = format!("{}/v1/apps/{}", server_url, self.app_id);
66-
}
67-
68-
/// Retrieves information about the application.
69-
///
70-
/// # Returns
71-
///
72-
/// A `Result` containing the `AppInfo` struct or an `Error`.
73-
///
74-
/// # Examples
75-
///
76-
/// ```ignore
77-
/// use passage_flex::PassageFlex;
78-
///
79-
/// let passage_flex = PassageFlex::new(
80-
/// std::env::var("PASSAGE_APP_ID").unwrap(),
81-
/// std::env::var("PASSAGE_API_KEY").unwrap(),
82-
/// );
83-
///
84-
/// let app_info = passage_flex.get_app().await.unwrap();
85-
/// println!("{}", app_info.auth_origin);
86-
/// ```
87-
pub async fn get_app(&self) -> Result<Box<AppInfo>, Error> {
88-
apps_api::get_app(&self.configuration)
89-
.await
90-
.map(|response| {
91-
Box::new(AppInfo {
92-
auth_origin: response.app.auth_origin,
93-
id: response.app.id,
94-
name: response.app.name,
95-
})
96-
})
97-
.map_err(Into::into)
98-
}
99-
100-
/// Creates a transaction to start a user's registration process.
101-
///
102-
/// # Arguments
103-
///
104-
/// * `external_id` - A unique, immutable string that represents the user.
105-
/// * `passkey_display_name` - The label for the user's passkey that they will see when logging in.
106-
///
107-
/// # Returns
108-
///
109-
/// A `Result` containing the transaction ID as a string or an `Error`.
110-
///
111-
/// # Examples
112-
///
113-
/// ```ignore
114-
/// use passage_flex::PassageFlex;
115-
///
116-
/// let passage_flex = PassageFlex::new(
117-
/// std::env::var("PASSAGE_APP_ID").unwrap(),
118-
/// std::env::var("PASSAGE_API_KEY").unwrap(),
119-
/// );
120-
///
121-
/// let transaction = passage_flex
122-
/// .create_register_transaction(
123-
/// "00000000-0000-0000-0000-000000000001".to_string(),
124-
/// "[email protected]".to_string(),
125-
/// )
126-
/// .await
127-
/// .unwrap();
128-
/// ```
129-
pub async fn create_register_transaction(
130-
&self,
131-
external_id: String,
132-
passkey_display_name: String,
133-
) -> Result<String, Error> {
134-
transactions_api::create_register_transaction(
135-
&self.configuration,
136-
crate::openapi::models::CreateTransactionRegisterRequest {
137-
external_id,
138-
passkey_display_name,
139-
},
140-
)
141-
.await
142-
.map(|response| response.transaction_id)
143-
.map_err(Into::into)
144-
}
145-
146-
/// Creates a transaction to start a user's authentication process.
147-
///
148-
/// # Arguments
149-
///
150-
/// * `external_id` - A unique, immutable string that represents the user.
151-
///
152-
/// # Returns
153-
///
154-
/// A `Result` containing the transaction ID as a string or an `Error`.
155-
///
156-
/// # Examples
157-
///
158-
/// ```ignore
159-
/// use passage_flex::PassageFlex;
160-
///
161-
/// let passage_flex = PassageFlex::new(
162-
/// std::env::var("PASSAGE_APP_ID").unwrap(),
163-
/// std::env::var("PASSAGE_API_KEY").unwrap(),
164-
/// );
165-
///
166-
/// let transaction = passage_flex
167-
/// .create_authenticate_transaction(
168-
/// "00000000-0000-0000-0000-000000000001".to_string(),
169-
/// )
170-
/// .await
171-
/// .unwrap();
172-
/// ```
173-
pub async fn create_authenticate_transaction(
174-
&self,
175-
external_id: String,
176-
) -> Result<String, Error> {
177-
transactions_api::create_authenticate_transaction(
178-
&self.configuration,
179-
crate::openapi::models::CreateTransactionAuthenticateRequest { external_id },
180-
)
181-
.await
182-
.map(|response| response.transaction_id)
183-
.map_err(Into::into)
184-
}
185-
186-
/// Verifies the nonce received from a WebAuthn registration or authentication ceremony.
187-
///
188-
/// # Arguments
189-
///
190-
/// * `nonce` - The nonce string to be verified.
191-
///
192-
/// # Returns
193-
///
194-
/// A `Result` containing the external ID as a string or an `Error`.
195-
///
196-
/// # Examples
197-
///
198-
/// ```ignore
199-
/// use passage_flex::PassageFlex;
200-
///
201-
/// let passage_flex = PassageFlex::new(
202-
/// std::env::var("PASSAGE_APP_ID").unwrap(),
203-
/// std::env::var("PASSAGE_API_KEY").unwrap(),
204-
/// );
205-
///
206-
/// match passage_flex.verify_nonce("01234567890123456789".to_string()).await {
207-
/// Ok(external_id) => {
208-
/// // use external_id to do things like generate and send your own auth token
209-
/// }
210-
/// Err(err) => {
211-
/// // nonce was invalid or unable to be verified
212-
/// }
213-
/// }
214-
/// ```
215-
pub async fn verify_nonce(&self, nonce: String) -> Result<String, Error> {
216-
authenticate_api::authenticate_verify_nonce(
217-
&self.configuration,
218-
crate::openapi::models::Nonce { nonce },
219-
)
220-
.await
221-
.map(|response| response.external_id)
222-
.map_err(Into::into)
60+
self.auth.configuration.base_path = format!("{}/v1/apps/{}", server_url, self.app_id);
22361
}
22462
}
22563

0 commit comments

Comments
 (0)