win-sso 1.1.5 → 1.1.8

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.
@@ -0,0 +1,3 @@
1
+ import { WinSso } from "./win-sso";
2
+ import { osSupported } from "./os-supported";
3
+ export { WinSso, osSupported };
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
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; } });
@@ -0,0 +1,7 @@
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
+ *
5
+ * @returns {boolean} Is the os supported
6
+ */
7
+ export declare function osSupported(): boolean;
@@ -0,0 +1,17 @@
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 = void 0;
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
+ *
12
+ * @returns {boolean} Is the os supported
13
+ */
14
+ function osSupported() {
15
+ return os_1.default.platform() === "win32";
16
+ }
17
+ exports.osSupported = osSupported;
@@ -0,0 +1,2 @@
1
+ /// <reference types="debug" />
2
+ export declare const debug: import("debug").Debugger;
@@ -0,0 +1,5 @@
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');
@@ -0,0 +1,69 @@
1
+ /// <reference types="node" />
2
+ import { PeerCertificate } from "tls";
3
+ /**
4
+ * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
5
+ */
6
+ export declare class WinSso {
7
+ private static NEGOTIATE_NTLM2_KEY;
8
+ private authContextId;
9
+ private securityPackage;
10
+ /**
11
+ * Creates an authentication context for SSO.
12
+ * This allocates memory buffers, the freeAuthContext method should be called
13
+ * to free them (on error or after authentication is no longer needed)
14
+ *
15
+ * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
16
+ * @param {string | undefined} targetHost The FQDN hostname of the target (optional)
17
+ * @param {PeerCertificate | undefined} peerCert The certificate of the target server
18
+ * (optional, for HTTPS channel binding)
19
+ */
20
+ constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined);
21
+ /**
22
+ * Retrieves the username of the logged in user
23
+ *
24
+ * @returns {string} user name including domain
25
+ */
26
+ static getLogonUserName(): string;
27
+ /**
28
+ * Transforms target TLS certificate into a channel binding application data buffer
29
+ *
30
+ * @param {PeerCertificate} peerCert Target TLS certificate
31
+ * @returns {Buffer} Application data buffer
32
+ */
33
+ private getChannelBindingsApplicationData;
34
+ /**
35
+ * Releases all allocated resources for the authorization context.
36
+ * Should be called when the context is no longer required, such as when the
37
+ * socket was closed.
38
+ */
39
+ freeAuthContext(): void;
40
+ /**
41
+ * Creates an authentication request token
42
+ *
43
+ * @returns {Buffer} Raw token buffer
44
+ */
45
+ createAuthRequest(): Buffer;
46
+ /**
47
+ * Creates an authentication request header
48
+ *
49
+ * @returns {string} The www-authenticate header
50
+ */
51
+ createAuthRequestHeader(): string;
52
+ /**
53
+ * Creates an authentication response token
54
+ *
55
+ * @param {string} inTokenHeader The www-authentication header received from the target
56
+ * in response to the authentication request
57
+ * @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
58
+ */
59
+ createAuthResponse(inTokenHeader: string): Buffer;
60
+ private isNtlmV1;
61
+ /**
62
+ * Creates an authentication response header
63
+ *
64
+ * @param {string} inTokenHeader The www-authentication header received from the target
65
+ * in response to the authentication request
66
+ * @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
67
+ */
68
+ createAuthResponseHeader(inTokenHeader: string): string;
69
+ }
@@ -0,0 +1,164 @@
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
+ winSsoAddon = require("node-gyp-build")(path_1.default.join(__dirname, ".."));
12
+ (0, debug_logger_1.debug)("Loaded win-sso native module");
13
+ }
14
+ catch (err) {
15
+ (0, debug_logger_1.debug)("Could not load win-sso native module");
16
+ }
17
+ /**
18
+ * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
19
+ */
20
+ class WinSso {
21
+ /**
22
+ * Creates an authentication context for SSO.
23
+ * This allocates memory buffers, the freeAuthContext method should be called
24
+ * to free them (on error or after authentication is no longer needed)
25
+ *
26
+ * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
27
+ * @param {string | undefined} targetHost The FQDN hostname of the target (optional)
28
+ * @param {PeerCertificate | undefined} peerCert The certificate of the target server
29
+ * (optional, for HTTPS channel binding)
30
+ */
31
+ constructor(securityPackage, targetHost, peerCert) {
32
+ this.securityPackage = securityPackage;
33
+ let applicationData;
34
+ if (!targetHost) {
35
+ targetHost = "";
36
+ }
37
+ if (peerCert) {
38
+ applicationData = this.getChannelBindingsApplicationData(peerCert);
39
+ }
40
+ else {
41
+ applicationData = Buffer.alloc(0);
42
+ }
43
+ this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData);
44
+ }
45
+ /**
46
+ * Retrieves the username of the logged in user
47
+ *
48
+ * @returns {string} user name including domain
49
+ */
50
+ static getLogonUserName() {
51
+ return winSsoAddon.getLogonUserName();
52
+ }
53
+ /**
54
+ * Transforms target TLS certificate into a channel binding application data buffer
55
+ *
56
+ * @param {PeerCertificate} peerCert Target TLS certificate
57
+ * @returns {Buffer} Application data buffer
58
+ */
59
+ getChannelBindingsApplicationData(peerCert) {
60
+ const cert = peerCert;
61
+ const hash = cert.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
+ *
80
+ * @returns {Buffer} Raw token buffer
81
+ */
82
+ createAuthRequest() {
83
+ const token = winSsoAddon.createAuthRequest(this.authContextId);
84
+ (0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication request token", token.toString("base64"));
85
+ return token;
86
+ }
87
+ /**
88
+ * Creates an authentication request header
89
+ *
90
+ * @returns {string} The www-authenticate header
91
+ */
92
+ createAuthRequestHeader() {
93
+ const header = this.securityPackage + " " + this.createAuthRequest().toString("base64");
94
+ return header;
95
+ }
96
+ /**
97
+ * Creates an authentication response token
98
+ *
99
+ * @param {string} inTokenHeader The www-authentication header received from the target
100
+ * in response to the authentication request
101
+ * @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
102
+ */
103
+ createAuthResponse(inTokenHeader) {
104
+ (0, debug_logger_1.debug)("Received www-authentication response", inTokenHeader);
105
+ const packageMatch = new RegExp("^" + this.securityPackage + "\\s([^,\\s]+)").exec(inTokenHeader);
106
+ if (!packageMatch) {
107
+ throw new Error("Invalid input token, missing " +
108
+ this.securityPackage +
109
+ " prefix: " +
110
+ inTokenHeader);
111
+ }
112
+ const inToken = Buffer.from(packageMatch[1], "base64");
113
+ try {
114
+ const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
115
+ if (token.length > 0) {
116
+ (0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication response token", token.toString("base64"));
117
+ }
118
+ else {
119
+ (0, debug_logger_1.debug)("No response token, authentication complete");
120
+ }
121
+ return token;
122
+ }
123
+ catch (err) {
124
+ if (err.message ===
125
+ "Could not init security context. Result: -2146893054") {
126
+ // If incoming token is for NTLMv1, this error can occur when
127
+ // LMCompatibilityLevel prevents the client to send NTLMv1 messages
128
+ if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
129
+ throw new Error("Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
130
+ "it is likely that the client is prevented from sending such messages. " +
131
+ "Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)");
132
+ }
133
+ }
134
+ throw err;
135
+ }
136
+ }
137
+ // eslint-disable-next-line require-jsdoc
138
+ isNtlmV1(type2message) {
139
+ if (type2message.length >= 24) {
140
+ const inTokenFlags = type2message.readInt32BE(20);
141
+ if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
142
+ return true;
143
+ }
144
+ }
145
+ return false;
146
+ }
147
+ /**
148
+ * Creates an authentication response header
149
+ *
150
+ * @param {string} inTokenHeader The www-authentication header received from the target
151
+ * in response to the authentication request
152
+ * @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
153
+ */
154
+ createAuthResponseHeader(inTokenHeader) {
155
+ const tokenBuffer = this.createAuthResponse(inTokenHeader);
156
+ if (tokenBuffer.length == 0) {
157
+ return "";
158
+ }
159
+ const header = this.securityPackage + " " + tokenBuffer.toString("base64");
160
+ return header;
161
+ }
162
+ }
163
+ exports.WinSso = WinSso;
164
+ WinSso.NEGOTIATE_NTLM2_KEY = 1 << 19;
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "win-sso",
3
- "version": "1.1.5",
3
+ "version": "1.1.8",
4
4
  "description": "NTLM single-sign-on for Node.js. Only Windows OS supported.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "build": "shx rm -rf dist && tsc",
8
- "lint": "tslint --project .",
8
+ "lint": "eslint src --ext ts",
9
9
  "test": "mocha --require ./test/ts.hooks.js test/**/*.spec.ts",
10
10
  "prebuildify": "node prebuildify_env.js",
11
- "native_prebuildify": "prebuildify --napi --strip",
11
+ "native_prebuildify": "prebuildify --napi --strip --target 16.14.2",
12
12
  "native_build": "node-gyp-build",
13
13
  "install": "node install_env.js"
14
14
  },
15
+ "engines": {
16
+ "node": "^14.13.1 || >=16.0.0"
17
+ },
15
18
  "repository": {
16
19
  "type": "git",
17
20
  "url": "git+https://github.com/bjowes/node-win-sso.git"
@@ -40,22 +43,26 @@
40
43
  "cross-spawn": "^7.0.3",
41
44
  "debug": "^4.3.1",
42
45
  "node-addon-api": "^4.3.0",
43
- "node-gyp": "^8.4.1",
44
- "node-gyp-build": "4.3"
46
+ "node-gyp": "^9.0.0",
47
+ "node-gyp-build": "^4.4.0"
45
48
  },
46
49
  "devDependencies": {
47
50
  "@types/debug": "^4.1.7",
48
51
  "@types/mocha": "^9.1.0",
49
- "@types/node": "^16.11.22",
52
+ "@types/node": "^17.0.25",
53
+ "@typescript-eslint/eslint-plugin": "^5.20.0",
54
+ "@typescript-eslint/parser": "^5.20.0",
50
55
  "asn1-parser": "^1.1.8",
56
+ "eslint": "^8.13.0",
57
+ "eslint-config-google": "^0.14.0",
58
+ "eslint-config-prettier": "^8.5.0",
59
+ "eslint-plugin-jsdoc": "^39.2.7",
60
+ "eslint-plugin-node": "^11.1.0",
51
61
  "mocha": "^9.2.0",
52
62
  "prebuildify": "^5.0.0",
53
63
  "shx": "^0.3.4",
54
64
  "ts-node": "^10.5.0",
55
- "tslint": "^6.1.3",
56
- "tslint-eslint-rules": "^5.4.0",
57
- "typescript": "4.5",
58
- "vrsource-tslint-rules": "^6.0.0"
65
+ "typescript": "^4.6.3"
59
66
  },
60
67
  "gypfile": true
61
68
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { WinSso } from './win-sso';
2
- import { osSupported } from './os-supported';
1
+ import { WinSso } from "./win-sso";
2
+ import { osSupported } from "./os-supported";
3
3
 
4
4
  export { WinSso, osSupported };
@@ -1,9 +1,11 @@
1
- import os from 'os';
1
+ import os from "os";
2
2
 
3
3
  /**
4
4
  * Returns true if the current platform is supported by the win-sso module. Only Windows OSs are supported.
5
5
  * If false, all other methods in the module will throw.
6
+ *
7
+ * @returns {boolean} Is the os supported
6
8
  */
7
9
  export function osSupported(): boolean {
8
- return (os.platform() === 'win32');
10
+ return os.platform() === "win32";
9
11
  }
package/src/win-sso.ts CHANGED
@@ -23,9 +23,11 @@ export class WinSso {
23
23
  * Creates an authentication context for SSO.
24
24
  * This allocates memory buffers, the freeAuthContext method should be called
25
25
  * to free them (on error or after authentication is no longer needed)
26
- * @param securityPackage {string} The name of the security package (NTLM or Negotiate)
27
- * @param targetHost {string | undefind} The FQDN hostname of the target (optional)
28
- * @param peerCert {PeerCertificate | undefined} The certificate of the target server (optional, for HTTPS channel binding)
26
+ *
27
+ * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
28
+ * @param {string | undefined} targetHost The FQDN hostname of the target (optional)
29
+ * @param {PeerCertificate | undefined} peerCert The certificate of the target server
30
+ * (optional, for HTTPS channel binding)
29
31
  */
30
32
  constructor(
31
33
  securityPackage: string,
@@ -51,6 +53,7 @@ export class WinSso {
51
53
 
52
54
  /**
53
55
  * Retrieves the username of the logged in user
56
+ *
54
57
  * @returns {string} user name including domain
55
58
  */
56
59
  static getLogonUserName(): string {
@@ -59,14 +62,16 @@ export class WinSso {
59
62
 
60
63
  /**
61
64
  * Transforms target TLS certificate into a channel binding application data buffer
62
- * @param peerCert {PeerCertificate} Target TLS certificate
65
+ *
66
+ * @param {PeerCertificate} peerCert Target TLS certificate
67
+ * @returns {Buffer} Application data buffer
63
68
  */
64
69
  private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
65
- let cert: any = peerCert;
66
- let hash = cert.fingerprint256.replace(/:/g, "");
67
- let hashBuf = Buffer.from(hash, "hex");
68
- let tlsServerEndPoint = "tls-server-end-point:";
69
- let applicationDataBuffer = Buffer.alloc(
70
+ const cert: any = peerCert;
71
+ const hash = cert.fingerprint256.replace(/:/g, "");
72
+ const hashBuf = Buffer.from(hash, "hex");
73
+ const tlsServerEndPoint = "tls-server-end-point:";
74
+ const applicationDataBuffer = Buffer.alloc(
70
75
  tlsServerEndPoint.length + hashBuf.length
71
76
  );
72
77
  applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
@@ -85,10 +90,11 @@ export class WinSso {
85
90
 
86
91
  /**
87
92
  * Creates an authentication request token
93
+ *
88
94
  * @returns {Buffer} Raw token buffer
89
95
  */
90
96
  createAuthRequest(): Buffer {
91
- let token = winSsoAddon.createAuthRequest(this.authContextId);
97
+ const token = winSsoAddon.createAuthRequest(this.authContextId);
92
98
  debug(
93
99
  "Created " + this.securityPackage + " authentication request token",
94
100
  token.toString("base64")
@@ -98,22 +104,25 @@ export class WinSso {
98
104
 
99
105
  /**
100
106
  * Creates an authentication request header
107
+ *
101
108
  * @returns {string} The www-authenticate header
102
109
  */
103
110
  createAuthRequestHeader(): string {
104
- let header =
111
+ const header =
105
112
  this.securityPackage + " " + this.createAuthRequest().toString("base64");
106
113
  return header;
107
114
  }
108
115
 
109
116
  /**
110
117
  * Creates an authentication response token
111
- * @param inTokenHeader {string} The www-authentication header received from the target in reponse to the authentication request
118
+ *
119
+ * @param {string} inTokenHeader The www-authentication header received from the target
120
+ * in response to the authentication request
112
121
  * @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
113
122
  */
114
123
  createAuthResponse(inTokenHeader: string): Buffer {
115
124
  debug("Received www-authentication response", inTokenHeader);
116
- let packageMatch = new RegExp(
125
+ const packageMatch = new RegExp(
117
126
  "^" + this.securityPackage + "\\s([^,\\s]+)"
118
127
  ).exec(inTokenHeader);
119
128
 
@@ -125,9 +134,9 @@ export class WinSso {
125
134
  inTokenHeader
126
135
  );
127
136
  }
128
- let inToken = Buffer.from(packageMatch[1], "base64");
137
+ const inToken = Buffer.from(packageMatch[1], "base64");
129
138
  try {
130
- let token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
139
+ const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
131
140
  if (token.length > 0) {
132
141
  debug(
133
142
  "Created " + this.securityPackage + " authentication response token",
@@ -142,8 +151,9 @@ export class WinSso {
142
151
  (err as Error).message ===
143
152
  "Could not init security context. Result: -2146893054"
144
153
  ) {
145
- // If incoming token is for NTLMv1, this error can occur when LMCompatibilityLevel prevents the client to send NTLMv1 messages
146
- if (this.securityPackage === "NTLM" && this.IsNtlmV1(inToken)) {
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)) {
147
157
  throw new Error(
148
158
  "Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
149
159
  "it is likely that the client is prevented from sending such messages. " +
@@ -155,7 +165,8 @@ export class WinSso {
155
165
  }
156
166
  }
157
167
 
158
- private IsNtlmV1(type2message: Buffer): boolean {
168
+ // eslint-disable-next-line require-jsdoc
169
+ private isNtlmV1(type2message: Buffer): boolean {
159
170
  if (type2message.length >= 24) {
160
171
  const inTokenFlags = type2message.readInt32BE(20);
161
172
  if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
@@ -167,7 +178,9 @@ export class WinSso {
167
178
 
168
179
  /**
169
180
  * Creates an authentication response header
170
- * @param inTokenHeader {string} The www-authentication header received from the target in reponse to the authentication request
181
+ *
182
+ * @param {string} inTokenHeader The www-authentication header received from the target
183
+ * in response to the authentication request
171
184
  * @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
172
185
  */
173
186
  createAuthResponseHeader(inTokenHeader: string): string {
@@ -175,7 +188,7 @@ export class WinSso {
175
188
  if (tokenBuffer.length == 0) {
176
189
  return "";
177
190
  }
178
- let header = this.securityPackage + " " + tokenBuffer.toString("base64");
191
+ const header = this.securityPackage + " " + tokenBuffer.toString("base64");
179
192
  return header;
180
193
  }
181
194
  }