blob: 0eaaefcbcaec670b648eb1f17eb481b09009d32f [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_CREDENTIALS_H
35#define GRPCXX_CREDENTIALS_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
37#include <memory>
38
39#include <grpc++/config.h>
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070040#include <grpc++/impl/grpc_library.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042namespace grpc {
Craig Tiller47c83fd2015-02-21 22:45:35 -080043class ChannelArguments;
44class ChannelInterface;
Craig Tillerad9d0c42015-02-23 10:53:01 -080045class SecureCredentials;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070047class Credentials : public GrpcLibrary {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048 public:
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070049 ~Credentials() GRPC_OVERRIDE;
Yang Gaoa8938922015-05-14 11:51:07 -070050 virtual bool ApplyToCall(grpc_call* call) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051
Craig Tillerad9d0c42015-02-23 10:53:01 -080052 protected:
Yang Gaoa8938922015-05-14 11:51:07 -070053 friend std::shared_ptr<Credentials> CompositeCredentials(
54 const std::shared_ptr<Credentials>& creds1,
55 const std::shared_ptr<Credentials>& creds2);
Craig Tillerad9d0c42015-02-23 10:53:01 -080056
57 virtual SecureCredentials* AsSecureCredentials() = 0;
58
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 private:
Craig Tiller47c83fd2015-02-21 22:45:35 -080060 friend std::shared_ptr<ChannelInterface> CreateChannel(
Yang Gaoa8938922015-05-14 11:51:07 -070061 const grpc::string& target, const std::shared_ptr<Credentials>& creds,
Craig Tiller47c83fd2015-02-21 22:45:35 -080062 const ChannelArguments& args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063
Craig Tiller47c83fd2015-02-21 22:45:35 -080064 virtual std::shared_ptr<ChannelInterface> CreateChannel(
65 const grpc::string& target, const ChannelArguments& args) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066};
67
68// Options used to build SslCredentials
yangg4105e2b2015-01-09 14:19:44 -080069// pem_roots_cert is the buffer containing the PEM encoding of the server root
Julien Boeuf026a4172015-02-02 18:36:37 -080070// certificates. If this parameter is empty, the default roots will be used.
yangg4105e2b2015-01-09 14:19:44 -080071// 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 Nobleb7ebd3b2014-11-26 16:33:03 -080077struct SslCredentialsOptions {
78 grpc::string pem_root_certs;
79 grpc::string pem_private_key;
80 grpc::string pem_cert_chain;
81};
82
Craig Tiller47c83fd2015-02-21 22:45:35 -080083// Factories for building different types of Credentials
Yang Gaoa8938922015-05-14 11:51:07 -070084// The functions may return empty shared_ptr when credentials cannot be created.
yangg4105e2b2015-01-09 14:19:44 -080085// 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 Nobleb7ebd3b2014-11-26 16:33:03 -080088
Craig Tiller47c83fd2015-02-21 22:45:35 -080089// Builds credentials with reasonable defaults.
Yang Gaoa8938922015-05-14 11:51:07 -070090std::shared_ptr<Credentials> GoogleDefaultCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091
Craig Tiller47c83fd2015-02-21 22:45:35 -080092// Builds SSL Credentials given SSL specific options
Yang Gaoa8938922015-05-14 11:51:07 -070093std::shared_ptr<Credentials> SslCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -080094 const SslCredentialsOptions& options);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Craig Tiller47c83fd2015-02-21 22:45:35 -080096// Builds credentials for use when running in GCE
Yang Gaoa8938922015-05-14 11:51:07 -070097std::shared_ptr<Credentials> ComputeEngineCredentials();
yangg4105e2b2015-01-09 14:19:44 -080098
Craig Tiller47c83fd2015-02-21 22:45:35 -080099// 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" Nobleb7bbffe2015-04-08 20:26:49 +0200102// 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 Gaoa8938922015-05-14 11:51:07 -0700105std::shared_ptr<Credentials> ServiceAccountCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800106 const grpc::string& json_key, const grpc::string& scope,
Nicolas "Pixel" Nobleb7bbffe2015-04-08 20:26:49 +0200107 long token_lifetime_seconds);
yangg4105e2b2015-01-09 14:19:44 -0800108
Yang Gaod7786512015-03-11 14:55:02 -0700109// Builds JWT credentials.
110// json_key is the JSON key string containing the client's private key.
Nicolas "Pixel" Nobleb7bbffe2015-04-08 20:26:49 +0200111// 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 Gaoa8938922015-05-14 11:51:07 -0700114std::shared_ptr<Credentials> JWTCredentials(const grpc::string& json_key,
115 long token_lifetime_seconds);
Yang Gao0535da32015-03-11 14:51:03 -0700116
Yang Gao5ebd6c72015-03-17 16:22:32 -0700117// 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 Gaoa8938922015-05-14 11:51:07 -0700120std::shared_ptr<Credentials> RefreshTokenCredentials(
Yang Gao5ebd6c72015-03-17 16:22:32 -0700121 const grpc::string& json_refresh_token);
122
Julien Boeuf2805be12015-07-01 02:47:18 -0700123// Builds access token credentials.
124// access_token is an oauth2 access token that was fetched using an out of band
125// mechanism.
126std::shared_ptr<Credentials> AccessTokenCredentials(
127 const grpc::string& access_token);
128
Craig Tiller47c83fd2015-02-21 22:45:35 -0800129// Builds IAM credentials.
Yang Gaoa8938922015-05-14 11:51:07 -0700130std::shared_ptr<Credentials> IAMCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800131 const grpc::string& authorization_token,
132 const grpc::string& authority_selector);
133
134// Combines two credentials objects into a composite credentials
Yang Gaoa8938922015-05-14 11:51:07 -0700135std::shared_ptr<Credentials> CompositeCredentials(
136 const std::shared_ptr<Credentials>& creds1,
137 const std::shared_ptr<Credentials>& creds2);
Craig Tiller47c83fd2015-02-21 22:45:35 -0800138
139// Credentials for an unencrypted, unauthenticated channel
Yang Gaoa8938922015-05-14 11:51:07 -0700140std::shared_ptr<Credentials> InsecureCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800141
142} // namespace grpc
143
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100144#endif // GRPCXX_CREDENTIALS_H