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