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 19 #ifndef GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H 20 #define GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H 21 22 #include <grpc/grpc_security.h> 23 24 #include <grpcpp/security/credentials.h> 25 #include <grpcpp/security/tls_credentials_options.h> 26 #include <grpcpp/support/config.h> 27 28 #include "absl/strings/str_cat.h" 29 // TODO(yashykt): We shouldn't be including "src/core" headers. 30 #include "src/core/lib/security/credentials/credentials.h" 31 #include "src/cpp/server/thread_pool_interface.h" 32 33 namespace grpc { 34 35 class Channel; 36 37 class SecureChannelCredentials final : public ChannelCredentials { 38 public: 39 explicit SecureChannelCredentials(grpc_channel_credentials* c_creds); ~SecureChannelCredentials()40 ~SecureChannelCredentials() override { 41 if (c_creds_ != nullptr) c_creds_->Unref(); 42 } GetRawCreds()43 grpc_channel_credentials* GetRawCreds() { return c_creds_; } 44 45 std::shared_ptr<Channel> CreateChannelImpl( 46 const std::string& target, const ChannelArguments& args) override; 47 AsSecureCredentials()48 SecureChannelCredentials* AsSecureCredentials() override { return this; } 49 50 private: 51 std::shared_ptr<Channel> CreateChannelWithInterceptors( 52 const std::string& target, const ChannelArguments& args, 53 std::vector<std::unique_ptr< 54 ::grpc::experimental::ClientInterceptorFactoryInterface>> 55 interceptor_creators) override; 56 grpc_channel_credentials* const c_creds_; 57 }; 58 59 class SecureCallCredentials final : public CallCredentials { 60 public: 61 explicit SecureCallCredentials(grpc_call_credentials* c_creds); ~SecureCallCredentials()62 ~SecureCallCredentials() override { 63 if (c_creds_ != nullptr) c_creds_->Unref(); 64 } GetRawCreds()65 grpc_call_credentials* GetRawCreds() { return c_creds_; } 66 67 bool ApplyToCall(grpc_call* call) override; AsSecureCredentials()68 SecureCallCredentials* AsSecureCredentials() override { return this; } DebugString()69 std::string DebugString() override { 70 return absl::StrCat("SecureCallCredentials{", 71 std::string(c_creds_->debug_string()), "}"); 72 } 73 74 private: 75 grpc_call_credentials* const c_creds_; 76 }; 77 78 namespace internal { 79 80 std::shared_ptr<ChannelCredentials> WrapChannelCredentials( 81 grpc_channel_credentials* creds); 82 83 } // namespace internal 84 85 namespace experimental { 86 87 // Transforms C++ STS Credentials options to core options. The pointers of the 88 // resulting core options point to the memory held by the C++ options so C++ 89 // options need to be kept alive until after the core credentials creation. 90 grpc_sts_credentials_options StsCredentialsCppToCoreOptions( 91 const StsCredentialsOptions& options); 92 93 } // namespace experimental 94 95 class MetadataCredentialsPluginWrapper final : private GrpcLibraryCodegen { 96 public: 97 static void Destroy(void* wrapper); 98 static int GetMetadata( 99 void* wrapper, grpc_auth_metadata_context context, 100 grpc_credentials_plugin_metadata_cb cb, void* user_data, 101 grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], 102 size_t* num_creds_md, grpc_status_code* status, 103 const char** error_details); 104 static char* DebugString(void* wrapper); 105 106 explicit MetadataCredentialsPluginWrapper( 107 std::unique_ptr<MetadataCredentialsPlugin> plugin); 108 109 private: 110 void InvokePlugin( 111 grpc_auth_metadata_context context, 112 grpc_credentials_plugin_metadata_cb cb, void* user_data, 113 grpc_metadata creds_md[GRPC_METADATA_CREDENTIALS_PLUGIN_SYNC_MAX], 114 size_t* num_creds_md, grpc_status_code* status_code, 115 const char** error_details); 116 std::unique_ptr<ThreadPoolInterface> thread_pool_; 117 std::unique_ptr<MetadataCredentialsPlugin> plugin_; 118 }; 119 120 } // namespace grpc 121 122 #endif // GRPC_INTERNAL_CPP_CLIENT_SECURE_CREDENTIALS_H 123