GRPC C++  1.19.0-dev
credentials.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_H
20 #define GRPCPP_SECURITY_CREDENTIALS_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
30 #include <grpcpp/support/status.h>
32 
33 struct grpc_call;
34 
35 namespace grpc {
36 class ChannelArguments;
37 class Channel;
38 class SecureChannelCredentials;
39 class CallCredentials;
40 class SecureCallCredentials;
41 
42 class ChannelCredentials;
43 
44 namespace experimental {
45 std::shared_ptr<Channel> CreateCustomChannelWithInterceptors(
46  const grpc::string& target,
47  const std::shared_ptr<ChannelCredentials>& creds,
48  const ChannelArguments& args,
49  std::vector<
50  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
51  interceptor_creators);
52 } // namespace experimental
53 
61  public:
64 
65  protected:
66  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
67  const std::shared_ptr<ChannelCredentials>& channel_creds,
68  const std::shared_ptr<CallCredentials>& call_creds);
69 
70  virtual SecureChannelCredentials* AsSecureCredentials() = 0;
71 
72  private:
73  friend std::shared_ptr<Channel> CreateCustomChannel(
74  const grpc::string& target,
75  const std::shared_ptr<ChannelCredentials>& creds,
76  const ChannelArguments& args);
77 
78  friend std::shared_ptr<Channel>
80  const grpc::string& target,
81  const std::shared_ptr<ChannelCredentials>& creds,
82  const ChannelArguments& args,
83  std::vector<
84  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
85  interceptor_creators);
86 
87  virtual std::shared_ptr<Channel> CreateChannel(
88  const grpc::string& target, const ChannelArguments& args) = 0;
89 
90  // This function should have been a pure virtual function, but it is
91  // implemented as a virtual function so that it does not break API.
92  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
93  const grpc::string& target, const ChannelArguments& args,
94  std::vector<
95  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
96  interceptor_creators) {
97  return nullptr;
98  };
99 };
100 
106  public:
107  CallCredentials();
108  ~CallCredentials();
109 
111  virtual bool ApplyToCall(grpc_call* call) = 0;
112 
113  protected:
114  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
115  const std::shared_ptr<ChannelCredentials>& channel_creds,
116  const std::shared_ptr<CallCredentials>& call_creds);
117 
118  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
119  const std::shared_ptr<CallCredentials>& creds1,
120  const std::shared_ptr<CallCredentials>& creds2);
121 
122  virtual SecureCallCredentials* AsSecureCredentials() = 0;
123 };
124 
133 
137 
142 };
143 
144 // Factories for building different types of Credentials The functions may
145 // return empty shared_ptr when credentials cannot be created. If a
146 // Credentials pointer is returned, it can still be invalid when used to create
147 // a channel. A lame channel will be created then and all rpcs will fail on it.
148 
155 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
156 
158 std::shared_ptr<ChannelCredentials> SslCredentials(
159  const SslCredentialsOptions& options);
160 
167 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
168 
170 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
171 
177 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
178  const grpc::string& json_key,
179  long token_lifetime_seconds = kMaxAuthTokenLifetimeSecs);
180 
189 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
190  const grpc::string& json_refresh_token);
191 
200 std::shared_ptr<CallCredentials> AccessTokenCredentials(
201  const grpc::string& access_token);
202 
209 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
210  const grpc::string& authorization_token,
211  const grpc::string& authority_selector);
212 
215 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
216  const std::shared_ptr<ChannelCredentials>& channel_creds,
217  const std::shared_ptr<CallCredentials>& call_creds);
218 
220 std::shared_ptr<CallCredentials> CompositeCallCredentials(
221  const std::shared_ptr<CallCredentials>& creds1,
222  const std::shared_ptr<CallCredentials>& creds2);
223 
225 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
226 
228 std::shared_ptr<ChannelCredentials> CronetChannelCredentials(void* engine);
229 
232  public:
234 
237  virtual bool IsBlocking() const { return true; }
238 
240  virtual const char* GetType() const { return ""; }
241 
247  virtual Status GetMetadata(
248  grpc::string_ref service_url, grpc::string_ref method_name,
249  const AuthContext& channel_auth_context,
250  std::multimap<grpc::string, grpc::string>* metadata) = 0;
251 };
252 
253 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
254  std::unique_ptr<MetadataCredentialsPlugin> plugin);
255 
256 namespace experimental {
257 
263  std::vector<grpc::string> target_service_accounts;
264 };
265 
267 std::shared_ptr<ChannelCredentials> AltsCredentials(
268  const AltsCredentialsOptions& options);
269 
271 std::shared_ptr<ChannelCredentials> LocalCredentials(
273 
274 } // namespace experimental
275 } // namespace grpc
276 
277 #endif // GRPCPP_SECURITY_CREDENTIALS_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
std::shared_ptr< ChannelCredentials > CronetChannelCredentials(void *engine)
Credentials for a channel using Cronet.
std::string string
Definition: config.h:35
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials.h:240
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.
std::shared_ptr< Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Create a new custom Channel pointing to target with interceptors being invoked per call...
Options for channel creation.
Definition: channel_arguments.h:39
Class encapsulating the Authentication Information.
Definition: auth_context.h:65
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
Create a new custom Channel pointing to target.
virtual bool IsBlocking() const
If this method returns true, the Process function will be scheduled in a different thread from the on...
Definition: credentials.h:237
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.
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const grpc::string &json_refresh_token)
Builds refresh token credentials.
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
Create a new Channel pointing to target.
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
Options used to build AltsCredentials.
Definition: credentials.h:259
User defined metadata credentials.
Definition: credentials.h:231
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:132
Options used to build SslCredentials.
Definition: credentials.h:126
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client&#39;s certificate chain.
Definition: credentials.h:141
std::vector< grpc::string > target_service_accounts
service accounts of target endpoint that will be acceptable by the client.
Definition: credentials.h:263
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::string pem_private_key
The buffer containing the PEM encoding of the client&#39;s private key.
Definition: credentials.h:136
This class is a non owning reference to a string.
Definition: string_ref.h:41
A channel credentials object encapsulates all the state needed by a client to authenticate with a ser...
Definition: credentials.h:60
Did it work? If it didn&#39;t, why?
Definition: status.h:31
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const grpc::string &authorization_token, const grpc::string &authority_selector)
Builds IAM credentials.
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials.h:105
constexpr long kMaxAuthTokenLifetimeSecs
Constant for maximum auth token lifetime.
Definition: credentials.h:170
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Builds credentials with reasonable defaults.
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
Builds access token credentials.
virtual ~MetadataCredentialsPlugin()
Definition: credentials.h:233
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
grpc_local_connect_type
Type of local connections for which local channel/server credentials will be applied.
Definition: grpc_security_constants.h:112