GRPC C++  0.12.0.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
credentials.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015-2016, 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_CREDENTIALS_H
35 #define GRPCXX_CREDENTIALS_H
36 
37 #include <map>
38 #include <memory>
39 
42 #include <grpc++/support/config.h>
43 #include <grpc++/support/status.h>
45 
46 namespace grpc {
47 class ChannelArguments;
48 class Channel;
49 class SecureChannelCredentials;
50 class CallCredentials;
51 class SecureCallCredentials;
52 
60  public:
62 
63  protected:
64  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
65  const std::shared_ptr<ChannelCredentials>& channel_creds,
66  const std::shared_ptr<CallCredentials>& call_creds);
67 
68  virtual SecureChannelCredentials* AsSecureCredentials() = 0;
69 
70  private:
71  friend std::shared_ptr<Channel> CreateCustomChannel(
72  const grpc::string& target,
73  const std::shared_ptr<ChannelCredentials>& creds,
74  const ChannelArguments& args);
75 
76  virtual std::shared_ptr<Channel> CreateChannel(
77  const grpc::string& target, const ChannelArguments& args) = 0;
78 };
79 
84 class CallCredentials : public GrpcLibrary {
85  public:
87 
89  virtual bool ApplyToCall(grpc_call* call) = 0;
90 
91  protected:
92  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
93  const std::shared_ptr<ChannelCredentials>& channel_creds,
94  const std::shared_ptr<CallCredentials>& call_creds);
95 
96  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
97  const std::shared_ptr<CallCredentials>& creds1,
98  const std::shared_ptr<CallCredentials>& creds2);
99 
100  virtual SecureCallCredentials* AsSecureCredentials() = 0;
101 };
102 
111 
115 
120 };
121 
122 // Factories for building different types of Credentials The functions may
123 // return empty shared_ptr when credentials cannot be created. If a
124 // Credentials pointer is returned, it can still be invalid when used to create
125 // a channel. A lame channel will be created then and all rpcs will fail on it.
126 
133 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
134 
136 std::shared_ptr<ChannelCredentials> SslCredentials(
137  const SslCredentialsOptions& options);
138 
145 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
146 
152 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
153  const grpc::string& json_key, long token_lifetime_seconds);
154 
163 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
164  const grpc::string& json_refresh_token);
165 
174 std::shared_ptr<CallCredentials> AccessTokenCredentials(
175  const grpc::string& access_token);
176 
183 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
184  const grpc::string& authorization_token,
185  const grpc::string& authority_selector);
186 
189 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
190  const std::shared_ptr<ChannelCredentials>& channel_creds,
191  const std::shared_ptr<CallCredentials>& call_creds);
192 
194 std::shared_ptr<CallCredentials> CompositeCallCredentials(
195  const std::shared_ptr<CallCredentials>& creds1,
196  const std::shared_ptr<CallCredentials>& creds2);
197 
199 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
200 
201 // User defined metadata credentials.
203  public:
205 
206  // If this method returns true, the Process function will be scheduled in
207  // a different thread from the one processing the call.
208  virtual bool IsBlocking() const { return true; }
209 
210  // Type of credentials this plugin is implementing.
211  virtual const char* GetType() const { return ""; }
212 
213  // Gets the auth metatada produced by this plugin.
214  // The fully qualified method name is:
215  // service_url + "/" + method_name.
216  // The channel_auth_context contains (among other things), the identity of
217  // the server.
218  virtual Status GetMetadata(
219  grpc::string_ref service_url, grpc::string_ref method_name,
220  const AuthContext& channel_auth_context,
221  std::multimap<grpc::string, grpc::string>* metadata) = 0;
222 };
223 
224 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
225  std::unique_ptr<MetadataCredentialsPlugin> plugin);
226 
227 } // namespace grpc
228 
229 #endif // GRPCXX_CREDENTIALS_H
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
std::string string
Definition: config.h:112
virtual SecureChannelCredentials * AsSecureCredentials()=0
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.
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:208
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.
virtual SecureCallCredentials * AsSecureCredentials()=0
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const grpc::string &json_refresh_token)
Builds refresh token credentials.
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.
friend 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:202
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:110
Options used to build SslCredentials.
Definition: credentials.h:104
virtual Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name, const AuthContext &channel_auth_context, std::multimap< grpc::string, grpc::string > *metadata)=0
~CallCredentials() GRPC_OVERRIDE
Definition: grpc_library.h:41
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client's certificate chain.
Definition: credentials.h:119
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.
grpc::string pem_private_key
The buffer containing the PEM encoding of the client's private key.
Definition: credentials.h:114
virtual bool ApplyToCall(grpc_call *call)=0
Apply this instance's credentials to call.
This class is a non owning reference to a string.
Definition: string_ref.h:52
virtual const char * GetType() const
Definition: credentials.h:211
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:59
Did it work? If it didn'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:84
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Builds credentials with reasonable defaults.
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
~ChannelCredentials() GRPC_OVERRIDE
#define GRPC_OVERRIDE
Definition: config.h:77
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
Builds access token credentials.
virtual ~MetadataCredentialsPlugin()
Definition: credentials.h:204