@@ -36,6 +36,43 @@ namespace oauth2_internal {
3636GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3737namespace {
3838
39+ StatusOr<std::unique_ptr<Credentials>> LoadCredsFromString (
40+ std::string const & contents, std::string const & path,
41+ Options const & options, HttpClientFactory client_factory) {
42+ auto const cred_json = nlohmann::json::parse (contents, nullptr , false );
43+ auto const cred_type = cred_json.value (" type" , " no type given" );
44+ // If non_service_account_ok==false and the cred_type is authorized_user,
45+ // we'll return "Unsupported credential type (authorized_user)".
46+ if (cred_type == " authorized_user" ) {
47+ auto info = ParseAuthorizedUserCredentials (contents, path);
48+ if (!info) return std::move (info).status ();
49+ return std::unique_ptr<Credentials>(
50+ std::make_unique<AuthorizedUserCredentials>(*info, options,
51+ std::move (client_factory)));
52+ }
53+ if (cred_type == " external_account" ) {
54+ auto info =
55+ ParseExternalAccountConfiguration (contents, internal::ErrorContext{});
56+ if (!info) return std::move (info).status ();
57+ return std::unique_ptr<Credentials>(
58+ std::make_unique<ExternalAccountCredentials>(
59+ *std::move (info), std::move (client_factory), options));
60+ }
61+ if (cred_type == " service_account" ) {
62+ auto info = ParseServiceAccountCredentials (contents, path);
63+ if (!info) return std::move (info).status ();
64+ return std::unique_ptr<Credentials>(
65+ std::make_unique<ServiceAccountCredentials>(*info, options,
66+ std::move (client_factory)));
67+ }
68+ return internal::InvalidArgumentError (
69+ " Unsupported credential type (" + cred_type +
70+ " ) when reading Application Default Credentials file "
71+ " from " +
72+ path + " ." ,
73+ GCP_ERROR_INFO ());
74+ }
75+
3976// Parses the JSON at `path` and creates the appropriate
4077// Credentials type.
4178//
@@ -72,37 +109,8 @@ StatusOr<std::unique_ptr<Credentials>> LoadCredsFromPath(
72109 std::make_unique<ServiceAccountCredentials>(*info, options,
73110 std::move (client_factory)));
74111 }
75- auto const cred_type = cred_json.value (" type" , " no type given" );
76- // If non_service_account_ok==false and the cred_type is authorized_user,
77- // we'll return "Unsupported credential type (authorized_user)".
78- if (cred_type == " authorized_user" ) {
79- auto info = ParseAuthorizedUserCredentials (contents, path);
80- if (!info) return std::move (info).status ();
81- return std::unique_ptr<Credentials>(
82- std::make_unique<AuthorizedUserCredentials>(*info, options,
83- std::move (client_factory)));
84- }
85- if (cred_type == " external_account" ) {
86- auto info =
87- ParseExternalAccountConfiguration (contents, internal::ErrorContext{});
88- if (!info) return std::move (info).status ();
89- return std::unique_ptr<Credentials>(
90- std::make_unique<ExternalAccountCredentials>(
91- *std::move (info), std::move (client_factory), options));
92- }
93- if (cred_type == " service_account" ) {
94- auto info = ParseServiceAccountCredentials (contents, path);
95- if (!info) return std::move (info).status ();
96- return std::unique_ptr<Credentials>(
97- std::make_unique<ServiceAccountCredentials>(*info, options,
98- std::move (client_factory)));
99- }
100- return internal::InvalidArgumentError (
101- " Unsupported credential type (" + cred_type +
102- " ) when reading Application Default Credentials file "
103- " from " +
104- path + " ." ,
105- GCP_ERROR_INFO ());
112+ return LoadCredsFromString (contents, path, options,
113+ std::move (client_factory));
106114}
107115
108116// Tries to load the file at the path specified by the value of the Application
0 commit comments