credentials.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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("createIam"),
  75. NanNew<FunctionTemplate>(CreateIam)->GetFunction());
  76. ctr->Set(NanNew("createInsecure"),
  77. NanNew<FunctionTemplate>(CreateInsecure)->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. const int argc = 1;
  88. Handle<Value> argv[argc] = {
  89. NanNew<External>(reinterpret_cast<void *>(credentials))};
  90. return NanEscapeScope(constructor->GetFunction()->NewInstance(argc, argv));
  91. }
  92. grpc_credentials *Credentials::GetWrappedCredentials() {
  93. return wrapped_credentials;
  94. }
  95. NAN_METHOD(Credentials::New) {
  96. NanScope();
  97. if (args.IsConstructCall()) {
  98. if (!args[0]->IsExternal()) {
  99. return NanThrowTypeError(
  100. "Credentials can only be created with the provided functions");
  101. }
  102. Handle<External> ext = args[0].As<External>();
  103. grpc_credentials *creds_value =
  104. reinterpret_cast<grpc_credentials *>(ext->Value());
  105. Credentials *credentials = new Credentials(creds_value);
  106. credentials->Wrap(args.This());
  107. NanReturnValue(args.This());
  108. } else {
  109. const int argc = 1;
  110. Local<Value> argv[argc] = {args[0]};
  111. NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv));
  112. }
  113. }
  114. NAN_METHOD(Credentials::CreateDefault) {
  115. NanScope();
  116. grpc_credentials *creds = grpc_google_default_credentials_create();
  117. if (creds == NULL) {
  118. NanReturnNull();
  119. }
  120. NanReturnValue(WrapStruct(creds));
  121. }
  122. NAN_METHOD(Credentials::CreateSsl) {
  123. NanScope();
  124. char *root_certs = NULL;
  125. grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
  126. if (::node::Buffer::HasInstance(args[0])) {
  127. root_certs = ::node::Buffer::Data(args[0]);
  128. } else if (!(args[0]->IsNull() || args[0]->IsUndefined())) {
  129. return NanThrowTypeError("createSsl's first argument must be a Buffer");
  130. }
  131. if (::node::Buffer::HasInstance(args[1])) {
  132. key_cert_pair.private_key = ::node::Buffer::Data(args[1]);
  133. } else if (!(args[1]->IsNull() || args[1]->IsUndefined())) {
  134. return NanThrowTypeError(
  135. "createSSl's second argument must be a Buffer if provided");
  136. }
  137. if (::node::Buffer::HasInstance(args[2])) {
  138. key_cert_pair.cert_chain = ::node::Buffer::Data(args[2]);
  139. } else if (!(args[2]->IsNull() || args[2]->IsUndefined())) {
  140. return NanThrowTypeError(
  141. "createSSl's third argument must be a Buffer if provided");
  142. }
  143. grpc_credentials *creds = grpc_ssl_credentials_create(
  144. root_certs, key_cert_pair.private_key == NULL ? NULL : &key_cert_pair,
  145. NULL);
  146. if (creds == NULL) {
  147. NanReturnNull();
  148. }
  149. NanReturnValue(WrapStruct(creds));
  150. }
  151. NAN_METHOD(Credentials::CreateComposite) {
  152. NanScope();
  153. if (!HasInstance(args[0])) {
  154. return NanThrowTypeError(
  155. "createComposite's first argument must be a Credentials object");
  156. }
  157. if (!HasInstance(args[1])) {
  158. return NanThrowTypeError(
  159. "createComposite's second argument must be a Credentials object");
  160. }
  161. Credentials *creds1 = ObjectWrap::Unwrap<Credentials>(args[0]->ToObject());
  162. Credentials *creds2 = ObjectWrap::Unwrap<Credentials>(args[1]->ToObject());
  163. grpc_credentials *creds = grpc_composite_credentials_create(
  164. creds1->wrapped_credentials, creds2->wrapped_credentials, NULL);
  165. if (creds == NULL) {
  166. NanReturnNull();
  167. }
  168. NanReturnValue(WrapStruct(creds));
  169. }
  170. NAN_METHOD(Credentials::CreateGce) {
  171. NanScope();
  172. grpc_credentials *creds = grpc_compute_engine_credentials_create(NULL);
  173. if (creds == NULL) {
  174. NanReturnNull();
  175. }
  176. NanReturnValue(WrapStruct(creds));
  177. }
  178. NAN_METHOD(Credentials::CreateIam) {
  179. NanScope();
  180. if (!args[0]->IsString()) {
  181. return NanThrowTypeError("createIam's first argument must be a string");
  182. }
  183. if (!args[1]->IsString()) {
  184. return NanThrowTypeError("createIam's second argument must be a string");
  185. }
  186. NanUtf8String auth_token(args[0]);
  187. NanUtf8String auth_selector(args[1]);
  188. grpc_credentials *creds =
  189. grpc_iam_credentials_create(*auth_token, *auth_selector, NULL);
  190. if (creds == NULL) {
  191. NanReturnNull();
  192. }
  193. NanReturnValue(WrapStruct(creds));
  194. }
  195. NAN_METHOD(Credentials::CreateInsecure) {
  196. NanScope();
  197. NanReturnValue(WrapStruct(NULL));
  198. }
  199. } // namespace node
  200. } // namespace grpc