Google Cloud Storage C++ Client  1.42.0
A C++ Client Library for Google Cloud Storage
google_credentials.h
Go to the documentation of this file.
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_GOOGLE_CREDENTIALS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_GOOGLE_CREDENTIALS_H
17 
18 #include "google/cloud/storage/client_options.h"
19 #include "google/cloud/storage/oauth2/credentials.h"
20 #include "google/cloud/storage/version.h"
21 #include "google/cloud/optional.h"
22 #include "absl/types/optional.h"
23 #include <memory>
24 #include <set>
25 #include <string>
26 
27 namespace google {
28 namespace cloud {
29 namespace storage {
31 namespace oauth2 {
32 
33 /**
34  * Produces a Credentials type based on the runtime environment.
35  *
36  * If the GOOGLE_APPLICATION_CREDENTIALS environment variable is set, the JSON
37  * file it points to will be loaded and used to create a credential of the
38  * specified type. Otherwise, if running on a Google-hosted environment (e.g.
39  * Compute Engine), credentials for the the environment's default service
40  * account will be used.
41  *
42  * @see https://cloud.google.com/docs/authentication/production for details
43  * about Application Default %Credentials.
44  */
45 StatusOr<std::shared_ptr<Credentials>> GoogleDefaultCredentials(
46  ChannelOptions const& options = {});
47 
48 //@{
49 /**
50  * @name Functions to manually create specific credential types.
51  */
52 
53 /// Creates an AnonymousCredentials.
55 
56 /**
57  * Creates an AuthorizedUserCredentials from a JSON file at the specified path.
58  *
59  * @note It is strongly preferred to instead use service account credentials
60  * with Cloud Storage client libraries.
61  */
62 StatusOr<std::shared_ptr<Credentials>>
64 
65 /**
66  * Creates an AuthorizedUserCredentials from a JSON string.
67  *
68  * @note It is strongly preferred to instead use service account credentials
69  * with Cloud Storage client libraries.
70  */
71 StatusOr<std::shared_ptr<Credentials>>
73  std::string const& contents, ChannelOptions const& options = {});
74 
75 //@{
76 /// @name Load service account key files.
77 
78 /**
79  * Creates a ServiceAccountCredentials from a file at the specified path.
80  *
81  * @note This function automatically detects if the file is a JSON or P12 (aka
82  * PFX aka PKCS#12) file and tries to load the file as a service account
83  * credential. We strongly recommend that applications use JSON files for
84  * service account key files.
85  *
86  * These credentials use the cloud-platform OAuth 2.0 scope, defined by
87  * `GoogleOAuthScopeCloudPlatform()`. To specify alternate scopes, use the
88  * overloaded version of this function.
89  */
90 StatusOr<std::shared_ptr<Credentials>>
92 
93 /**
94  * Creates a ServiceAccountCredentials from a file at the specified path.
95  *
96  * @note This function automatically detects if the file is a JSON or P12 (aka
97  * PFX aka PKCS#12) file and tries to load the file as a service account
98  * credential. We strongly recommend that applications use JSON files for
99  * service account key files.
100  *
101  * @param path the path to the file containing service account JSON credentials.
102  * @param scopes the scopes to request during the authorization grant. If
103  * omitted, the cloud-platform scope, defined by
104  * `GoogleOAuthScopeCloudPlatform()`, is used as a default.
105  * @param subject for domain-wide delegation; the email address of the user for
106  * which to request delegated access. If omitted, no "subject" attribute is
107  * included in the authorization grant.
108  *
109  * @see https://developers.google.com/identity/protocols/googlescopes for a list
110  * of OAuth 2.0 scopes used with Google APIs.
111  *
112  * @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
113  * for more information about domain-wide delegation.
114  */
115 StatusOr<std::shared_ptr<Credentials>>
117  std::string const& path, absl::optional<std::set<std::string>> scopes,
118  absl::optional<std::string> subject);
119 
120 /**
121  * Creates a ServiceAccountCredentials from a JSON file at the specified path.
122  *
123  * These credentials use the cloud-platform OAuth 2.0 scope, defined by
124  * `GoogleOAuthScopeCloudPlatform()`. To specify alternate scopes, use the
125  * overloaded version of this function.
126  */
127 StatusOr<std::shared_ptr<Credentials>>
129 
130 /**
131  * Creates a ServiceAccountCredentials from a JSON file at the specified path.
132  *
133  * @param path the path to the file containing service account JSON credentials.
134  * @param scopes the scopes to request during the authorization grant. If
135  * omitted, the cloud-platform scope, defined by
136  * `GoogleOAuthScopeCloudPlatform()`, is used as a default.
137  * @param subject for domain-wide delegation; the email address of the user for
138  * which to request delegated access. If omitted, no "subject" attribute is
139  * included in the authorization grant.
140  * @param options any configuration needed for the transport channel to
141  * Google's authentication servers.
142  *
143  * @see https://developers.google.com/identity/protocols/googlescopes for a list
144  * of OAuth 2.0 scopes used with Google APIs.
145  *
146  * @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
147  * for more information about domain-wide delegation.
148  */
149 StatusOr<std::shared_ptr<Credentials>>
151  std::string const& path, absl::optional<std::set<std::string>> scopes,
152  absl::optional<std::string> subject, ChannelOptions const& options = {});
153 
154 /**
155  * Creates a ServiceAccountCredentials from a P12 file at the specified path.
156  *
157  * These credentials use the cloud-platform OAuth 2.0 scope, defined by
158  * `GoogleOAuthScopeCloudPlatform()`. To specify alternate scopes, use the
159  * overloaded version of this function.
160  */
161 StatusOr<std::shared_ptr<Credentials>>
163 
164 /**
165  * Creates a ServiceAccountCredentials from a P12 file at the specified path.
166  *
167  * @param path the path to the file containing service account JSON credentials.
168  * @param scopes the scopes to request during the authorization grant. If
169  * omitted, the cloud-platform scope, defined by
170  * `GoogleOAuthScopeCloudPlatform()`, is used as a default.
171  * @param subject for domain-wide delegation; the email address of the user for
172  * which to request delegated access. If omitted, no "subject" attribute is
173  * included in the authorization grant.
174  * @param options any configuration needed for the transport channel to
175  * Google's authentication servers.
176  *
177  * @see https://developers.google.com/identity/protocols/googlescopes for a list
178  * of OAuth 2.0 scopes used with Google APIs.
179  *
180  * @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
181  * for more information about domain-wide delegation.
182  */
183 StatusOr<std::shared_ptr<Credentials>>
185  std::string const& path, absl::optional<std::set<std::string>> scopes,
186  absl::optional<std::string> subject, ChannelOptions const& options = {});
187 //@}
188 
189 /**
190  * Produces a ServiceAccountCredentials type by trying to load the standard
191  * Application Default %Credentials paths.
192  *
193  * If the GOOGLE_APPLICATION_CREDENTIALS environment variable is set, the JSON
194  * or P12 file it points to will be loaded. Otherwise, if the gcloud utility
195  * has configured an Application Default %Credentials file, that file is
196  * loaded. The loaded file is used to create a ServiceAccountCredentials.
197  *
198  * @param options any configuration needed for the transport channel to
199  * Google's authentication servers.
200  *
201  * @see https://cloud.google.com/docs/authentication/production for details
202  * about Application Default %Credentials.
203  */
204 StatusOr<std::shared_ptr<Credentials>>
206  ChannelOptions const& options = {});
207 
208 /**
209  * Produces a ServiceAccountCredentials type by trying to load the standard
210  * Application Default %Credentials paths.
211  *
212  * If the GOOGLE_APPLICATION_CREDENTIALS environment variable is set, the JSON
213  * or P12 file it points to will be loaded. Otherwise, if the gcloud utility
214  * has configured an Application Default %Credentials file, that file is
215  * loaded. The loaded file is used to create a ServiceAccountCredentials.
216  *
217  * @param scopes the scopes to request during the authorization grant. If
218  * omitted, the cloud-platform scope, defined by
219  * `GoogleOAuthScopeCloudPlatform()`, is used as a default.
220  * @param subject for domain-wide delegation; the email address of the user for
221  * which to request delegated access. If omitted, no "subject" attribute is
222  * included in the authorization grant.
223  * @param options any configuration needed for the transport channel to
224  * Google's authentication servers.
225  *
226  * @see https://developers.google.com/identity/protocols/googlescopes for a list
227  * of OAuth 2.0 scopes used with Google APIs.
228  *
229  * @see https://cloud.google.com/docs/authentication/production for details
230  * about Application Default %Credentials.
231  */
232 StatusOr<std::shared_ptr<Credentials>>
234  absl::optional<std::set<std::string>> scopes,
235  absl::optional<std::string> subject, ChannelOptions const& options = {});
236 
237 /**
238  * Creates a ServiceAccountCredentials from a JSON string.
239  *
240  * These credentials use the cloud-platform OAuth 2.0 scope, defined by
241  * `GoogleOAuthScopeCloudPlatform()`. To specify an alternate set of scopes, use
242  * the overloaded version of this function.
243  */
244 StatusOr<std::shared_ptr<Credentials>>
246  std::string const& contents, ChannelOptions const& options = {});
247 
248 /**
249  * Creates a ServiceAccountCredentials from a JSON string.
250  *
251  * @param contents the string containing the JSON contents of a service account
252  * credentials file.
253  * @param scopes the scopes to request during the authorization grant. If
254  * omitted, the cloud-platform scope, defined by
255  * `GoogleOAuthScopeCloudPlatform()`, is used as a default.
256  * @param subject for domain-wide delegation; the email address of the user for
257  * which to request delegated access. If omitted, no "subject" attribute is
258  * included in the authorization grant.
259  * @param options any configuration needed for the transport channel to
260  * Google's authentication servers.
261  *
262  * @see https://developers.google.com/identity/protocols/googlescopes for a list
263  * of OAuth 2.0 scopes used with Google APIs.
264  *
265  * @see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
266  * for more information about domain-wide delegation.
267  */
268 StatusOr<std::shared_ptr<Credentials>>
270  std::string const& contents, absl::optional<std::set<std::string>> scopes,
271  absl::optional<std::string> subject, ChannelOptions const& options = {});
272 
273 /// Creates a ComputeEngineCredentials for the VM's default service account.
275 
276 /// Creates a ComputeEngineCredentials for the VM's specified service account.
278  std::string const& service_account_email);
279 
280 //@}
281 
282 } // namespace oauth2
284 } // namespace storage
285 } // namespace cloud
286 } // namespace google
287 
288 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_GOOGLE_CREDENTIALS_H