blob: 59861b78d86cfb6ee9c97dafee58c44da4f4ee41 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
David Garcia Quintas3598d442016-03-15 14:53:05 -070034#ifndef GRPCXX_SECURITY_CREDENTIALS_H
35#define GRPCXX_SECURITY_CREDENTIALS_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Julien Boeuf2d041182015-08-31 20:30:09 -070037#include <map>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <memory>
39
David Garcia Quintase1300de2016-01-27 18:41:26 -080040#include <grpc++/impl/codegen/grpc_library.h>
Julien Boeuf114f3942015-11-19 21:45:52 -080041#include <grpc++/security/auth_context.h>
Julien Boeuf2d041182015-08-31 20:30:09 -070042#include <grpc++/support/status.h>
43#include <grpc++/support/string_ref.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
David Garcia Quintase1300de2016-01-27 18:41:26 -080045struct grpc_call;
46
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047namespace grpc {
Craig Tiller47c83fd2015-02-21 22:45:35 -080048class ChannelArguments;
yang-g8c2be9f2015-08-19 16:28:09 -070049class Channel;
Julien Boeuf54a902e2015-10-12 13:26:21 -070050class SecureChannelCredentials;
51class CallCredentials;
52class SecureCallCredentials;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Julien Boeuf54a902e2015-10-12 13:26:21 -070054/// A channel credentials object encapsulates all the state needed by a client
55/// to authenticate with a server for a given channel.
56/// It can make various assertions, e.g., about the client’s identity, role
57/// for all the calls on that channel.
Craig Tillerd6599a32015-09-03 09:37:02 -070058///
Julien Boeuf77bc16e2015-12-28 12:39:44 -080059/// \see http://www.grpc.io/docs/guides/auth.html
David Garcia Quintas60ee8dd2016-03-08 17:21:42 -080060class ChannelCredentials : private GrpcLibraryCodegen {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061 public:
David Garcia Quintase1300de2016-01-27 18:41:26 -080062 ChannelCredentials();
63 ~ChannelCredentials();
Julien Boeuf54a902e2015-10-12 13:26:21 -070064
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
82/// A call credentials object encapsulates the state needed by a client to
83/// authenticate with a server for a given call on a channel.
84///
Julien Boeuf77bc16e2015-12-28 12:39:44 -080085/// \see http://www.grpc.io/docs/guides/auth.html
David Garcia Quintas60ee8dd2016-03-08 17:21:42 -080086class CallCredentials : private GrpcLibraryCodegen {
Julien Boeuf54a902e2015-10-12 13:26:21 -070087 public:
David Garcia Quintase1300de2016-01-27 18:41:26 -080088 CallCredentials();
89 ~CallCredentials();
Craig Tillerd6599a32015-09-03 09:37:02 -070090
91 /// Apply this instance's credentials to \a call.
Yang Gaoa8938922015-05-14 11:51:07 -070092 virtual bool ApplyToCall(grpc_call* call) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093
Craig Tillerad9d0c42015-02-23 10:53:01 -080094 protected:
Julien Boeuf54a902e2015-10-12 13:26:21 -070095 friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
96 const std::shared_ptr<ChannelCredentials>& channel_creds,
97 const std::shared_ptr<CallCredentials>& call_creds);
Craig Tillerad9d0c42015-02-23 10:53:01 -080098
Julien Boeuf54a902e2015-10-12 13:26:21 -070099 friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
100 const std::shared_ptr<CallCredentials>& creds1,
101 const std::shared_ptr<CallCredentials>& creds2);
Craig Tillerad9d0c42015-02-23 10:53:01 -0800102
Julien Boeuf54a902e2015-10-12 13:26:21 -0700103 virtual SecureCallCredentials* AsSecureCredentials() = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104};
105
Craig Tillerd6599a32015-09-03 09:37:02 -0700106/// Options used to build SslCredentials.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800107struct SslCredentialsOptions {
Craig Tillerd6599a32015-09-03 09:37:02 -0700108 /// The buffer containing the PEM encoding of the server root certificates. If
109 /// this parameter is empty, the default roots will be used. The default
110 /// roots can be overridden using the \a GRPC_DEFAULT_SSL_ROOTS_FILE_PATH
111 /// environment variable pointing to a file on the file system containing the
112 /// roots.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 grpc::string pem_root_certs;
Craig Tillerd6599a32015-09-03 09:37:02 -0700114
115 /// The buffer containing the PEM encoding of the client's private key. This
116 /// parameter can be empty if the client does not have a private key.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 grpc::string pem_private_key;
Craig Tillerd6599a32015-09-03 09:37:02 -0700118
119 /// The buffer containing the PEM encoding of the client's certificate chain.
120 /// This parameter can be empty if the client does not have a certificate
121 /// chain.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122 grpc::string pem_cert_chain;
123};
124
Craig Tillerd6599a32015-09-03 09:37:02 -0700125// 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129
Craig Tillerd6599a32015-09-03 09:37:02 -0700130/// Builds credentials with reasonable defaults.
131///
132/// \warning Only use these credentials when connecting to a Google endpoint.
133/// Using these credentials to connect to any other service may result in this
134/// service being able to impersonate your client for requests to Google
135/// services.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700136std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137
Craig Tillerd6599a32015-09-03 09:37:02 -0700138/// Builds SSL Credentials given SSL specific options
Julien Boeuf54a902e2015-10-12 13:26:21 -0700139std::shared_ptr<ChannelCredentials> SslCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800140 const SslCredentialsOptions& options);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141
Craig Tillerd6599a32015-09-03 09:37:02 -0700142/// Builds credentials for use when running in GCE
143///
144/// \warning Only use these credentials when connecting to a Google endpoint.
145/// Using these credentials to connect to any other service may result in this
146/// service being able to impersonate your client for requests to Google
147/// services.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700148std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
yangg4105e2b2015-01-09 14:19:44 -0800149
Craig Tillerd6599a32015-09-03 09:37:02 -0700150/// Builds Service Account JWT Access credentials.
151/// json_key is the JSON key string containing the client's private key.
152/// token_lifetime_seconds is the lifetime in seconds of each Json Web Token
153/// (JWT) created with this credentials. It should not exceed
154/// grpc_max_auth_token_lifetime or will be cropped to this value.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700155std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
Julien Boeuffe4c3f42015-07-22 16:20:13 -0700156 const grpc::string& json_key, long token_lifetime_seconds);
Yang Gao0535da32015-03-11 14:51:03 -0700157
Craig Tillerd6599a32015-09-03 09:37:02 -0700158/// Builds refresh token credentials.
159/// json_refresh_token is the JSON string containing the refresh token along
160/// with a client_id and client_secret.
161///
162/// \warning Only use these credentials when connecting to a Google endpoint.
163/// Using these credentials to connect to any other service may result in this
164/// service being able to impersonate your client for requests to Google
165/// services.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700166std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
Yang Gao5ebd6c72015-03-17 16:22:32 -0700167 const grpc::string& json_refresh_token);
168
Craig Tillerd6599a32015-09-03 09:37:02 -0700169/// Builds access token credentials.
170/// access_token is an oauth2 access token that was fetched using an out of band
171/// mechanism.
172///
173/// \warning Only use these credentials when connecting to a Google endpoint.
174/// Using these credentials to connect to any other service may result in this
175/// service being able to impersonate your client for requests to Google
176/// services.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700177std::shared_ptr<CallCredentials> AccessTokenCredentials(
Julien Boeuf2805be12015-07-01 02:47:18 -0700178 const grpc::string& access_token);
179
Craig Tillerd6599a32015-09-03 09:37:02 -0700180/// Builds IAM credentials.
181///
182/// \warning Only use these credentials when connecting to a Google endpoint.
183/// Using these credentials to connect to any other service may result in this
184/// service being able to impersonate your client for requests to Google
185/// services.
Julien Boeuf54a902e2015-10-12 13:26:21 -0700186std::shared_ptr<CallCredentials> GoogleIAMCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800187 const grpc::string& authorization_token,
188 const grpc::string& authority_selector);
189
Julien Boeuf54a902e2015-10-12 13:26:21 -0700190/// Combines a channel credentials and a call credentials into a composite
191/// channel credentials.
192std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
Nicolas "Pixel" Nobleba404822015-12-01 22:52:20 +0100193 const std::shared_ptr<ChannelCredentials>& channel_creds,
194 const std::shared_ptr<CallCredentials>& call_creds);
Julien Boeuf54a902e2015-10-12 13:26:21 -0700195
196/// Combines two call credentials objects into a composite call credentials.
197std::shared_ptr<CallCredentials> CompositeCallCredentials(
198 const std::shared_ptr<CallCredentials>& creds1,
199 const std::shared_ptr<CallCredentials>& creds2);
Craig Tiller47c83fd2015-02-21 22:45:35 -0800200
Craig Tillerd6599a32015-09-03 09:37:02 -0700201/// Credentials for an unencrypted, unauthenticated channel
Julien Boeuf54a902e2015-10-12 13:26:21 -0700202std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203
Garrett Casto931a26b2016-10-04 09:18:29 -0700204/// Credentials for a channel using Cronet.
205std::shared_ptr<ChannelCredentials> CronetChannelCredentials(void* engine);
206
Julien Boeuf2d041182015-08-31 20:30:09 -0700207// User defined metadata credentials.
208class MetadataCredentialsPlugin {
209 public:
210 virtual ~MetadataCredentialsPlugin() {}
211
212 // If this method returns true, the Process function will be scheduled in
213 // a different thread from the one processing the call.
214 virtual bool IsBlocking() const { return true; }
215
Julien Boeuf114f3942015-11-19 21:45:52 -0800216 // Type of credentials this plugin is implementing.
217 virtual const char* GetType() const { return ""; }
218
Julien Boeuf14929d42015-09-21 20:57:26 -0700219 // Gets the auth metatada produced by this plugin.
Julien Boeuf64897402015-11-25 14:42:55 -0800220 // The fully qualified method name is:
221 // service_url + "/" + method_name.
222 // The channel_auth_context contains (among other things), the identity of
223 // the server.
Julien Boeuf2d041182015-08-31 20:30:09 -0700224 virtual Status GetMetadata(
Nicolas "Pixel" Nobleba404822015-12-01 22:52:20 +0100225 grpc::string_ref service_url, grpc::string_ref method_name,
Julien Boeuf67c2c952015-11-25 14:33:19 -0800226 const AuthContext& channel_auth_context,
Julien Boeuf8b0b6f42015-09-22 13:31:16 -0700227 std::multimap<grpc::string, grpc::string>* metadata) = 0;
Julien Boeuf2d041182015-08-31 20:30:09 -0700228};
229
Julien Boeuf54a902e2015-10-12 13:26:21 -0700230std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
Julien Boeuf2d041182015-08-31 20:30:09 -0700231 std::unique_ptr<MetadataCredentialsPlugin> plugin);
232
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800233} // namespace grpc
234
David Garcia Quintas3598d442016-03-15 14:53:05 -0700235#endif // GRPCXX_SECURITY_CREDENTIALS_H