Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow callers to specify the 'protect' flag. #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/kerberos.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ KerberosClient.prototype.step = defineOperation(KerberosClient.prototype.step, [
* @param {string} challenge The response returned after calling `unwrap`
* @param {object} [options] Optional settings
* @param {string} [options.user] The user to authorize
* @param {string} [options.protect] Indicates if the wrap should request message confidentiality
* @param {function} [callback]
* @return {Promise} returns Promise if no callback passed
*/
Expand Down
96 changes: 50 additions & 46 deletions src/kerberos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ struct InstanceData {
};

static constexpr napi_property_attributes writable_and_configurable =
static_cast<napi_property_attributes>(
static_cast<int>(napi_writable) | static_cast<int>(napi_configurable));
static_cast<napi_property_attributes>(static_cast<int>(napi_writable) |
static_cast<int>(napi_configurable));

inline String NewMaybeWideString(Env env, const char* str) {
return String::New(env, str);
}
#ifdef _WIN32
inline String NewMaybeWideString(Env env, const WCHAR* str) {
static_assert(sizeof(std::wstring::value_type) == sizeof(std::u16string::value_type),
"wstring and u16string have the same value type on Windows");
"wstring and u16string have the same value type on Windows");
std::wstring wstr(str);
std::u16string u16(wstr.begin(), wstr.end());
return String::New(env, u16);
}
#endif
}
} // namespace

std::string ToStringWithNonStringAsEmpty(Napi::Value value) {
if (!value.IsString()) {
Expand All @@ -37,19 +37,28 @@ std::string ToStringWithNonStringAsEmpty(Napi::Value value) {
return value.As<String>();
}

int KerberosClient::ParseWrapOptionsProtect(const Napi::Object& options) {
if (!options.Has("protect")) return 0;

if (!options.Get("protect").IsBoolean()) {
throw TypeError::New(options.Env(), "options.protect must be a boolean.");
}

bool protect = options.Get("protect");
return protect ? 1 : 0;
}

Function KerberosClient::Init(Napi::Env env) {
return
DefineClass(env,
"KerberosClient",
{
InstanceMethod("step", &KerberosClient::Step, writable_and_configurable),
InstanceMethod("wrap", &KerberosClient::WrapData, writable_and_configurable),
InstanceMethod("unwrap", &KerberosClient::UnwrapData, writable_and_configurable),
InstanceAccessor("username", &KerberosClient::UserNameGetter, nullptr),
InstanceAccessor("response", &KerberosClient::ResponseGetter, nullptr),
InstanceAccessor("responseConf", &KerberosClient::ResponseConfGetter, nullptr),
InstanceAccessor("contextComplete", &KerberosClient::ContextCompleteGetter, nullptr)
});
return DefineClass(
env,
"KerberosClient",
{InstanceMethod("step", &KerberosClient::Step, writable_and_configurable),
InstanceMethod("wrap", &KerberosClient::WrapData, writable_and_configurable),
InstanceMethod("unwrap", &KerberosClient::UnwrapData, writable_and_configurable),
InstanceAccessor("username", &KerberosClient::UserNameGetter, nullptr),
InstanceAccessor("response", &KerberosClient::ResponseGetter, nullptr),
InstanceAccessor("responseConf", &KerberosClient::ResponseConfGetter, nullptr),
InstanceAccessor("contextComplete", &KerberosClient::ContextCompleteGetter, nullptr)});
}

Object KerberosClient::NewInstance(Napi::Env env, std::shared_ptr<krb_client_state> state) {
Expand All @@ -60,8 +69,7 @@ Object KerberosClient::NewInstance(Napi::Env env, std::shared_ptr<krb_client_sta
return obj;
}

KerberosClient::KerberosClient(const CallbackInfo& info)
: ObjectWrap(info) {}
KerberosClient::KerberosClient(const CallbackInfo& info) : ObjectWrap(info) {}

std::shared_ptr<krb_client_state> KerberosClient::state() const {
return _state;
Expand Down Expand Up @@ -91,16 +99,14 @@ Value KerberosClient::ContextCompleteGetter(const CallbackInfo& info) {

/// KerberosServer
Function KerberosServer::Init(Napi::Env env) {
return
DefineClass(env,
"KerberosServer",
{
InstanceMethod("step", &KerberosServer::Step, writable_and_configurable),
InstanceAccessor("username", &KerberosServer::UserNameGetter, nullptr),
InstanceAccessor("response", &KerberosServer::ResponseGetter, nullptr),
InstanceAccessor("targetName", &KerberosServer::TargetNameGetter, nullptr),
InstanceAccessor("contextComplete", &KerberosServer::ContextCompleteGetter, nullptr)
});
return DefineClass(
env,
"KerberosServer",
{InstanceMethod("step", &KerberosServer::Step, writable_and_configurable),
InstanceAccessor("username", &KerberosServer::UserNameGetter, nullptr),
InstanceAccessor("response", &KerberosServer::ResponseGetter, nullptr),
InstanceAccessor("targetName", &KerberosServer::TargetNameGetter, nullptr),
InstanceAccessor("contextComplete", &KerberosServer::ContextCompleteGetter, nullptr)});
}

Object KerberosServer::NewInstance(Napi::Env env, std::shared_ptr<krb_server_state> state) {
Expand All @@ -111,8 +117,7 @@ Object KerberosServer::NewInstance(Napi::Env env, std::shared_ptr<krb_server_sta
return obj;
}

KerberosServer::KerberosServer(const CallbackInfo& info)
: ObjectWrap(info) {}
KerberosServer::KerberosServer(const CallbackInfo& info) : ObjectWrap(info) {}

std::shared_ptr<krb_server_state> KerberosServer::state() const {
return _state;
Expand Down Expand Up @@ -156,18 +161,19 @@ void TestMethod(const CallbackInfo& info) {
callback = info[3].As<Function>();
}

KerberosWorker::Run(callback, "kerberos:TestMethod", [=](KerberosWorker::SetOnFinishedHandler onFinished) {
return onFinished([=](KerberosWorker* worker) {
Napi::Env env = worker->Env();
if (shouldError) {
worker->Call(std::initializer_list<napi_value>
{ Error::New(env).Value(), env.Null() });
} else {
worker->Call(std::initializer_list<napi_value>
{ env.Null(), String::New(env, optionalString) });
}
KerberosWorker::Run(
callback, "kerberos:TestMethod", [=](KerberosWorker::SetOnFinishedHandler onFinished) {
return onFinished([=](KerberosWorker* worker) {
Napi::Env env = worker->Env();
if (shouldError) {
worker->Call(
std::initializer_list<napi_value>{Error::New(env).Value(), env.Null()});
} else {
worker->Call(std::initializer_list<napi_value>{
env.Null(), String::New(env, optionalString)});
}
});
});
});
}

static Object Init(Env env, Object exports) {
Expand All @@ -179,10 +185,8 @@ static Object Init(Env env, Object exports) {
Function KerberosServerCtor = KerberosServer::Init(env);
exports["KerberosClient"] = KerberosClientCtor;
exports["KerberosServer"] = KerberosServerCtor;
env.SetInstanceData(new InstanceData {
Reference<Function>::New(KerberosClientCtor, 1),
Reference<Function>::New(KerberosServerCtor, 1)
});
env.SetInstanceData(new InstanceData{Reference<Function>::New(KerberosClientCtor, 1),
Reference<Function>::New(KerberosServerCtor, 1)});
exports["initializeClient"] = Function::New(env, InitializeClient);
exports["initializeServer"] = Function::New(env, InitializeServer);
exports["principalDetails"] = Function::New(env, PrincipalDetails);
Expand All @@ -193,4 +197,4 @@ static Object Init(Env env, Object exports) {

NODE_API_MODULE(kerberos, Init)

}
} // namespace node_kerberos
5 changes: 3 additions & 2 deletions src/kerberos.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class KerberosClient : public Napi::ObjectWrap<KerberosClient> {
void UnwrapData(const Napi::CallbackInfo& info);
void WrapData(const Napi::CallbackInfo& info);

int ParseWrapOptionsProtect(const Napi::Object& options);

private:
friend class Napi::ObjectWrap<KerberosClient>;
explicit KerberosClient(const Napi::CallbackInfo& info);
Expand All @@ -71,7 +73,6 @@ void CheckPassword(const Napi::CallbackInfo& info);
void TestMethod(const Napi::CallbackInfo& info);

std::string ToStringWithNonStringAsEmpty(Napi::Value value);

}
} // namespace node_kerberos

#endif // KERBEROS_NATIVE_EXTENSION_H
Loading