credentials.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "credentials.h"
  38. namespace grpc {
  39. namespace node {
  40. using v8::Exception;
  41. using v8::External;
  42. using v8::Function;
  43. using v8::FunctionTemplate;
  44. using v8::Handle;
  45. using v8::HandleScope;
  46. using v8::Integer;
  47. using v8::Local;
  48. using v8::Object;
  49. using v8::ObjectTemplate;
  50. using v8::Persistent;
  51. using v8::Value;
  52. NanCallback *Credentials::constructor;
  53. Persistent<FunctionTemplate> Credentials::fun_tpl;
  54. Credentials::Credentials(grpc_credentials *credentials)
  55. : wrapped_credentials(credentials) {}
  56. Credentials::~Credentials() {
  57. grpc_credentials_release(wrapped_credentials);
  58. }
  59. void Credentials::Init(Handle<Object> exports) {
  60. NanScope();
  61. Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
  62. tpl->SetClassName(NanNew("Credentials"));
  63. tpl->InstanceTemplate()->SetInternalFieldCount(1);
  64. NanAssignPersistent(fun_tpl, tpl);
  65. Handle<Function> ctr = tpl->GetFunction();
  66. ctr->Set(NanNew("createDefault"),
  67. NanNew<FunctionTemplate>(CreateDefault)->GetFunction());
  68. ctr->Set(NanNew("createSsl"),
  69. NanNew<FunctionTemplate>(CreateSsl)->GetFunction());
  70. ctr->Set(NanNew("createComposite"),
  71. NanNew<FunctionTemplate>(CreateComposite)->GetFunction());
  72. ctr->Set(NanNew("createGce"),
  73. NanNew<FunctionTemplate>(CreateGce)->GetFunction());
  74. ctr->Set(NanNew("createFake"),
  75. NanNew<FunctionTemplate>(CreateFake)->GetFunction());
  76. ctr->Set(NanNew("createIam"),
  77. NanNew<FunctionTemplate>(CreateIam)->GetFunction());
  78. constructor = new NanCallback(ctr);
  79. exports->Set(NanNew("Credentials"), ctr);
  80. }
  81. bool Credentials::HasInstance(Handle<Value> val) {
  82. NanScope();
  83. return NanHasInstance(fun_tpl, val);
  84. }
  85. Handle<Value> Credentials::WrapStruct(grpc_credentials *credentials) {
  86. NanEscapableScope();
  87. if (credentials == NULL) {
  88. return NanEscapeScope(NanNull());
  89. }
  90. const int argc = 1;
  91. Handle<Value> argv[argc] = {
  92. NanNew<External>(reinterpret_cast<void *>(credentials))};
  93. return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
  94. }
  95. grpc_credentials *Credentials::GetWrappedCredentials() {
  96. return wrapped_credentials;
  97. }
  98. NAN_METHOD(Credentials::New) {
  99. NanScope();
  100. if (args.IsConstructCall()) {
  101. if (!args[0]->IsExternal()) {
  102. return NanThrowTypeError(
  103. "Credentials can only be created with the provided functions");
  104. }
  105. Handle<External> ext = args[0].As<External>();
  106. grpc_credentials *creds_value =
  107. reinterpret_cast<grpc_credentials *>(ext->Value());
  108. Credentials *credentials = new Credentials(creds_value);
  109. credentials->Wrap(args.This());
  110. NanReturnValue(args.This());
  111. } else {
  112. const int argc = 1;
  113. Local<Value> argv[argc] = {args[0]};
  114. NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  115. }
  116. }
  117. NAN_METHOD(Credentials::CreateDefault) {
  118. NanScope();
  119. NanReturnValue(WrapStruct(grpc_google_default_credentials_create()));
  120. }
  121. NAN_METHOD(Credentials::CreateSsl) {
  122. NanScope();
  123. char *root_certs = NULL;
  124. grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
  125. if (::node::Buffer::HasInstance(args[0])) {
  126. root_certs = ::node::Buffer::Data(args[0]);
  127. } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
  128. return NanThrowTypeError("createSsl's first argument must be a Buffer");
  129. }
  130. if (::node::Buffer::HasInstance(args[1])) {
  131. key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
  132. } else if (!(args[1]->IsNull() || args[1]->IsUndefined())) {
  133. return NanThrowTypeError(
  134. "createSSl's second argument must be a Buffer if provided");
  135. }
  136. if (::node::Buffer::HasInstance(args[2])) {
  137. key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
  138. } else if (!(args[2]->IsNull() || args[2]->IsUndefined())) {
  139. return NanThrowTypeError(
  140. "createSSl's third argument must be a Buffer if provided");
  141. }
  142. NanReturnValue(WrapStruct(grpc_ssl_credentials_create(
  143. root_certs, key_cert_pair.private_key == NULL ? NULL : &key_cert_pair)));
  144. }
  145. NAN_METHOD(Credentials::CreateComposite) {
  146. NanScope();
  147. if (!HasInstance(args[0])) {
  148. return NanThrowTypeError(
  149. "createComposite's first argument must be a Credentials object");
  150. }
  151. if (!HasInstance(args[1])) {
  152. return NanThrowTypeError(
  153. "createComposite's second argument must be a Credentials object");
  154. }
  155. Credentials *creds1 = ObjectWrap::Unwrap<Credentials>(args[0]->ToObject());
  156. Credentials *creds2 = ObjectWrap::Unwrap<Credentials>(args[1]->ToObject());
  157. NanReturnValue(WrapStruct(grpc_composite_credentials_create(
  158. creds1->wrapped_credentials, creds2->wrapped_credentials)));
  159. }
  160. NAN_METHOD(Credentials::CreateGce) {
  161. NanScope();
  162. NanReturnValue(WrapStruct(grpc_compute_engine_credentials_create()));
  163. }
  164. NAN_METHOD(Credentials::CreateFake) {
  165. NanScope();
  166. NanReturnValue(WrapStruct(grpc_fake_transport_security_credentials_create()));
  167. }
  168. NAN_METHOD(Credentials::CreateIam) {
  169. NanScope();
  170. if (!args[0]->IsString()) {
  171. return NanThrowTypeError("createIam's first argument must be a string");
  172. }
  173. if (!args[1]->IsString()) {
  174. return NanThrowTypeError("createIam's second argument must be a string");
  175. }
  176. NanUtf8String auth_token(args[0]);
  177. NanUtf8String auth_selector(args[1]);
  178. NanReturnValue(
  179. WrapStruct(grpc_iam_credentials_create(*auth_token, *auth_selector)));
  180. }
  181. } // namespace node
  182. } // namespace grpc