Skip to content

Commit

Permalink
tidy: remove duplicated code (#119)
Browse files Browse the repository at this point in the history
* tidy: remove duplicated code

follow up #118

* chore: fix typo
  • Loading branch information
i10416 authored Jan 8, 2024
1 parent bb6ef21 commit d0320ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
2 changes: 1 addition & 1 deletion gcloud-sdk/src/token_source/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ mod oauth2 {
client_id: &user.client_id,
client_secret: user.client_secret.as_sensitive_str(),
grant_type: GRANT_TYPE,
// The reflesh token is not included in the response from google's server,
// The refresh token is not included in the response from google's server,
// so it always uses the specified refresh token from the file.
refresh_token: user.refresh_token.as_sensitive_str(),
});
Expand Down
70 changes: 27 additions & 43 deletions gcloud-sdk/src/token_source/ext_creds_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,7 @@ pub async fn subject_token_url(
None | Some(ExternalCredentialUrlFormat::Text) => Ok(response.text().await?.into()),
Some(ExternalCredentialUrlFormat::Json(json_settings)) => {
let json: serde_json::Value = response.json().await?;
let json_object = json.as_object().ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON format is not object: {}",
json
))
})?;

let subject_json_value = json_object
.get(&json_settings.subject_token_field_name)
.ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON field must have string type: {}",
&json_settings.subject_token_field_name
))
})?;
subject_json_value.as_str().map(Into::into).ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON field must have string type: {}",
&json_settings.subject_token_field_name
))
.into()
})
subject_token_from_json(&json, &json_settings.subject_token_field_name)
}
}
} else {
Expand Down Expand Up @@ -131,27 +110,32 @@ pub async fn subject_token_file(
e
))
})?;
let json_object = json.as_object().ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON format is not object: {}",
json
))
})?;
let subject_json_value = json_object
.get(&json_settings.subject_token_field_name)
.ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON format doesn't contain required field: {}",
&json_settings.subject_token_field_name
))
})?;
subject_json_value.as_str().map(Into::into).ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON field must have string type: {}",
&json_settings.subject_token_field_name
))
.into()
})
subject_token_from_json(&json, &json_settings.subject_token_field_name)
}
}
}

fn subject_token_from_json(
json: &serde_json::Value,
subject_token_field_name: &str,
) -> crate::error::Result<SecretValue> {
let json_object = json.as_object().ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON format is not object: {}",
json
))
})?;
let subject_json_value = json_object.get(subject_token_field_name).ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON format doesn't contain required field: {}",
subject_token_field_name
))
})?;
subject_json_value.as_str().map(Into::into).ok_or_else(|| {
crate::error::ErrorKind::ExternalCredsSourceError(format!(
"External subject JSON field must have string type: {}",
subject_token_field_name
))
.into()
})
}

0 comments on commit d0320ae

Please sign in to comment.