Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-2076] Properly handle error responses in send_identity_connect_request #109

Merged
merged 4 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/bitwarden/src/auth/api/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ async fn send_identity_connect_request(
request = request.header("Auth-Email", BASE64_ENGINE.encode(email.as_bytes()));
}

let raw_response = request
let response = request
.body(serde_qs::to_string(&body).unwrap())
.send()
.await?
.text()
.await?;

parse_identity_response(&raw_response)
let status = response.status();
let text = response.text().await?;

parse_identity_response(status, &text)
}
17 changes: 12 additions & 5 deletions crates/bitwarden/src/auth/api/response/identity_token_response.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -17,7 +18,10 @@ pub enum IdentityTokenResponse {
CaptchaRequired(IdentityCaptchaResponse),
}

pub fn parse_identity_response(response: &str) -> Result<IdentityTokenResponse> {
pub fn parse_identity_response(
status: StatusCode,
response: &str,
) -> Result<IdentityTokenResponse> {
if let Ok(r) = serde_json::from_str::<IdentityTokenSuccessResponse>(response) {
Ok(IdentityTokenResponse::Authenticated(r))
} else if let Ok(r) = serde_json::from_str::<IdentityTokenPayloadResponse>(response) {
Expand All @@ -31,7 +35,10 @@ pub fn parse_identity_response(response: &str) -> Result<IdentityTokenResponse>
} else if let Ok(r) = serde_json::from_str::<IdentityTokenFailResponse>(response) {
Err(Error::IdentityFail(r))
} else {
Err(Error::Internal("Failed to parse IdentityTokenResponse"))
Err(Error::ResponseContent {
status: status,
message: response.to_owned(),
})
}
}

Expand All @@ -44,7 +51,7 @@ mod test {
let expected = IdentityTokenSuccessResponse::default();
let success = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::Authenticated(expected);
let actual = parse_identity_response(&success).unwrap();
let actual = parse_identity_response(StatusCode::OK, &success).unwrap();
assert_eq!(expected, actual);
}

Expand All @@ -53,7 +60,7 @@ mod test {
let expected = IdentityTwoFactorResponse::default();
let two_factor = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::TwoFactorRequired(expected);
let actual = parse_identity_response(&two_factor).unwrap();
let actual = parse_identity_response(StatusCode::BAD_REQUEST, &two_factor).unwrap();
assert_eq!(expected, actual);
}

Expand All @@ -62,7 +69,7 @@ mod test {
let expected = IdentityCaptchaResponse::default();
let captcha = serde_json::to_string(&expected).unwrap();
let expected = IdentityTokenResponse::CaptchaRequired(expected);
let actual = parse_identity_response(&captcha).unwrap();
let actual = parse_identity_response(StatusCode::BAD_REQUEST, &captcha).unwrap();
assert_eq!(expected, actual);
}
}