secure_server_credentials.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_SERVER_SECURE_SERVER_CREDENTIALS_H
  19. #define GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H
  20. #include <memory>
  21. #include <grpcpp/security/server_credentials.h>
  22. #include <grpc/grpc_security.h>
  23. #include "src/cpp/server/thread_pool_interface.h"
  24. namespace grpc_impl {
  25. class SecureServerCredentials;
  26. } // namespace grpc_impl
  27. namespace grpc {
  28. typedef ::grpc_impl::SecureServerCredentials SecureServerCredentials;
  29. class AuthMetadataProcessorAyncWrapper final {
  30. public:
  31. static void Destroy(void* wrapper);
  32. static void Process(void* wrapper, grpc_auth_context* context,
  33. const grpc_metadata* md, size_t num_md,
  34. grpc_process_auth_metadata_done_cb cb, void* user_data);
  35. AuthMetadataProcessorAyncWrapper(
  36. const std::shared_ptr<AuthMetadataProcessor>& processor)
  37. : processor_(processor) {
  38. if (processor && processor->IsBlocking()) {
  39. thread_pool_.reset(CreateDefaultThreadPool());
  40. }
  41. }
  42. private:
  43. void InvokeProcessor(grpc_auth_context* context, const grpc_metadata* md,
  44. size_t num_md, grpc_process_auth_metadata_done_cb cb,
  45. void* user_data);
  46. std::unique_ptr<ThreadPoolInterface> thread_pool_;
  47. std::shared_ptr<AuthMetadataProcessor> processor_;
  48. };
  49. } // namespace grpc
  50. namespace grpc_impl {
  51. class SecureServerCredentials final : public ServerCredentials {
  52. public:
  53. explicit SecureServerCredentials(grpc_server_credentials* creds)
  54. : creds_(creds) {}
  55. ~SecureServerCredentials() override {
  56. grpc_server_credentials_release(creds_);
  57. }
  58. int AddPortToServer(const grpc::string& addr, grpc_server* server) override;
  59. void SetAuthMetadataProcessor(
  60. const std::shared_ptr<grpc::AuthMetadataProcessor>& processor) override;
  61. private:
  62. grpc_server_credentials* creds_;
  63. std::unique_ptr<grpc::AuthMetadataProcessorAyncWrapper> processor_;
  64. };
  65. } // namespace grpc_impl
  66. #endif // GRPC_INTERNAL_CPP_SERVER_SECURE_SERVER_CREDENTIALS_H