Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: third_party/grpc/src/node/ext/channel_credentials.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 Persistent<FunctionTemplate> ChannelCredentials::fun_tpl;
67
68 ChannelCredentials::ChannelCredentials(grpc_channel_credentials *credentials)
69 : wrapped_credentials(credentials) {}
70
71 ChannelCredentials::~ChannelCredentials() {
72 grpc_channel_credentials_release(wrapped_credentials);
73 }
74
75 void ChannelCredentials::Init(Local<Object> exports) {
76 HandleScope scope;
77 Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
78 tpl->SetClassName(Nan::New("ChannelCredentials").ToLocalChecked());
79 tpl->InstanceTemplate()->SetInternalFieldCount(1);
80 Nan::SetPrototypeMethod(tpl, "compose", Compose);
81 fun_tpl.Reset(tpl);
82 Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked();
83 Nan::Set(ctr, Nan::New("createSsl").ToLocalChecked(),
84 Nan::GetFunction(
85 Nan::New<FunctionTemplate>(CreateSsl)).ToLocalChecked());
86 Nan::Set(ctr, Nan::New("createInsecure").ToLocalChecked(),
87 Nan::GetFunction(
88 Nan::New<FunctionTemplate>(CreateInsecure)).ToLocalChecked());
89 Nan::Set(exports, Nan::New("ChannelCredentials").ToLocalChecked(), ctr);
90 constructor = new Nan::Callback(ctr);
91 }
92
93 bool ChannelCredentials::HasInstance(Local<Value> val) {
94 HandleScope scope;
95 return Nan::New(fun_tpl)->HasInstance(val);
96 }
97
98 Local<Value> ChannelCredentials::WrapStruct(
99 grpc_channel_credentials *credentials) {
100 EscapableHandleScope scope;
101 const int argc = 1;
102 Local<Value> argv[argc] = {
103 Nan::New<External>(reinterpret_cast<void *>(credentials))};
104 MaybeLocal<Object> maybe_instance = Nan::NewInstance(
105 constructor->GetFunction(), argc, argv);
106 if (maybe_instance.IsEmpty()) {
107 return scope.Escape(Nan::Null());
108 } else {
109 return scope.Escape(maybe_instance.ToLocalChecked());
110 }
111 }
112
113 grpc_channel_credentials *ChannelCredentials::GetWrappedCredentials() {
114 return wrapped_credentials;
115 }
116
117 NAN_METHOD(ChannelCredentials::New) {
118 if (info.IsConstructCall()) {
119 if (!info[0]->IsExternal()) {
120 return Nan::ThrowTypeError(
121 "ChannelCredentials can only be created with the provided functions");
122 }
123 Local<External> ext = info[0].As<External>();
124 grpc_channel_credentials *creds_value =
125 reinterpret_cast<grpc_channel_credentials *>(ext->Value());
126 ChannelCredentials *credentials = new ChannelCredentials(creds_value);
127 credentials->Wrap(info.This());
128 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 NAN_METHOD(ChannelCredentials::CreateSsl) {
138 char *root_certs = NULL;
139 grpc_ssl_pem_key_cert_pair key_cert_pair = {NULL, NULL};
140 if (::node::Buffer::HasInstance(info[0])) {
141 root_certs = ::node::Buffer::Data(info[0]);
142 } else if (!(info[0]->IsNull() || info[0]->IsUndefined())) {
143 return Nan::ThrowTypeError("createSsl's first argument must be a Buffer");
144 }
145 if (::node::Buffer::HasInstance(info[1])) {
146 key_cert_pair.private_key = ::node::Buffer::Data(info[1]);
147 } 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 if (::node::Buffer::HasInstance(info[2])) {
152 key_cert_pair.cert_chain = ::node::Buffer::Data(info[2]);
153 } 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 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 NULL);
166 if (creds == NULL) {
167 info.GetReturnValue().SetNull();
168 } else {
169 info.GetReturnValue().Set(WrapStruct(creds));
170 }
171 }
172
173 NAN_METHOD(ChannelCredentials::Compose) {
174 if (!ChannelCredentials::HasInstance(info.This())) {
175 return Nan::ThrowTypeError(
176 "compose can only be called on ChannelCredentials objects");
177 }
178 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 info.This());
184 if (self->wrapped_credentials == NULL) {
185 return Nan::ThrowTypeError(
186 "Cannot compose insecure credential");
187 }
188 CallCredentials *other = ObjectWrap::Unwrap<CallCredentials>(
189 Nan::To<Object>(info[0]).ToLocalChecked());
190 grpc_channel_credentials *creds = grpc_composite_channel_credentials_create(
191 self->wrapped_credentials, other->GetWrappedCredentials(), NULL);
192 if (creds == NULL) {
193 info.GetReturnValue().SetNull();
194 } else {
195 info.GetReturnValue().Set(WrapStruct(creds));
196 }
197 }
198
199 NAN_METHOD(ChannelCredentials::CreateInsecure) {
200 info.GetReturnValue().Set(WrapStruct(NULL));
201 }
202
203 } // namespace node
204 } // namespace grpc
OLDNEW
« no previous file with comments | « third_party/grpc/src/node/ext/channel_credentials.h ('k') | third_party/grpc/src/node/ext/completion_queue_async_worker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698