1
- use crate :: models:: { AppInfo , UserInfo } ;
1
+ use crate :: models:: AppInfo ;
2
2
use crate :: openapi:: apis:: configuration:: Configuration ;
3
- use crate :: openapi:: apis:: {
4
- apps_api, authenticate_api, transactions_api, user_devices_api, users_api,
5
- } ;
3
+ use crate :: openapi:: apis:: { apps_api, authenticate_api, transactions_api} ;
4
+ use crate :: user:: User ;
6
5
use crate :: Error ;
7
6
8
7
pub struct PassageFlex {
9
8
app_id : String ,
10
9
configuration : Configuration ,
10
+ pub user : User ,
11
11
}
12
12
13
13
const SERVER_URL : & str = "https://api.passage.id" ;
@@ -31,10 +31,6 @@ impl PassageFlex {
31
31
/// );
32
32
/// ```
33
33
pub fn new ( app_id : String , api_key : String ) -> Self {
34
- let mut configuration = Configuration :: new ( ) ;
35
- // Use the api_key as the bearer access token
36
- configuration. bearer_access_token = Some ( api_key) ;
37
- // Set the Passage-Version header to the version of the crate
38
34
let mut headers = reqwest:: header:: HeaderMap :: with_capacity ( 1 ) ;
39
35
headers. insert (
40
36
"Passage-Version" ,
@@ -43,14 +39,20 @@ impl PassageFlex {
43
39
env!( "CARGO_PKG_VERSION" )
44
40
) ) ,
45
41
) ;
42
+
43
+ let mut configuration = Configuration :: new ( ) ;
44
+ configuration. bearer_access_token = Some ( api_key) ;
46
45
configuration. client = reqwest:: Client :: builder ( )
47
46
. default_headers ( headers)
48
47
. build ( )
49
48
. expect ( "Failed to create reqwest client for Passage" ) ;
50
49
50
+ let user = User :: new ( configuration. clone ( ) ) ;
51
+
51
52
let mut client = Self {
52
53
app_id,
53
54
configuration,
55
+ user,
54
56
} ;
55
57
// Set the default server URL
56
58
client. set_server_url ( SERVER_URL . to_string ( ) ) ;
@@ -59,7 +61,7 @@ impl PassageFlex {
59
61
}
60
62
61
63
fn set_server_url ( & mut self , server_url : String ) {
62
- // Use the app_id and server_url to set the base_path
64
+ self . user . configuration . base_path = format ! ( "{}/v1/apps/{}" , server_url, self . app_id ) ;
63
65
self . configuration . base_path = format ! ( "{}/v1/apps/{}" , server_url, self . app_id) ;
64
66
}
65
67
@@ -219,198 +221,6 @@ impl PassageFlex {
219
221
. map ( |response| response. external_id )
220
222
. map_err ( Into :: into)
221
223
}
222
-
223
- /// Get a user's ID in Passage by their external ID
224
- async fn get_id ( & self , external_id : String ) -> Result < String , Error > {
225
- let users = users_api:: list_paginated_users (
226
- & self . configuration ,
227
- Some ( 1 ) ,
228
- Some ( 1 ) ,
229
- None ,
230
- None ,
231
- Some ( & external_id) ,
232
- None ,
233
- None ,
234
- None ,
235
- None ,
236
- None ,
237
- None ,
238
- )
239
- . await
240
- . map ( |response| response. users )
241
- . map_err ( Into :: into) ;
242
-
243
- match users {
244
- Ok ( mut users) => match users. len ( ) {
245
- 0 => Err ( Error :: UserNotFound ) ,
246
- 1 => {
247
- let user = users. remove ( 0 ) ;
248
- Ok ( user. id )
249
- }
250
- _ => Err ( Error :: Other ( "Multiple users found" . to_string ( ) ) ) ,
251
- } ,
252
- Err ( e) => Err ( e) ,
253
- }
254
- }
255
-
256
- /// Retrieves information about a user by their external ID.
257
- ///
258
- /// # Arguments
259
- ///
260
- /// * `external_id` - The unique, immutable ID that represents the user.
261
- ///
262
- /// # Returns
263
- ///
264
- /// A `Result` containing the `UserInfo` struct or an `Error`.
265
- ///
266
- /// # Examples
267
- ///
268
- /// ```ignore
269
- /// use passage_flex::PassageFlex;
270
- ///
271
- /// let passage_flex = PassageFlex::new(
272
- /// std::env::var("PASSAGE_APP_ID").unwrap(),
273
- /// std::env::var("PASSAGE_API_KEY").unwrap(),
274
- /// );
275
- ///
276
- /// let external_id = "00000000-0000-0000-0000-000000000001";
277
- /// let user_info = passage_flex.get_user(external_id.to_string()).await.unwrap();
278
- /// println!("{:?}", user_info.id);
279
- /// ```
280
- pub async fn get_user ( & self , external_id : String ) -> Result < Box < UserInfo > , Error > {
281
- let user_id = self . get_id ( external_id) . await ?;
282
- self . get_user_by_id ( user_id) . await
283
- }
284
-
285
- /// Retrieves information about a user's passkey devices.
286
- ///
287
- /// # Arguments
288
- ///
289
- /// * `external_id` - The unique, immutable ID that represents the user.
290
- ///
291
- /// # Returns
292
- ///
293
- /// A `Result` containing a vector of `WebAuthnDevices` or an `Error`.
294
- ///
295
- /// # Examples
296
- ///
297
- /// ```ignore
298
- /// use passage_flex::PassageFlex;
299
- ///
300
- /// let passage_flex = PassageFlex::new(
301
- /// std::env::var("PASSAGE_APP_ID").unwrap(),
302
- /// std::env::var("PASSAGE_API_KEY").unwrap(),
303
- /// );
304
- ///
305
- /// let external_id = "00000000-0000-0000-0000-000000000001";
306
- /// let passkey_devices = passage_flex.get_devices(external_id.to_string()).await.unwrap();
307
- /// for device in passkey_devices {
308
- /// println!("{}", device.usage_count);
309
- /// }
310
- /// ```
311
- pub async fn get_devices (
312
- & self ,
313
- external_id : String ,
314
- ) -> Result < Vec < crate :: openapi:: models:: WebAuthnDevices > , Error > {
315
- let user_id = self . get_id ( external_id) . await ?;
316
- user_devices_api:: list_user_devices ( & self . configuration , & user_id)
317
- . await
318
- . map ( |response| response. devices )
319
- . map_err ( Into :: into)
320
- }
321
-
322
- /// Revokes a user's passkey device.
323
- ///
324
- /// # Arguments
325
- ///
326
- /// * `external_id` - The unique, immutable ID that represents the user.
327
- /// * `device_id` - The ID of the device to be revoked.
328
- ///
329
- /// # Returns
330
- ///
331
- /// A `Result` containing `()` or an `Error`.
332
- ///
333
- /// # Examples
334
- ///
335
- /// ```ignore
336
- /// use passage_flex::PassageFlex;
337
- /// use chrono::{Duration, NaiveDate, Utc};
338
- ///
339
- /// let passage_flex = PassageFlex::new(
340
- /// std::env::var("PASSAGE_APP_ID").unwrap(),
341
- /// std::env::var("PASSAGE_API_KEY").unwrap(),
342
- /// );
343
- ///
344
- /// let external_id = "00000000-0000-0000-0000-000000000001";
345
- /// let last_year = Utc::now().naive_utc().date() - Duration::days(365);
346
- ///
347
- /// let passkey_devices = passage_flex.get_devices(external_id.to_string()).await.unwrap();
348
- ///
349
- /// for device in passkey_devices {
350
- /// let last_login_at_parsed =
351
- /// NaiveDate::parse_from_str(&device.last_login_at, "%Y-%m-%dT%H:%M:%S%z").unwrap();
352
- ///
353
- /// if last_login_at_parsed < last_year {
354
- /// if let Err(err) = passage_flex
355
- /// .revoke_device(external_id.clone(), device.id)
356
- /// .await
357
- /// {
358
- /// // device couldn't be revoked
359
- /// }
360
- /// }
361
- /// }
362
- /// ```
363
- pub async fn revoke_device ( & self , external_id : String , device_id : String ) -> Result < ( ) , Error > {
364
- let user_id = self . get_id ( external_id) . await ?;
365
- user_devices_api:: delete_user_devices ( & self . configuration , & user_id, & device_id)
366
- . await
367
- . map_err ( Into :: into)
368
- }
369
-
370
- /// Retrieves information about a user by their user ID in Passage.
371
- ///
372
- /// # Arguments
373
- ///
374
- /// * `user_id` - The ID of the user in Passage.
375
- ///
376
- /// # Returns
377
- ///
378
- /// A `Result` containing the `UserInfo` struct or an `Error`.
379
- ///
380
- /// # Examples
381
- ///
382
- /// ```ignore
383
- /// use passage_flex::PassageFlex;
384
- ///
385
- /// let passage_flex = PassageFlex::new(
386
- /// std::env::var("PASSAGE_APP_ID").unwrap(),
387
- /// std::env::var("PASSAGE_API_KEY").unwrap(),
388
- /// );
389
- ///
390
- /// let user_id = "user_id_string";
391
- /// let user_info = passage_flex.get_user_by_id(user_id.to_string()).await.unwrap();
392
- /// println!("{:?}", user_info.external_id);
393
- /// ```
394
- pub async fn get_user_by_id ( & self , user_id : String ) -> Result < Box < UserInfo > , Error > {
395
- users_api:: get_user ( & self . configuration , & user_id)
396
- . await
397
- . map ( |response| {
398
- Box :: new ( UserInfo {
399
- created_at : response. user . created_at ,
400
- external_id : response. user . external_id ,
401
- id : response. user . id ,
402
- last_login_at : response. user . last_login_at ,
403
- login_count : response. user . login_count ,
404
- status : response. user . status ,
405
- updated_at : response. user . updated_at ,
406
- user_metadata : response. user . user_metadata ,
407
- webauthn : response. user . webauthn ,
408
- webauthn_devices : response. user . webauthn_devices ,
409
- webauthn_types : response. user . webauthn_types ,
410
- } )
411
- } )
412
- . map_err ( Into :: into)
413
- }
414
224
}
415
225
416
226
#[ cfg( test) ]
0 commit comments