win-sso 1.3.2 → 1.3.3

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.
@@ -1,162 +1,162 @@
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
+ #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 +1,52 @@
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
+ #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 +1,25 @@
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
+ #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 +1,15 @@
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
+ #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