Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
authorized_user_credentials.h
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_AUTHORIZED_USER_CREDENTIALS_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_AUTHORIZED_USER_CREDENTIALS_H
17
18#include "google/cloud/storage/client_options.h"
19#include "google/cloud/storage/internal/curl_request_builder.h"
20#include "google/cloud/storage/internal/http_response.h"
21#include "google/cloud/storage/oauth2/credential_constants.h"
22#include "google/cloud/storage/oauth2/credentials.h"
23#include "google/cloud/storage/oauth2/refreshing_credentials_wrapper.h"
24#include "google/cloud/storage/version.h"
25#include "google/cloud/internal/oauth2_authorized_user_credentials.h"
26#include "google/cloud/internal/oauth2_cached_credentials.h"
27#include "google/cloud/internal/oauth2_credential_constants.h"
28#include "google/cloud/status.h"
29#include <chrono>
30#include <iostream>
31#include <mutex>
32#include <string>
33
34namespace google {
35namespace cloud {
36namespace storage {
37GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
38namespace oauth2 {
39
40/**
41 * Object to hold information used to instantiate an AuthorizedUserCredentials.
42 *
43 * @deprecated Prefer using the unified credentials documented in @ref guac
44 */
46 std::string client_id;
47 std::string client_secret;
48 std::string refresh_token;
49 std::string token_uri;
50};
51
52/**
53 * Parses a refresh response JSON string into an authorization header.
54 *
55 * The header and the current time (for the expiration) form a TemporaryToken.
56 *
57 * @deprecated Prefer using the unified credentials documented in @ref guac
58 */
61 storage::internal::HttpResponse const& response,
62 std::chrono::system_clock::time_point now);
63
64/**
65 * Parses a user credentials JSON string into an AuthorizedUserCredentialsInfo.
66 *
67 * @deprecated Prefer using the unified credentials documented in @ref guac
68 */
70 std::string const& content, std::string const& source,
71 std::string const& default_token_uri =
72 google::cloud::oauth2_internal::GoogleOAuthRefreshEndpoint());
73
74/**
75 * Wrapper class for Google OAuth 2.0 user account credentials.
76 *
77 * Takes a AuthorizedUserCredentialsInfo and obtains access tokens from the
78 * Google Authorization Service as needed. Instances of this class should
79 * usually be created via the convenience methods declared in
80 * google_credentials.h.
81 *
82 * An HTTP Authorization header, with an access token as its value,
83 * can be obtained by calling the AuthorizationHeader() method; if the current
84 * access token is invalid or nearing expiration, this will class will first
85 * obtain a new access token before returning the Authorization header string.
86 *
87 * @see https://developers.google.com/identity/protocols/OAuth2 for an overview
88 * of using user credentials with Google's OAuth 2.0 system.
89 *
90 * @tparam HttpRequestBuilderType a dependency injection point. It makes it
91 * possible to mock internal libcurl wrappers. This should generally not be
92 * overridden except for testing.
93 * @tparam ClockType a dependency injection point to fetch the current time.
94 * This should generally not be overridden except for testing.
95 *
96 * @deprecated Prefer using the unified credentials documented in @ref guac
97 */
98template <typename HttpRequestBuilderType =
99 storage::internal::CurlRequestBuilder,
100 typename ClockType = std::chrono::system_clock>
101class AuthorizedUserCredentials;
102
103/// @copydoc AuthorizedUserCredentials
104template <>
105class AuthorizedUserCredentials<storage::internal::CurlRequestBuilder,
106 std::chrono::system_clock>
107 : public Credentials {
108 public:
109 explicit AuthorizedUserCredentials(
111 ChannelOptions const& channel_options = {});
112
113 explicit AuthorizedUserCredentials(
114 google::cloud::oauth2_internal::AuthorizedUserCredentialsInfo info,
115 ChannelOptions const& channel_options = {});
116
117 StatusOr<std::string> AuthorizationHeader() override {
118 return oauth2_internal::AuthorizationHeaderJoined(*impl_);
119 }
120
121 private:
122 friend struct AuthorizedUserCredentialsTester;
123 AuthorizedUserCredentials(
124 google::cloud::oauth2_internal::AuthorizedUserCredentialsInfo,
125 Options options, oauth2_internal::HttpClientFactory client_factory);
126
127 StatusOr<std::string> AuthorizationHeaderForTesting(
128 std::chrono::system_clock::time_point tp) {
129 return oauth2_internal::AuthorizationHeaderJoined(*impl_, tp);
130 }
131
132 std::shared_ptr<google::cloud::oauth2_internal::Credentials> impl_;
133};
134
135/// @copydoc AuthorizedUserCredentials
136template <typename HttpRequestBuilderType, typename ClockType>
137class AuthorizedUserCredentials : public Credentials {
138 public:
140 ChannelOptions const& channel_options = {})
141 : info_(std::move(info)),
143 channel_options.ssl_root_path())),
144 clock_() {}
145
146 StatusOr<std::string> AuthorizationHeader() override {
147 std::unique_lock<std::mutex> lock(mu_);
148 return refreshing_creds_.AuthorizationHeader(clock_.now(),
149 [this] { return Refresh(); });
150 }
151
152 private:
153 StatusOr<RefreshingCredentialsWrapper::TemporaryToken> Refresh() {
154 HttpRequestBuilderType builder(
155 info_.token_uri,
156 storage::internal::GetDefaultCurlHandleFactory(options_));
157 std::string payload("grant_type=refresh_token");
158 payload += "&client_id=";
159 payload += builder.MakeEscapedString(info_.client_id).get();
160 payload += "&client_secret=";
161 payload += builder.MakeEscapedString(info_.client_secret).get();
162 payload += "&refresh_token=";
163 payload += builder.MakeEscapedString(info_.refresh_token).get();
164 auto response = std::move(builder).BuildRequest().MakeRequest(payload);
165 if (!response) return std::move(response).status();
166 if (response->status_code >= 300) return AsStatus(*response);
167 return ParseAuthorizedUserRefreshResponse(*response, clock_.now());
168 }
169
171 Options options_;
172 ClockType clock_;
173 mutable std::mutex mu_;
174 RefreshingCredentialsWrapper refreshing_creds_;
175};
176
177} // namespace oauth2
178GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
179} // namespace storage
180} // namespace cloud
181} // namespace google
182
183#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_OAUTH2_AUTHORIZED_USER_CREDENTIALS_H
Options & set(ValueTypeT< T > v)
Options(Options const &rhs)
Describes the configuration for low-level connection features.
Definition: client_options.h:74
std::string ssl_root_path() const
Definition: client_options.h:77
AuthorizedUserCredentials(AuthorizedUserCredentialsInfo info, ChannelOptions const &channel_options={})
Definition: authorized_user_credentials.h:139
StatusOr< std::string > AuthorizationHeader() override
Attempts to obtain a value for the Authorization HTTP header.
Definition: authorized_user_credentials.h:146
Interface for OAuth 2.0 credentials used to access Google Cloud services.
Definition: credentials.h:47
Wrapper for refreshable parts of a Credentials object.
Definition: refreshing_credentials_wrapper.h:37
Authentication components for Google Cloud Storage.
Definition: anonymous_credentials.h:26
StatusOr< AuthorizedUserCredentialsInfo > ParseAuthorizedUserCredentials(std::string const &content, std::string const &source, std::string const &default_token_uri=google::cloud::oauth2_internal::GoogleOAuthRefreshEndpoint())
Parses a user credentials JSON string into an AuthorizedUserCredentialsInfo.
StatusOr< RefreshingCredentialsWrapper::TemporaryToken > ParseAuthorizedUserRefreshResponse(storage::internal::HttpResponse const &response, std::chrono::system_clock::time_point now)
Parses a refresh response JSON string into an authorization header.
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
Object to hold information used to instantiate an AuthorizedUserCredentials.
Definition: authorized_user_credentials.h:45
std::string token_uri
Definition: authorized_user_credentials.h:49
std::string client_id
Definition: authorized_user_credentials.h:46
std::string client_secret
Definition: authorized_user_credentials.h:47
std::string refresh_token
Definition: authorized_user_credentials.h:48