Skip to content

Commit

Permalink
Merge pull request #825 from dcSpark/fix/oauth-state
Browse files Browse the repository at this point in the history
added client secret & capture errors
  • Loading branch information
acedward authored Jan 30, 2025
2 parents b44e39d + da40eb7 commit 72d7fb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Node {
let client = Client::new();
let mut request_body = serde_json::json!({
"client_id": oauth_data.client_id.as_deref().unwrap_or_default(),
// "client_secret": oauth_data.client_secret.as_deref().unwrap_or_default(),
"client_secret": oauth_data.client_secret.as_deref().unwrap_or_default(),
"code": code,
"redirect_uri": oauth_data.redirect_url.as_deref().unwrap_or_default(),
"grant_type": "authorization_code"
Expand Down Expand Up @@ -136,6 +136,14 @@ impl Node {
});
}
let response = response.unwrap();
println!("[OAuth] Response status {}", response.status());
if !response.status().is_success() {
return Err(APIError {
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
error: "Internal Server Error".to_string(),
message: format!("Failed to get OAuth token: {}", response.status()),
});
}
let response = response.json::<serde_json::Value>().await;
if response.is_err() {
return Err(APIError {
Expand All @@ -156,6 +164,15 @@ impl Node {
// "token_type":"bearer"
// }
// Update the token with the new code and OAuth response data
if let Some(error) = response["error"].as_str() {
if !error.is_empty() {
return Err(APIError {
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
error: "Internal Server Error".to_string(),
message: format!("Failed to get OAuth token: {:?}", response),
});
}
}
oauth_data.code = Some(code);
if let Some(access_token) = response["access_token"].as_str() {
oauth_data.access_token = Some(access_token.to_string());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,23 @@ pub async fn handle_oauth(

match response {
Ok(response) => {
if !response.status().is_success() {
return Err(ToolError::ExecutionError(format!(
"Failed to refresh OAuth token: {}",
response.status()
)));
}

if let Ok(response_json) = response.json::<serde_json::Value>().await {
println!("[OAuth] Response {}", response_json.to_string());
if let Some(error) = response_json["error"].as_str() {
if !error.is_empty() {
return Err(ToolError::ExecutionError(format!(
"Failed to refresh OAuth token: {}",
response_json
)));
}
}
// Update token with new values
let mut updated_token = token.clone();
if let Some(access_token) = response_json["access_token"].as_str() {
Expand Down

0 comments on commit 72d7fb7

Please sign in to comment.