win-sso 1.3.3 → 2.0.0-beta.1

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.
package/dist/win-sso.js DELETED
@@ -1,159 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WinSso = void 0;
7
- const debug_logger_1 = require("./utils/debug.logger");
8
- const path_1 = __importDefault(require("path"));
9
- let winSsoAddon;
10
- try {
11
- // eslint-disable-next-line @typescript-eslint/no-require-imports
12
- winSsoAddon = require("node-gyp-build")(path_1.default.join(__dirname, ".."));
13
- (0, debug_logger_1.debug)("Loaded win-sso native module");
14
- }
15
- catch (_a) {
16
- (0, debug_logger_1.debug)("Could not load win-sso native module");
17
- }
18
- /**
19
- * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
20
- */
21
- class WinSso {
22
- /**
23
- * Creates an authentication context for SSO.
24
- * This allocates memory buffers, the freeAuthContext method should be called
25
- * to free them (on error or after authentication is no longer needed)
26
- * @param securityPackage The name of the security package (NTLM or Negotiate)
27
- * @param targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
28
- * @param peerCert The certificate of the target server
29
- * (optional, for HTTPS channel binding)
30
- * @param flags Flags to set in the authentication context
31
- * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
32
- * (optional, allows customizing security features)
33
- */
34
- constructor(securityPackage, targetHost, peerCert, flags) {
35
- this.securityPackage = securityPackage;
36
- let applicationData;
37
- if (!targetHost) {
38
- targetHost = "";
39
- }
40
- if (peerCert) {
41
- applicationData = this.getChannelBindingsApplicationData(peerCert);
42
- }
43
- else {
44
- applicationData = Buffer.alloc(0);
45
- }
46
- this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, flags);
47
- }
48
- /**
49
- * Retrieves the username of the logged in user
50
- * @returns user name including domain
51
- */
52
- static getLogonUserName() {
53
- return winSsoAddon.getLogonUserName();
54
- }
55
- /**
56
- * Transforms target TLS certificate into a channel binding application data buffer
57
- * @param peerCert Target TLS certificate
58
- * @returns Application data buffer
59
- */
60
- getChannelBindingsApplicationData(peerCert) {
61
- const hash = peerCert.fingerprint256.replace(/:/g, "");
62
- const hashBuf = Buffer.from(hash, "hex");
63
- const tlsServerEndPoint = "tls-server-end-point:";
64
- const applicationDataBuffer = Buffer.alloc(tlsServerEndPoint.length + hashBuf.length);
65
- applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
66
- hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
67
- return applicationDataBuffer;
68
- }
69
- /**
70
- * Releases all allocated resources for the authorization context.
71
- * Should be called when the context is no longer required, such as when the
72
- * socket was closed.
73
- */
74
- freeAuthContext() {
75
- winSsoAddon.freeAuthContext(this.authContextId);
76
- }
77
- /**
78
- * Creates an authentication request token
79
- * @returns Raw token buffer
80
- */
81
- createAuthRequest() {
82
- const token = winSsoAddon.createAuthRequest(this.authContextId);
83
- (0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication request token", token.toString("base64"));
84
- return token;
85
- }
86
- /**
87
- * Creates an authentication request header
88
- * @returns The www-authenticate header
89
- */
90
- createAuthRequestHeader() {
91
- const header = this.securityPackage + " " + this.createAuthRequest().toString("base64");
92
- return header;
93
- }
94
- /**
95
- * Creates an authentication response token
96
- * @param inTokenHeader The www-authentication header received from the target
97
- * in response to the authentication request
98
- * @returns Raw token buffer. May be empty if Negotiate handshake is complete.
99
- */
100
- createAuthResponse(inTokenHeader) {
101
- (0, debug_logger_1.debug)("Received www-authentication response", inTokenHeader);
102
- const packageMatch = new RegExp("^" + this.securityPackage + "\\s([^,\\s]+)").exec(inTokenHeader);
103
- if (!packageMatch) {
104
- throw new Error("Invalid input token, missing " +
105
- this.securityPackage +
106
- " prefix: " +
107
- inTokenHeader);
108
- }
109
- const inToken = Buffer.from(packageMatch[1], "base64");
110
- try {
111
- const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
112
- if (token.length > 0) {
113
- (0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication response token", token.toString("base64"));
114
- }
115
- else {
116
- (0, debug_logger_1.debug)("No response token, authentication complete");
117
- }
118
- return token;
119
- }
120
- catch (err) {
121
- if (err.message ===
122
- "Could not init security context. Result: -2146893054") {
123
- // If incoming token is for NTLMv1, this error can occur when
124
- // LMCompatibilityLevel prevents the client to send NTLMv1 messages
125
- if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
126
- throw new Error("Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
127
- "it is likely that the client is prevented from sending such messages. " +
128
- "Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)");
129
- }
130
- }
131
- throw err;
132
- }
133
- }
134
- isNtlmV1(type2message) {
135
- if (type2message.length >= 24) {
136
- const inTokenFlags = type2message.readInt32BE(20);
137
- if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
138
- return true;
139
- }
140
- }
141
- return false;
142
- }
143
- /**
144
- * Creates an authentication response header
145
- * @param inTokenHeader The www-authentication header received from the target
146
- * in response to the authentication request
147
- * @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
148
- */
149
- createAuthResponseHeader(inTokenHeader) {
150
- const tokenBuffer = this.createAuthResponse(inTokenHeader);
151
- if (tokenBuffer.length == 0) {
152
- return "";
153
- }
154
- const header = this.securityPackage + " " + tokenBuffer.toString("base64");
155
- return header;
156
- }
157
- }
158
- exports.WinSso = WinSso;
159
- WinSso.NEGOTIATE_NTLM2_KEY = 1 << 19;
package/install_env.js DELETED
@@ -1,12 +0,0 @@
1
- const os = require('os');
2
- const spawn = require('cross-spawn');
3
-
4
- if (os.platform() === 'win32') {
5
- console.log('Windows OS detected. Building native module')
6
- spawn.sync('npm', ['run', 'native_build'], {
7
- input: 'Windows OS detected. Build native module.',
8
- stdio: 'inherit'
9
- });
10
- } else {
11
- console.log('Unsupported OS detected. Native module requires Windows OS. Aborting native module build.');
12
- }
@@ -1,12 +0,0 @@
1
- const os = require('os');
2
- const spawn = require('cross-spawn');
3
-
4
- if (os.platform() === 'win32') {
5
- console.log('Windows OS detected. Prebuilding native modules')
6
- spawn.sync('npm', ['run', 'native_prebuildify'], {
7
- input: 'Windows OS detected. Prebuilding native module.',
8
- stdio: 'inherit'
9
- });
10
- } else {
11
- console.log('Unsupported OS detected. Native module requires Windows OS. Aborting prebuildify.');
12
- }
Binary file
Binary file
@@ -1,162 +0,0 @@
1
- #include "auth-context.hh"
2
- #include "secur32-facade.hh"
3
- #include "exception-handler.hh"
4
-
5
- AuthContext::AuthContext() {
6
- credHandle = {};
7
- lifeTime = {};
8
- ctxHandle = {};
9
- flags = 0;
10
- maxTokenLength = 0;
11
- outToken = nullptr;
12
- outTokenLength = 0;
13
- credHandleAllocated = false;
14
- ctxHandleAllocated = false;
15
- channelBindings = {};
16
- channelBindingsLength = 0;
17
- targetHostnameSpn = nullptr;
18
- };
19
-
20
- AuthContext::~AuthContext() {
21
- // Napi::Env is not available in destructor, but we can still do cleanup without
22
- // reporting JS exceptions to Node
23
- Cleanup(0);
24
- }
25
-
26
- void AuthContext::Cleanup(Napi::Env* env) {
27
- if (credHandleAllocated) {
28
- Secur32Facade::FreeCredentialsHandle(&credHandle, env);
29
- credHandleAllocated = false;
30
- }
31
- if (ctxHandleAllocated) {
32
- Secur32Facade::FreeContextHandle(&ctxHandle, env);
33
- ctxHandleAllocated = false;
34
- }
35
- if (outToken) {
36
- delete outToken;
37
- outToken = nullptr;
38
- }
39
- if (targetHostnameSpn) {
40
- delete targetHostnameSpn;
41
- targetHostnameSpn = nullptr;;
42
- }
43
- }
44
-
45
- bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, unsigned long flags, Napi::Env* env) {
46
- this->flags = flags;
47
- auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
48
- packageName[packageNameLen] = '\0';
49
- targetHostname = *targetHost;
50
- if (targetHostname.length() > 0) {
51
- int len = targetHostname.length() + 5 + 1;
52
- targetHostnameSpn = new char[len];
53
- strncpy(targetHostnameSpn, ("HTTP/" + targetHostname).c_str(), len);
54
- }
55
- maxTokenLength = Secur32Facade::GetMaxTokenLength(packageName, env);
56
- SetupChannelBindings(applicationDataBuffer);
57
- Secur32Facade::AcquireCredentialsHandle(packageName, &credHandle, &lifeTime, env);
58
- if (env->IsExceptionPending()) {
59
- Cleanup(env);
60
- return false;
61
- }
62
- credHandleAllocated = true;
63
- return true;
64
- }
65
-
66
- void AuthContext::SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer) {
67
- if (applicationDataBuffer.Length() > 0) {
68
- channelBindings.secChannelBindings.dwInitiatorAddrType = 0;
69
- channelBindings.secChannelBindings.cbInitiatorLength = 0;
70
- channelBindings.secChannelBindings.dwInitiatorOffset = 0;
71
- channelBindings.secChannelBindings.dwAcceptorAddrType = 0;
72
- channelBindings.secChannelBindings.cbAcceptorLength = 0;
73
- channelBindings.secChannelBindings.dwAcceptorOffset = 0;
74
- channelBindings.secChannelBindings.cbApplicationDataLength = applicationDataBuffer.Length();
75
- channelBindings.secChannelBindings.dwApplicationDataOffset = 32;
76
- memcpy(channelBindings.applicationData, applicationDataBuffer.Data(), applicationDataBuffer.Length());
77
- channelBindingsLength = (32 + applicationDataBuffer.Length());
78
- }
79
- }
80
-
81
- bool AuthContext::InitContext(Napi::Env* env) {
82
- SecBufferDesc outSecBufferDesc;
83
- SecBuffer outSecBuff;
84
- outToken = new unsigned char[maxTokenLength];
85
-
86
- outSecBufferDesc.ulVersion = 0;
87
- outSecBufferDesc.cBuffers = 1;
88
- outSecBufferDesc.pBuffers = &outSecBuff;
89
-
90
- outSecBuff.cbBuffer = maxTokenLength;
91
- outSecBuff.BufferType = SECBUFFER_TOKEN;
92
- outSecBuff.pvBuffer = outToken;
93
-
94
- int result = Secur32Facade::InitializeSecurityContext(
95
- NULL, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
96
- if (env->IsExceptionPending()) {
97
- Cleanup(env);
98
- return false;
99
- }
100
- if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
101
- ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
102
- Cleanup(env);
103
- return false;
104
- }
105
- ctxHandleAllocated = true;
106
- outTokenLength = outSecBuff.cbBuffer;
107
- return true;
108
- }
109
-
110
- bool AuthContext::HandleResponse(
111
- Napi::Buffer<unsigned char>& inTokenBuffer,
112
- Napi::Env* env) {
113
- SecBufferDesc outSecBufferDesc;
114
- SecBuffer outSecBuff;
115
- SecBufferDesc inSecBufferDesc;
116
- SecBuffer inSecBuffers[2];
117
-
118
- outSecBufferDesc.ulVersion = 0;
119
- outSecBufferDesc.cBuffers = 1;
120
- outSecBufferDesc.pBuffers = &outSecBuff;
121
-
122
- outSecBuff.cbBuffer = maxTokenLength;
123
- outSecBuff.BufferType = SECBUFFER_TOKEN;
124
- outSecBuff.pvBuffer = outToken;
125
-
126
- inSecBufferDesc.ulVersion = 0;
127
- inSecBufferDesc.cBuffers = 1;
128
- inSecBufferDesc.pBuffers = inSecBuffers;
129
-
130
- inSecBuffers[0].cbBuffer = inTokenBuffer.Length();
131
- inSecBuffers[0].BufferType = SECBUFFER_TOKEN;
132
- inSecBuffers[0].pvBuffer = inTokenBuffer.Data();
133
-
134
- if (channelBindingsLength > 0) {
135
- inSecBufferDesc.cBuffers++;
136
- inSecBuffers[1].cbBuffer = channelBindingsLength;
137
- inSecBuffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
138
- inSecBuffers[1].pvBuffer = &channelBindings;
139
- }
140
-
141
- auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
142
- if (env->IsExceptionPending()) {
143
- Cleanup(env);
144
- return false;
145
- }
146
- if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
147
- ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
148
- Cleanup(env);
149
- return false;
150
- }
151
-
152
- outTokenLength = outSecBuff.cbBuffer;
153
- return true;
154
- }
155
-
156
- Napi::Buffer<unsigned char> AuthContext::OutToken(Napi::Env* env) {
157
- if (outToken) {
158
- return Napi::Buffer<unsigned char>::Copy(*env, outToken, outTokenLength);
159
- } else {
160
- return Napi::Buffer<unsigned char>::Buffer();
161
- }
162
- }
@@ -1,52 +0,0 @@
1
- #ifndef AUTH_CONTEXT_HH
2
- #define AUTH_CONTEXT_HH
3
-
4
- #define SECURITY_WIN32
5
-
6
- #include <napi.h>
7
-
8
- #include <windows.h>
9
- #include <sspi.h>
10
- #include <Secext.h>
11
- #include <Security.h>
12
-
13
- class AuthContext {
14
- public:
15
- char packageName[128];
16
- std::string targetHostname;
17
- char* targetHostnameSpn;
18
- CredHandle credHandle;
19
- SECURITY_INTEGER lifeTime;
20
- struct _SecHandle ctxHandle;
21
- unsigned long flags;
22
- unsigned long maxTokenLength;
23
- unsigned char* outToken;
24
- unsigned long outTokenLength;
25
-
26
- private:
27
- struct SecChannelBindingsCombined {
28
- struct _SEC_CHANNEL_BINDINGS secChannelBindings;
29
- unsigned char applicationData[128];
30
- };
31
- struct SecChannelBindingsCombined channelBindings;
32
- unsigned long channelBindingsLength;
33
-
34
- bool credHandleAllocated;
35
- bool ctxHandleAllocated;
36
-
37
- public:
38
- AuthContext();
39
- virtual ~AuthContext();
40
- void Cleanup(Napi::Env* env);
41
-
42
- bool Init(std::string* packageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, unsigned long flags, Napi::Env* env);
43
- bool InitContext(Napi::Env* env);
44
- bool HandleResponse(Napi::Buffer<unsigned char>& inTokenBuffer, Napi::Env* env);
45
-
46
- Napi::Buffer<unsigned char> OutToken(Napi::Env* env);
47
-
48
- private:
49
- void SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer);
50
- };
51
-
52
- #endif
@@ -1,25 +0,0 @@
1
- #include "exception-handler.hh"
2
-
3
- void ExceptionHandler::CreateAndThrow(Napi::Env& env, std::string& message) {
4
- if (env.IsExceptionPending()) {
5
- // Merge exceptions if an exception has already been thrown
6
- auto error = env.GetAndClearPendingException();
7
- message = error.Message() + "\n" + message;
8
- }
9
- Napi::Error::New(env, message).ThrowAsJavaScriptException();
10
- }
11
-
12
- void ExceptionHandler::CreateAndThrow(Napi::Env& env, const char* message) {
13
- std::string exMessage = message;
14
- CreateAndThrow(env, exMessage);
15
- }
16
-
17
- void ExceptionHandler::CreateAndThrow(Napi::Env& env, const char* message, int resultCode) {
18
- std::string exMessage = message;
19
- CreateAndThrow(env, exMessage, resultCode);
20
- }
21
-
22
- void ExceptionHandler::CreateAndThrow(Napi::Env& env, std::string& message, int resultCode) {
23
- message = message + std::to_string(resultCode);
24
- CreateAndThrow(env, message);
25
- }
@@ -1,15 +0,0 @@
1
- #ifndef EXCEPTION_HANDLER_HH
2
- #define EXCEPTION_HANDLER_HH
3
-
4
- #include <string.h>
5
- #include <napi.h>
6
-
7
- class ExceptionHandler {
8
- public:
9
- static void CreateAndThrow(Napi::Env& env, const char* message);
10
- static void CreateAndThrow(Napi::Env& env, std::string& message);
11
- static void CreateAndThrow(Napi::Env& env, const char* message, int resultCode);
12
- static void CreateAndThrow(Napi::Env& env, std::string& message, int resultCode);
13
- };
14
-
15
- #endif
@@ -1,243 +0,0 @@
1
- #include "secur32-facade.hh"
2
- #include "exception-handler.hh"
3
- #include "auth-context.hh"
4
-
5
- #include <Secext.h>
6
- #include <Security.h>
7
- #include <string.h>
8
-
9
- std::map<unsigned long, std::shared_ptr<AuthContext>> Secur32Facade::acMap = std::map<unsigned long, std::shared_ptr<AuthContext>>();
10
- unsigned long Secur32Facade::acKey = 0;
11
-
12
- Napi::String Secur32Facade::GetLogonUserName(const Napi::CallbackInfo& info) {
13
- static char _userName[256] = "";
14
- Napi::Env env = info.Env();
15
- if (_userName[0]) {
16
- return Napi::String::New(env, _userName);
17
- }
18
-
19
- unsigned long userNameLength = 256;
20
- int result = ::GetUserNameExA(NameSamCompatible, _userName, &userNameLength);
21
- if (result < 0) {
22
- ExceptionHandler::CreateAndThrow(env, "Could not get user name of logged in user. Result: ", result);
23
- return Napi::String::New(env, "");
24
- }
25
- _userName[userNameLength] = '\0';
26
- auto name = Napi::String::New(env, _userName);
27
-
28
- return name;
29
- }
30
-
31
- unsigned int Secur32Facade::GetMaxTokenLength(char* packageName, Napi::Env* env) {
32
- static unsigned int _maxTokenLength = 0;
33
-
34
- if (_maxTokenLength != 0) {
35
- return _maxTokenLength;
36
- }
37
-
38
- PSecPkgInfoA pkgInfo;
39
- int result = ::QuerySecurityPackageInfoA(packageName, &pkgInfo);
40
- if (result != 0) {
41
- ExceptionHandler::CreateAndThrow(*env, "Could not get SecurityPackageInfo. Result: ", result);
42
- return 0;
43
- }
44
- _maxTokenLength = pkgInfo->cbMaxToken;
45
- result = ::FreeContextBuffer(pkgInfo);
46
- if (result != 0) {
47
- ExceptionHandler::CreateAndThrow(*env, "Could not free context buffer. Result: ", result);
48
- return 0;
49
- }
50
- return _maxTokenLength;
51
- }
52
-
53
- void Secur32Facade::AcquireCredentialsHandle(char* packageName, CredHandle* credHandle, SECURITY_INTEGER* lifeTime, Napi::Env* env) {
54
- int result = ::AcquireCredentialsHandleA(NULL, packageName, SECPKG_CRED_OUTBOUND, NULL, NULL, NULL, NULL, credHandle, lifeTime);
55
- if (result < 0) {
56
- ExceptionHandler::CreateAndThrow(*env, "Could not acquire credentials handle. Result: ", result);
57
- }
58
- }
59
-
60
- void Secur32Facade::FreeCredentialsHandle(CredHandle* credHandle, Napi::Env* env) {
61
- int result = ::FreeCredentialsHandle(credHandle);
62
- if (result != 0 && env != 0) {
63
- ExceptionHandler::CreateAndThrow(*env, "Could not free credentials handle. Result: ", result);
64
- }
65
- }
66
-
67
- int Secur32Facade::InitializeSecurityContext(
68
- SecBufferDesc* inSecBufferDesc,
69
- SecBufferDesc* outSecBufferDesc,
70
- char* targetHostSpn,
71
- CredHandle* credHandle,
72
- struct _SecHandle* ctxHandle,
73
- unsigned long flags,
74
- SECURITY_INTEGER* lifeTime,
75
- Napi::Env* env)
76
- {
77
- int result = 0;
78
- unsigned long ignored;
79
-
80
- if (inSecBufferDesc == NULL) {
81
- result = ::InitializeSecurityContextA(
82
- credHandle,
83
- NULL,
84
- targetHostSpn,
85
- flags,
86
- 0,
87
- SECURITY_NATIVE_DREP,
88
- NULL,
89
- 0,
90
- ctxHandle,
91
- outSecBufferDesc,
92
- &ignored,
93
- lifeTime
94
- );
95
- } else {
96
- result = ::InitializeSecurityContextA(
97
- credHandle,
98
- ctxHandle,
99
- targetHostSpn,
100
- flags,
101
- 0,
102
- SECURITY_NATIVE_DREP,
103
- inSecBufferDesc,
104
- 0,
105
- ctxHandle,
106
- outSecBufferDesc,
107
- &ignored,
108
- lifeTime
109
- );
110
- }
111
- if (result < 0) {
112
- ExceptionHandler::CreateAndThrow(*env, "Could not init security context. Result: ", result);
113
- }
114
- return result;
115
- }
116
-
117
- void Secur32Facade::FreeContextHandle(struct _SecHandle* ctxHandle, Napi::Env* env) {
118
- int result = ::DeleteSecurityContext(ctxHandle);
119
- if (result != 0 && env != 0) {
120
- ExceptionHandler::CreateAndThrow(*env, "Could not delete security context. Result: ", result);
121
- }
122
- }
123
-
124
- unsigned long Secur32Facade::GetDefaultFlags(std::string securityPackageName) {
125
- if (securityPackageName == "Negotiate" || securityPackageName == "Kerberos") {
126
- return ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT;
127
- }
128
- return 0;
129
- }
130
-
131
- Napi::Number Secur32Facade::CreateAuthContext(const Napi::CallbackInfo& info) {
132
- Napi::Env env = info.Env();
133
- if (info.Length() < 4) {
134
- ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
135
- return Napi::Number::New(env, 0);
136
- }
137
-
138
- if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer() || !(info[3].IsUndefined() || info[3].IsNumber())) {
139
- ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
140
- return Napi::Number::New(env, 0);
141
- }
142
- auto securityPackageName = info[0].ToString();
143
- auto targetHost = info[1].ToString();
144
- auto applicationDataBuffer = info[2].As<Napi::Buffer<unsigned char>>();
145
- auto flags = info[3].IsNumber() ? (unsigned long)(info[3].ToNumber().Uint32Value()) : GetDefaultFlags(securityPackageName);
146
-
147
- auto ac = std::make_shared<AuthContext>();
148
- ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, flags, &env);
149
- acKey++;
150
- acMap[acKey] = ac;
151
- return Napi::Number::New(env, acKey);
152
- }
153
-
154
- Napi::Number Secur32Facade::FreeAuthContext(const Napi::CallbackInfo& info) {
155
- Napi::Env env = info.Env();
156
- if (info.Length() < 1) {
157
- ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
158
- return Napi::Number::New(env, 0);
159
- }
160
-
161
- if (!info[0].IsNumber()) {
162
- ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
163
- return Napi::Number::New(env, 0);
164
- }
165
-
166
- auto acKey = info[0].ToNumber().Uint32Value();
167
- size_t erased = 0;
168
- if (acMap.find(acKey) != acMap.end()) {
169
- acMap[acKey]->Cleanup(&env);
170
- erased = acMap.erase(acKey);
171
- }
172
- return Napi::Number::New(env, erased);
173
- }
174
-
175
- /**
176
- * Creates a NTLM type 1 authentication token
177
- */
178
- Napi::Buffer<unsigned char> Secur32Facade::CreateAuthRequest(const Napi::CallbackInfo& info) {
179
- Napi::Env env = info.Env();
180
- if (info.Length() < 1) {
181
- ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
182
- return Napi::Buffer<unsigned char>::Buffer();
183
- }
184
-
185
- if (!info[0].IsNumber()) {
186
- ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
187
- return Napi::Buffer<unsigned char>::Buffer();
188
- }
189
-
190
- auto acKey = info[0].ToNumber().Uint32Value();
191
-
192
- if (acMap.find(acKey) == acMap.end()) {
193
- ExceptionHandler::CreateAndThrow(env, "AuthContext not found");
194
- return Napi::Buffer<unsigned char>::Buffer();
195
- }
196
-
197
- auto ac = acMap[acKey];
198
- ac->InitContext(&env);
199
- return ac->OutToken(&env);
200
- }
201
-
202
- /**
203
- * Creates a NTLM type 3 authentication token
204
- */
205
- Napi::Buffer<unsigned char> Secur32Facade::CreateAuthResponse(const Napi::CallbackInfo& info) {
206
- Napi::Env env = info.Env();
207
- if (info.Length() < 2) {
208
- ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
209
- return Napi::Buffer<unsigned char>::Buffer();
210
- }
211
-
212
- if (!info[0].IsNumber() && !info[1].IsBuffer()) {
213
- ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
214
- return Napi::Buffer<unsigned char>::Buffer();
215
- }
216
-
217
- auto acKey = info[0].ToNumber().Uint32Value();
218
- if (acMap.find(acKey) == acMap.end()) {
219
- ExceptionHandler::CreateAndThrow(env, "AuthContext not found");
220
- return Napi::Buffer<unsigned char>::Buffer();
221
- }
222
-
223
- auto inTokenBuffer = info[1].As<Napi::Buffer<unsigned char>>();
224
- auto ac = acMap[acKey];
225
- ac->HandleResponse(inTokenBuffer, &env);
226
- return ac->OutToken(&env);
227
- }
228
-
229
- Napi::Object Init(Napi::Env env, Napi::Object exports) {
230
- exports.Set(Napi::String::New(env, "getLogonUserName"),
231
- Napi::Function::New(env, Secur32Facade::GetLogonUserName));
232
- exports.Set(Napi::String::New(env, "createAuthContext"),
233
- Napi::Function::New(env, Secur32Facade::CreateAuthContext));
234
- exports.Set(Napi::String::New(env, "freeAuthContext"),
235
- Napi::Function::New(env, Secur32Facade::FreeAuthContext));
236
- exports.Set(Napi::String::New(env, "createAuthRequest"),
237
- Napi::Function::New(env, Secur32Facade::CreateAuthRequest));
238
- exports.Set(Napi::String::New(env, "createAuthResponse"),
239
- Napi::Function::New(env, Secur32Facade::CreateAuthResponse));
240
- return exports;
241
- }
242
-
243
- NODE_API_MODULE(WinSso, Init)