8000 refactor: prepare to parse ADC json from string by dbolduc · Pull Request #14810 · googleapis/google-cloud-cpp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: prepare to parse ADC json from string #14810

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

Merged
merged 2 commits into from
Oct 31, 2024
Merged
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
70 changes: 39 additions & 31 deletions google/cloud/internal/oauth2_google_credentials.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ namespace oauth2_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

StatusOr<std::unique_ptr<Credentials>> LoadCredsFromString(
std::string const& contents, std::string const& path,
Options const& options, HttpClientFactory client_factory) {
auto const cred_json = nlohmann::json::parse(contents, nullptr, false);
auto const cred_type = cred_json.value("type", "no type given");
// If non_service_account_ok==false and the cred_type is authorized_user,
// we'll return "Unsupported credential type (authorized_user)".
if (cred_type == "authorized_user") {
auto info = ParseAuthorizedUserCredentials(contents, path);
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<AuthorizedUserCredentials>(*info, options,
std::move(client_factory)));
}
if (cred_type == "external_account") {
auto info =
ParseExternalAccountConfiguration(contents, internal::ErrorContext{});
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<ExternalAccountCredentials>(
*std::move(info), std::move(client_factory), options));
}
if (cred_type == "service_account") {
auto info = ParseServiceAccountCredentials(contents, path);
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<ServiceAccountCredentials>(*info, options,
std::move(client_factory)));
}
return internal::InvalidArgumentError(
"Unsupported credential type (" + cred_type +
") when reading Application Default Credentials file "
"from " +
path + ".",
GCP_ERROR_INFO());
}

// Parses the JSON at `path` and creates the appropriate
// Credentials type.
//
Expand Down Expand Up @@ -72,37 +109,8 @@ StatusOr<std::unique_ptr<Credentials>> LoadCredsFromPath(
std::make_unique<ServiceAccountCredentials>(*info, options,
std::move(client_factory)));
}
auto const cred_type = cred_json.value("type", "no type given");
// If non_service_account_ok==false and the cred_type is authorized_user,
// we'll return "Unsupported credential type (authorized_user)".
if (cred_type == "authorized_user") {
auto info = ParseAuthorizedUserCredentials(contents, path);
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<AuthorizedUserCredentials>(*info, options,
std::move(client_factory)));
}
if (cred_type == "external_account") {
auto info =
ParseExternalAccountConfiguration(contents, internal::ErrorContext{});
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<ExternalAccountCredentials>(
*std::move(info), std::move(client_factory), options));
}
if (cred_type == "service_account") {
auto info = ParseServiceAccountCredentials(contents, path);
if (!info) return std::move(info).status();
return std::unique_ptr<Credentials>(
std::make_unique<ServiceAccountCredentials>(*info, options,
std::move(client_factory)));
}
return internal::InvalidArgumentError(
"Unsupported credential type (" + cred_type +
") when reading Application Default Credentials file "
"from " +
path + ".",
GCP_ERROR_INFO());
return LoadCredsFromString(contents, path, options,
std::move(client_factory));
}

// Tries to load the file at the path specified by the value of the Application
Expand Down
Loading
0