win-sso 1.3.3 → 1.4.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/src/win-sso.ts CHANGED
@@ -1,192 +1,193 @@
1
- import { debug } from "./utils/debug.logger";
2
- import { PeerCertificate } from "tls";
3
- import path from "path";
4
- import { IWinSsoAddon } from "./utils/i.win-sso-addon";
5
-
6
- let winSsoAddon: IWinSsoAddon;
7
- try {
8
- // eslint-disable-next-line @typescript-eslint/no-require-imports
9
- winSsoAddon = require("node-gyp-build")(path.join(__dirname, ".."));
10
- debug("Loaded win-sso native module");
11
- } catch {
12
- debug("Could not load win-sso native module");
13
- }
14
-
15
- /**
16
- * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
17
- */
18
- export class WinSso {
19
- private static NEGOTIATE_NTLM2_KEY = 1 << 19;
20
-
21
- private authContextId: number;
22
- private securityPackage: string;
23
-
24
- /**
25
- * Creates an authentication context for SSO.
26
- * This allocates memory buffers, the freeAuthContext method should be called
27
- * to free them (on error or after authentication is no longer needed)
28
- * @param securityPackage The name of the security package (NTLM or Negotiate)
29
- * @param targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
30
- * @param peerCert The certificate of the target server
31
- * (optional, for HTTPS channel binding)
32
- * @param flags Flags to set in the authentication context
33
- * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
34
- * (optional, allows customizing security features)
35
- */
36
- constructor(
37
- securityPackage: string,
38
- targetHost: string | undefined,
39
- peerCert: PeerCertificate | undefined,
40
- flags: number | undefined
41
- ) {
42
- this.securityPackage = securityPackage;
43
- let applicationData: Buffer;
44
- if (!targetHost) {
45
- targetHost = "";
46
- }
47
- if (peerCert) {
48
- applicationData = this.getChannelBindingsApplicationData(peerCert);
49
- } else {
50
- applicationData = Buffer.alloc(0);
51
- }
52
- this.authContextId = winSsoAddon.createAuthContext(
53
- securityPackage,
54
- targetHost,
55
- applicationData,
56
- flags
57
- );
58
- }
59
-
60
- /**
61
- * Retrieves the username of the logged in user
62
- * @returns user name including domain
63
- */
64
- static getLogonUserName(): string {
65
- return winSsoAddon.getLogonUserName();
66
- }
67
-
68
- /**
69
- * Transforms target TLS certificate into a channel binding application data buffer
70
- * @param peerCert Target TLS certificate
71
- * @returns Application data buffer
72
- */
73
- private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
74
- const hash = peerCert.fingerprint256.replace(/:/g, "");
75
- const hashBuf = Buffer.from(hash, "hex");
76
- const tlsServerEndPoint = "tls-server-end-point:";
77
- const applicationDataBuffer = Buffer.alloc(
78
- tlsServerEndPoint.length + hashBuf.length
79
- );
80
- applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
81
- hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
82
- return applicationDataBuffer;
83
- }
84
-
85
- /**
86
- * Releases all allocated resources for the authorization context.
87
- * Should be called when the context is no longer required, such as when the
88
- * socket was closed.
89
- */
90
- freeAuthContext() {
91
- winSsoAddon.freeAuthContext(this.authContextId);
92
- }
93
-
94
- /**
95
- * Creates an authentication request token
96
- * @returns Raw token buffer
97
- */
98
- createAuthRequest(): Buffer {
99
- const token = winSsoAddon.createAuthRequest(this.authContextId);
100
- debug(
101
- "Created " + this.securityPackage + " authentication request token",
102
- token.toString("base64")
103
- );
104
- return token;
105
- }
106
-
107
- /**
108
- * Creates an authentication request header
109
- * @returns The www-authenticate header
110
- */
111
- createAuthRequestHeader(): string {
112
- const header =
113
- this.securityPackage + " " + this.createAuthRequest().toString("base64");
114
- return header;
115
- }
116
-
117
- /**
118
- * Creates an authentication response token
119
- * @param inTokenHeader The www-authentication header received from the target
120
- * in response to the authentication request
121
- * @returns Raw token buffer. May be empty if Negotiate handshake is complete.
122
- */
123
- createAuthResponse(inTokenHeader: string): Buffer {
124
- debug("Received www-authentication response", inTokenHeader);
125
- const packageMatch = new RegExp(
126
- "^" + this.securityPackage + "\\s([^,\\s]+)"
127
- ).exec(inTokenHeader);
128
-
129
- if (!packageMatch) {
130
- throw new Error(
131
- "Invalid input token, missing " +
132
- this.securityPackage +
133
- " prefix: " +
134
- inTokenHeader
135
- );
136
- }
137
- const inToken = Buffer.from(packageMatch[1], "base64");
138
- try {
139
- const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
140
- if (token.length > 0) {
141
- debug(
142
- "Created " + this.securityPackage + " authentication response token",
143
- token.toString("base64")
144
- );
145
- } else {
146
- debug("No response token, authentication complete");
147
- }
148
- return token;
149
- } catch (err) {
150
- if (
151
- (err as Error).message ===
152
- "Could not init security context. Result: -2146893054"
153
- ) {
154
- // If incoming token is for NTLMv1, this error can occur when
155
- // LMCompatibilityLevel prevents the client to send NTLMv1 messages
156
- if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
157
- throw new Error(
158
- "Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
159
- "it is likely that the client is prevented from sending such messages. " +
160
- "Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)"
161
- );
162
- }
163
- }
164
- throw err;
165
- }
166
- }
167
-
168
- private isNtlmV1(type2message: Buffer): boolean {
169
- if (type2message.length >= 24) {
170
- const inTokenFlags = type2message.readInt32BE(20);
171
- if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
172
- return true;
173
- }
174
- }
175
- return false;
176
- }
177
-
178
- /**
179
- * Creates an authentication response header
180
- * @param inTokenHeader The www-authentication header received from the target
181
- * in response to the authentication request
182
- * @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
183
- */
184
- createAuthResponseHeader(inTokenHeader: string): string {
185
- const tokenBuffer = this.createAuthResponse(inTokenHeader);
186
- if (tokenBuffer.length == 0) {
187
- return "";
188
- }
189
- const header = this.securityPackage + " " + tokenBuffer.toString("base64");
190
- return header;
191
- }
192
- }
1
+ import { debug } from "./utils/debug.logger";
2
+ import { PeerCertificate } from "tls";
3
+ import path from "path";
4
+ import { IWinSsoAddon } from "./utils/i.win-sso-addon";
5
+
6
+ let winSsoAddon: IWinSsoAddon;
7
+ try {
8
+ const currentDir = __dirname; // eslint-disable-line
9
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
10
+ winSsoAddon = require("node-gyp-build")(path.join(currentDir, "..", "native"));
11
+ debug("Loaded win-sso native module");
12
+ } catch {
13
+ debug("Could not load win-sso native module");
14
+ }
15
+
16
+ /**
17
+ * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
18
+ */
19
+ export class WinSso {
20
+ private static NEGOTIATE_NTLM2_KEY = 1 << 19;
21
+
22
+ private authContextId: number;
23
+ private securityPackage: string;
24
+
25
+ /**
26
+ * Creates an authentication context for SSO.
27
+ * This allocates memory buffers, the freeAuthContext method should be called
28
+ * to free them (on error or after authentication is no longer needed)
29
+ * @param securityPackage The name of the security package (NTLM or Negotiate)
30
+ * @param targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
31
+ * @param peerCert The certificate of the target server
32
+ * (optional, for HTTPS channel binding)
33
+ * @param flags Flags to set in the authentication context
34
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
35
+ * (optional, allows customizing security features)
36
+ */
37
+ constructor(
38
+ securityPackage: string,
39
+ targetHost: string | undefined,
40
+ peerCert: PeerCertificate | undefined,
41
+ flags: number | undefined
42
+ ) {
43
+ this.securityPackage = securityPackage;
44
+ let applicationData: Buffer;
45
+ if (!targetHost) {
46
+ targetHost = "";
47
+ }
48
+ if (peerCert) {
49
+ applicationData = this.getChannelBindingsApplicationData(peerCert);
50
+ } else {
51
+ applicationData = Buffer.alloc(0);
52
+ }
53
+ this.authContextId = winSsoAddon.createAuthContext(
54
+ securityPackage,
55
+ targetHost,
56
+ applicationData,
57
+ flags
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Retrieves the username of the logged in user
63
+ * @returns user name including domain
64
+ */
65
+ static getLogonUserName(): string {
66
+ return winSsoAddon.getLogonUserName();
67
+ }
68
+
69
+ /**
70
+ * Transforms target TLS certificate into a channel binding application data buffer
71
+ * @param peerCert Target TLS certificate
72
+ * @returns Application data buffer
73
+ */
74
+ private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
75
+ const hash = peerCert.fingerprint256.replace(/:/g, "");
76
+ const hashBuf = Buffer.from(hash, "hex");
77
+ const tlsServerEndPoint = "tls-server-end-point:";
78
+ const applicationDataBuffer = Buffer.alloc(
79
+ tlsServerEndPoint.length + hashBuf.length
80
+ );
81
+ applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
82
+ hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
83
+ return applicationDataBuffer;
84
+ }
85
+
86
+ /**
87
+ * Releases all allocated resources for the authorization context.
88
+ * Should be called when the context is no longer required, such as when the
89
+ * socket was closed.
90
+ */
91
+ freeAuthContext() {
92
+ winSsoAddon.freeAuthContext(this.authContextId);
93
+ }
94
+
95
+ /**
96
+ * Creates an authentication request token
97
+ * @returns Raw token buffer
98
+ */
99
+ createAuthRequest(): Buffer {
100
+ const token = winSsoAddon.createAuthRequest(this.authContextId);
101
+ debug(
102
+ "Created " + this.securityPackage + " authentication request token",
103
+ token.toString("base64")
104
+ );
105
+ return token;
106
+ }
107
+
108
+ /**
109
+ * Creates an authentication request header
110
+ * @returns The www-authenticate header
111
+ */
112
+ createAuthRequestHeader(): string {
113
+ const header =
114
+ this.securityPackage + " " + this.createAuthRequest().toString("base64");
115
+ return header;
116
+ }
117
+
118
+ /**
119
+ * Creates an authentication response token
120
+ * @param inTokenHeader The www-authentication header received from the target
121
+ * in response to the authentication request
122
+ * @returns Raw token buffer. May be empty if Negotiate handshake is complete.
123
+ */
124
+ createAuthResponse(inTokenHeader: string): Buffer {
125
+ debug("Received www-authentication response", inTokenHeader);
126
+ const packageMatch = new RegExp(
127
+ "^" + this.securityPackage + "\\s([^,\\s]+)"
128
+ ).exec(inTokenHeader);
129
+
130
+ if (!packageMatch) {
131
+ throw new Error(
132
+ "Invalid input token, missing " +
133
+ this.securityPackage +
134
+ " prefix: " +
135
+ inTokenHeader
136
+ );
137
+ }
138
+ const inToken = Buffer.from(packageMatch[1], "base64");
139
+ try {
140
+ const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
141
+ if (token.length > 0) {
142
+ debug(
143
+ "Created " + this.securityPackage + " authentication response token",
144
+ token.toString("base64")
145
+ );
146
+ } else {
147
+ debug("No response token, authentication complete");
148
+ }
149
+ return token;
150
+ } catch (err) {
151
+ if (
152
+ (err as Error).message ===
153
+ "Could not init security context. Result: -2146893054"
154
+ ) {
155
+ // If incoming token is for NTLMv1, this error can occur when
156
+ // LMCompatibilityLevel prevents the client to send NTLMv1 messages
157
+ if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
158
+ throw new Error(
159
+ "Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
160
+ "it is likely that the client is prevented from sending such messages. " +
161
+ "Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)"
162
+ );
163
+ }
164
+ }
165
+ throw err;
166
+ }
167
+ }
168
+
169
+ private isNtlmV1(type2message: Buffer): boolean {
170
+ if (type2message.length >= 24) {
171
+ const inTokenFlags = type2message.readInt32BE(20);
172
+ if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
173
+ return true;
174
+ }
175
+ }
176
+ return false;
177
+ }
178
+
179
+ /**
180
+ * Creates an authentication response header
181
+ * @param inTokenHeader The www-authentication header received from the target
182
+ * in response to the authentication request
183
+ * @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
184
+ */
185
+ createAuthResponseHeader(inTokenHeader: string): string {
186
+ const tokenBuffer = this.createAuthResponse(inTokenHeader);
187
+ if (tokenBuffer.length == 0) {
188
+ return "";
189
+ }
190
+ const header = this.securityPackage + " " + tokenBuffer.toString("base64");
191
+ return header;
192
+ }
193
+ }
package/binding.gyp DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "targets": [
3
- {
4
- "target_name": "win-sso",
5
- "cflags!": [ "-fno-exceptions" ],
6
- "cflags_cc!": [ "-fno-exceptions" ],
7
- "sources": [
8
- "src/node-addon/auth-context.cc",
9
- "src/node-addon/exception-handler.cc",
10
- "src/node-addon/secur32-facade.cc"
11
- ],
12
- "libraries": [
13
- "-lsecur32"
14
- ],
15
- "include_dirs": [
16
- "<!@(node -p \"require('node-addon-api').include\")"
17
- ],
18
- 'dependencies': [
19
- "<!(node -p \"require('node-addon-api').gyp\")"
20
- ],
21
- 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
22
- }
23
- ]
24
- }
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