blob: cbf94457508f7727f6cf06d383d8849683d2921b [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
yangg4105e2b2015-01-09 14:19:44 -080037#include <chrono>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <memory>
39
40#include <grpc++/config.h>
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070041#include <grpc++/impl/grpc_library.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043namespace grpc {
Craig Tiller47c83fd2015-02-21 22:45:35 -080044class ChannelArguments;
45class ChannelInterface;
Craig Tillerad9d0c42015-02-23 10:53:01 -080046class SecureCredentials;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070048class Credentials : public GrpcLibrary {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049 public:
Yang Gaoc4b6ffb2015-04-23 16:35:24 -070050 ~Credentials() GRPC_OVERRIDE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051
Craig Tillerad9d0c42015-02-23 10:53:01 -080052 protected:
Craig Tillere8eb8a42015-02-23 14:56:42 -080053 friend std::unique_ptr<Credentials> CompositeCredentials(
Yang Gao5ebd6c72015-03-17 16:22:32 -070054 const std::unique_ptr<Credentials>& creds1,
55 const std::unique_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(
61 const grpc::string& target, const std::unique_ptr<Credentials>& creds,
62 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
84// The functions may return empty unique_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.
Craig Tillere8eb8a42015-02-23 14:56:42 -080090std::unique_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
93std::unique_ptr<Credentials> SslCredentials(
94 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
97std::unique_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.
102// token_lifetime is the lifetime of each token acquired through this service
103// account credentials. It should be positive and should not exceed
104// grpc_max_auth_token_lifetime or will be cropped to this value.
105std::unique_ptr<Credentials> ServiceAccountCredentials(
106 const grpc::string& json_key, const grpc::string& scope,
107 std::chrono::seconds token_lifetime);
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.
111// token_lifetime is the lifetime of each Json Web Token (JWT) created with
112// this credentials. It should not exceed grpc_max_auth_token_lifetime or
113// will be cropped to this value.
Yang Gao0535da32015-03-11 14:51:03 -0700114std::unique_ptr<Credentials> JWTCredentials(
115 const grpc::string& json_key, std::chrono::seconds token_lifetime);
116
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.
120std::unique_ptr<Credentials> RefreshTokenCredentials(
121 const grpc::string& json_refresh_token);
122
Craig Tiller47c83fd2015-02-21 22:45:35 -0800123// Builds IAM credentials.
124std::unique_ptr<Credentials> IAMCredentials(
125 const grpc::string& authorization_token,
126 const grpc::string& authority_selector);
127
128// Combines two credentials objects into a composite credentials
Craig Tillere8eb8a42015-02-23 14:56:42 -0800129std::unique_ptr<Credentials> CompositeCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800130 const std::unique_ptr<Credentials>& creds1,
131 const std::unique_ptr<Credentials>& creds2);
132
133// Credentials for an unencrypted, unauthenticated channel
134std::unique_ptr<Credentials> InsecureCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135
136} // namespace grpc
137
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100138#endif // GRPCXX_CREDENTIALS_H