channel_credentials.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "channel_credentials.h"
  38. #include "call_credentials.h"
  39. #include "call.h"
  40. namespace grpc {
  41. namespace node {
  42. using Nan::Callback;
  43. using Nan::EscapableHandleScope;
  44. using Nan::HandleScope;
  45. using Nan::Maybe;
  46. using Nan::MaybeLocal;
  47. using Nan::ObjectWrap;
  48. using Nan::Persistent;
  49. using Nan::Utf8String;
  50. using v8::Exception;
  51. using v8::External;
  52. using v8::Function;
  53. using v8::FunctionTemplate;
  54. using v8::Integer;
  55. using v8::Local;
  56. using v8::Object;
  57. using v8::ObjectTemplate;
  58. using v8::Value;
  59. Nan::Callback *ChannelCredentials::constructor;
  60. Persistent<FunctionTemplate> ChannelCredentials::fun_tpl;
  61. ChannelCredentials::ChannelCredentials(grpc_credentials *credentials)
  62. : wrapped_credentials(credentials) {}
  63. ChannelCredentials::~ChannelCredentials() {
  64. grpc_credentials_release(wrapped_credentials);
  65. }
  66. void ChannelCredentials::Init(Local<Object> exports) {
  67. HandleScope scope;
  68. Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
  69. tpl->SetClassName(Nan::New("ChannelCredentials").ToLocalChecked());
  70. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  71. Nan::SetPrototypeMethod(tpl, "compose", Compose);
  72. fun_tpl.Reset(tpl);
  73. Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
  74. Nan::Set(ctr, Nan::New("createSsl").ToLocalChecked(),
  75. Nan::GetFunction(
  76. Nan::New<FunctionTemplate>(CreateSsl)).ToLocalChecked());
  77. Nan::Set(ctr, Nan::New("createInsecure").ToLocalChecked(),
  78. Nan::GetFunction(
  79. Nan::New<FunctionTemplate>(CreateInsecure)).ToLocalChecked());
  80. Nan::Set(exports, Nan::New("ChannelCredentials").ToLocalChecked(), ctr);
  81. constructor = new Nan::Callback(ctr);
  82. }
  83. bool ChannelCredentials::HasInstance(Local<Value> val) {
  84. HandleScope scope;
  85. return Nan::New(fun_tpl)->HasInstance(val);
  86. }
  87. Local<Value> ChannelCredentials::WrapStruct(grpc_credentials *credentials) {
  88. EscapableHandleScope scope;
  89. const int argc = 1;
  90. Local<Value> argv[argc] = {
  91. Nan::New<External>(reinterpret_cast<void *>(credentials))};
  92. MaybeLocal<Object> maybe_instance = Nan::NewInstance(
  93. constructor->GetFunction(), argc, argv);
  94. if (maybe_instance.IsEmpty()) {
  95. return scope.Escape(Nan::Null());
  96. } else {
  97. return scope.Escape(maybe_instance.ToLocalChecked());
  98. }
  99. }
  100. grpc_credentials *ChannelCredentials::GetWrappedCredentials() {
  101. return wrapped_credentials;
  102. }
  103. NAN_METHOD(ChannelCredentials::New) {
  104. if (info.IsConstructCall()) {
  105. if (!info[0]->IsExternal()) {
  106. return Nan::ThrowTypeError(
  107. "ChannelCredentials can only be created with the provided functions");
  108. }
  109. Local<External> ext = info[0].As<External>();
  110. grpc_credentials *creds_value =
  111. reinterpret_cast<grpc_credentials *>(ext->Value());
  112. ChannelCredentials *credentials = new ChannelCredentials(creds_value);
  113. credentials->Wrap(info.This());
  114. info.GetReturnValue().Set(info.This());
  115. return;
  116. } else {
  117. // This should never be called directly
  118. return Nan::ThrowTypeError(
  119. "ChannelCredentials can only be created with the provided functions");
  120. }
  121. }
  122. NAN_METHOD(ChannelCredentials::CreateSsl) {
  123. char *root_certs = NULL;
  124. grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
  125. if (::node::Buffer::HasInstance(info[0])) {
  126. root_certs = ::node::Buffer::Data(info[0]);
  127. } else if (!(info[0]->IsNull() || info[0]->IsUndefined())) {
  128. return Nan::ThrowTypeError("createSsl's first argument must be a Buffer");
  129. }
  130. if (::node::Buffer::HasInstance(info[1])) {
  131. key_cert_pair.private_key = ::node::Buffer::Data(info[1]);
  132. } else if (!(info[1]->IsNull() || info[1]->IsUndefined())) {
  133. return Nan::ThrowTypeError(
  134. "createSSl's second argument must be a Buffer if provided");
  135. }
  136. if (::node::Buffer::HasInstance(info[2])) {
  137. key_cert_pair.cert_chain = ::node::Buffer::Data(info[2]);
  138. } else if (!(info[2]->IsNull() || info[2]->IsUndefined())) {
  139. return Nan::ThrowTypeError(
  140. "createSSl's third argument must be a Buffer if provided");
  141. }
  142. grpc_credentials *creds = grpc_ssl_credentials_create(
  143. root_certs, key_cert_pair.private_key == NULL ? NULL : &key_cert_pair,
  144. NULL);
  145. if (creds == NULL) {
  146. info.GetReturnValue().SetNull();
  147. } else {
  148. info.GetReturnValue().Set(WrapStruct(creds));
  149. }
  150. }
  151. NAN_METHOD(ChannelCredentials::Compose) {
  152. if (!ChannelCredentials::HasInstance(info.This())) {
  153. return Nan::ThrowTypeError(
  154. "compose can only be called on ChannelCredentials objects");
  155. }
  156. if (!CallCredentials::HasInstance(info[0])) {
  157. return Nan::ThrowTypeError(
  158. "compose's first argument must be a CallCredentials object");
  159. }
  160. ChannelCredentials *self = ObjectWrap::Unwrap<ChannelCredentials>(
  161. info.This());
  162. if (self->wrapped_credentials == NULL) {
  163. return Nan::ThrowTypeError(
  164. "Cannot compose insecure credential");
  165. }
  166. CallCredentials *other = ObjectWrap::Unwrap<CallCredentials>(
  167. Nan::To<Object>(info[0]).ToLocalChecked());
  168. grpc_credentials *creds = grpc_composite_credentials_create(
  169. self->wrapped_credentials, other->GetWrappedCredentials(), NULL);
  170. if (creds == NULL) {
  171. info.GetReturnValue().SetNull();
  172. } else {
  173. info.GetReturnValue().Set(WrapStruct(creds));
  174. }
  175. }
  176. NAN_METHOD(ChannelCredentials::CreateInsecure) {
  177. info.GetReturnValue().Set(WrapStruct(NULL));
  178. }
  179. } // namespace node
  180. } // namespace grpc