win-sso 1.0.1 → 1.1.6

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,159 +1,159 @@
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
- ctxAttributes = 0;
10
- maxTokenLength = 0;
11
- outToken = 0;
12
- outTokenLength = 0;
13
- credHandleAllocated = false;
14
- ctxHandleAllocated = false;
15
- channelBindings = {};
16
- channelBindingsLength = 0;
17
- };
18
-
19
- AuthContext::~AuthContext() {
20
- // Napi::Env is not available in destructor, but we can still do cleanup without
21
- // reporting JS exceptions to Node
22
- Cleanup(0);
23
- }
24
-
25
- void AuthContext::Cleanup(Napi::Env* env) {
26
- if (credHandleAllocated) {
27
- Secur32Facade::FreeCredentialsHandle(&credHandle, env);
28
- credHandleAllocated = false;
29
- }
30
- if (ctxHandleAllocated) {
31
- Secur32Facade::FreeContextHandle(&ctxHandle, env);
32
- ctxHandleAllocated = false;
33
- }
34
- if (outToken) {
35
- delete outToken;
36
- outToken = 0;
37
- }
38
- }
39
-
40
- bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env) {
41
- auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
42
- packageName[packageNameLen] = '\0';
43
- targetHostname = *targetHost;
44
- maxTokenLength = Secur32Facade::GetMaxTokenLength(packageName, env);
45
- SetupChannelBindings(applicationDataBuffer);
46
- Secur32Facade::AcquireCredentialsHandle(packageName, &credHandle, &lifeTime, env);
47
- if (env->IsExceptionPending()) {
48
- Cleanup(env);
49
- return false;
50
- }
51
- credHandleAllocated = true;
52
- return true;
53
- }
54
-
55
- void AuthContext::SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer) {
56
- if (applicationDataBuffer.Length() > 0) {
57
- channelBindings.secChannelBindings.dwInitiatorAddrType = 0;
58
- channelBindings.secChannelBindings.cbInitiatorLength = 0;
59
- channelBindings.secChannelBindings.dwInitiatorOffset = 0;
60
- channelBindings.secChannelBindings.dwAcceptorAddrType = 0;
61
- channelBindings.secChannelBindings.cbAcceptorLength = 0;
62
- channelBindings.secChannelBindings.dwAcceptorOffset = 0;
63
- channelBindings.secChannelBindings.cbApplicationDataLength = applicationDataBuffer.Length();
64
- channelBindings.secChannelBindings.dwApplicationDataOffset = 32;
65
- memcpy(channelBindings.applicationData, applicationDataBuffer.Data(), applicationDataBuffer.Length());
66
- channelBindingsLength = (32 + applicationDataBuffer.Length());
67
- }
68
- }
69
-
70
- bool AuthContext::InitContext(Napi::Env* env) {
71
- SecBufferDesc outSecBufferDesc;
72
- SecBuffer outSecBuff;
73
- outToken = new unsigned char[maxTokenLength];
74
-
75
- outSecBufferDesc.ulVersion = 0;
76
- outSecBufferDesc.cBuffers = 1;
77
- outSecBufferDesc.pBuffers = &outSecBuff;
78
-
79
- outSecBuff.cbBuffer = maxTokenLength;
80
- outSecBuff.BufferType = SECBUFFER_TOKEN;
81
- outSecBuff.pvBuffer = outToken;
82
-
83
- int result = Secur32Facade::InitializeSecurityContext(
84
- NULL, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
85
- if (env->IsExceptionPending()) {
86
- Cleanup(env);
87
- return false;
88
- }
89
- if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
90
- ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
91
- Cleanup(env);
92
- return false;
93
- }
94
- ctxHandleAllocated = true;
95
- outTokenLength = outSecBuff.cbBuffer;
96
- return true;
97
- }
98
-
99
- bool AuthContext::HandleResponse(
100
- Napi::Buffer<unsigned char>& inTokenBuffer,
101
- Napi::Env* env) {
102
- SecBufferDesc outSecBufferDesc;
103
- SecBuffer outSecBuff;
104
- SecBufferDesc inSecBufferDesc;
105
- SecBuffer inSecBuffers[2];
106
-
107
- outSecBufferDesc.ulVersion = 0;
108
- outSecBufferDesc.cBuffers = 1;
109
- outSecBufferDesc.pBuffers = &outSecBuff;
110
-
111
- outSecBuff.cbBuffer = maxTokenLength;
112
- outSecBuff.BufferType = SECBUFFER_TOKEN;
113
- outSecBuff.pvBuffer = outToken;
114
-
115
- inSecBufferDesc.ulVersion = 0;
116
- inSecBufferDesc.cBuffers = 1;
117
- inSecBufferDesc.pBuffers = inSecBuffers;
118
-
119
- inSecBuffers[0].cbBuffer = inTokenBuffer.Length();
120
- inSecBuffers[0].BufferType = SECBUFFER_TOKEN;
121
- inSecBuffers[0].pvBuffer = inTokenBuffer.Data();
122
-
123
- if (channelBindingsLength > 0) {
124
- inSecBufferDesc.cBuffers++;
125
- inSecBuffers[1].cbBuffer = channelBindingsLength;
126
- inSecBuffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
127
- inSecBuffers[1].pvBuffer = &channelBindings;
128
- }
129
-
130
- auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
131
- if (env->IsExceptionPending()) {
132
- Cleanup(env);
133
- return false;
134
- }
135
- if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
136
- ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
137
- Cleanup(env);
138
- return false;
139
- }
140
-
141
- outTokenLength = outSecBuff.cbBuffer;
142
- return true;
143
- }
144
-
145
- Napi::Buffer<unsigned char> AuthContext::OutToken(Napi::Env* env) {
146
- if (outToken) {
147
- return Napi::Buffer<unsigned char>::Copy(*env, outToken, outTokenLength);
148
- } else {
149
- return Napi::Buffer<unsigned char>::Buffer();
150
- }
151
- }
152
-
153
- std::string* AuthContext::TargetHostnameRef() {
154
- std::string* targetHostnameRef = NULL;
155
- if (targetHostname.length() > 0) {
156
- targetHostnameRef = &targetHostname;
157
- }
158
- return targetHostnameRef;
159
- }
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
+ ctxAttributes = 0;
10
+ maxTokenLength = 0;
11
+ outToken = 0;
12
+ outTokenLength = 0;
13
+ credHandleAllocated = false;
14
+ ctxHandleAllocated = false;
15
+ channelBindings = {};
16
+ channelBindingsLength = 0;
17
+ };
18
+
19
+ AuthContext::~AuthContext() {
20
+ // Napi::Env is not available in destructor, but we can still do cleanup without
21
+ // reporting JS exceptions to Node
22
+ Cleanup(0);
23
+ }
24
+
25
+ void AuthContext::Cleanup(Napi::Env* env) {
26
+ if (credHandleAllocated) {
27
+ Secur32Facade::FreeCredentialsHandle(&credHandle, env);
28
+ credHandleAllocated = false;
29
+ }
30
+ if (ctxHandleAllocated) {
31
+ Secur32Facade::FreeContextHandle(&ctxHandle, env);
32
+ ctxHandleAllocated = false;
33
+ }
34
+ if (outToken) {
35
+ delete outToken;
36
+ outToken = 0;
37
+ }
38
+ }
39
+
40
+ bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env) {
41
+ auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
42
+ packageName[packageNameLen] = '\0';
43
+ targetHostname = *targetHost;
44
+ maxTokenLength = Secur32Facade::GetMaxTokenLength(packageName, env);
45
+ SetupChannelBindings(applicationDataBuffer);
46
+ Secur32Facade::AcquireCredentialsHandle(packageName, &credHandle, &lifeTime, env);
47
+ if (env->IsExceptionPending()) {
48
+ Cleanup(env);
49
+ return false;
50
+ }
51
+ credHandleAllocated = true;
52
+ return true;
53
+ }
54
+
55
+ void AuthContext::SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer) {
56
+ if (applicationDataBuffer.Length() > 0) {
57
+ channelBindings.secChannelBindings.dwInitiatorAddrType = 0;
58
+ channelBindings.secChannelBindings.cbInitiatorLength = 0;
59
+ channelBindings.secChannelBindings.dwInitiatorOffset = 0;
60
+ channelBindings.secChannelBindings.dwAcceptorAddrType = 0;
61
+ channelBindings.secChannelBindings.cbAcceptorLength = 0;
62
+ channelBindings.secChannelBindings.dwAcceptorOffset = 0;
63
+ channelBindings.secChannelBindings.cbApplicationDataLength = applicationDataBuffer.Length();
64
+ channelBindings.secChannelBindings.dwApplicationDataOffset = 32;
65
+ memcpy(channelBindings.applicationData, applicationDataBuffer.Data(), applicationDataBuffer.Length());
66
+ channelBindingsLength = (32 + applicationDataBuffer.Length());
67
+ }
68
+ }
69
+
70
+ bool AuthContext::InitContext(Napi::Env* env) {
71
+ SecBufferDesc outSecBufferDesc;
72
+ SecBuffer outSecBuff;
73
+ outToken = new unsigned char[maxTokenLength];
74
+
75
+ outSecBufferDesc.ulVersion = 0;
76
+ outSecBufferDesc.cBuffers = 1;
77
+ outSecBufferDesc.pBuffers = &outSecBuff;
78
+
79
+ outSecBuff.cbBuffer = maxTokenLength;
80
+ outSecBuff.BufferType = SECBUFFER_TOKEN;
81
+ outSecBuff.pvBuffer = outToken;
82
+
83
+ int result = Secur32Facade::InitializeSecurityContext(
84
+ NULL, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
85
+ if (env->IsExceptionPending()) {
86
+ Cleanup(env);
87
+ return false;
88
+ }
89
+ if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
90
+ ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
91
+ Cleanup(env);
92
+ return false;
93
+ }
94
+ ctxHandleAllocated = true;
95
+ outTokenLength = outSecBuff.cbBuffer;
96
+ return true;
97
+ }
98
+
99
+ bool AuthContext::HandleResponse(
100
+ Napi::Buffer<unsigned char>& inTokenBuffer,
101
+ Napi::Env* env) {
102
+ SecBufferDesc outSecBufferDesc;
103
+ SecBuffer outSecBuff;
104
+ SecBufferDesc inSecBufferDesc;
105
+ SecBuffer inSecBuffers[2];
106
+
107
+ outSecBufferDesc.ulVersion = 0;
108
+ outSecBufferDesc.cBuffers = 1;
109
+ outSecBufferDesc.pBuffers = &outSecBuff;
110
+
111
+ outSecBuff.cbBuffer = maxTokenLength;
112
+ outSecBuff.BufferType = SECBUFFER_TOKEN;
113
+ outSecBuff.pvBuffer = outToken;
114
+
115
+ inSecBufferDesc.ulVersion = 0;
116
+ inSecBufferDesc.cBuffers = 1;
117
+ inSecBufferDesc.pBuffers = inSecBuffers;
118
+
119
+ inSecBuffers[0].cbBuffer = inTokenBuffer.Length();
120
+ inSecBuffers[0].BufferType = SECBUFFER_TOKEN;
121
+ inSecBuffers[0].pvBuffer = inTokenBuffer.Data();
122
+
123
+ if (channelBindingsLength > 0) {
124
+ inSecBufferDesc.cBuffers++;
125
+ inSecBuffers[1].cbBuffer = channelBindingsLength;
126
+ inSecBuffers[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
127
+ inSecBuffers[1].pvBuffer = &channelBindings;
128
+ }
129
+
130
+ auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
131
+ if (env->IsExceptionPending()) {
132
+ Cleanup(env);
133
+ return false;
134
+ }
135
+ if (result != SEC_I_CONTINUE_NEEDED && result != SEC_E_OK) {
136
+ ExceptionHandler::CreateAndThrow(*env, "Init security context did not return SEC_I_CONTINUE_NEEDED or SEC_E_OK. Result: ", result);
137
+ Cleanup(env);
138
+ return false;
139
+ }
140
+
141
+ outTokenLength = outSecBuff.cbBuffer;
142
+ return true;
143
+ }
144
+
145
+ Napi::Buffer<unsigned char> AuthContext::OutToken(Napi::Env* env) {
146
+ if (outToken) {
147
+ return Napi::Buffer<unsigned char>::Copy(*env, outToken, outTokenLength);
148
+ } else {
149
+ return Napi::Buffer<unsigned char>::Buffer();
150
+ }
151
+ }
152
+
153
+ std::string* AuthContext::TargetHostnameRef() {
154
+ std::string* targetHostnameRef = NULL;
155
+ if (targetHostname.length() > 0) {
156
+ targetHostnameRef = &targetHostname;
157
+ }
158
+ return targetHostnameRef;
159
+ }
@@ -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
- CredHandle credHandle;
18
- SECURITY_INTEGER lifeTime;
19
- struct _SecHandle ctxHandle;
20
- unsigned long ctxAttributes;
21
- unsigned long maxTokenLength;
22
- unsigned char* outToken;
23
- unsigned long outTokenLength;
24
-
25
- private:
26
- struct SecChannelBindingsCombined {
27
- struct _SEC_CHANNEL_BINDINGS secChannelBindings;
28
- unsigned char applicationData[128];
29
- };
30
- struct SecChannelBindingsCombined channelBindings;
31
- unsigned long channelBindingsLength;
32
-
33
- bool credHandleAllocated;
34
- bool ctxHandleAllocated;
35
-
36
- public:
37
- AuthContext();
38
- virtual ~AuthContext();
39
- void Cleanup(Napi::Env* env);
40
-
41
- bool Init(std::string* packageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env);
42
- bool InitContext(Napi::Env* env);
43
- bool HandleResponse(Napi::Buffer<unsigned char>& inTokenBuffer, Napi::Env* env);
44
-
45
- Napi::Buffer<unsigned char> OutToken(Napi::Env* env);
46
-
47
- private:
48
- void SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer);
49
- std::string* TargetHostnameRef();
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
+ CredHandle credHandle;
18
+ SECURITY_INTEGER lifeTime;
19
+ struct _SecHandle ctxHandle;
20
+ unsigned long ctxAttributes;
21
+ unsigned long maxTokenLength;
22
+ unsigned char* outToken;
23
+ unsigned long outTokenLength;
24
+
25
+ private:
26
+ struct SecChannelBindingsCombined {
27
+ struct _SEC_CHANNEL_BINDINGS secChannelBindings;
28
+ unsigned char applicationData[128];
29
+ };
30
+ struct SecChannelBindingsCombined channelBindings;
31
+ unsigned long channelBindingsLength;
32
+
33
+ bool credHandleAllocated;
34
+ bool ctxHandleAllocated;
35
+
36
+ public:
37
+ AuthContext();
38
+ virtual ~AuthContext();
39
+ void Cleanup(Napi::Env* env);
40
+
41
+ bool Init(std::string* packageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env);
42
+ bool InitContext(Napi::Env* env);
43
+ bool HandleResponse(Napi::Buffer<unsigned char>& inTokenBuffer, Napi::Env* env);
44
+
45
+ Napi::Buffer<unsigned char> OutToken(Napi::Env* env);
46
+
47
+ private:
48
+ void SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer);
49
+ std::string* TargetHostnameRef();
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