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
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"
37GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
41
42
43
44
53
54
55
56
57
58
61 storage::internal::HttpResponse
const& response,
62 std::chrono::system_clock::time_point now);
65
66
67
68
70 std::string
const& content, std::string
const& source,
71 std::string
const& default_token_uri =
72 google::
cloud::oauth2_internal::GoogleOAuthRefreshEndpoint());
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98template <
typename HttpRequestBuilderType =
99 storage::internal::CurlRequestBuilder,
100 typename ClockType = std::chrono::system_clock>
101class AuthorizedUserCredentials;
105class AuthorizedUserCredentials<
storage::internal::CurlRequestBuilder,
106 std::chrono::system_clock>
109 explicit AuthorizedUserCredentials(
113 explicit AuthorizedUserCredentials(
114 google::
cloud::oauth2_internal::AuthorizedUserCredentialsInfo info,
117 StatusOr<std::string> AuthorizationHeader()
override {
118 return oauth2_internal::AuthorizationHeaderJoined(*impl_);
122 friend struct AuthorizedUserCredentialsTester;
123 AuthorizedUserCredentials(
124 google::
cloud::oauth2_internal::AuthorizedUserCredentialsInfo,
125 Options options, oauth2_internal::HttpClientFactory client_factory);
127 StatusOr<std::string> AuthorizationHeaderForTesting(
128 std::chrono::system_clock::time_point tp) {
129 return oauth2_internal::AuthorizationHeaderJoined(*impl_, tp);
132 std::shared_ptr<
google::
cloud::oauth2_internal::Credentials> impl_;
136template <
typename HttpRequestBuilderType,
typename ClockType>
137class AuthorizedUserCredentials :
public Credentials {
141 : info_(std::move(info)),
147 std::unique_lock<std::mutex> lock(mu_);
148 return refreshing_creds_.AuthorizationHeader(clock_.now(),
149 [
this] {
return Refresh(); });
154 HttpRequestBuilderType builder(
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=";
162 payload +=
"&refresh_token=";
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());
173 mutable std::mutex mu_;
178GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
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
Definition: refreshing_credentials_wrapper.h:41