Skip to content

Commit 9b7ea60

Browse files
authored
feat: add parameter guards for all methods (#34)
* feat: add new error type for invalid method arguments * feat: add parameter guards for the constructor and all methods * fix: use the correct error type for the auth methods
1 parent 68f69a6 commit 9b7ea60

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/auth.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ impl Auth {
4747
external_id: String,
4848
passkey_display_name: String,
4949
) -> Result<String, Error> {
50+
if external_id.is_empty() {
51+
return Err(Error::InvalidArgument(
52+
"external_id is required".to_string(),
53+
));
54+
}
55+
56+
if passkey_display_name.is_empty() {
57+
return Err(Error::InvalidArgument(
58+
"passkey_display_name is required".to_string(),
59+
));
60+
}
61+
5062
transactions_api::create_register_transaction(
5163
&self.configuration,
5264
crate::openapi::models::CreateTransactionRegisterRequest {
@@ -91,6 +103,12 @@ impl Auth {
91103
&self,
92104
external_id: String,
93105
) -> Result<String, Error> {
106+
if external_id.is_empty() {
107+
return Err(Error::InvalidArgument(
108+
"external_id is required".to_string(),
109+
));
110+
}
111+
94112
transactions_api::create_authenticate_transaction(
95113
&self.configuration,
96114
crate::openapi::models::CreateTransactionAuthenticateRequest { external_id },
@@ -130,6 +148,10 @@ impl Auth {
130148
/// }
131149
/// ```
132150
pub async fn verify_nonce(&self, nonce: String) -> Result<String, Error> {
151+
if nonce.is_empty() {
152+
return Err(Error::InvalidArgument("nonce is required".to_string()));
153+
}
154+
133155
authenticate_api::authenticate_verify_nonce(
134156
&self.configuration,
135157
crate::openapi::models::Nonce { nonce },

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub enum Error {
3131
UserHasNoPasskeys,
3232
InternalServerError,
3333
Other(String),
34+
InvalidArgument(String),
3435
}
3536

3637
impl fmt::Display for Error {
@@ -48,6 +49,7 @@ impl fmt::Display for Error {
4849
Error::UserHasNoPasskeys => ("response", "user has no passkeys".to_string()),
4950
Error::InternalServerError => ("response", "internal server error".to_string()),
5051
Error::Other(e) => ("response", e.to_string()),
52+
Error::InvalidArgument(e) => ("argument", e.to_string()),
5153
};
5254
write!(f, "error in {}: {}", module, e)
5355
}

src/passage_flex.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ impl PassageFlex {
2929
/// );
3030
/// ```
3131
pub fn new(app_id: String, api_key: String) -> Self {
32+
if app_id.is_empty() {
33+
panic!("A Passage App ID is required. Please include (app_id: YOUR_APP_ID, api_key: YOUR_APP_ID).");
34+
}
35+
36+
if api_key.is_empty() {
37+
panic!("A Passage API key is required. Please include (app_id: YOUR_APP_ID, api_key: YOUR_APP_ID).");
38+
}
39+
3240
let mut headers = reqwest::header::HeaderMap::with_capacity(1);
3341
headers.insert(
3442
"Passage-Version",

src/user.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ impl User {
1515

1616
/// Get a user's ID in Passage by their external ID
1717
async fn get_id(&self, external_id: String) -> Result<String, Error> {
18+
if external_id.is_empty() {
19+
return Err(Error::InvalidArgument(
20+
"external_id is required".to_string(),
21+
));
22+
}
23+
1824
let users = users_api::list_paginated_users(
1925
&self.configuration,
2026
Some(1),
@@ -172,6 +178,10 @@ impl User {
172178
/// }
173179
/// ```
174180
pub async fn revoke_device(&self, external_id: String, device_id: String) -> Result<(), Error> {
181+
if device_id.is_empty() {
182+
return Err(Error::InvalidArgument("device_id is required".to_string()));
183+
}
184+
175185
let user_id = self.get_id(external_id).await?;
176186
user_devices_api::delete_user_devices(&self.configuration, &user_id, &device_id)
177187
.await

0 commit comments

Comments
 (0)