diff options
author | 2015-11-19 21:45:52 -0800 | |
---|---|---|
committer | 2015-11-19 22:24:05 -0800 | |
commit | 114f39475fa6f28beb51f1a4265e377aa122fbdb (patch) | |
tree | 656d3748d121a981aa32aa7820e5fe6124de9cb1 /src/cpp | |
parent | caf9935e456436a54931bf8112c25515100ccd20 (diff) |
Cpp example of how the plugins can be done.
Diffstat (limited to 'src/cpp')
-rw-r--r-- | src/cpp/client/secure_credentials.cc | 21 | ||||
-rw-r--r-- | src/cpp/client/secure_credentials.h | 2 |
2 files changed, 16 insertions, 7 deletions
diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index bd68228460..6409323262 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -37,6 +37,7 @@ #include <grpc++/support/channel_arguments.h> #include "src/cpp/client/create_channel_internal.h" #include "src/cpp/client/secure_credentials.h" +#include "src/cpp/common/secure_auth_context.h" namespace grpc { @@ -171,18 +172,25 @@ void MetadataCredentialsPluginWrapper::GetMetadata( } if (w->plugin_->IsBlocking()) { w->thread_pool_->Add( - std::bind(&MetadataCredentialsPluginWrapper::InvokePlugin, w, - context.service_url, cb, user_data)); + std::bind(&MetadataCredentialsPluginWrapper::InvokePlugin, w, context, + cb, user_data)); } else { - w->InvokePlugin(context.service_url, cb, user_data); + w->InvokePlugin(context, cb, user_data); } } void MetadataCredentialsPluginWrapper::InvokePlugin( - const char* service_url, grpc_credentials_plugin_metadata_cb cb, + grpc_auth_metadata_context context, grpc_credentials_plugin_metadata_cb cb, void* user_data) { std::multimap<grpc::string, grpc::string> metadata; - Status status = plugin_->GetMetadata(service_url, &metadata); + + // const_cast is safe since the SecureAuthContext does not take owndership and + // the object is passed as a const ref to plugin_->GetMetadata. + SecureAuthContext cpp_channel_auth_context( + const_cast<grpc_auth_context*>(context.channel_auth_context), false); + + Status status = plugin_->GetMetadata(context.service_url, context.method_name, + cpp_channel_auth_context, &metadata); std::vector<grpc_metadata> md; for (auto it = metadata.begin(); it != metadata.end(); ++it) { grpc_metadata md_entry; @@ -204,11 +212,12 @@ MetadataCredentialsPluginWrapper::MetadataCredentialsPluginWrapper( std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin( std::unique_ptr<MetadataCredentialsPlugin> plugin) { GrpcLibrary init; // To call grpc_init(). + const char* type = plugin->GetType(); MetadataCredentialsPluginWrapper* wrapper = new MetadataCredentialsPluginWrapper(std::move(plugin)); grpc_metadata_credentials_plugin c_plugin = { MetadataCredentialsPluginWrapper::GetMetadata, - MetadataCredentialsPluginWrapper::Destroy, wrapper, ""}; + MetadataCredentialsPluginWrapper::Destroy, wrapper, type}; return WrapCallCredentials( grpc_metadata_credentials_create_from_plugin(c_plugin, nullptr)); } diff --git a/src/cpp/client/secure_credentials.h b/src/cpp/client/secure_credentials.h index b8fe075dc7..8e3c713a50 100644 --- a/src/cpp/client/secure_credentials.h +++ b/src/cpp/client/secure_credentials.h @@ -88,7 +88,7 @@ class MetadataCredentialsPluginWrapper GRPC_FINAL { std::unique_ptr<MetadataCredentialsPlugin> plugin); private: - void InvokePlugin(const char* service_url, + void InvokePlugin(grpc_auth_metadata_context context, grpc_credentials_plugin_metadata_cb cb, void* user_data); std::unique_ptr<ThreadPoolInterface> thread_pool_; std::unique_ptr<MetadataCredentialsPlugin> plugin_; |