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