GRPC C++  1.16.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 
29 #include <grpcpp/support/status.h>
31 
32 struct grpc_call;
33 
34 namespace grpc {
35 class ChannelArguments;
36 class Channel;
37 class SecureChannelCredentials;
38 class CallCredentials;
39 class SecureCallCredentials;
40 
48  public:
51 
52  protected:
53  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
54  const std::shared_ptr<ChannelCredentials>& channel_creds,
55  const std::shared_ptr<CallCredentials>& call_creds);
56 
57  virtual SecureChannelCredentials* AsSecureCredentials() = 0;
58 
59  private:
60  friend std::shared_ptr<Channel> CreateCustomChannel(
61  const grpc::string& target,
62  const std::shared_ptr<ChannelCredentials>& creds,
63  const ChannelArguments& args);
64 
65  virtual std::shared_ptr<Channel> CreateChannel(
66  const grpc::string& target, const ChannelArguments& args) = 0;
67 };
68 
74  public:
76  ~CallCredentials();
77 
79  virtual bool ApplyToCall(grpc_call* call) = 0;
80 
81  protected:
82  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
83  const std::shared_ptr<ChannelCredentials>& channel_creds,
84  const std::shared_ptr<CallCredentials>& call_creds);
85 
86  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
87  const std::shared_ptr<CallCredentials>& creds1,
88  const std::shared_ptr<CallCredentials>& creds2);
89 
90  virtual SecureCallCredentials* AsSecureCredentials() = 0;
91 };
92 
101 
105 
110 };
111 
112 // Factories for building different types of Credentials The functions may
113 // return empty shared_ptr when credentials cannot be created. If a
114 // Credentials pointer is returned, it can still be invalid when used to create
115 // a channel. A lame channel will be created then and all rpcs will fail on it.
116 
123 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
124 
126 std::shared_ptr<ChannelCredentials> SslCredentials(
127  const SslCredentialsOptions& options);
128 
135 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
136 
138 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
139 
145 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
146  const grpc::string& json_key,
147  long token_lifetime_seconds = kMaxAuthTokenLifetimeSecs);
148 
157 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
158  const grpc::string& json_refresh_token);
159 
168 std::shared_ptr<CallCredentials> AccessTokenCredentials(
169  const grpc::string& access_token);
170 
177 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
178  const grpc::string& authorization_token,
179  const grpc::string& authority_selector);
180 
183 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
184  const std::shared_ptr<ChannelCredentials>& channel_creds,
185  const std::shared_ptr<CallCredentials>& call_creds);
186 
188 std::shared_ptr<CallCredentials> CompositeCallCredentials(
189  const std::shared_ptr<CallCredentials>& creds1,
190  const std::shared_ptr<CallCredentials>& creds2);
191 
193 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
194 
196 std::shared_ptr<ChannelCredentials> CronetChannelCredentials(void* engine);
197 
200  public:
202 
205  virtual bool IsBlocking() const { return true; }
206 
208  virtual const char* GetType() const { return ""; }
209 
215  virtual Status GetMetadata(
216  grpc::string_ref service_url, grpc::string_ref method_name,
217  const AuthContext& channel_auth_context,
218  std::multimap<grpc::string, grpc::string>* metadata) = 0;
219 };
220 
221 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
222  std::unique_ptr<MetadataCredentialsPlugin> plugin);
223 
224 namespace experimental {
225 
231  std::vector<grpc::string> target_service_accounts;
232 };
233 
235 std::shared_ptr<ChannelCredentials> AltsCredentials(
236  const AltsCredentialsOptions& options);
237 
239 std::shared_ptr<ChannelCredentials> LocalCredentials(
241 
242 } // namespace experimental
243 } // namespace grpc
244 
245 #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 SecureChannelCredentials * AsSecureCredentials()=0
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials.h:208
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.
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:205
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
friend 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.
friend 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< 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:227
User defined metadata credentials.
Definition: credentials.h:199
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:100
Options used to build SslCredentials.
Definition: credentials.h:94
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client&#39;s certificate chain.
Definition: credentials.h:109
std::vector< grpc::string > target_service_accounts
service accounts of target endpoint that will be acceptable by the client.
Definition: credentials.h:231
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
grpc::string pem_private_key
The buffer containing the PEM encoding of the client&#39;s private key.
Definition: credentials.h:104
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:47
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:73
constexpr long kMaxAuthTokenLifetimeSecs
Constant for maximum auth token lifetime.
Definition: credentials.h:138
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:201
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
grpc_local_connect_type
Type of local connection for which local channel/server credentials will be applied.
Definition: grpc_security_constants.h:112