Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPCXX_CREDENTIALS_H |
| 35 | #define GRPCXX_CREDENTIALS_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| 37 | #include <memory> |
| 38 | |
| 39 | #include <grpc++/config.h> |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 40 | #include <grpc++/impl/grpc_library.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | namespace grpc { |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 43 | class ChannelArguments; |
| 44 | class ChannelInterface; |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 45 | class SecureCredentials; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 47 | class Credentials : public GrpcLibrary { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | public: |
Yang Gao | c4b6ffb | 2015-04-23 16:35:24 -0700 | [diff] [blame] | 49 | ~Credentials() GRPC_OVERRIDE; |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 50 | virtual bool ApplyToCall(grpc_call* call) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 52 | protected: |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 53 | friend std::shared_ptr<Credentials> CompositeCredentials( |
| 54 | const std::shared_ptr<Credentials>& creds1, |
| 55 | const std::shared_ptr<Credentials>& creds2); |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 56 | |
| 57 | virtual SecureCredentials* AsSecureCredentials() = 0; |
| 58 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | private: |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 60 | friend std::shared_ptr<ChannelInterface> CreateChannel( |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 61 | const grpc::string& target, const std::shared_ptr<Credentials>& creds, |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 62 | const ChannelArguments& args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 64 | virtual std::shared_ptr<ChannelInterface> CreateChannel( |
| 65 | const grpc::string& target, const ChannelArguments& args) = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // Options used to build SslCredentials |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 69 | // pem_roots_cert is the buffer containing the PEM encoding of the server root |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 70 | // certificates. If this parameter is empty, the default roots will be used. |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 71 | // pem_private_key is the buffer containing the PEM encoding of the client's |
| 72 | // private key. This parameter can be empty if the client does not have a |
| 73 | // private key. |
| 74 | // pem_cert_chain is the buffer containing the PEM encoding of the client's |
| 75 | // certificate chain. This parameter can be empty if the client does not have |
| 76 | // a certificate chain. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | struct SslCredentialsOptions { |
| 78 | grpc::string pem_root_certs; |
| 79 | grpc::string pem_private_key; |
| 80 | grpc::string pem_cert_chain; |
| 81 | }; |
| 82 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 83 | // Factories for building different types of Credentials |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 84 | // The functions may return empty shared_ptr when credentials cannot be created. |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 85 | // If a Credentials pointer is returned, it can still be invalid when used to |
| 86 | // create a channel. A lame channel will be created then and all rpcs will |
| 87 | // fail on it. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 89 | // Builds credentials with reasonable defaults. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 90 | std::shared_ptr<Credentials> GoogleDefaultCredentials(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 92 | // Builds SSL Credentials given SSL specific options |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 93 | std::shared_ptr<Credentials> SslCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 94 | const SslCredentialsOptions& options); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 96 | // Builds credentials for use when running in GCE |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 97 | std::shared_ptr<Credentials> ComputeEngineCredentials(); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 98 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 99 | // Builds service account credentials. |
| 100 | // json_key is the JSON key string containing the client's private key. |
| 101 | // scope is a space-delimited list of the requested permissions. |
Nicolas "Pixel" Noble | b7bbffe | 2015-04-08 20:26:49 +0200 | [diff] [blame] | 102 | // token_lifetime_seconds is the lifetime in seconds of each token acquired |
| 103 | // through this service account credentials. It should be positive and should |
| 104 | // not exceed grpc_max_auth_token_lifetime or will be cropped to this value. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 105 | std::shared_ptr<Credentials> ServiceAccountCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 106 | const grpc::string& json_key, const grpc::string& scope, |
Nicolas "Pixel" Noble | b7bbffe | 2015-04-08 20:26:49 +0200 | [diff] [blame] | 107 | long token_lifetime_seconds); |
yangg | 4105e2b | 2015-01-09 14:19:44 -0800 | [diff] [blame] | 108 | |
Yang Gao | d778651 | 2015-03-11 14:55:02 -0700 | [diff] [blame] | 109 | // Builds JWT credentials. |
| 110 | // json_key is the JSON key string containing the client's private key. |
Nicolas "Pixel" Noble | b7bbffe | 2015-04-08 20:26:49 +0200 | [diff] [blame] | 111 | // token_lifetime_seconds is the lifetime in seconds of each Json Web Token |
| 112 | // (JWT) created with this credentials. It should not exceed |
| 113 | // grpc_max_auth_token_lifetime or will be cropped to this value. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 114 | std::shared_ptr<Credentials> JWTCredentials(const grpc::string& json_key, |
| 115 | long token_lifetime_seconds); |
Yang Gao | 0535da3 | 2015-03-11 14:51:03 -0700 | [diff] [blame] | 116 | |
Yang Gao | 5ebd6c7 | 2015-03-17 16:22:32 -0700 | [diff] [blame] | 117 | // Builds refresh token credentials. |
| 118 | // json_refresh_token is the JSON string containing the refresh token along |
| 119 | // with a client_id and client_secret. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 120 | std::shared_ptr<Credentials> RefreshTokenCredentials( |
Yang Gao | 5ebd6c7 | 2015-03-17 16:22:32 -0700 | [diff] [blame] | 121 | const grpc::string& json_refresh_token); |
| 122 | |
Julien Boeuf | 2805be1 | 2015-07-01 02:47:18 -0700 | [diff] [blame] | 123 | // Builds access token credentials. |
| 124 | // access_token is an oauth2 access token that was fetched using an out of band |
| 125 | // mechanism. |
| 126 | std::shared_ptr<Credentials> AccessTokenCredentials( |
| 127 | const grpc::string& access_token); |
| 128 | |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 129 | // Builds IAM credentials. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 130 | std::shared_ptr<Credentials> IAMCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 131 | const grpc::string& authorization_token, |
| 132 | const grpc::string& authority_selector); |
| 133 | |
| 134 | // Combines two credentials objects into a composite credentials |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 135 | std::shared_ptr<Credentials> CompositeCredentials( |
| 136 | const std::shared_ptr<Credentials>& creds1, |
| 137 | const std::shared_ptr<Credentials>& creds2); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 138 | |
| 139 | // Credentials for an unencrypted, unauthenticated channel |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 140 | std::shared_ptr<Credentials> InsecureCredentials(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | |
| 142 | } // namespace grpc |
| 143 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 144 | #endif // GRPCXX_CREDENTIALS_H |