win-sso 1.2.0 → 1.3.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/README.md CHANGED
@@ -68,14 +68,14 @@ Utility method to simplify the case where an application supports multiple platf
68
68
 
69
69
  This class provides an interface to create and use an authentication context. Due to protocol details, one instance of this class should be created for each connection (socket) communicating with an endpoint where single-sign-on should be used. The class will free the resources it has allocated when it is destroyed, but it is still recommended to call the freeAuthContext() method when the instance will no longer be used.
70
70
 
71
- #### WinSso(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, delegate: boolean)
71
+ #### WinSso(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, flags: number | undefined)
72
72
 
73
73
  Class constructor.
74
74
 
75
75
  * securityPackage: The name of the authentication method to use. Valid values are 'NTLM' and 'Negotiate'.
76
76
  * targetHost (optional): The FQDN of the target host. This is used to build a SPN string in the authentication message for enhanced security.
77
77
  * peerCert (optional): If the connection is http, pass undefined. If the connection is https, pass the peer certificate to add Channel Binding to the authentication message for enhanced security.
78
- * delegate: If the negoatiated token should support delegation of credentials. Requires Kerberos (Negotiate).
78
+ * flags: Flags to set in the authentication context. If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
79
79
 
80
80
  #### WinSso.getUserName(): string
81
81
 
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.osSupported = void 0;
6
+ exports.osSupported = osSupported;
7
7
  const os_1 = __importDefault(require("os"));
8
8
  /**
9
9
  * Returns true if the current platform is supported by the win-sso module. Only Windows OSs are supported.
@@ -14,4 +14,3 @@ const os_1 = __importDefault(require("os"));
14
14
  function osSupported() {
15
15
  return os_1.default.platform() === "win32";
16
16
  }
17
- exports.osSupported = osSupported;
@@ -1,2 +1 @@
1
- /// <reference types="debug" />
2
1
  export declare const debug: import("debug").Debugger;
package/dist/win-sso.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { PeerCertificate } from "tls";
4
2
  /**
5
3
  * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
@@ -15,10 +13,13 @@ export declare class WinSso {
15
13
  *
16
14
  * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
17
15
  * @param {string | undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
18
- * @param {PeerCertificate | undefined} peerCert The certificate of the target server (optional, for HTTPS channel binding)
19
- * @param {string} delegate The tokens created should allow delegation of credentials. Only supported by Kerberos
16
+ * @param {PeerCertificate | undefined} peerCert The certificate of the target server
17
+ * (optional, for HTTPS channel binding)
18
+ * @param {number | undefined} flags Flags to set in the authentication context
19
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
20
+ * (optional, allows customizing security features)
20
21
  */
21
- constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, delegate: boolean);
22
+ constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, flags: number | undefined);
22
23
  /**
23
24
  * Retrieves the username of the logged in user
24
25
  *
package/dist/win-sso.js CHANGED
@@ -25,10 +25,13 @@ class WinSso {
25
25
  *
26
26
  * @param {string} securityPackage The name of the security package (NTLM or Negotiate)
27
27
  * @param {string | undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
28
- * @param {PeerCertificate | undefined} peerCert The certificate of the target server (optional, for HTTPS channel binding)
29
- * @param {string} delegate The tokens created should allow delegation of credentials. Only supported by Kerberos
28
+ * @param {PeerCertificate | undefined} peerCert The certificate of the target server
29
+ * (optional, for HTTPS channel binding)
30
+ * @param {number | undefined} flags Flags to set in the authentication context
31
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
32
+ * (optional, allows customizing security features)
30
33
  */
31
- constructor(securityPackage, targetHost, peerCert, delegate) {
34
+ constructor(securityPackage, targetHost, peerCert, flags) {
32
35
  this.securityPackage = securityPackage;
33
36
  let applicationData;
34
37
  if (!targetHost) {
@@ -40,7 +43,7 @@ class WinSso {
40
43
  else {
41
44
  applicationData = Buffer.alloc(0);
42
45
  }
43
- this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, delegate);
46
+ this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, flags);
44
47
  }
45
48
  /**
46
49
  * Retrieves the username of the logged in user
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "win-sso",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
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": "eslint src --ext ts",
8
+ "lint": "eslint src",
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 --target 16.14.2",
11
+ "native_prebuildify": "prebuildify --napi --strip --target 18.18.0",
12
12
  "native_build": "node-gyp-build",
13
13
  "install": "node install_env.js"
14
14
  },
15
15
  "engines": {
16
- "node": "^14.13.1 || >=16.0.0"
16
+ "node": "^18.18.0 || ^20.9.0 || >=21.1.0"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
@@ -42,27 +42,24 @@
42
42
  "bindings": "*",
43
43
  "cross-spawn": "^7.0.3",
44
44
  "debug": "^4.3.1",
45
- "node-gyp": "^9.0.0",
45
+ "node-gyp": "^11.0.0",
46
46
  "node-gyp-build": "^4.4.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/debug": "^4.1.7",
50
- "@types/mocha": "^9.1.0",
51
- "@types/node": "^18.7.18",
52
- "@typescript-eslint/eslint-plugin": "^5.20.0",
53
- "@typescript-eslint/parser": "^5.20.0",
50
+ "@types/mocha": "^10.0.10",
51
+ "@types/node": "^22.10.10",
54
52
  "asn1-parser": "^1.1.8",
55
- "eslint": "^8.13.0",
56
- "eslint-config-google": "^0.14.0",
57
- "eslint-config-prettier": "^8.5.0",
58
- "eslint-plugin-jsdoc": "^39.2.7",
53
+ "eslint": "^9.19.0",
54
+ "eslint-plugin-jsdoc": "^50.6.3",
59
55
  "eslint-plugin-node": "^11.1.0",
60
- "mocha": "^10.0.0",
61
- "node-addon-api": "^5.0.0",
62
- "prebuildify": "^5.0.0",
56
+ "mocha": "^11.1.0",
57
+ "node-addon-api": "^8.3.0",
58
+ "prebuildify": "^6.0.1",
63
59
  "shx": "^0.3.4",
64
60
  "ts-node": "^10.5.0",
65
- "typescript": "^4.6.3"
61
+ "typescript": "^5.7.3",
62
+ "typescript-eslint": "^8.22.0"
66
63
  },
67
64
  "gypfile": true
68
65
  }
@@ -2,14 +2,11 @@
2
2
  #include "secur32-facade.hh"
3
3
  #include "exception-handler.hh"
4
4
 
5
- AuthContext::AuthContext(bool delegate) {
5
+ AuthContext::AuthContext() {
6
6
  credHandle = {};
7
7
  lifeTime = {};
8
8
  ctxHandle = {};
9
- flags = ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT;
10
- if (delegate) {
11
- flags |= ISC_REQ_DELEGATE;
12
- }
9
+ flags = 0;
13
10
  maxTokenLength = 0;
14
11
  outToken = nullptr;
15
12
  outTokenLength = 0;
@@ -45,7 +42,8 @@ void AuthContext::Cleanup(Napi::Env* env) {
45
42
  }
46
43
  }
47
44
 
48
- bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env) {
45
+ bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, unsigned long flags, Napi::Env* env) {
46
+ this->flags = flags;
49
47
  auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
50
48
  packageName[packageNameLen] = '\0';
51
49
  targetHostname = *targetHost;
@@ -35,11 +35,11 @@ class AuthContext {
35
35
  bool ctxHandleAllocated;
36
36
 
37
37
  public:
38
- AuthContext(bool delegate);
38
+ AuthContext();
39
39
  virtual ~AuthContext();
40
40
  void Cleanup(Napi::Env* env);
41
41
 
42
- bool Init(std::string* packageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, Napi::Env* env);
42
+ bool Init(std::string* packageName, std::string* targetHost, Napi::Buffer<unsigned char>& applicationDataBuffer, unsigned long flags, Napi::Env* env);
43
43
  bool InitContext(Napi::Env* env);
44
44
  bool HandleResponse(Napi::Buffer<unsigned char>& inTokenBuffer, Napi::Env* env);
45
45
 
@@ -121,6 +121,13 @@ void Secur32Facade::FreeContextHandle(struct _SecHandle* ctxHandle, Napi::Env* e
121
121
  }
122
122
  }
123
123
 
124
+ unsigned long Secur32Facade::GetDefaultFlags(std::string securityPackageName) {
125
+ if (securityPackageName == "Negotiate" || securityPackageName == "Kerberos") {
126
+ return ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT;
127
+ }
128
+ return 0;
129
+ }
130
+
124
131
  Napi::Number Secur32Facade::CreateAuthContext(const Napi::CallbackInfo& info) {
125
132
  Napi::Env env = info.Env();
126
133
  if (info.Length() < 4) {
@@ -128,17 +135,17 @@ Napi::Number Secur32Facade::CreateAuthContext(const Napi::CallbackInfo& info) {
128
135
  return Napi::Number::New(env, 0);
129
136
  }
130
137
 
131
- if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer() || !info[3].IsBoolean()) {
138
+ if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer() || !(info[3].IsUndefined() || info[3].IsNumber())) {
132
139
  ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
133
140
  return Napi::Number::New(env, 0);
134
141
  }
135
142
  auto securityPackageName = info[0].ToString();
136
143
  auto targetHost = info[1].ToString();
137
144
  auto applicationDataBuffer = info[2].As<Napi::Buffer<unsigned char>>();
138
- auto delegate = info[3].ToBoolean();
145
+ auto flags = info[3].IsNumber() ? (unsigned long)(info[3].ToNumber().Uint32Value()) : GetDefaultFlags(securityPackageName);
139
146
 
140
- auto ac = std::make_shared<AuthContext>(delegate);
141
- ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, &env);
147
+ auto ac = std::make_shared<AuthContext>();
148
+ ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, flags, &env);
142
149
  acKey++;
143
150
  acMap[acKey] = ac;
144
151
  return Napi::Number::New(env, acKey);
@@ -48,6 +48,9 @@ class Secur32Facade {
48
48
  Napi::Env* env);
49
49
  static void FreeContextHandle(struct _SecHandle* ctxHandle, Napi::Env* env);
50
50
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
51
+
52
+ private:
53
+ static unsigned long GetDefaultFlags(std::string securityPackageName);
51
54
  };
52
55
 
53
56
  #endif
@@ -3,8 +3,7 @@ import os from "os";
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
+ * @returns Is the os supported
8
7
  */
9
8
  export function osSupported(): boolean {
10
9
  return os.platform() === "win32";
@@ -0,0 +1,15 @@
1
+ export interface IWinSsoAddon {
2
+ createAuthContext(
3
+ securityPackage: string,
4
+ targetHost: string | undefined,
5
+ applicationData: Buffer,
6
+ flags: number | undefined
7
+ ): number,
8
+
9
+ freeAuthContext(authContextId: number): void,
10
+
11
+ getLogonUserName(): string,
12
+
13
+ createAuthRequest(authContextId: number): Buffer,
14
+ createAuthResponse(authContextId: number, inToken: Buffer): Buffer,
15
+ }
package/src/win-sso.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import { debug } from "./utils/debug.logger";
2
2
  import { PeerCertificate } from "tls";
3
3
  import path from "path";
4
+ import { IWinSsoAddon } from "./utils/i.win-sso-addon";
4
5
 
5
- let winSsoAddon: any;
6
+ let winSsoAddon: IWinSsoAddon;
6
7
  try {
8
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
7
9
  winSsoAddon = require("node-gyp-build")(path.join(__dirname, ".."));
8
10
  debug("Loaded win-sso native module");
9
- } catch (err) {
11
+ } catch {
10
12
  debug("Could not load win-sso native module");
11
13
  }
12
14
 
@@ -23,17 +25,19 @@ export class WinSso {
23
25
  * Creates an authentication context for SSO.
24
26
  * This allocates memory buffers, the freeAuthContext method should be called
25
27
  * to free them (on error or after authentication is no longer needed)
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 for NTLM, required for Kerberos)
29
- * @param {PeerCertificate | undefined} peerCert The certificate of the target server (optional, for HTTPS channel binding)
30
- * @param {string} delegate The tokens created should allow delegation of credentials. Only supported by Kerberos
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)
31
35
  */
32
36
  constructor(
33
37
  securityPackage: string,
34
38
  targetHost: string | undefined,
35
39
  peerCert: PeerCertificate | undefined,
36
- delegate: boolean
40
+ flags: number | undefined
37
41
  ) {
38
42
  this.securityPackage = securityPackage;
39
43
  let applicationData: Buffer;
@@ -49,14 +53,13 @@ export class WinSso {
49
53
  securityPackage,
50
54
  targetHost,
51
55
  applicationData,
52
- delegate
56
+ flags
53
57
  );
54
58
  }
55
59
 
56
60
  /**
57
61
  * Retrieves the username of the logged in user
58
- *
59
- * @returns {string} user name including domain
62
+ * @returns user name including domain
60
63
  */
61
64
  static getLogonUserName(): string {
62
65
  return winSsoAddon.getLogonUserName();
@@ -64,13 +67,11 @@ export class WinSso {
64
67
 
65
68
  /**
66
69
  * Transforms target TLS certificate into a channel binding application data buffer
67
- *
68
- * @param {PeerCertificate} peerCert Target TLS certificate
69
- * @returns {Buffer} Application data buffer
70
+ * @param peerCert Target TLS certificate
71
+ * @returns Application data buffer
70
72
  */
71
73
  private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
72
- const cert: any = peerCert;
73
- const hash = cert.fingerprint256.replace(/:/g, "");
74
+ const hash = peerCert.fingerprint256.replace(/:/g, "");
74
75
  const hashBuf = Buffer.from(hash, "hex");
75
76
  const tlsServerEndPoint = "tls-server-end-point:";
76
77
  const applicationDataBuffer = Buffer.alloc(
@@ -92,8 +93,7 @@ export class WinSso {
92
93
 
93
94
  /**
94
95
  * Creates an authentication request token
95
- *
96
- * @returns {Buffer} Raw token buffer
96
+ * @returns Raw token buffer
97
97
  */
98
98
  createAuthRequest(): Buffer {
99
99
  const token = winSsoAddon.createAuthRequest(this.authContextId);
@@ -106,8 +106,7 @@ export class WinSso {
106
106
 
107
107
  /**
108
108
  * Creates an authentication request header
109
- *
110
- * @returns {string} The www-authenticate header
109
+ * @returns The www-authenticate header
111
110
  */
112
111
  createAuthRequestHeader(): string {
113
112
  const header =
@@ -117,10 +116,9 @@ export class WinSso {
117
116
 
118
117
  /**
119
118
  * Creates an authentication response token
120
- *
121
- * @param {string} inTokenHeader The www-authentication header received from the target
119
+ * @param inTokenHeader The www-authentication header received from the target
122
120
  * in response to the authentication request
123
- * @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
121
+ * @returns Raw token buffer. May be empty if Negotiate handshake is complete.
124
122
  */
125
123
  createAuthResponse(inTokenHeader: string): Buffer {
126
124
  debug("Received www-authentication response", inTokenHeader);
@@ -167,7 +165,6 @@ export class WinSso {
167
165
  }
168
166
  }
169
167
 
170
- // eslint-disable-next-line require-jsdoc
171
168
  private isNtlmV1(type2message: Buffer): boolean {
172
169
  if (type2message.length >= 24) {
173
170
  const inTokenFlags = type2message.readInt32BE(20);
@@ -180,10 +177,9 @@ export class WinSso {
180
177
 
181
178
  /**
182
179
  * Creates an authentication response header
183
- *
184
- * @param {string} inTokenHeader The www-authentication header received from the target
180
+ * @param inTokenHeader The www-authentication header received from the target
185
181
  * in response to the authentication request
186
- * @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
182
+ * @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
187
183
  */
188
184
  createAuthResponseHeader(inTokenHeader: string): string {
189
185
  const tokenBuffer = this.createAuthResponse(inTokenHeader);
Binary file
Binary file