call_credentials.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <node.h>
  34. #include "grpc/grpc.h"
  35. #include "grpc/grpc_security.h"
  36. #include "grpc/support/log.h"
  37. #include "call_credentials.h"
  38. #include "call.h"
  39. namespace grpc {
  40. namespace node {
  41. using Nan::Callback;
  42. using Nan::EscapableHandleScope;
  43. using Nan::HandleScope;
  44. using Nan::Maybe;
  45. using Nan::MaybeLocal;
  46. using Nan::ObjectWrap;
  47. using Nan::Persistent;
  48. using Nan::Utf8String;
  49. using v8::Exception;
  50. using v8::External;
  51. using v8::Function;
  52. using v8::FunctionTemplate;
  53. using v8::Integer;
  54. using v8::Local;
  55. using v8::Object;
  56. using v8::ObjectTemplate;
  57. using v8::Value;
  58. Nan::Callback *CallCredentials::constructor;
  59. Persistent<FunctionTemplate> CallCredentials::fun_tpl;
  60. CallCredentials::CallCredentials(grpc_credentials *credentials)
  61. : wrapped_credentials(credentials) {}
  62. CallCredentials::~CallCredentials() {
  63. grpc_credentials_release(wrapped_credentials);
  64. }
  65. void CallCredentials::Init(Local<Object> exports) {
  66. HandleScope scope;
  67. Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
  68. tpl->SetClassName(Nan::New("CallCredentials").ToLocalChecked());
  69. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  70. Nan::SetPrototypeMethod(tpl, "compose", Compose);
  71. fun_tpl.Reset(tpl);
  72. Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
  73. Nan::Set(ctr, Nan::New("createFromPlugin").ToLocalChecked(),
  74. Nan::GetFunction(
  75. Nan::New<FunctionTemplate>(CreateFromPlugin)).ToLocalChecked());
  76. Nan::Set(exports, Nan::New("CallCredentials").ToLocalChecked(), ctr);
  77. constructor = new Nan::Callback(ctr);
  78. }
  79. bool CallCredentials::HasInstance(Local<Value> val) {
  80. HandleScope scope;
  81. return Nan::New(fun_tpl)->HasInstance(val);
  82. }
  83. Local<Value> CallCredentials::WrapStruct(grpc_credentials *credentials) {
  84. EscapableHandleScope scope;
  85. const int argc = 1;
  86. if (credentials == NULL) {
  87. return scope.Escape(Nan::Null());
  88. }
  89. Local<Value> argv[argc] = {
  90. Nan::New<External>(reinterpret_cast<void *>(credentials))};
  91. MaybeLocal<Object> maybe_instance = Nan::NewInstance(
  92. constructor->GetFunction(), argc, argv);
  93. if (maybe_instance.IsEmpty()) {
  94. return scope.Escape(Nan::Null());
  95. } else {
  96. return scope.Escape(maybe_instance.ToLocalChecked());
  97. }
  98. }
  99. grpc_credentials *CallCredentials::GetWrappedCredentials() {
  100. return wrapped_credentials;
  101. }
  102. NAN_METHOD(CallCredentials::New) {
  103. if (info.IsConstructCall()) {
  104. if (!info[0]->IsExternal()) {
  105. return Nan::ThrowTypeError(
  106. "CallCredentials can only be created with the provided functions");
  107. }
  108. Local<External> ext = info[0].As<External>();
  109. grpc_credentials *creds_value =
  110. reinterpret_cast<grpc_credentials *>(ext->Value());
  111. CallCredentials *credentials = new CallCredentials(creds_value);
  112. credentials->Wrap(info.This());
  113. info.GetReturnValue().Set(info.This());
  114. return;
  115. } else {
  116. // This should never be called directly
  117. return Nan::ThrowTypeError(
  118. "CallCredentials can only be created with the provided functions");
  119. }
  120. }
  121. NAN_METHOD(CallCredentials::Compose) {
  122. if (!CallCredentials::HasInstance(info.This())) {
  123. return Nan::ThrowTypeError(
  124. "compose can only be called on CallCredentials objects");
  125. }
  126. if (!CallCredentials::HasInstance(info[0])) {
  127. return Nan::ThrowTypeError(
  128. "compose's first argument must be a CallCredentials object");
  129. }
  130. CallCredentials *self = ObjectWrap::Unwrap<CallCredentials>(info.This());
  131. CallCredentials *other = ObjectWrap::Unwrap<CallCredentials>(
  132. Nan::To<Object>(info[0]).ToLocalChecked());
  133. grpc_credentials *creds = grpc_composite_credentials_create(
  134. self->wrapped_credentials, other->wrapped_credentials, NULL);
  135. info.GetReturnValue().Set(WrapStruct(creds));
  136. }
  137. NAN_METHOD(CallCredentials::CreateFromPlugin) {
  138. if (!info[0]->IsFunction()) {
  139. return Nan::ThrowTypeError(
  140. "createFromPlugin's argument must be a function");
  141. }
  142. grpc_metadata_credentials_plugin plugin;
  143. plugin_state *state = new plugin_state;
  144. state->callback = new Nan::Callback(info[0].As<Function>());
  145. plugin.get_metadata = plugin_get_metadata;
  146. plugin.destroy = plugin_destroy_state;
  147. plugin.state = reinterpret_cast<void*>(state);
  148. grpc_credentials *creds = grpc_metadata_credentials_create_from_plugin(plugin,
  149. NULL);
  150. info.GetReturnValue().Set(WrapStruct(creds));
  151. }
  152. NAN_METHOD(PluginCallback) {
  153. // Arguments: status code, error details, metadata
  154. if (!info[0]->IsUint32()) {
  155. return Nan::ThrowTypeError(
  156. "The callback's first argument must be a status code");
  157. }
  158. if (!info[1]->IsString()) {
  159. return Nan::ThrowTypeError(
  160. "The callback's second argument must be a string");
  161. }
  162. if (!info[2]->IsObject()) {
  163. return Nan::ThrowTypeError(
  164. "The callback's third argument must be an object");
  165. }
  166. shared_ptr<Resources> resources(new Resources);
  167. grpc_status_code code = static_cast<grpc_status_code>(
  168. Nan::To<uint32_t>(info[0]).FromJust());
  169. char *details = *Utf8String(info[1]);
  170. grpc_metadata_array array;
  171. if (!CreateMetadataArray(Nan::To<Object>(info[2]).ToLocalChecked(),
  172. &array, resources)){
  173. return Nan::ThrowError("Failed to parse metadata");
  174. }
  175. grpc_credentials_plugin_metadata_cb cb =
  176. reinterpret_cast<grpc_credentials_plugin_metadata_cb>(
  177. Nan::Get(info.Callee(),
  178. Nan::New("cb").ToLocalChecked()
  179. ).ToLocalChecked().As<External>()->Value());
  180. void *user_data =
  181. Nan::Get(info.Callee(),
  182. Nan::New("user_data").ToLocalChecked()
  183. ).ToLocalChecked().As<External>()->Value();
  184. cb(user_data, array.metadata, array.count, code, details);
  185. }
  186. NAUV_WORK_CB(SendPluginCallback) {
  187. Nan::HandleScope scope;
  188. plugin_callback_data *data = reinterpret_cast<plugin_callback_data*>(
  189. async->data);
  190. // Attach cb and user_data to plugin_callback so that it can access them later
  191. v8::Local<v8::Function> plugin_callback = Nan::GetFunction(
  192. Nan::New<v8::FunctionTemplate>(PluginCallback)).ToLocalChecked();
  193. Nan::Set(plugin_callback, Nan::New("cb").ToLocalChecked(),
  194. Nan::New<v8::External>(reinterpret_cast<void*>(data->cb)));
  195. Nan::Set(plugin_callback, Nan::New("user_data").ToLocalChecked(),
  196. Nan::New<v8::External>(data->user_data));
  197. const int argc = 2;
  198. v8::Local<v8::Value> argv[argc] = {
  199. Nan::New(data->service_url).ToLocalChecked(),
  200. plugin_callback
  201. };
  202. Nan::Callback *callback = data->state->callback;
  203. callback->Call(argc, argv);
  204. delete data;
  205. uv_unref((uv_handle_t *)async);
  206. uv_close((uv_handle_t *)async, (uv_close_cb)free);
  207. }
  208. void plugin_get_metadata(void *state, const char *service_url,
  209. grpc_credentials_plugin_metadata_cb cb,
  210. void *user_data) {
  211. uv_async_t *async = static_cast<uv_async_t*>(malloc(sizeof(uv_async_t)));
  212. uv_async_init(uv_default_loop(),
  213. async,
  214. SendPluginCallback);
  215. plugin_callback_data *data = new plugin_callback_data;
  216. data->state = reinterpret_cast<plugin_state*>(state);
  217. data->service_url = service_url;
  218. data->cb = cb;
  219. data->user_data = user_data;
  220. async->data = data;
  221. /* libuv says that it will coalesce calls to uv_async_send. If there is ever a
  222. * problem with a callback not getting called, that is probably the reason */
  223. uv_async_send(async);
  224. }
  225. void plugin_destroy_state(void *ptr) {
  226. plugin_state *state = reinterpret_cast<plugin_state *>(ptr);
  227. delete state->callback;
  228. }
  229. } // namespace node
  230. } // namespace grpc