1515#include " google/cloud/internal/unified_rest_credentials.h"
1616#include " google/cloud/common_options.h"
1717#include " google/cloud/internal/make_jwt_assertion.h"
18+ #include " google/cloud/internal/make_status.h"
1819#include " google/cloud/internal/oauth2_access_token_credentials.h"
1920#include " google/cloud/internal/oauth2_anonymous_credentials.h"
2021#include " google/cloud/internal/oauth2_api_key_credentials.h"
2526#include " google/cloud/internal/oauth2_google_credentials.h"
2627#include " google/cloud/internal/oauth2_impersonate_service_account_credentials.h"
2728#include " google/cloud/internal/oauth2_service_account_credentials.h"
28- #include " google/cloud/internal/parse_service_account_p12_file.h"
29- #include < fstream>
3029
3130namespace google {
3231namespace cloud {
@@ -51,67 +50,6 @@ std::shared_ptr<oauth2_internal::Credentials> MakeErrorCredentials(
5150 return std::make_shared<oauth2_internal::ErrorCredentials>(std::move (status));
5251}
5352
54- StatusOr<std::shared_ptr<oauth2_internal::Credentials>>
55- CreateServiceAccountCredentialsFromJsonContents (
56- std::string const & contents, Options const & options,
57- oauth2_internal::HttpClientFactory client_factory) {
58- auto info =
59- oauth2_internal::ParseServiceAccountCredentials (contents, " memory" );
60- if (!info) return info.status ();
61- // Verify this is usable before returning it.
62- auto const tp = std::chrono::system_clock::time_point{};
63- auto const components = AssertionComponentsFromInfo (*info, tp);
64- auto jwt = internal::MakeJWTAssertionNoThrow (
65- components.first , components.second , info->private_key );
66- if (!jwt) return jwt.status ();
67- return StatusOr<std::shared_ptr<oauth2_internal::Credentials>>(
68- std::make_shared<oauth2_internal::ServiceAccountCredentials>(
69- *info, options, std::move (client_factory)));
70- }
71-
72- StatusOr<std::shared_ptr<oauth2_internal::Credentials>>
73- CreateServiceAccountCredentialsFromJsonFilePath (
74- std::string const & path, absl::optional<std::set<std::string>>,
75- absl::optional<std::string>, Options const & options,
76- oauth2_internal::HttpClientFactory client_factory) {
77- std::ifstream is (path);
78- std::string contents (std::istreambuf_iterator<char >{is}, {});
79- return CreateServiceAccountCredentialsFromJsonContents (
80- std::move (contents), options, std::move (client_factory));
81- }
82-
83- std::shared_ptr<oauth2_internal::Credentials>
84- CreateServiceAccountCredentialsFromP12FilePath (
85- std::string const & path, absl::optional<std::set<std::string>> scopes,
86- absl::optional<std::string> subject, Options const & options,
87- oauth2_internal::HttpClientFactory client_factory) {
88- auto info = oauth2_internal::ParseServiceAccountP12File (path);
89- if (!info) {
90- return MakeErrorCredentials (std::move (info).status ());
91- }
92- // These are supplied as extra parameters to this method, not in the P12
93- // file.
94- info->subject = std::move (subject);
95- info->scopes = std::move (scopes);
96- return std::make_shared<oauth2_internal::ServiceAccountCredentials>(
97- *info, options, std::move (client_factory));
98- }
99-
100- std::shared_ptr<oauth2_internal::Credentials>
101- CreateServiceAccountCredentialsFromFilePath (
102- std::string const & path, absl::optional<std::set<std::string>> scopes,
103- absl::optional<std::string> subject, Options const & options,
104- oauth2_internal::HttpClientFactory client_factory) {
105- auto credentials = CreateServiceAccountCredentialsFromJsonFilePath (
106- path, scopes, subject, options, client_factory);
107- if (credentials) {
108- return *credentials;
109- }
110- return CreateServiceAccountCredentialsFromP12FilePath (
111- path, std::move (scopes), std::move (subject), options,
112- std::move (client_factory));
113- }
114-
11553} // namespace
11654
11755std::shared_ptr<oauth2_internal::Credentials> MapCredentials (
@@ -165,18 +103,27 @@ std::shared_ptr<oauth2_internal::Credentials> MapCredentials(
165103
166104 void visit (ServiceAccountConfig const & cfg) override {
167105 if (cfg.file_path ().has_value ()) {
168- result = Decorate (CreateServiceAccountCredentialsFromFilePath (
169- *cfg.file_path (), {}, {}, cfg.options (),
170- std::move (client_factory_)),
171- cfg.options ());
172- } else {
173- auto creds = CreateServiceAccountCredentialsFromJsonContents (
174- cfg.json_object (), cfg.options (), std::move (client_factory_));
106+ auto creds =
107+ oauth2_internal::CreateServiceAccountCredentialsFromFilePath (
108+ *cfg.file_path (), cfg.options (), std::move (client_factory_));
109+ if (creds) {
110+ result = Decorate (*creds, cfg.options ());
111+ } else {
112+ result = MakeErrorCredentials (std::move (creds).status ());
113+ }
114+ } else if (cfg.json_object ().has_value ()) {
115+ auto creds =
116+ oauth2_internal::CreateServiceAccountCredentialsFromJsonContents (
117+ *cfg.json_object (), cfg.options (), std::move (client_factory_));
175118 if (creds) {
176119 result = Decorate (std::move (*creds), cfg.options ());
177120 return ;
178121 }
179122 result = MakeErrorCredentials (std::move (creds).status ());
123+ } else {
124+ result = MakeErrorCredentials (internal::InternalError (
125+ " ServiceAccountConfig has neither json_object nor file_path" ,
126+ GCP_ERROR_INFO ()));
180127 }
181128 }
182129
0 commit comments