Skip to content

Commit fd4b852

Browse files
committedJul 8, 2018
fix(context): add NewInstance methods, and make getters safer
1 parent 274cad6 commit fd4b852

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed
 

‎src/kerberos_context.cc

+39-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Nan::Persistent<v8::Function> KerberosClientContext::constructor;
44
NAN_MODULE_INIT(KerberosClientContext::Init) {
5-
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
5+
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>();
66
tpl->SetClassName(Nan::New("KerberosClientContext").ToLocalChecked());
77

88
v8::Local<v8::ObjectTemplate> itpl = tpl->InstanceTemplate();
@@ -16,6 +16,15 @@ NAN_MODULE_INIT(KerberosClientContext::Init) {
1616
Nan::Set(target, Nan::New("KerberosClientContext").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
1717
}
1818

19+
v8::Local<v8::Object> KerberosClientContext::NewInstance(gss_client_state* state) {
20+
Nan::EscapableHandleScope scope;
21+
v8::Local<v8::Function> ctor = Nan::New<v8::Function>(KerberosClientContext::constructor);
22+
v8::Local<v8::Object> object = Nan::NewInstance(ctor).ToLocalChecked();
23+
KerberosClientContext *class_instance = new KerberosClientContext(state);
24+
class_instance->Wrap(object);
25+
return scope.Escape(object);
26+
}
27+
1928
KerberosClientContext::KerberosClientContext(gss_client_state* state)
2029
: _state(state)
2130
{}
@@ -29,39 +38,52 @@ NAN_GETTER(KerberosClientContext::UserNameGetter) {
2938
KerberosClientContext* context =
3039
Nan::ObjectWrap::Unwrap<KerberosClientContext>(info.Holder());
3140

32-
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
41+
(context->_state->username == NULL) ?
42+
info.GetReturnValue().Set(Nan::Null()) :
43+
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
3344
}
3445

3546
NAN_GETTER(KerberosClientContext::ResponseGetter) {
3647
KerberosClientContext* context =
3748
Nan::ObjectWrap::Unwrap<KerberosClientContext>(info.Holder());
3849

39-
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
50+
(context->_state->response == NULL) ?
51+
info.GetReturnValue().Set(Nan::Null()) :
52+
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
4053
}
4154

4255
NAN_GETTER(KerberosClientContext::ResponseConfGetter) {
4356
KerberosClientContext* context =
4457
Nan::ObjectWrap::Unwrap<KerberosClientContext>(info.Holder());
4558

46-
info.GetReturnValue().Set(Nan::New(context->_state->responseConf).ToLocalChecked());
59+
info.GetReturnValue().Set(Nan::New(context->_state->responseConf));
4760
}
4861

4962
Nan::Persistent<v8::Function> KerberosServerContext::constructor;
50-
NAN_MODULE_INIT(KerberosClientContext::Init) {
51-
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
63+
NAN_MODULE_INIT(KerberosServerContext::Init) {
64+
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>();
5265
tpl->SetClassName(Nan::New("KerberosServerContext").ToLocalChecked());
5366

5467
v8::Local<v8::ObjectTemplate> itpl = tpl->InstanceTemplate();
5568
itpl->SetInternalFieldCount(1);
5669

5770
Nan::SetAccessor(itpl, Nan::New("username").ToLocalChecked(), KerberosServerContext::UserNameGetter);
5871
Nan::SetAccessor(itpl, Nan::New("response").ToLocalChecked(), KerberosServerContext::ResponseGetter);
59-
Nan::SetAccessor(itpl, Nan::New("targetName").ToLocalChecked(), KerberosServerContext::ResponseConfGetter);
72+
Nan::SetAccessor(itpl, Nan::New("targetName").ToLocalChecked(), KerberosServerContext::TargetNameGetter);
6073

6174
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
6275
Nan::Set(target, Nan::New("KerberosServerContext").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked());
6376
}
6477

78+
v8::Local<v8::Object> KerberosServerContext::NewInstance(gss_server_state* state) {
79+
Nan::EscapableHandleScope scope;
80+
v8::Local<v8::Function> ctor = Nan::New<v8::Function>(KerberosServerContext::constructor);
81+
v8::Local<v8::Object> object = Nan::NewInstance(ctor).ToLocalChecked();
82+
KerberosServerContext *class_instance = new KerberosServerContext(state);
83+
class_instance->Wrap(object);
84+
return scope.Escape(object);
85+
}
86+
6587
KerberosServerContext::KerberosServerContext(gss_server_state* state)
6688
: _state(state)
6789
{}
@@ -75,19 +97,25 @@ NAN_GETTER(KerberosServerContext::UserNameGetter) {
7597
KerberosServerContext* context =
7698
Nan::ObjectWrap::Unwrap<KerberosServerContext>(info.Holder());
7799

78-
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
100+
(context->_state->username == NULL) ?
101+
info.GetReturnValue().Set(Nan::Null()) :
102+
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
79103
}
80104

81105
NAN_GETTER(KerberosServerContext::ResponseGetter) {
82106
KerberosServerContext* context =
83107
Nan::ObjectWrap::Unwrap<KerberosServerContext>(info.Holder());
84108

85-
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
109+
(context->_state->response == NULL) ?
110+
info.GetReturnValue().Set(Nan::Null()) :
111+
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
86112
}
87113

88-
NAN_GETTER(KerberosServerContext::ResponseConfGetter) {
114+
NAN_GETTER(KerberosServerContext::TargetNameGetter) {
89115
KerberosServerContext* context =
90116
Nan::ObjectWrap::Unwrap<KerberosServerContext>(info.Holder());
91117

92-
info.GetReturnValue().Set(Nan::New(context->_state->responseConf).ToLocalChecked());
118+
(context->_state->targetname == NULL) ?
119+
info.GetReturnValue().Set(Nan::Null()) :
120+
info.GetReturnValue().Set(Nan::New(context->_state->targetname).ToLocalChecked());
93121
}

‎src/kerberos_context.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class KerberosClientContext : public Nan::ObjectWrap {
88
public:
99
static NAN_MODULE_INIT(Init);
10+
static v8::Local<v8::Object> NewInstance(gss_client_state* state);
1011

1112
private:
1213
static Nan::Persistent<v8::Function> constructor;
@@ -25,6 +26,7 @@ class KerberosClientContext : public Nan::ObjectWrap {
2526
class KerberosServerContext : public Nan::ObjectWrap {
2627
public:
2728
static NAN_MODULE_INIT(Init);
29+
static v8::Local<v8::Object> NewInstance(gss_server_state* state);
2830

2931
private:
3032
static Nan::Persistent<v8::Function> constructor;
@@ -38,6 +40,6 @@ class KerberosServerContext : public Nan::ObjectWrap {
3840
~KerberosServerContext();
3941

4042
gss_server_state* _state;
41-
}
43+
};
4244

4345
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.