blob: c3f48592cc741ec01023b19adb56d51551dbad59 [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>
41
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
Craig Tiller47c83fd2015-02-21 22:45:35 -080047class Credentials {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048 public:
Craig Tiller47c83fd2015-02-21 22:45:35 -080049 virtual ~Credentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050
Craig Tillerad9d0c42015-02-23 10:53:01 -080051 protected:
Craig Tillere8eb8a42015-02-23 14:56:42 -080052 friend std::unique_ptr<Credentials> CompositeCredentials(
Yang Gao6baa9b62015-03-17 10:49:39 -070053 const std::unique_ptr<Credentials>& creds1,
54 const std::unique_ptr<Credentials>& creds2);
Craig Tillerad9d0c42015-02-23 10:53:01 -080055
56 virtual SecureCredentials* AsSecureCredentials() = 0;
57
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 private:
Craig Tiller47c83fd2015-02-21 22:45:35 -080059 friend std::shared_ptr<ChannelInterface> CreateChannel(
60 const grpc::string& target, const std::unique_ptr<Credentials>& creds,
61 const ChannelArguments& args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062
Craig Tiller47c83fd2015-02-21 22:45:35 -080063 virtual std::shared_ptr<ChannelInterface> CreateChannel(
64 const grpc::string& target, const ChannelArguments& args) = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065};
66
67// Options used to build SslCredentials
yangg4105e2b2015-01-09 14:19:44 -080068// pem_roots_cert is the buffer containing the PEM encoding of the server root
Julien Boeuf026a4172015-02-02 18:36:37 -080069// certificates. If this parameter is empty, the default roots will be used.
yangg4105e2b2015-01-09 14:19:44 -080070// pem_private_key is the buffer containing the PEM encoding of the client's
71// private key. This parameter can be empty if the client does not have a
72// private key.
73// pem_cert_chain is the buffer containing the PEM encoding of the client's
74// certificate chain. This parameter can be empty if the client does not have
75// a certificate chain.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076struct SslCredentialsOptions {
77 grpc::string pem_root_certs;
78 grpc::string pem_private_key;
79 grpc::string pem_cert_chain;
80};
81
Craig Tiller47c83fd2015-02-21 22:45:35 -080082// Factories for building different types of Credentials
83// The functions may return empty unique_ptr when credentials cannot be created.
yangg4105e2b2015-01-09 14:19:44 -080084// If a Credentials pointer is returned, it can still be invalid when used to
85// create a channel. A lame channel will be created then and all rpcs will
86// fail on it.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087
Craig Tiller47c83fd2015-02-21 22:45:35 -080088// Builds credentials with reasonable defaults.
Craig Tillere8eb8a42015-02-23 14:56:42 -080089std::unique_ptr<Credentials> GoogleDefaultCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090
Craig Tiller47c83fd2015-02-21 22:45:35 -080091// Builds SSL Credentials given SSL specific options
92std::unique_ptr<Credentials> SslCredentials(
93 const SslCredentialsOptions& options);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094
Craig Tiller47c83fd2015-02-21 22:45:35 -080095// Builds credentials for use when running in GCE
96std::unique_ptr<Credentials> ComputeEngineCredentials();
yangg4105e2b2015-01-09 14:19:44 -080097
Craig Tiller47c83fd2015-02-21 22:45:35 -080098// Builds service account credentials.
99// json_key is the JSON key string containing the client's private key.
100// scope is a space-delimited list of the requested permissions.
101// token_lifetime is the lifetime of each token acquired through this service
102// account credentials. It should be positive and should not exceed
103// grpc_max_auth_token_lifetime or will be cropped to this value.
104std::unique_ptr<Credentials> ServiceAccountCredentials(
105 const grpc::string& json_key, const grpc::string& scope,
106 std::chrono::seconds token_lifetime);
yangg4105e2b2015-01-09 14:19:44 -0800107
Yang Gaod7786512015-03-11 14:55:02 -0700108// Builds JWT credentials.
109// json_key is the JSON key string containing the client's private key.
110// token_lifetime is the lifetime of each Json Web Token (JWT) created with
111// this credentials. It should not exceed grpc_max_auth_token_lifetime or
112// will be cropped to this value.
Yang Gao0535da32015-03-11 14:51:03 -0700113std::unique_ptr<Credentials> JWTCredentials(
114 const grpc::string& json_key, std::chrono::seconds token_lifetime);
115
Craig Tiller47c83fd2015-02-21 22:45:35 -0800116// Builds IAM credentials.
117std::unique_ptr<Credentials> IAMCredentials(
118 const grpc::string& authorization_token,
119 const grpc::string& authority_selector);
120
121// Combines two credentials objects into a composite credentials
Craig Tillere8eb8a42015-02-23 14:56:42 -0800122std::unique_ptr<Credentials> CompositeCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800123 const std::unique_ptr<Credentials>& creds1,
124 const std::unique_ptr<Credentials>& creds2);
125
126// Credentials for an unencrypted, unauthenticated channel
127std::unique_ptr<Credentials> InsecureCredentials();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128
129} // namespace grpc
130
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100131#endif // GRPCXX_CREDENTIALS_H