Skip to content
Permalink
Browse files

fix(context): add `NewInstance` methods, and make getters safer

  • Loading branch information
mbroadst committed Jun 25, 2018
1 parent 274cad6 commit fd4b85227c20ab301b5cb6f1266748c6ed75d9d1
Showing with 42 additions and 12 deletions.
  1. +39 −11 src/kerberos_context.cc
  2. +3 −1 src/kerberos_context.h
@@ -2,7 +2,7 @@

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

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

v8::Local<v8::Object> KerberosClientContext::NewInstance(gss_client_state* state) {
Nan::EscapableHandleScope scope;
v8::Local<v8::Function> ctor = Nan::New<v8::Function>(KerberosClientContext::constructor);
v8::Local<v8::Object> object = Nan::NewInstance(ctor).ToLocalChecked();
KerberosClientContext *class_instance = new KerberosClientContext(state);
class_instance->Wrap(object);
return scope.Escape(object);
}

KerberosClientContext::KerberosClientContext(gss_client_state* state)
: _state(state)
{}
@@ -29,39 +38,52 @@ NAN_GETTER(KerberosClientContext::UserNameGetter) {
KerberosClientContext* context =
Nan::ObjectWrap::Unwrap<KerberosClientContext>(info.Holder());

info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
(context->_state->username == NULL) ?
info.GetReturnValue().Set(Nan::Null()) :
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
}

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

info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
(context->_state->response == NULL) ?
info.GetReturnValue().Set(Nan::Null()) :
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
}

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

info.GetReturnValue().Set(Nan::New(context->_state->responseConf).ToLocalChecked());
info.GetReturnValue().Set(Nan::New(context->_state->responseConf));
}

Nan::Persistent<v8::Function> KerberosServerContext::constructor;
NAN_MODULE_INIT(KerberosClientContext::Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
NAN_MODULE_INIT(KerberosServerContext::Init) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>();
tpl->SetClassName(Nan::New("KerberosServerContext").ToLocalChecked());

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

Nan::SetAccessor(itpl, Nan::New("username").ToLocalChecked(), KerberosServerContext::UserNameGetter);
Nan::SetAccessor(itpl, Nan::New("response").ToLocalChecked(), KerberosServerContext::ResponseGetter);
Nan::SetAccessor(itpl, Nan::New("targetName").ToLocalChecked(), KerberosServerContext::ResponseConfGetter);
Nan::SetAccessor(itpl, Nan::New("targetName").ToLocalChecked(), KerberosServerContext::TargetNameGetter);

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

v8::Local<v8::Object> KerberosServerContext::NewInstance(gss_server_state* state) {
Nan::EscapableHandleScope scope;
v8::Local<v8::Function> ctor = Nan::New<v8::Function>(KerberosServerContext::constructor);
v8::Local<v8::Object> object = Nan::NewInstance(ctor).ToLocalChecked();
KerberosServerContext *class_instance = new KerberosServerContext(state);
class_instance->Wrap(object);
return scope.Escape(object);
}

KerberosServerContext::KerberosServerContext(gss_server_state* state)
: _state(state)
{}
@@ -75,19 +97,25 @@ NAN_GETTER(KerberosServerContext::UserNameGetter) {
KerberosServerContext* context =
Nan::ObjectWrap::Unwrap<KerberosServerContext>(info.Holder());

info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
(context->_state->username == NULL) ?
info.GetReturnValue().Set(Nan::Null()) :
info.GetReturnValue().Set(Nan::New(context->_state->username).ToLocalChecked());
}

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

info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
(context->_state->response == NULL) ?
info.GetReturnValue().Set(Nan::Null()) :
info.GetReturnValue().Set(Nan::New(context->_state->response).ToLocalChecked());
}

NAN_GETTER(KerberosServerContext::ResponseConfGetter) {
NAN_GETTER(KerberosServerContext::TargetNameGetter) {
KerberosServerContext* context =
Nan::ObjectWrap::Unwrap<KerberosServerContext>(info.Holder());

info.GetReturnValue().Set(Nan::New(context->_state->responseConf).ToLocalChecked());
(context->_state->targetname == NULL) ?
info.GetReturnValue().Set(Nan::Null()) :
info.GetReturnValue().Set(Nan::New(context->_state->targetname).ToLocalChecked());
}
@@ -7,6 +7,7 @@
class KerberosClientContext : public Nan::ObjectWrap {
public:
static NAN_MODULE_INIT(Init);
static v8::Local<v8::Object> NewInstance(gss_client_state* state);

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

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

gss_server_state* _state;
}
};

#endif

0 comments on commit fd4b852

Please sign in to comment.
You can’t perform that action at this time.