win-sso 1.1.8 → 1.3.0

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
@@ -25,7 +25,7 @@ Usage of this module requires some knowledge of the NTLM or Negotiate handshake
25
25
  1. Client: Send http request to server (with FQDN `app.server.com`)
26
26
  2. Server: Respond with 401 where the `www-authenticate` header includes NTLM
27
27
  3. Client: Create WinSso instance for NTLM:
28
- `let winSso = new WinSso('NTLM', 'app.server.com', undefined);`
28
+ `let winSso = new WinSso('NTLM', 'app.server.com', undefined, false);`
29
29
  4. Client: Get authentication request token:
30
30
  `let authRequestToken = winSso.createAuthRequestHeader();`
31
31
  5. Client: Resend the http request to the server, setting the `authorization` header to the value of the token
@@ -42,7 +42,7 @@ The NTLM handshake always follows this pattern.
42
42
  1. Client: Send http request to server (with FQDN `app.server.com`)
43
43
  2. Server: Respond with 401 where the `www-authenticate` header includes `Negotiate`
44
44
  3. Client: Create WinSso instance for Negotiate:
45
- `let winSso = new WinSso('Negotiate', 'app.server.com', undefined);`
45
+ `let winSso = new WinSso('Negotiate', 'app.server.com', undefined, false);`
46
46
  4. Client: Get authentication request token:
47
47
  `let authRequestToken = winSso.createAuthRequestHeader();`
48
48
  5. Client: Resend the http request to the server, setting the `authorization` header to the value of the token
@@ -68,13 +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)
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
+ * 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
78
79
 
79
80
  #### WinSso.getUserName(): string
80
81
 
package/dist/win-sso.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { PeerCertificate } from "tls";
3
4
  /**
4
5
  * Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
@@ -13,11 +14,14 @@ export declare class WinSso {
13
14
  * to free them (on error or after authentication is no longer needed)
14
15
  *
15
16
  * @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 {string | undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
17
18
  * @param {PeerCertificate | undefined} peerCert The certificate of the target server
18
19
  * (optional, for HTTPS channel binding)
20
+ * @param {number | undefined} flags Flags to set in the authentication context
21
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
22
+ * (optional, allows customizing security features)
19
23
  */
20
- constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined);
24
+ constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, flags: number | undefined);
21
25
  /**
22
26
  * Retrieves the username of the logged in user
23
27
  *
package/dist/win-sso.js CHANGED
@@ -24,11 +24,14 @@ class WinSso {
24
24
  * to free them (on error or after authentication is no longer needed)
25
25
  *
26
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)
27
+ * @param {string | undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
28
28
  * @param {PeerCertificate | undefined} peerCert The certificate of the target server
29
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) {
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);
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,6 +1,6 @@
1
1
  {
2
2
  "name": "win-sso",
3
- "version": "1.1.8",
3
+ "version": "1.3.0",
4
4
  "description": "NTLM single-sign-on for Node.js. Only Windows OS supported.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -42,14 +42,13 @@
42
42
  "bindings": "*",
43
43
  "cross-spawn": "^7.0.3",
44
44
  "debug": "^4.3.1",
45
- "node-addon-api": "^4.3.0",
46
45
  "node-gyp": "^9.0.0",
47
46
  "node-gyp-build": "^4.4.0"
48
47
  },
49
48
  "devDependencies": {
50
49
  "@types/debug": "^4.1.7",
51
50
  "@types/mocha": "^9.1.0",
52
- "@types/node": "^17.0.25",
51
+ "@types/node": "^18.7.18",
53
52
  "@typescript-eslint/eslint-plugin": "^5.20.0",
54
53
  "@typescript-eslint/parser": "^5.20.0",
55
54
  "asn1-parser": "^1.1.8",
@@ -58,7 +57,8 @@
58
57
  "eslint-config-prettier": "^8.5.0",
59
58
  "eslint-plugin-jsdoc": "^39.2.7",
60
59
  "eslint-plugin-node": "^11.1.0",
61
- "mocha": "^9.2.0",
60
+ "mocha": "^10.0.0",
61
+ "node-addon-api": "^5.0.0",
62
62
  "prebuildify": "^5.0.0",
63
63
  "shx": "^0.3.4",
64
64
  "ts-node": "^10.5.0",
Binary file
Binary file
@@ -6,14 +6,15 @@ AuthContext::AuthContext() {
6
6
  credHandle = {};
7
7
  lifeTime = {};
8
8
  ctxHandle = {};
9
- ctxAttributes = 0;
9
+ flags = 0;
10
10
  maxTokenLength = 0;
11
- outToken = 0;
11
+ outToken = nullptr;
12
12
  outTokenLength = 0;
13
13
  credHandleAllocated = false;
14
14
  ctxHandleAllocated = false;
15
15
  channelBindings = {};
16
16
  channelBindingsLength = 0;
17
+ targetHostnameSpn = nullptr;
17
18
  };
18
19
 
19
20
  AuthContext::~AuthContext() {
@@ -33,14 +34,24 @@ void AuthContext::Cleanup(Napi::Env* env) {
33
34
  }
34
35
  if (outToken) {
35
36
  delete outToken;
36
- outToken = 0;
37
+ outToken = nullptr;
38
+ }
39
+ if (targetHostnameSpn) {
40
+ delete targetHostnameSpn;
41
+ targetHostnameSpn = nullptr;;
37
42
  }
38
43
  }
39
44
 
40
- 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;
41
47
  auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
42
48
  packageName[packageNameLen] = '\0';
43
49
  targetHostname = *targetHost;
50
+ if (targetHostname.length() > 0) {
51
+ int len = targetHostname.length() + 5 + 1;
52
+ targetHostnameSpn = new char[len];
53
+ strncpy(targetHostnameSpn, ("HTTP/" + targetHostname).c_str(), len);
54
+ }
44
55
  maxTokenLength = Secur32Facade::GetMaxTokenLength(packageName, env);
45
56
  SetupChannelBindings(applicationDataBuffer);
46
57
  Secur32Facade::AcquireCredentialsHandle(packageName, &credHandle, &lifeTime, env);
@@ -81,7 +92,7 @@ bool AuthContext::InitContext(Napi::Env* env) {
81
92
  outSecBuff.pvBuffer = outToken;
82
93
 
83
94
  int result = Secur32Facade::InitializeSecurityContext(
84
- NULL, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
95
+ NULL, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
85
96
  if (env->IsExceptionPending()) {
86
97
  Cleanup(env);
87
98
  return false;
@@ -127,7 +138,7 @@ bool AuthContext::HandleResponse(
127
138
  inSecBuffers[1].pvBuffer = &channelBindings;
128
139
  }
129
140
 
130
- auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
141
+ auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
131
142
  if (env->IsExceptionPending()) {
132
143
  Cleanup(env);
133
144
  return false;
@@ -149,11 +160,3 @@ Napi::Buffer<unsigned char> AuthContext::OutToken(Napi::Env* env) {
149
160
  return Napi::Buffer<unsigned char>::Buffer();
150
161
  }
151
162
  }
152
-
153
- std::string* AuthContext::TargetHostnameRef() {
154
- std::string* targetHostnameRef = NULL;
155
- if (targetHostname.length() > 0) {
156
- targetHostnameRef = &targetHostname;
157
- }
158
- return targetHostnameRef;
159
- }
@@ -14,10 +14,11 @@ class AuthContext {
14
14
  public:
15
15
  char packageName[128];
16
16
  std::string targetHostname;
17
+ char* targetHostnameSpn;
17
18
  CredHandle credHandle;
18
19
  SECURITY_INTEGER lifeTime;
19
20
  struct _SecHandle ctxHandle;
20
- unsigned long ctxAttributes;
21
+ unsigned long flags;
21
22
  unsigned long maxTokenLength;
22
23
  unsigned char* outToken;
23
24
  unsigned long outTokenLength;
@@ -38,7 +39,7 @@ class AuthContext {
38
39
  virtual ~AuthContext();
39
40
  void Cleanup(Napi::Env* env);
40
41
 
41
- 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);
42
43
  bool InitContext(Napi::Env* env);
43
44
  bool HandleResponse(Napi::Buffer<unsigned char>& inTokenBuffer, Napi::Env* env);
44
45
 
@@ -46,7 +47,6 @@ class AuthContext {
46
47
 
47
48
  private:
48
49
  void SetupChannelBindings(Napi::Buffer<unsigned char>& applicationDataBuffer);
49
- std::string* TargetHostnameRef();
50
50
  };
51
51
 
52
52
  #endif
@@ -67,47 +67,44 @@ void Secur32Facade::FreeCredentialsHandle(CredHandle* credHandle, Napi::Env* env
67
67
  int Secur32Facade::InitializeSecurityContext(
68
68
  SecBufferDesc* inSecBufferDesc,
69
69
  SecBufferDesc* outSecBufferDesc,
70
- std::string* targetHost,
70
+ char* targetHostSpn,
71
71
  CredHandle* credHandle,
72
72
  struct _SecHandle* ctxHandle,
73
- unsigned long* ctxAttributes,
73
+ unsigned long flags,
74
74
  SECURITY_INTEGER* lifeTime,
75
75
  Napi::Env* env)
76
76
  {
77
77
  int result = 0;
78
+ unsigned long ignored;
79
+
78
80
  if (inSecBufferDesc == NULL) {
79
81
  result = ::InitializeSecurityContextA(
80
82
  credHandle,
81
83
  NULL,
82
- NULL,
83
- 0,
84
+ targetHostSpn,
85
+ flags,
84
86
  0,
85
87
  SECURITY_NATIVE_DREP,
86
88
  NULL,
87
89
  0,
88
90
  ctxHandle,
89
91
  outSecBufferDesc,
90
- ctxAttributes,
92
+ &ignored,
91
93
  lifeTime
92
94
  );
93
95
  } else {
94
- char* spn = NULL;
95
- if (targetHost != NULL) {
96
- std::string spnPrep = "HTTP/" + *targetHost;
97
- spn = (char*)spnPrep.c_str();
98
- }
99
96
  result = ::InitializeSecurityContextA(
100
97
  credHandle,
101
98
  ctxHandle,
102
- spn,
103
- *ctxAttributes,
99
+ targetHostSpn,
100
+ flags,
104
101
  0,
105
102
  SECURITY_NATIVE_DREP,
106
103
  inSecBufferDesc,
107
104
  0,
108
105
  ctxHandle,
109
106
  outSecBufferDesc,
110
- ctxAttributes,
107
+ &ignored,
111
108
  lifeTime
112
109
  );
113
110
  }
@@ -124,27 +121,31 @@ void Secur32Facade::FreeContextHandle(struct _SecHandle* ctxHandle, Napi::Env* e
124
121
  }
125
122
  }
126
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
+ }
127
130
 
128
- /**
129
- * Creates a ...
130
- */
131
131
  Napi::Number Secur32Facade::CreateAuthContext(const Napi::CallbackInfo& info) {
132
132
  Napi::Env env = info.Env();
133
- if (info.Length() < 3) {
133
+ if (info.Length() < 4) {
134
134
  ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
135
135
  return Napi::Number::New(env, 0);
136
136
  }
137
137
 
138
- if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer()) {
138
+ if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer() || !(info[3].IsUndefined() || info[3].IsNumber())) {
139
139
  ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
140
140
  return Napi::Number::New(env, 0);
141
141
  }
142
142
  auto securityPackageName = info[0].ToString();
143
143
  auto targetHost = info[1].ToString();
144
144
  auto applicationDataBuffer = info[2].As<Napi::Buffer<unsigned char>>();
145
+ auto flags = info[3].IsNumber() ? (unsigned long)(info[3].ToNumber().Uint32Value()) : GetDefaultFlags(securityPackageName);
145
146
 
146
147
  auto ac = std::make_shared<AuthContext>();
147
- ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, &env);
148
+ ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, flags, &env);
148
149
  acKey++;
149
150
  acMap[acKey] = ac;
150
151
  return Napi::Number::New(env, acKey);
@@ -40,14 +40,17 @@ class Secur32Facade {
40
40
  static int InitializeSecurityContext(
41
41
  SecBufferDesc* inSecBufferDesc,
42
42
  SecBufferDesc* outSecBufferDesc,
43
- std::string* targetHost,
43
+ char* targetHost,
44
44
  CredHandle* credHandle,
45
45
  struct _SecHandle* ctxHandle,
46
- unsigned long* ctxAttributes,
46
+ unsigned long flags,
47
47
  SECURITY_INTEGER* lifeTime,
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
package/src/win-sso.ts CHANGED
@@ -25,14 +25,18 @@ export class WinSso {
25
25
  * to free them (on error or after authentication is no longer needed)
26
26
  *
27
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)
28
+ * @param {string | undefined} targetHost The FQDN hostname of the target (optional for NTLM, required for Kerberos)
29
29
  * @param {PeerCertificate | undefined} peerCert The certificate of the target server
30
30
  * (optional, for HTTPS channel binding)
31
+ * @param {number | undefined} flags Flags to set in the authentication context
32
+ * If not set, NTML defaults to no flags, while Negotiate defaults to ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT
33
+ * (optional, allows customizing security features)
31
34
  */
32
35
  constructor(
33
36
  securityPackage: string,
34
37
  targetHost: string | undefined,
35
- peerCert: PeerCertificate | undefined
38
+ peerCert: PeerCertificate | undefined,
39
+ flags: number | undefined
36
40
  ) {
37
41
  this.securityPackage = securityPackage;
38
42
  let applicationData: Buffer;
@@ -47,7 +51,8 @@ export class WinSso {
47
51
  this.authContextId = winSsoAddon.createAuthContext(
48
52
  securityPackage,
49
53
  targetHost,
50
- applicationData
54
+ applicationData,
55
+ flags
51
56
  );
52
57
  }
53
58