secure_credentials.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <grpc++/security/credentials.h>
  22. #include <grpc++/support/config.h>
  23. #include "src/cpp/server/thread_pool_interface.h"
  24. namespace grpc {
  25. class SecureChannelCredentials final : public ChannelCredentials {
  26. public:
  27. explicit SecureChannelCredentials(grpc_channel_credentials* c_creds);
  28. ~SecureChannelCredentials() { grpc_channel_credentials_release(c_creds_); }
  29. grpc_channel_credentials* GetRawCreds() { return c_creds_; }
  30. std::shared_ptr<grpc::Channel> CreateChannel(
  31. const string& target, const grpc::ChannelArguments& args) override;
  32. SecureChannelCredentials* AsSecureCredentials() override { return this; }
  33. private:
  34. grpc_channel_credentials* const c_creds_;
  35. };
  36. class SecureCallCredentials final : public CallCredentials {
  37. public:
  38. explicit SecureCallCredentials(grpc_call_credentials* c_creds);
  39. ~SecureCallCredentials() { grpc_call_credentials_release(c_creds_); }
  40. grpc_call_credentials* GetRawCreds() { return c_creds_; }
  41. bool ApplyToCall(grpc_call* call) override;
  42. SecureCallCredentials* AsSecureCredentials() override { return this; }
  43. private:
  44. grpc_call_credentials* const c_creds_;
  45. };
  46. class MetadataCredentialsPluginWrapper final : private GrpcLibraryCodegen {
  47. public:
  48. static void Destroy(void* wrapper);
  49. static int GetMetadata(
  50. void* wrapper, grpc_auth_metadata_context context,
  51. grpc_credentials_plugin_metadata_cb cb, void* user_data,
  52. grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  53. size_t* num_creds_md, grpc_status_code* status,
  54. const char** error_details);
  55. explicit MetadataCredentialsPluginWrapper(
  56. std::unique_ptr<MetadataCredentialsPlugin> plugin);
  57. private:
  58. void InvokePlugin(
  59. grpc_auth_metadata_context context,
  60. grpc_credentials_plugin_metadata_cb cb, void* user_data,
  61. grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX],
  62. size_t* num_creds_md, grpc_status_code* status_code,
  63. const char** error_details);
  64. std::unique_ptr<ThreadPoolInterface> thread_pool_;
  65. std::unique_ptr<MetadataCredentialsPlugin> plugin_;
  66. };
  67. } // namespace grpc
  68. #endif // GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H