Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 34 | #ifndef GRPCXX_SECURITY_CREDENTIALS_H |
| 35 | #define GRPCXX_SECURITY_CREDENTIALS_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Julien Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 37 | #include <map> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <memory> |
| 39 | |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 40 | #include <grpc++/impl/codegen/grpc_library.h> |
Julien Boeuf | 114f394 | 2015-11-19 21:45:52 -0800 | [diff] [blame] | 41 | #include <grpc++/security/auth_context.h> |
Julien Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 42 | #include <grpc++/support/status.h> |
| 43 | #include <grpc++/support/string_ref.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 45 | struct grpc_call; |
| 46 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | namespace grpc { |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 48 | class ChannelArguments; |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 49 | class Channel; |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 50 | class SecureChannelCredentials; |
| 51 | class CallCredentials; |
| 52 | class SecureCallCredentials; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 54 | /// 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 Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 58 | /// |
Julien Boeuf | 77bc16e | 2015-12-28 12:39:44 -0800 | [diff] [blame] | 59 | /// \see http://www.grpc.io/docs/guides/auth.html |
David Garcia Quintas | 60ee8dd | 2016-03-08 17:21:42 -0800 | [diff] [blame] | 60 | class ChannelCredentials : private GrpcLibraryCodegen { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | public: |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 62 | ChannelCredentials(); |
| 63 | ~ChannelCredentials(); |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 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 | |
| 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 Boeuf | 77bc16e | 2015-12-28 12:39:44 -0800 | [diff] [blame] | 85 | /// \see http://www.grpc.io/docs/guides/auth.html |
David Garcia Quintas | 60ee8dd | 2016-03-08 17:21:42 -0800 | [diff] [blame] | 86 | class CallCredentials : private GrpcLibraryCodegen { |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 87 | public: |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 88 | CallCredentials(); |
| 89 | ~CallCredentials(); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 90 | |
| 91 | /// Apply this instance's credentials to \a call. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 92 | virtual bool ApplyToCall(grpc_call* call) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 94 | protected: |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 95 | friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials( |
| 96 | const std::shared_ptr<ChannelCredentials>& channel_creds, |
| 97 | const std::shared_ptr<CallCredentials>& call_creds); |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 98 | |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 99 | friend std::shared_ptr<CallCredentials> CompositeCallCredentials( |
| 100 | const std::shared_ptr<CallCredentials>& creds1, |
| 101 | const std::shared_ptr<CallCredentials>& creds2); |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 102 | |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 103 | virtual SecureCallCredentials* AsSecureCredentials() = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 106 | /// Options used to build SslCredentials. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | struct SslCredentialsOptions { |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 108 | /// 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | grpc::string pem_root_certs; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 114 | |
| 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | grpc::string pem_private_key; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 118 | |
| 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | grpc::string pem_cert_chain; |
| 123 | }; |
| 124 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 130 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 136 | std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 137 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 138 | /// Builds SSL Credentials given SSL specific options |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 139 | std::shared_ptr<ChannelCredentials> SslCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 140 | const SslCredentialsOptions& options); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 142 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 148 | std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials(); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 149 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 150 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 155 | std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials( |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 156 | const grpc::string& json_key, long token_lifetime_seconds); |
Yang Gao | 0535da3 | 2015-03-11 14:51:03 -0700 | [diff] [blame] | 157 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 158 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 166 | std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials( |
Yang Gao | 5ebd6c7 | 2015-03-17 16:22:32 -0700 | [diff] [blame] | 167 | const grpc::string& json_refresh_token); |
| 168 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 169 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 177 | std::shared_ptr<CallCredentials> AccessTokenCredentials( |
Julien Boeuf | 2805be1 | 2015-07-01 02:47:18 -0700 | [diff] [blame] | 178 | const grpc::string& access_token); |
| 179 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 180 | /// 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 Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 186 | std::shared_ptr<CallCredentials> GoogleIAMCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 187 | const grpc::string& authorization_token, |
| 188 | const grpc::string& authority_selector); |
| 189 | |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 190 | /// Combines a channel credentials and a call credentials into a composite |
| 191 | /// channel credentials. |
| 192 | std::shared_ptr<ChannelCredentials> CompositeChannelCredentials( |
Nicolas "Pixel" Noble | ba40482 | 2015-12-01 22:52:20 +0100 | [diff] [blame] | 193 | const std::shared_ptr<ChannelCredentials>& channel_creds, |
| 194 | const std::shared_ptr<CallCredentials>& call_creds); |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 195 | |
| 196 | /// Combines two call credentials objects into a composite call credentials. |
| 197 | std::shared_ptr<CallCredentials> CompositeCallCredentials( |
| 198 | const std::shared_ptr<CallCredentials>& creds1, |
| 199 | const std::shared_ptr<CallCredentials>& creds2); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 200 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 201 | /// Credentials for an unencrypted, unauthenticated channel |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 202 | std::shared_ptr<ChannelCredentials> InsecureChannelCredentials(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 203 | |
Garrett Casto | 931a26b | 2016-10-04 09:18:29 -0700 | [diff] [blame] | 204 | /// Credentials for a channel using Cronet. |
| 205 | std::shared_ptr<ChannelCredentials> CronetChannelCredentials(void* engine); |
| 206 | |
Julien Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 207 | // User defined metadata credentials. |
| 208 | class 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 Boeuf | 114f394 | 2015-11-19 21:45:52 -0800 | [diff] [blame] | 216 | // Type of credentials this plugin is implementing. |
| 217 | virtual const char* GetType() const { return ""; } |
| 218 | |
Julien Boeuf | 14929d4 | 2015-09-21 20:57:26 -0700 | [diff] [blame] | 219 | // Gets the auth metatada produced by this plugin. |
Julien Boeuf | 6489740 | 2015-11-25 14:42:55 -0800 | [diff] [blame] | 220 | // 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 Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 224 | virtual Status GetMetadata( |
Nicolas "Pixel" Noble | ba40482 | 2015-12-01 22:52:20 +0100 | [diff] [blame] | 225 | grpc::string_ref service_url, grpc::string_ref method_name, |
Julien Boeuf | 67c2c95 | 2015-11-25 14:33:19 -0800 | [diff] [blame] | 226 | const AuthContext& channel_auth_context, |
Julien Boeuf | 8b0b6f4 | 2015-09-22 13:31:16 -0700 | [diff] [blame] | 227 | std::multimap<grpc::string, grpc::string>* metadata) = 0; |
Julien Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
Julien Boeuf | 54a902e | 2015-10-12 13:26:21 -0700 | [diff] [blame] | 230 | std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin( |
Julien Boeuf | 2d04118 | 2015-08-31 20:30:09 -0700 | [diff] [blame] | 231 | std::unique_ptr<MetadataCredentialsPlugin> plugin); |
| 232 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 233 | } // namespace grpc |
| 234 | |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 235 | #endif // GRPCXX_SECURITY_CREDENTIALS_H |