GRPC C++  1.0.0
credentials.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_SECURITY_CREDENTIALS_H
35 #define GRPCXX_SECURITY_CREDENTIALS_H
36 
37 #include <map>
38 #include <memory>
39 
42 #include <grpc++/support/status.h>
44 
45 struct grpc_call;
46 
47 namespace grpc {
48 class ChannelArguments;
49 class Channel;
50 class SecureChannelCredentials;
51 class CallCredentials;
52 class SecureCallCredentials;
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  virtual std::shared_ptr<Channel> CreateChannel(
79  const grpc::string& target, const ChannelArguments& args) = 0;
80 };
81 
87  public:
89  ~CallCredentials();
90 
92  virtual bool ApplyToCall(grpc_call* call) = 0;
93 
94  protected:
95  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
96  const std::shared_ptr<ChannelCredentials>& channel_creds,
97  const std::shared_ptr<CallCredentials>& call_creds);
98 
99  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
100  const std::shared_ptr<CallCredentials>& creds1,
101  const std::shared_ptr<CallCredentials>& creds2);
102 
103  virtual SecureCallCredentials* AsSecureCredentials() = 0;
104 };
105 
114 
118 
123 };
124 
125 // Factories for building different types of Credentials The functions may
126 // return empty shared_ptr when credentials cannot be created. If a
127 // Credentials pointer is returned, it can still be invalid when used to create
128 // a channel. A lame channel will be created then and all rpcs will fail on it.
129 
136 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
137 
139 std::shared_ptr<ChannelCredentials> SslCredentials(
140  const SslCredentialsOptions& options);
141 
148 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
149 
155 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
156  const grpc::string& json_key, long token_lifetime_seconds);
157 
166 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
167  const grpc::string& json_refresh_token);
168 
177 std::shared_ptr<CallCredentials> AccessTokenCredentials(
178  const grpc::string& access_token);
179 
186 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
187  const grpc::string& authorization_token,
188  const grpc::string& authority_selector);
189 
192 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
193  const std::shared_ptr<ChannelCredentials>& channel_creds,
194  const std::shared_ptr<CallCredentials>& call_creds);
195 
197 std::shared_ptr<CallCredentials> CompositeCallCredentials(
198  const std::shared_ptr<CallCredentials>& creds1,
199  const std::shared_ptr<CallCredentials>& creds2);
200 
202 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
203 
204 // User defined metadata credentials.
206  public:
208 
209  // If this method returns true, the Process function will be scheduled in
210  // a different thread from the one processing the call.
211  virtual bool IsBlocking() const { return true; }
212 
213  // Type of credentials this plugin is implementing.
214  virtual const char* GetType() const { return ""; }
215 
216  // Gets the auth metatada produced by this plugin.
217  // The fully qualified method name is:
218  // service_url + "/" + method_name.
219  // The channel_auth_context contains (among other things), the identity of
220  // the server.
221  virtual Status GetMetadata(
222  grpc::string_ref service_url, grpc::string_ref method_name,
223  const AuthContext& channel_auth_context,
224  std::multimap<grpc::string, grpc::string>* metadata) = 0;
225 };
226 
227 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
228  std::unique_ptr<MetadataCredentialsPlugin> plugin);
229 
230 } // namespace grpc
231 
232 #endif // GRPCXX_SECURITY_CREDENTIALS_H
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:62
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
std::string string
Definition: config.h:118
virtual SecureChannelCredentials * AsSecureCredentials()=0
Options for channel creation.
Definition: channel_arguments.h:52
Class encapsulating the Authentication Information.
Definition: auth_context.h:80
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
virtual bool IsBlocking() const
Definition: credentials.h:211
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:53
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 > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
Definition: credentials.h:205
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:113
Options used to build SslCredentials.
Definition: credentials.h:107
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client&#39;s certificate chain.
Definition: credentials.h:122
Definition: alarm.h:48
grpc::string pem_private_key
The buffer containing the PEM encoding of the client&#39;s private key.
Definition: credentials.h:117
This class is a non owning reference to a string.
Definition: string_ref.h:56
virtual const char * GetType() const
Definition: credentials.h:214
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds)
Builds Service Account JWT Access credentials.
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:45
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:86
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:207