win-sso 2.0.0-beta.1 → 2.0.0-beta.2

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/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { WinSso } from "./win-sso.js";
2
+ import { osSupported } from "./os-supported.js";
3
+ export { WinSso, osSupported };
@@ -0,0 +1,9 @@
1
+ import os from "node:os";
2
+ /**
3
+ * Returns true if the current platform is supported by the win-sso module. Only Windows OSs are supported.
4
+ * If false, all other methods in the module will throw.
5
+ * @returns {boolean} Is the os supported
6
+ */
7
+ export function osSupported() {
8
+ return os.platform() === "win32";
9
+ }
@@ -0,0 +1,2 @@
1
+ import debugInit from 'debug';
2
+ export const debug = debugInit('win-sso');
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,155 @@
1
+ import { debug } from "./utils/debug.logger.js";
2
+ import path from "node:path";
3
+ let winSsoAddon;
4
+ try {
5
+ const { default: load } = await import("node-gyp-build");
6
+ const currentDir = import.meta.dirname; // eslint-disable-line
7
+ winSsoAddon = load(path.join(currentDir, "..", "native"));
8
+ debug("Loaded win-sso native module");
9
+ }
10
+ catch (err) {
11
+ debug("Could not load win-sso native module", { cause: err });
12
+ }
13
+ /**
14
+ * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
15
+ */
16
+ export class WinSso {
17
+ static NEGOTIATE_NTLM2_KEY = 1 << 19;
18
+ authContextId;
19
+ securityPackage;
20
+ /**
21
+ * Creates an authentication context for SSO.
22
+ * This allocates memory buffers, the freeAuthContext method should be called
23
+ * to free them (on error or after authentication is no longer needed)
24
+ * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
25
+ * @param {string|undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
26
+ * @param {PeerCertificate|undefined} peerCert The certificate of the target server
27
+ * (optional, for HTTPS channel binding)
28
+ * @param {number|undefined} flags Flags to set in the authentication context
29
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
30
+ * (optional, allows customizing security features)
31
+ */
32
+ constructor(securityPackage, targetHost, peerCert, flags) {
33
+ this.securityPackage = securityPackage;
34
+ let applicationData;
35
+ if (!targetHost) {
36
+ targetHost = "";
37
+ }
38
+ if (peerCert) {
39
+ applicationData = this.getChannelBindingsApplicationData(peerCert);
40
+ }
41
+ else {
42
+ applicationData = Buffer.alloc(0);
43
+ }
44
+ this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, flags);
45
+ }
46
+ /**
47
+ * Retrieves the username of the logged in user
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
+ * @param {PeerCertificate} peerCert Target TLS certificate
56
+ * @returns {Buffer} Application data buffer
57
+ */
58
+ getChannelBindingsApplicationData(peerCert) {
59
+ const hash = peerCert.fingerprint256.replace(/:/g, "");
60
+ const hashBuf = Buffer.from(hash, "hex");
61
+ const tlsServerEndPoint = "tls-server-end-point:";
62
+ const applicationDataBuffer = Buffer.alloc(tlsServerEndPoint.length + hashBuf.length);
63
+ applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
64
+ hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
65
+ return applicationDataBuffer;
66
+ }
67
+ /**
68
+ * Releases all allocated resources for the authorization context.
69
+ * Should be called when the context is no longer required, such as when the
70
+ * socket was closed.
71
+ */
72
+ freeAuthContext() {
73
+ winSsoAddon.freeAuthContext(this.authContextId);
74
+ }
75
+ /**
76
+ * Creates an authentication request token
77
+ * @returns {Buffer} Raw token buffer
78
+ */
79
+ createAuthRequest() {
80
+ const token = winSsoAddon.createAuthRequest(this.authContextId);
81
+ debug("Created " + this.securityPackage + " authentication request token", token.toString("base64"));
82
+ return token;
83
+ }
84
+ /**
85
+ * Creates an authentication request header
86
+ * @returns {string} The www-authenticate header
87
+ */
88
+ createAuthRequestHeader() {
89
+ const header = this.securityPackage + " " + this.createAuthRequest().toString("base64");
90
+ return header;
91
+ }
92
+ /**
93
+ * Creates an authentication response token
94
+ * @param {string} inTokenHeader The www-authentication header received from the target
95
+ * in response to the authentication request
96
+ * @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
97
+ */
98
+ createAuthResponse(inTokenHeader) {
99
+ debug("Received www-authentication response", inTokenHeader);
100
+ const packageMatch = new RegExp("^" + this.securityPackage + "\\s([^,\\s]+)").exec(inTokenHeader);
101
+ if (!packageMatch) {
102
+ throw new Error("Invalid input token, missing " +
103
+ this.securityPackage +
104
+ " prefix: " +
105
+ inTokenHeader);
106
+ }
107
+ const inToken = Buffer.from(packageMatch[1], "base64");
108
+ try {
109
+ const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
110
+ if (token.length > 0) {
111
+ debug("Created " + this.securityPackage + " authentication response token", token.toString("base64"));
112
+ }
113
+ else {
114
+ debug("No response token, authentication complete");
115
+ }
116
+ return token;
117
+ }
118
+ catch (err) {
119
+ if (err.message ===
120
+ "Could not init security context. Result: -2146893054") {
121
+ // If incoming token is for NTLMv1, this error can occur when
122
+ // LMCompatibilityLevel prevents the client to send NTLMv1 messages
123
+ if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
124
+ throw new Error("Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
125
+ "it is likely that the client is prevented from sending such messages. " +
126
+ "Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)", { cause: err });
127
+ }
128
+ }
129
+ throw err;
130
+ }
131
+ }
132
+ isNtlmV1(type2message) {
133
+ if (type2message.length >= 24) {
134
+ const inTokenFlags = type2message.readInt32BE(20);
135
+ if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
136
+ return true;
137
+ }
138
+ }
139
+ return false;
140
+ }
141
+ /**
142
+ * Creates an authentication response header
143
+ * @param {string} inTokenHeader The www-authentication header received from the target
144
+ * in response to the authentication request
145
+ * @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
146
+ */
147
+ createAuthResponseHeader(inTokenHeader) {
148
+ const tokenBuffer = this.createAuthResponse(inTokenHeader);
149
+ if (tokenBuffer.length == 0) {
150
+ return "";
151
+ }
152
+ const header = this.securityPackage + " " + tokenBuffer.toString("base64");
153
+ return header;
154
+ }
155
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "win-sso",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.2",
4
4
  "description": "NTLM single-sign-on for Node.js. Only Windows OS supported.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -9,11 +9,12 @@
9
9
  "test": "mocha test/**/*.spec.ts",
10
10
  "typecheck": "tsc --noEmit",
11
11
  "prebuildify": "node native/prebuildify_env.ts",
12
- "native_prebuildify": "prebuildify --napi --strip --target 22.18.0 --cwd native"
12
+ "native_prebuildify": "prebuildify --napi --strip --target 22.22.2 --cwd native",
13
+ "prepare": "tsc -p tsconfig.publish.json"
13
14
  },
14
15
  "type": "module",
15
16
  "engines": {
16
- "node": ">=22.18.0 || >=24.0.0 || >=26.0.0"
17
+ "node": ">=22.22.2 || >=24.15.0 || >=26.0.0"
17
18
  },
18
19
  "repository": {
19
20
  "type": "git",
@@ -36,9 +37,7 @@
36
37
  },
37
38
  "homepage": "https://github.com/bjowes/node-win-sso#readme",
38
39
  "dependencies": {
39
- "bindings": "^1.5.0",
40
- "debug": "^4.4.3",
41
- "node-gyp": "^13.1.0",
40
+ "debug": "^4.4.3",
42
41
  "node-gyp-build": "^4.8.4"
43
42
  },
44
43
  "devDependencies": {
@@ -47,16 +46,17 @@
47
46
  "@types/debug": "^4.1.13",
48
47
  "@types/mocha": "^10.0.10",
49
48
  "@types/node": "^26.6.6",
49
+ "bindings": "^1.5.0",
50
50
  "cross-spawn": "^7.0.6",
51
51
  "eslint": "^10.15.0",
52
52
  "eslint-plugin-jsdoc": "^65.4.7",
53
53
  "eslint-plugin-n": "^18.5.0",
54
54
  "mocha": "^12.0.3",
55
55
  "node-addon-api": "^8.9.2",
56
+ "node-gyp": "^13.1.0",
56
57
  "prebuildify": "^6.0.1",
57
58
  "shx": "^0.4.0",
58
59
  "typescript": "^6.0.3",
59
60
  "typescript-eslint": "^8.72.2"
60
- },
61
- "gypfile": true
61
+ }
62
62
  }