GRPC C++  1.23.0
credentials_impl.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 GRPCPP_SECURITY_CREDENTIALS_IMPL_H
20 #define GRPCPP_SECURITY_CREDENTIALS_IMPL_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
27 #include <grpcpp/channel_impl.h>
32 #include <grpcpp/support/status.h>
34 
35 struct grpc_call;
36 
37 namespace grpc_impl {
38 
39 class ChannelCredentials;
40 class CallCredentials;
43 
44 std::shared_ptr<Channel> CreateCustomChannelImpl(
45  const grpc::string& target,
46  const std::shared_ptr<ChannelCredentials>& creds,
47  const ChannelArguments& args);
48 
49 namespace experimental {
50 std::shared_ptr<Channel> CreateCustomChannelWithInterceptors(
51  const grpc::string& target,
52  const std::shared_ptr<ChannelCredentials>& creds,
53  const ChannelArguments& args,
54  std::vector<
55  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
56  interceptor_creators);
57 }
58 
66  public:
69 
70  protected:
71  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
72  const std::shared_ptr<ChannelCredentials>& channel_creds,
73  const std::shared_ptr<CallCredentials>& call_creds);
74 
75  virtual SecureChannelCredentials* AsSecureCredentials() = 0;
76 
77  private:
78  friend std::shared_ptr<Channel> CreateCustomChannelImpl(
79  const grpc::string& target,
80  const std::shared_ptr<ChannelCredentials>& creds,
81  const ChannelArguments& args);
82 
83  friend std::shared_ptr<Channel>
85  const grpc::string& target,
86  const std::shared_ptr<ChannelCredentials>& creds,
87  const ChannelArguments& args,
88  std::vector<std::unique_ptr<
90  interceptor_creators);
91 
92  virtual std::shared_ptr<Channel> CreateChannelImpl(
93  const grpc::string& target, const ChannelArguments& args) = 0;
94 
95  // This function should have been a pure virtual function, but it is
96  // implemented as a virtual function so that it does not break API.
97  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
98  const grpc::string& target, const ChannelArguments& args,
99  std::vector<std::unique_ptr<
101  interceptor_creators) {
102  return nullptr;
103  }
104 };
105 
111  public:
112  CallCredentials();
113  ~CallCredentials();
114 
116  virtual bool ApplyToCall(grpc_call* call) = 0;
117 
118  protected:
119  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
120  const std::shared_ptr<ChannelCredentials>& channel_creds,
121  const std::shared_ptr<CallCredentials>& call_creds);
122 
123  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
124  const std::shared_ptr<CallCredentials>& creds1,
125  const std::shared_ptr<CallCredentials>& creds2);
126 
127  virtual SecureCallCredentials* AsSecureCredentials() = 0;
128 };
129 
138 
142 
147 };
148 
149 // Factories for building different types of Credentials The functions may
150 // return empty shared_ptr when credentials cannot be created. If a
151 // Credentials pointer is returned, it can still be invalid when used to create
152 // a channel. A lame channel will be created then and all rpcs will fail on it.
153 
160 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
161 
163 std::shared_ptr<ChannelCredentials> SslCredentials(
164  const SslCredentialsOptions& options);
165 
172 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
173 
174 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
175 
181 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
182  const grpc::string& json_key,
183  long token_lifetime_seconds = grpc_impl::kMaxAuthTokenLifetimeSecs);
184 
193 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
194  const grpc::string& json_refresh_token);
195 
204 std::shared_ptr<CallCredentials> AccessTokenCredentials(
205  const grpc::string& access_token);
206 
213 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
214  const grpc::string& authorization_token,
215  const grpc::string& authority_selector);
216 
219 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
220  const std::shared_ptr<ChannelCredentials>& channel_creds,
221  const std::shared_ptr<CallCredentials>& call_creds);
222 
224 std::shared_ptr<CallCredentials> CompositeCallCredentials(
225  const std::shared_ptr<CallCredentials>& creds1,
226  const std::shared_ptr<CallCredentials>& creds2);
227 
229 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
230 
233  public:
235 
238  virtual bool IsBlocking() const { return true; }
239 
241  virtual const char* GetType() const { return ""; }
242 
248  virtual grpc::Status GetMetadata(
249  grpc::string_ref service_url, grpc::string_ref method_name,
250  const grpc::AuthContext& channel_auth_context,
251  std::multimap<grpc::string, grpc::string>* metadata) = 0;
252 };
253 
254 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
255  std::unique_ptr<MetadataCredentialsPlugin> plugin);
256 
257 namespace experimental {
258 
266  grpc::string resource; // Optional.
267  grpc::string audience; // Optional.
268  grpc::string scope; // Optional.
274 };
275 
313  StsCredentialsOptions* options);
314 
319 
320 std::shared_ptr<CallCredentials> StsCredentials(
321  const StsCredentialsOptions& options);
322 
328  std::vector<grpc::string> target_service_accounts;
329 };
330 
332 std::shared_ptr<ChannelCredentials> AltsCredentials(
333  const AltsCredentialsOptions& options);
334 
336 std::shared_ptr<ChannelCredentials> LocalCredentials(
338 
339 } // namespace experimental
340 } // namespace grpc_impl
341 
342 #endif // GRPCPP_SECURITY_CREDENTIALS_IMPL_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Builds credentials with reasonable defaults.
std::string string
Definition: config.h:35
Options for creating STS Oauth Token Exchange credentials following the IETF draft https://tools...
Definition: credentials_impl.h:264
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials_impl.h:110
::grpc_impl::SecureChannelCredentials SecureChannelCredentials
Definition: credentials.h:30
Options used to build AltsCredentials.
Definition: credentials_impl.h:324
grpc::string pem_private_key
The buffer containing the PEM encoding of the client&#39;s private key.
Definition: credentials_impl.h:141
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials_impl.h:241
::grpc_impl::ChannelCredentials ChannelCredentials
Definition: credentials.h:26
Class encapsulating the Authentication Information.
Definition: auth_context.h:65
std::shared_ptr<::grpc::Channel > CreateChannelImpl(const grpc::string &target, const std::shared_ptr<::grpc::ChannelCredentials > &creds)
Create a new Channel pointing to target.
::grpc_impl::ChannelArguments ChannelArguments
Definition: channel_arguments.h:32
grpc::string actor_token_path
Definition: credentials_impl.h:272
grpc::string token_exchange_service_uri
Definition: credentials_impl.h:265
grpc::string subject_token_path
Definition: credentials_impl.h:270
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const grpc::string &authorization_token, const grpc::string &authority_selector)
Builds IAM credentials.
grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions *options)
Creates STS credentials options from the $STS_CREDENTIALS environment variable.
grpc::Status StsCredentialsOptionsFromJson(const grpc::string &json_string, StsCredentialsOptions *options)
Creates STS Options from a JSON string.
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=grpc_impl::kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
grpc::string subject_token_type
Definition: credentials_impl.h:271
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
virtual bool IsBlocking() const
If this method returns true, the Process function will be scheduled in a different thread from the on...
Definition: credentials_impl.h:238
Options used to build SslCredentials.
Definition: credentials_impl.h:131
std::shared_ptr< CallCredentials > StsCredentials(const StsCredentialsOptions &options)
std::vector< grpc::string > target_service_accounts
service accounts of target endpoint that will be acceptable by the client.
Definition: credentials_impl.h:328
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
Builds access token credentials.
::grpc_impl::CallCredentials CallCredentials
Definition: credentials.h:27
virtual ~MetadataCredentialsPlugin()
Definition: credentials_impl.h:234
Options for channel creation.
Definition: channel_arguments_impl.h:43
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client&#39;s certificate chain.
Definition: credentials_impl.h:146
std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Combines a channel credentials and a call credentials into a composite channel credentials.
Definition: credentials.h:77
A channel credentials object encapsulates all the state needed by a client to authenticate with a ser...
Definition: credentials_impl.h:65
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::string audience
Definition: credentials_impl.h:267
constexpr long kMaxAuthTokenLifetimeSecs
Definition: credentials_impl.h:174
std::shared_ptr<::grpc::Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< grpc::ChannelCredentials > &creds, const ::grpc::ChannelArguments &args, std::vector< std::unique_ptr< grpc::experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Create a new custom Channel pointing to target with interceptors being invoked per call...
This class is a non owning reference to a string.
Definition: string_ref.h:41
::grpc_impl::SecureCallCredentials SecureCallCredentials
Definition: credentials.h:29
grpc::string scope
Definition: credentials_impl.h:268
grpc::string actor_token_type
Definition: credentials_impl.h:273
Did it work? If it didn&#39;t, why?
Definition: status.h:31
std::shared_ptr<::grpc::Channel > CreateCustomChannelImpl(const grpc::string &target, const std::shared_ptr<::grpc::ChannelCredentials > &creds, const ::grpc::ChannelArguments &args)
Create a new custom Channel pointing to target.
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const grpc::string &json_refresh_token)
Builds refresh token credentials.
std::shared_ptr< CallCredentials > CompositeCallCredentials(const std::shared_ptr< CallCredentials > &creds1, const std::shared_ptr< CallCredentials > &creds2)
Combines two call credentials objects into a composite call credentials.
Definition: credentials.h:84
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials_impl.h:137
User defined metadata credentials.
Definition: credentials_impl.h:232
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
grpc::string resource
Definition: credentials_impl.h:266
grpc_local_connect_type
Type of local connections for which local channel/server credentials will be applied.
Definition: grpc_security_constants.h:112
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
grpc::string requested_token_type
Definition: credentials_impl.h:269