@azure/msal-node-extensions 1.0.0-alpha.23 → 1.0.0-alpha.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,10 +8,5 @@ export interface DpapiBindings{
8
8
  unprotectData(encryptData: Uint8Array, optionalEntropy: Uint8Array, scope: string): Uint8Array
9
9
  }
10
10
  /* eslint-disable-next-line @typescript-eslint/no-var-requires, no-var, import/no-commonjs */
11
- export var Dpapi: DpapiBindings = require("bindings")({
12
- bindings: "dpapi",
13
- userDefinedTries: [
14
- ["module_root", "node_modules", "@azure", "msal-node-extensions", "build", "Release", "bindings"],
15
- ]
16
- });
11
+ export var Dpapi: DpapiBindings = require("../build/Release/dpapi.node");
17
12
  export default Dpapi;
@@ -2,5 +2,6 @@
2
2
  * Copyright (c) Microsoft Corporation. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
+ #include <napi.h>
5
6
 
6
- void ProtectDataCommon(bool protect, Nan::NAN_METHOD_ARGS_TYPE info);
7
+ Napi::Value ProtectDataCommon(bool protect, const Napi::CallbackInfo& info);
@@ -3,17 +3,11 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- #include <node.h>
6
+ #include <napi.h>
7
7
 
8
- v8::Local<v8::String> CreateUtf8String(v8::Isolate* isolate, char* strData)
8
+ void ProtectDataCommon(bool protect, const Napi::CallbackInfo& info)
9
9
  {
10
- return v8::String::NewFromUtf8(isolate, strData, v8::NewStringType::kNormal).ToLocalChecked();
11
- }
12
-
13
- void ProtectDataCommon(bool protect, Nan::NAN_METHOD_ARGS_TYPE info)
14
- {
15
- v8::Isolate* isolate = info.GetIsolate();
10
+ Napi::Env env = info.Env();
16
11
 
17
- isolate->ThrowException(v8::Exception::Error(
18
- CreateUtf8String(isolate, "Data protection API is not available on macOs or Linux")));
12
+ throw Napi::Error::New(env, "Data protection API is not available on macOs or Linux");
19
13
  }
@@ -4,63 +4,58 @@
4
4
  */
5
5
  // Implementation referenced from https://github.com/bradhugh/node-dpapi
6
6
 
7
- #include <node.h>
8
- #include <nan.h>
7
+ #include <napi.h>
8
+ #include <uv.h>
9
9
  #include <Windows.h>
10
10
  #include <dpapi.h>
11
11
  #include <functional>
12
12
  #include <iostream>
13
13
  #include <string>
14
14
 
15
- v8::Local<v8::String> CreateUtf8String(v8::Isolate* isolate, char* strData)
15
+ Napi::Value ProtectDataCommon(bool protect, const Napi::CallbackInfo& info)
16
16
  {
17
- return v8::String::NewFromUtf8(isolate, strData, v8::NewStringType::kNormal).ToLocalChecked();
18
- }
19
-
20
- void ProtectDataCommon(bool protect, Nan::NAN_METHOD_ARGS_TYPE info)
21
- {
22
- v8::Isolate* isolate = info.GetIsolate();
17
+ Napi::Env env = info.Env();
23
18
 
24
19
  if (info.Length() != 3) {
25
- isolate->ThrowException(v8::Exception::RangeError(
26
- CreateUtf8String(isolate, "3 arguments are required")));
20
+ throw Napi::RangeError::New(env, "3 arguments are required");
27
21
  }
28
22
 
29
- if (info[0]->IsNullOrUndefined() || !info[0]->IsUint8Array())
23
+ if (info[0].IsNull() ||
24
+ info[0].IsUndefined() ||
25
+ !info[0].IsTypedArray() ||
26
+ info[0].As<Napi::TypedArray>().TypedArrayType() != napi_uint8_array)
30
27
  {
31
- isolate->ThrowException(v8::Exception::TypeError(
32
- CreateUtf8String(isolate, "First argument, data, must be a valid Uint8Array")));
28
+ throw Napi::TypeError::New(env, "First argument, data, must be a valid Uint8Array");
33
29
  }
34
30
 
35
- if (!info[1]->IsNull() && !info[1]->IsUint8Array())
31
+ if (!info[1].IsNull() &&
32
+ (!info[1].IsTypedArray() || info[1].As<Napi::TypedArray>().TypedArrayType() != napi_uint8_array))
36
33
  {
37
- isolate->ThrowException(v8::Exception::TypeError(
38
- CreateUtf8String(isolate, "Second argument, optionalEntropy, must be null or an ArrayBuffer")));
34
+ throw Napi::TypeError::New(env, "Second argument, optionalEntropy, must be null or an ArrayBuffer");
39
35
  }
40
36
 
41
- if (info[2]->IsNullOrUndefined() || !info[2]->IsString())
37
+ if (info[2].IsNull() || info[2].IsUndefined() || !info[2].IsString())
42
38
  {
43
- isolate->ThrowException(v8::Exception::TypeError(
44
- CreateUtf8String(isolate, "Third argument, scope, must be a string")));
39
+ throw Napi::TypeError::New(env, "Third argument, scope, must be a string");
45
40
  }
46
41
 
47
42
  DWORD flags = 0;
48
- v8::String::Utf8Value strData(isolate, info[2]);
49
- std::string scope(*strData);
43
+ Napi::String strData = info[2].As<Napi::String>();
44
+ std::string scope = strData.Utf8Value();
50
45
  if (stricmp(scope.c_str(), "LocalMachine") == 0)
51
46
  {
52
47
  flags = CRYPTPROTECT_LOCAL_MACHINE;
53
48
  }
54
49
 
55
- auto buffer = node::Buffer::Data(info[0]);
56
- auto len = node::Buffer::Length(info[0]);
50
+ auto buffer = info[0].As<Napi::Buffer<char>>().Data();
51
+ auto len = info[0].As<Napi::Buffer<char>>().ElementLength();
57
52
 
58
53
  DATA_BLOB entropyBlob;
59
54
  entropyBlob.pbData = nullptr;
60
- if (!info[1]->IsNull())
55
+ if (!info[1].IsNull())
61
56
  {
62
- entropyBlob.pbData = reinterpret_cast<BYTE*>(node::Buffer::Data(info[1]));
63
- entropyBlob.cbData = node::Buffer::Length(info[1]);
57
+ entropyBlob.pbData = reinterpret_cast<BYTE*>(info[1].As<Napi::Buffer<char>>().Data());
58
+ entropyBlob.cbData = info[1].As<Napi::Buffer<char>>().ElementLength();
64
59
  }
65
60
 
66
61
  DATA_BLOB dataIn;
@@ -100,15 +95,12 @@ void ProtectDataCommon(bool protect, Nan::NAN_METHOD_ARGS_TYPE info)
100
95
  {
101
96
  DWORD errorCode = GetLastError();
102
97
  std::string errorMessage = std::string("Encryption/Decryption failed. Error code: ") + std::to_string(errorCode);
103
- isolate->ThrowException(v8::Exception::Error(
104
- CreateUtf8String(isolate, &errorMessage[0])));
105
-
106
- return;
98
+ throw Napi::Error::New(env, &errorMessage[0]);
107
99
  }
108
100
 
109
101
  // Copy and free the buffer
110
- auto returnBuffer = Nan::CopyBuffer(reinterpret_cast<const char*>(dataOut.pbData), dataOut.cbData).ToLocalChecked();
102
+ Napi::Buffer<char> returnBuffer = Napi::Buffer<char>::Copy(env, reinterpret_cast<char*>(dataOut.pbData), dataOut.cbData);
111
103
  LocalFree(dataOut.pbData);
112
104
 
113
- info.GetReturnValue().Set(returnBuffer);
105
+ return returnBuffer;
114
106
  }
@@ -3,30 +3,28 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- #include <nan.h>
6
+ #include <napi.h>
7
+ #include <uv.h>
7
8
  #include "dpapi_addon.h"
8
9
 
9
- NAN_METHOD(protectData)
10
+ Napi::Value protectData(const Napi::CallbackInfo& info)
10
11
  {
11
- ProtectDataCommon(true, info);
12
+ return ProtectDataCommon(true, info);
12
13
  }
13
14
 
14
- NAN_METHOD(unprotectData)
15
+ Napi::Value unprotectData(const Napi::CallbackInfo& info)
15
16
  {
16
- ProtectDataCommon(false, info);
17
+ return ProtectDataCommon(false, info);
17
18
  }
18
19
 
19
- NAN_MODULE_INIT(init)
20
- {
21
- Nan::Set(
22
- target,
23
- Nan::New<v8::String>("protectData").ToLocalChecked(),
24
- Nan::GetFunction(Nan::New<v8::FunctionTemplate>(protectData)).ToLocalChecked());
20
+ Napi::Object init(Napi::Env env, Napi::Object exports) {
21
+ exports.Set(Napi::String::New(env, "protectData"),
22
+ Napi::Function::New(env, protectData));
25
23
 
26
- Nan::Set(
27
- target,
28
- Nan::New<v8::String>("unprotectData").ToLocalChecked(),
29
- Nan::GetFunction(Nan::New<v8::FunctionTemplate>(unprotectData)).ToLocalChecked());
24
+ exports.Set(Napi::String::New(env, "unprotectData"),
25
+ Napi::Function::New(env, unprotectData));
26
+
27
+ return exports;
30
28
  }
31
29
 
32
- NODE_MODULE(binding, init)
30
+ NODE_API_MODULE(dpapi, init)
@@ -6,7 +6,7 @@
6
6
  import { IPersistence } from "./IPersistence";
7
7
  import { FilePersistence } from "./FilePersistence";
8
8
  import { PersistenceError } from "../error/PersistenceError";
9
- import { Dpapi } from "../dpapi-addon/Dpapi";
9
+ import { Dpapi } from "../Dpapi";
10
10
  import { DataProtectionScope } from "./DataProtectionScope";
11
11
  import { Logger, LoggerOptions } from "@azure/msal-common";
12
12
  import { dirname } from "path";