credentials.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  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. #ifndef GRPCXX_CREDENTIALS_H
  34. #define GRPCXX_CREDENTIALS_H
  35. #include <memory>
  36. #include <grpc++/impl/grpc_library.h>
  37. #include <grpc++/support/config.h>
  38. namespace grpc {
  39. class ChannelArguments;
  40. class Channel;
  41. class SecureCredentials;
  42. /// A credentials object encapsulates all the state needed by a client to
  43. /// authenticate with a server and make various assertions, e.g., about the
  44. /// client’s identity, role, or whether it is authorized to make a particular
  45. /// call.
  46. ///
  47. /// \see https://github.com/grpc/grpc-common/blob/master/grpc-auth-support.md
  48. class Credentials : public GrpcLibrary {
  49. public:
  50. ~Credentials() GRPC_OVERRIDE;
  51. /// Apply this instance's credentials to \a call.
  52. virtual bool ApplyToCall(grpc_call* call) = 0;
  53. protected:
  54. friend std::shared_ptr<Credentials> CompositeCredentials(
  55. const std::shared_ptr<Credentials>& creds1,
  56. const std::shared_ptr<Credentials>& creds2);
  57. virtual SecureCredentials* AsSecureCredentials() = 0;
  58. private:
  59. friend std::shared_ptr<Channel> CreateChannel(
  60. const grpc::string& target, const std::shared_ptr<Credentials>& creds,
  61. const ChannelArguments& args);
  62. virtual std::shared_ptr<Channel> CreateChannel(
  63. const grpc::string& target, const ChannelArguments& args) = 0;
  64. };
  65. /// Options used to build SslCredentials.
  66. ///
  67. /// pem_roots_cert is the buffer containing the PEM encoding of the server root
  68. /// certificates. If this parameter is empty, the default roots will be used.
  69. /// pem_private_key is the buffer containing the PEM encoding of the client's
  70. /// private key. This parameter can be empty if the client does not have a
  71. /// private key.
  72. /// pem_cert_chain is the buffer containing the PEM encoding of the client's
  73. /// certificate chain. This parameter can be empty if the client does not have
  74. /// a certificate chain.
  75. struct SslCredentialsOptions {
  76. grpc::string pem_root_certs;
  77. grpc::string pem_private_key;
  78. grpc::string pem_cert_chain;
  79. };
  80. // Factories for building different types of Credentials The functions may
  81. // return empty shared_ptr when credentials cannot be created. If a
  82. // Credentials pointer is returned, it can still be invalid when used to create
  83. // a channel. A lame channel will be created then and all rpcs will fail on it.
  84. /// Builds credentials with reasonable defaults.
  85. std::shared_ptr<Credentials> GoogleDefaultCredentials();
  86. /// Builds SSL Credentials given SSL specific options
  87. std::shared_ptr<Credentials> SslCredentials(
  88. const SslCredentialsOptions& options);
  89. /// Builds credentials for use when running in GCE
  90. std::shared_ptr<Credentials> ComputeEngineCredentials();
  91. /// Builds service account credentials.
  92. /// json_key is the JSON key string containing the client's private key.
  93. /// scope is a space-delimited list of the requested permissions.
  94. /// token_lifetime_seconds is the lifetime in seconds of each token acquired
  95. /// through this service account credentials. It should be positive and should
  96. /// not exceed grpc_max_auth_token_lifetime or will be cropped to this value.
  97. std::shared_ptr<Credentials> ServiceAccountCredentials(
  98. const grpc::string& json_key, const grpc::string& scope,
  99. long token_lifetime_seconds);
  100. /// Builds Service Account JWT Access credentials.
  101. /// json_key is the JSON key string containing the client's private key.
  102. /// token_lifetime_seconds is the lifetime in seconds of each Json Web Token
  103. /// (JWT) created with this credentials. It should not exceed
  104. /// grpc_max_auth_token_lifetime or will be cropped to this value.
  105. std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials(
  106. const grpc::string& json_key, long token_lifetime_seconds);
  107. /// Builds refresh token credentials.
  108. /// json_refresh_token is the JSON string containing the refresh token along
  109. /// with a client_id and client_secret.
  110. std::shared_ptr<Credentials> RefreshTokenCredentials(
  111. const grpc::string& json_refresh_token);
  112. /// Builds access token credentials.
  113. /// access_token is an oauth2 access token that was fetched using an out of band
  114. /// mechanism.
  115. std::shared_ptr<Credentials> AccessTokenCredentials(
  116. const grpc::string& access_token);
  117. /// Builds IAM credentials.
  118. std::shared_ptr<Credentials> IAMCredentials(
  119. const grpc::string& authorization_token,
  120. const grpc::string& authority_selector);
  121. /// Combines two credentials objects into a composite credentials
  122. std::shared_ptr<Credentials> CompositeCredentials(
  123. const std::shared_ptr<Credentials>& creds1,
  124. const std::shared_ptr<Credentials>& creds2);
  125. /// Credentials for an unencrypted, unauthenticated channel
  126. std::shared_ptr<Credentials> InsecureCredentials();
  127. } // namespace grpc
  128. #endif // GRPCXX_CREDENTIALS_H