secure_credentials.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H
  19. #define GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H
  20. #include <grpc/grpc_security.h>
  21. #include <grpcpp/security/credentials.h>
  22. #include <grpcpp/security/credentials_impl.h>
  23. #include <grpcpp/support/config.h>
  24. #include "src/core/lib/security/credentials/credentials.h"
  25. #include "src/cpp/server/thread_pool_interface.h"
  26. namespace grpc_impl {
  27. class Channel;
  28. class SecureChannelCredentials final : public ChannelCredentials {
  29. public:
  30. explicit SecureChannelCredentials(grpc_channel_credentials* c_creds);
  31. ~SecureChannelCredentials() {
  32. if (c_creds_ != nullptr) c_creds_->Unref();
  33. }
  34. grpc_channel_credentials* GetRawCreds() { return c_creds_; }
  35. std::shared_ptr<Channel> CreateChannelImpl(
  36. const grpc::string& target, const ChannelArguments& args) override;
  37. SecureChannelCredentials* AsSecureCredentials() override { return this; }
  38. private:
  39. std::shared_ptr<Channel> CreateChannelWithInterceptors(
  40. const grpc::string& target, const ChannelArguments& args,
  41. std::vector<std::unique_ptr<
  42. ::grpc::experimental::ClientInterceptorFactoryInterface>>
  43. interceptor_creators) override;
  44. grpc_channel_credentials* const c_creds_;
  45. };
  46. class SecureCallCredentials final : public CallCredentials {
  47. public:
  48. explicit SecureCallCredentials(grpc_call_credentials* c_creds);
  49. ~SecureCallCredentials() {
  50. if (c_creds_ != nullptr) c_creds_->Unref();
  51. }
  52. grpc_call_credentials* GetRawCreds() { return c_creds_; }
  53. bool ApplyToCall(grpc_call* call) override;
  54. SecureCallCredentials* AsSecureCredentials() override { return this; }
  55. private:
  56. grpc_call_credentials* const c_creds_;
  57. };
  58. namespace experimental {
  59. // Transforms C++ STS Credentials options to core options. The pointers of the
  60. // resulting core options point to the memory held by the C++ options so C++
  61. // options need to be kept alive until after the core credentials creation.
  62. grpc_sts_credentials_options StsCredentialsCppToCoreOptions(
  63. const StsCredentialsOptions& options);
  64. } // namespace experimental
  65. } // namespace grpc_impl
  66. namespace grpc {
  67. class MetadataCredentialsPluginWrapper final : private GrpcLibraryCodegen {
  68. public:
  69. static void Destroy(void* wrapper);
  70. static int GetMetadata(
  71. void* wrapper, grpc_auth_metadata_context context,
  72. grpc_credentials_plugin_metadata_cb cb, void* user_data,
  73. grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  74. size_t* num_creds_md, grpc_status_code* status,
  75. const char** error_details);
  76. explicit MetadataCredentialsPluginWrapper(
  77. std::unique_ptr<MetadataCredentialsPlugin> plugin);
  78. private:
  79. void InvokePlugin(
  80. grpc_auth_metadata_context context,
  81. grpc_credentials_plugin_metadata_cb cb, void* user_data,
  82. grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  83. size_t* num_creds_md, grpc_status_code* status_code,
  84. const char** error_details);
  85. std::unique_ptr<ThreadPoolInterface> thread_pool_;
  86. std::unique_ptr<MetadataCredentialsPlugin> plugin_;
  87. };
  88. } // namespace grpc
  89. #endif // GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H