oauth2_credentials.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H
20 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H
21 
23 
24 #include <atomic>
25 #include <string>
26 #include <utility>
27 
28 #include "absl/status/statusor.h"
29 #include "absl/types/optional.h"
30 
31 #include <grpc/grpc_security.h>
33 #include <grpc/support/sync.h>
34 
46 #include "src/core/lib/json/json.h"
53 
54 // Constants.
55 #define GRPC_STS_POST_MINIMAL_BODY_FORMAT_STRING \
56  "grant_type=urn:ietf:params:oauth:grant-type:token-exchange&subject_token=%" \
57  "s&subject_token_type=%s"
58 
59 // auth_refresh_token parsing.
61  const char* type;
62  char* client_id;
65 };
68  const grpc_auth_refresh_token* refresh_token);
69 
73  const char* json_string);
74 
78  const grpc_core::Json& json);
79 
82 
83 // -- Credentials Metadata Request. --
84 
88  : creds(std::move(creds)) {}
91  }
92 
95 };
96 
98  : public grpc_core::RefCounted<grpc_oauth2_pending_get_request_metadata> {
99  std::atomic<bool> done{false};
105 };
106 
107 // -- Oauth2 Token Fetcher credentials --
108 //
109 // This object is a base for credentials that need to acquire an oauth2 token
110 // from an http service.
111 
113  public:
116 
119  const GetRequestMetadataArgs* args) override;
120 
123  std::string debug_string() override;
124 
125  grpc_core::UniqueTypeName type() const override;
126 
127  protected:
130  grpc_core::Timestamp deadline) = 0;
131 
132  private:
133  int cmp_impl(const grpc_call_credentials* other) const override {
134  // TODO(yashykt): Check if we can do something better here
136  static_cast<const grpc_call_credentials*>(this), other);
137  }
138 
142  bool token_fetch_pending_ = false;
145 };
146 
147 // Google refresh token credentials.
150  public:
154 
156  return refresh_token_;
157  }
158 
159  std::string debug_string() override;
160 
161  grpc_core::UniqueTypeName type() const override;
162 
163  protected:
166  grpc_core::Timestamp deadline) override;
167 
168  private:
172 };
173 
174 // Access token credentials.
176  public:
177  explicit grpc_access_token_credentials(const char* access_token);
178 
181  const GetRequestMetadataArgs* args) override;
182 
183  std::string debug_string() override;
184 
186 
187  grpc_core::UniqueTypeName type() const override { return Type(); }
188 
189  private:
190  int cmp_impl(const grpc_call_credentials* other) const override {
191  // TODO(yashykt): Check if we can do something better here
193  static_cast<const grpc_call_credentials*>(this), other);
194  }
195 
197 };
198 
199 // Private constructor for refresh token credentials from an already parsed
200 // refresh token. Takes ownership of the refresh token.
204 
205 // Exposed for testing only.
208  const struct grpc_http_response* response,
210  grpc_core::Duration* token_lifetime);
211 
212 namespace grpc_core {
213 // Exposed for testing only. This function validates the options, ensuring that
214 // the required fields are set, and outputs the parsed URL of the STS token
215 // exchanged service.
218 } // namespace grpc_core
219 
220 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H */
grpc_access_token_credentials::Type
static grpc_core::UniqueTypeName Type()
Definition: oauth2_credentials.cc:730
slice.h
orphanable.h
grpc_call_credentials::GetRequestMetadataArgs
Definition: src/core/lib/security/credentials/credentials.h:196
grpc_oauth2_token_fetcher_credentials_parse_server_response
grpc_credentials_status grpc_oauth2_token_fetcher_credentials_parse_server_response(const struct grpc_http_response *response, absl::optional< grpc_core::Slice > *token_value, grpc_core::Duration *token_lifetime)
grpc_google_refresh_token_credentials::refresh_token_
grpc_auth_refresh_token refresh_token_
Definition: oauth2_credentials.h:169
grpc_auth_refresh_token::client_id
char * client_id
Definition: oauth2_credentials.h:62
grpc_auth_refresh_token_create_from_string
grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string(const char *json_string)
Definition: oauth2_credentials.cc:115
polling_entity.h
grpc_google_refresh_token_credentials::~grpc_google_refresh_token_credentials
~grpc_google_refresh_token_credentials() override
Definition: oauth2_credentials.cc:436
grpc_auth_refresh_token::type
const char * type
Definition: oauth2_credentials.h:61
grpc_credentials_metadata_request::grpc_credentials_metadata_request
grpc_credentials_metadata_request(grpc_core::RefCountedPtr< grpc_call_credentials > creds)
Definition: oauth2_credentials.h:86
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_auth_refresh_token::client_secret
char * client_secret
Definition: oauth2_credentials.h:63
grpc_sts_credentials_options
Definition: grpc_security.h:355
options
double_dict options[]
Definition: capstone_test.c:55
grpc_oauth2_pending_get_request_metadata::md
grpc_core::ClientMetadataHandle md
Definition: oauth2_credentials.h:102
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
useful.h
grpc_access_token_credentials::grpc_access_token_credentials
grpc_access_token_credentials(const char *access_token)
Definition: oauth2_credentials.cc:735
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_credentials_metadata_request::creds
grpc_core::RefCountedPtr< grpc_call_credentials > creds
Definition: oauth2_credentials.h:93
grpc_auth_refresh_token_is_valid
int grpc_auth_refresh_token_is_valid(const grpc_auth_refresh_token *refresh_token)
Returns 1 if the object is valid, 0 otherwise.
Definition: oauth2_credentials.cc:73
grpc_oauth2_pending_get_request_metadata
Definition: oauth2_credentials.h:97
grpc_google_refresh_token_credentials::fetch_oauth2
void fetch_oauth2(grpc_credentials_metadata_request *req, grpc_polling_entity *pollent, grpc_iomgr_cb_func cb, grpc_core::Timestamp deadline) override
Definition: oauth2_credentials.cc:440
closure.h
grpc_call_credentials
Definition: src/core/lib/security/credentials/credentials.h:189
grpc_google_refresh_token_credentials::debug_string
std::string debug_string() override
Definition: oauth2_credentials.cc:486
grpc_access_token_credentials::GetRequestMetadata
grpc_core::ArenaPromise< absl::StatusOr< grpc_core::ClientMetadataHandle > > GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, const GetRequestMetadataArgs *args) override
Definition: oauth2_credentials.cc:721
grpc_core::ValidateStsCredentialsOptions
absl::StatusOr< URI > ValidateStsCredentialsOptions(const grpc_sts_credentials_options *options)
Definition: oauth2_credentials.cc:663
grpc_oauth2_pending_get_request_metadata::next
struct grpc_oauth2_pending_get_request_metadata * next
Definition: oauth2_credentials.h:103
grpc_security.h
grpc_auth_refresh_token
Definition: oauth2_credentials.h:60
grpc_oauth2_token_fetcher_credentials::mu_
gpr_mu mu_
Definition: oauth2_credentials.h:139
grpc_access_token_credentials
Definition: oauth2_credentials.h:175
credentials.h
grpc_oauth2_token_fetcher_credentials::GetRequestMetadata
grpc_core::ArenaPromise< absl::StatusOr< grpc_core::ClientMetadataHandle > > GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata, const GetRequestMetadataArgs *args) override
Definition: oauth2_credentials.cc:297
grpc_oauth2_token_fetcher_credentials
Definition: oauth2_credentials.h:112
grpc_oauth2_pending_get_request_metadata::done
std::atomic< bool > done
Definition: oauth2_credentials.h:99
grpc_http_response
Definition: src/core/lib/http/parser.h:85
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr< grpc_call_credentials >
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_oauth2_token_fetcher_credentials::cmp_impl
int cmp_impl(const grpc_call_credentials *other) const override
Definition: oauth2_credentials.h:133
grpc_access_token_credentials::cmp_impl
int cmp_impl(const grpc_call_credentials *other) const override
Definition: oauth2_credentials.h:190
req
static uv_connect_t req
Definition: test-connection-fail.c:30
grpc_oauth2_token_fetcher_credentials::on_http_response
void on_http_response(grpc_credentials_metadata_request *r, grpc_error_handle error)
Definition: oauth2_credentials.cc:248
grpc_google_refresh_token_credentials::http_request_
grpc_core::OrphanablePtr< grpc_core::HttpRequest > http_request_
Definition: oauth2_credentials.h:171
grpc_google_refresh_token_credentials::grpc_google_refresh_token_credentials
grpc_google_refresh_token_credentials(grpc_auth_refresh_token refresh_token)
Definition: oauth2_credentials.cc:471
grpc_google_refresh_token_credentials::type
grpc_core::UniqueTypeName type() const override
Definition: oauth2_credentials.cc:492
httpcli.h
grpc_oauth2_token_fetcher_credentials::debug_string
std::string debug_string() override
Definition: oauth2_credentials.cc:358
absl::optional< grpc_core::Slice >
grpc_oauth2_token_fetcher_credentials::token_expiration_
gpr_timespec token_expiration_
Definition: oauth2_credentials.h:141
grpc_refresh_token_credentials_create_from_auth_refresh_token
grpc_core::RefCountedPtr< grpc_call_credentials > grpc_refresh_token_credentials_create_from_auth_refresh_token(grpc_auth_refresh_token token)
Definition: oauth2_credentials.cc:476
arena_promise.h
time.h
grpc_auth_refresh_token_destruct
void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token *refresh_token)
Destructs the object.
Definition: oauth2_credentials.cc:127
grpc_oauth2_token_fetcher_credentials::~grpc_oauth2_token_fetcher_credentials
~grpc_oauth2_token_fetcher_credentials() override
Definition: oauth2_credentials.cc:149
grpc_http_response_destroy
void grpc_http_response_destroy(grpc_http_response *response)
Definition: src/core/lib/http/parser.cc:434
error.h
grpc_polling_entity
Definition: polling_entity.h:38
json.h
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_oauth2_token_fetcher_credentials::pollent_
grpc_polling_entity pollent_
Definition: oauth2_credentials.h:144
grpc_oauth2_token_fetcher_credentials::pending_requests_
grpc_oauth2_pending_get_request_metadata * pending_requests_
Definition: oauth2_credentials.h:143
grpc_credentials_metadata_request
Definition: oauth2_credentials.h:85
grpc_google_refresh_token_credentials::refresh_token
const grpc_auth_refresh_token & refresh_token() const
Definition: oauth2_credentials.h:155
grpc_access_token_credentials::type
grpc_core::UniqueTypeName type() const override
Definition: oauth2_credentials.h:187
grpc_oauth2_token_fetcher_credentials::access_token_value_
absl::optional< grpc_core::Slice > access_token_value_
Definition: oauth2_credentials.h:140
grpc_auth_refresh_token::refresh_token
char * refresh_token
Definition: oauth2_credentials.h:64
grpc_core::MetadataHandle< ClientMetadata >
grpc_oauth2_token_fetcher_credentials::token_fetch_pending_
bool token_fetch_pending_
Definition: oauth2_credentials.h:142
gpr_types.h
parser.h
grpc_core::UniqueTypeName
Definition: unique_type_name.h:56
grpc_oauth2_pending_get_request_metadata::pollent
grpc_polling_entity * pollent
Definition: oauth2_credentials.h:101
grpc_core::ArenaPromise
Definition: arena_promise.h:152
grpc_core::Waker
Definition: activity.h:61
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
ref_counted.h
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_access_token_credentials::access_token_value_
const grpc_core::Slice access_token_value_
Definition: oauth2_credentials.h:196
grpc_credentials_metadata_request::response
grpc_http_response response
Definition: oauth2_credentials.h:94
grpc_oauth2_token_fetcher_credentials::fetch_oauth2
virtual void fetch_oauth2(grpc_credentials_metadata_request *req, grpc_polling_entity *pollent, grpc_iomgr_cb_func cb, grpc_core::Timestamp deadline)=0
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
fix_build_deps.r
r
Definition: fix_build_deps.py:491
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_iomgr_cb_func
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
Definition: closure.h:53
grpc_access_token_credentials::debug_string
std::string debug_string() override
Definition: oauth2_credentials.cc:740
grpc_oauth2_pending_get_request_metadata::result
absl::StatusOr< grpc_core::ClientMetadataHandle > result
Definition: oauth2_credentials.h:104
unique_type_name.h
ref_counted_ptr.h
transport.h
grpc_oauth2_pending_get_request_metadata::waker
grpc_core::Waker waker
Definition: oauth2_credentials.h:100
grpc_oauth2_token_fetcher_credentials::type
grpc_core::UniqueTypeName type() const override
Definition: oauth2_credentials.cc:362
grpc_google_refresh_token_credentials::http_post_cb_closure_
grpc_closure http_post_cb_closure_
Definition: oauth2_credentials.h:170
grpc_oauth2_token_fetcher_credentials::grpc_oauth2_token_fetcher_credentials
grpc_oauth2_token_fetcher_credentials()
Definition: oauth2_credentials.cc:351
grpc_google_refresh_token_credentials
Definition: oauth2_credentials.h:148
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
uri_parser.h
gpr_timespec
Definition: gpr_types.h:50
grpc_credentials_status
grpc_credentials_status
Definition: src/core/lib/security/credentials/credentials.h:48
grpc_error
Definition: error_internal.h:42
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
activity.h
sync.h
grpc_closure
Definition: closure.h:56
grpc_core::Json
Definition: src/core/lib/json/json.h:37
grpc_credentials_metadata_request::~grpc_credentials_metadata_request
~grpc_credentials_metadata_request()
Definition: oauth2_credentials.h:89
grpc_auth_refresh_token_create_from_json
grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json(const grpc_core::Json &json)
Definition: oauth2_credentials.cc:79
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:43