Debian Codegraph @

src/core/lib/security/credentials/oauth2/oauth2_credentials.h

raw
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
22 #include <grpc/support/port_platform.h>
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>
32 #include <grpc/impl/codegen/gpr_types.h>
33 #include <grpc/support/sync.h>
34
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/gprpp/orphanable.h"
37 #include "src/core/lib/gprpp/ref_counted.h"
38 #include "src/core/lib/gprpp/ref_counted_ptr.h"
39 #include "src/core/lib/gprpp/time.h"
40 #include "src/core/lib/gprpp/unique_type_name.h"
41 #include "src/core/lib/http/httpcli.h"
42 #include "src/core/lib/http/parser.h"
43 #include "src/core/lib/iomgr/closure.h"
44 #include "src/core/lib/iomgr/error.h"
45 #include "src/core/lib/iomgr/polling_entity.h"
46 #include "src/core/lib/json/json.h"
47 #include "src/core/lib/promise/activity.h"
48 #include "src/core/lib/promise/arena_promise.h"
49 #include "src/core/lib/security/credentials/credentials.h"
50 #include "src/core/lib/slice/slice.h"
51 #include "src/core/lib/transport/transport.h"
52 #include "src/core/lib/uri/uri_parser.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.
60 struct grpc_auth_refresh_token {
61 const char* type;
62 char* client_id;
63 char* client_secret;
64 char* refresh_token;
65 };
66 /// Returns 1 if the object is valid, 0 otherwise.
67 int grpc_auth_refresh_token_is_valid(
68 const grpc_auth_refresh_token* refresh_token);
69
70 /// Creates a refresh token object from string. Returns an invalid object if a
71 /// parsing error has been encountered.
72 grpc_auth_refresh_token grpc_auth_refresh_token_create_from_string(
73 const char* json_string);
74
75 /// Creates a refresh token object from parsed json. Returns an invalid object
76 /// if a parsing error has been encountered.
77 grpc_auth_refresh_token grpc_auth_refresh_token_create_from_json(
78 const grpc_core::Json& json);
79
80 /// Destructs the object.
81 void grpc_auth_refresh_token_destruct(grpc_auth_refresh_token* refresh_token);
82
83 // -- Credentials Metadata Request. --
84
85 struct grpc_credentials_metadata_request {
86 explicit grpc_credentials_metadata_request(
87 grpc_core::RefCountedPtr<grpc_call_credentials> creds)
88 : creds(std::move(creds)) {}
89 ~grpc_credentials_metadata_request() {
90 grpc_http_response_destroy(&response);
91 }
92
93 grpc_core::RefCountedPtr<grpc_call_credentials> creds;
94 grpc_http_response response;
95 };
96
97 struct grpc_oauth2_pending_get_request_metadata
98 : public grpc_core::RefCounted<grpc_oauth2_pending_get_request_metadata> {
99 std::atomic<bool> done{false};
100 grpc_core::Waker waker;
101 grpc_polling_entity* pollent;
102 grpc_core::ClientMetadataHandle md;
103 struct grpc_oauth2_pending_get_request_metadata* next;
104 absl::StatusOr<grpc_core::ClientMetadataHandle> result;
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
112 class grpc_oauth2_token_fetcher_credentials : public grpc_call_credentials {
113 public:
114 grpc_oauth2_token_fetcher_credentials();
115 ~grpc_oauth2_token_fetcher_credentials() override;
116
117 grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
118 GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
119 const GetRequestMetadataArgs* args) override;
120
121 void on_http_response(grpc_credentials_metadata_request* r,
122 grpc_error_handle error);
123 std::string debug_string() override;
124
125 grpc_core::UniqueTypeName type() const override;
126
127 protected:
128 virtual void fetch_oauth2(grpc_credentials_metadata_request* req,
129 grpc_polling_entity* pollent, grpc_iomgr_cb_func cb,
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
135 return grpc_core::QsortCompare(
136 static_cast<const grpc_call_credentials*>(this), other);
137 }
138
139 gpr_mu mu_;
140 absl::optional<grpc_core::Slice> access_token_value_;
141 gpr_timespec token_expiration_;
142 bool token_fetch_pending_ = false;
143 grpc_oauth2_pending_get_request_metadata* pending_requests_ = nullptr;
144 grpc_polling_entity pollent_;
145 };
146
147 // Google refresh token credentials.
148 class grpc_google_refresh_token_credentials final
149 : public grpc_oauth2_token_fetcher_credentials {
150 public:
151 explicit grpc_google_refresh_token_credentials(
152 grpc_auth_refresh_token refresh_token);
153 ~grpc_google_refresh_token_credentials() override;
154
155 const grpc_auth_refresh_token& refresh_token() const {
156 return refresh_token_;
157 }
158
159 std::string debug_string() override;
160
161 grpc_core::UniqueTypeName type() const override;
162
163 protected:
164 void fetch_oauth2(grpc_credentials_metadata_request* req,
165 grpc_polling_entity* pollent, grpc_iomgr_cb_func cb,
166 grpc_core::Timestamp deadline) override;
167
168 private:
169 grpc_auth_refresh_token refresh_token_;
170 grpc_closure http_post_cb_closure_;
171 grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request_;
172 };
173
174 // Access token credentials.
175 class grpc_access_token_credentials final : public grpc_call_credentials {
176 public:
177 explicit grpc_access_token_credentials(const char* access_token);
178
179 grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
180 GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
181 const GetRequestMetadataArgs* args) override;
182
183 std::string debug_string() override;
184
185 static grpc_core::UniqueTypeName Type();
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
192 return grpc_core::QsortCompare(
193 static_cast<const grpc_call_credentials*>(this), other);
194 }
195
196 const grpc_core::Slice access_token_value_;
197 };
198
199 // Private constructor for refresh token credentials from an already parsed
200 // refresh token. Takes ownership of the refresh token.
201 grpc_core::RefCountedPtr<grpc_call_credentials>
202 grpc_refresh_token_credentials_create_from_auth_refresh_token(
203 grpc_auth_refresh_token token);
204
205 // Exposed for testing only.
206 grpc_credentials_status
207 grpc_oauth2_token_fetcher_credentials_parse_server_response(
208 const struct grpc_http_response* response,
209 absl::optional<grpc_core::Slice>* token_value,
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.
216 absl::StatusOr<URI> ValidateStsCredentialsOptions(
217 const grpc_sts_credentials_options* options);
218 } // namespace grpc_core
219
220 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_OAUTH2_OAUTH2_CREDENTIALS_H */