win-sso 1.1.8 → 1.2.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, delegate: boolean)
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
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,11 @@ 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 {PeerCertificate | undefined} peerCert The certificate of the target server
18
- * (optional, for HTTPS channel binding)
17
+ * @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
19
20
  */
20
- constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined);
21
+ constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, delegate: boolean);
21
22
  /**
22
23
  * Retrieves the username of the logged in user
23
24
  *
package/dist/win-sso.js CHANGED
@@ -24,11 +24,11 @@ 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)
28
- * @param {PeerCertificate | undefined} peerCert The certificate of the target server
29
- * (optional, for HTTPS channel binding)
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
30
30
  */
31
- constructor(securityPackage, targetHost, peerCert) {
31
+ constructor(securityPackage, targetHost, peerCert, delegate) {
32
32
  this.securityPackage = securityPackage;
33
33
  let applicationData;
34
34
  if (!targetHost) {
@@ -40,7 +40,7 @@ class WinSso {
40
40
  else {
41
41
  applicationData = Buffer.alloc(0);
42
42
  }
43
- this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData);
43
+ this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, delegate);
44
44
  }
45
45
  /**
46
46
  * 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.2.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
@@ -2,18 +2,22 @@
2
2
  #include "secur32-facade.hh"
3
3
  #include "exception-handler.hh"
4
4
 
5
- AuthContext::AuthContext() {
5
+ AuthContext::AuthContext(bool delegate) {
6
6
  credHandle = {};
7
7
  lifeTime = {};
8
8
  ctxHandle = {};
9
- ctxAttributes = 0;
9
+ flags = ISC_REQ_MUTUAL_AUTH | ISC_REQ_SEQUENCE_DETECT;
10
+ if (delegate) {
11
+ flags |= ISC_REQ_DELEGATE;
12
+ }
10
13
  maxTokenLength = 0;
11
- outToken = 0;
14
+ outToken = nullptr;
12
15
  outTokenLength = 0;
13
16
  credHandleAllocated = false;
14
17
  ctxHandleAllocated = false;
15
18
  channelBindings = {};
16
19
  channelBindingsLength = 0;
20
+ targetHostnameSpn = nullptr;
17
21
  };
18
22
 
19
23
  AuthContext::~AuthContext() {
@@ -33,7 +37,11 @@ void AuthContext::Cleanup(Napi::Env* env) {
33
37
  }
34
38
  if (outToken) {
35
39
  delete outToken;
36
- outToken = 0;
40
+ outToken = nullptr;
41
+ }
42
+ if (targetHostnameSpn) {
43
+ delete targetHostnameSpn;
44
+ targetHostnameSpn = nullptr;;
37
45
  }
38
46
  }
39
47
 
@@ -41,6 +49,11 @@ bool AuthContext::Init(std::string* securityPackageName, std::string* targetHost
41
49
  auto packageNameLen = securityPackageName->copy(packageName, sizeof(packageName) - 1);
42
50
  packageName[packageNameLen] = '\0';
43
51
  targetHostname = *targetHost;
52
+ if (targetHostname.length() > 0) {
53
+ int len = targetHostname.length() + 5 + 1;
54
+ targetHostnameSpn = new char[len];
55
+ strncpy(targetHostnameSpn, ("HTTP/" + targetHostname).c_str(), len);
56
+ }
44
57
  maxTokenLength = Secur32Facade::GetMaxTokenLength(packageName, env);
45
58
  SetupChannelBindings(applicationDataBuffer);
46
59
  Secur32Facade::AcquireCredentialsHandle(packageName, &credHandle, &lifeTime, env);
@@ -81,7 +94,7 @@ bool AuthContext::InitContext(Napi::Env* env) {
81
94
  outSecBuff.pvBuffer = outToken;
82
95
 
83
96
  int result = Secur32Facade::InitializeSecurityContext(
84
- NULL, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
97
+ NULL, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
85
98
  if (env->IsExceptionPending()) {
86
99
  Cleanup(env);
87
100
  return false;
@@ -127,7 +140,7 @@ bool AuthContext::HandleResponse(
127
140
  inSecBuffers[1].pvBuffer = &channelBindings;
128
141
  }
129
142
 
130
- auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, TargetHostnameRef(), &credHandle, &ctxHandle, &ctxAttributes, &lifeTime, env);
143
+ auto result = Secur32Facade::InitializeSecurityContext(&inSecBufferDesc, &outSecBufferDesc, targetHostnameSpn, &credHandle, &ctxHandle, flags, &lifeTime, env);
131
144
  if (env->IsExceptionPending()) {
132
145
  Cleanup(env);
133
146
  return false;
@@ -149,11 +162,3 @@ Napi::Buffer<unsigned char> AuthContext::OutToken(Napi::Env* env) {
149
162
  return Napi::Buffer<unsigned char>::Buffer();
150
163
  }
151
164
  }
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;
@@ -34,7 +35,7 @@ class AuthContext {
34
35
  bool ctxHandleAllocated;
35
36
 
36
37
  public:
37
- AuthContext();
38
+ AuthContext(bool delegate);
38
39
  virtual ~AuthContext();
39
40
  void Cleanup(Napi::Env* env);
40
41
 
@@ -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,26 +121,23 @@ void Secur32Facade::FreeContextHandle(struct _SecHandle* ctxHandle, Napi::Env* e
124
121
  }
125
122
  }
126
123
 
127
-
128
- /**
129
- * Creates a ...
130
- */
131
124
  Napi::Number Secur32Facade::CreateAuthContext(const Napi::CallbackInfo& info) {
132
125
  Napi::Env env = info.Env();
133
- if (info.Length() < 3) {
126
+ if (info.Length() < 4) {
134
127
  ExceptionHandler::CreateAndThrow(env, "Wrong number of arguments");
135
128
  return Napi::Number::New(env, 0);
136
129
  }
137
130
 
138
- if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer()) {
131
+ if (!info[0].IsString() || !info[1].IsString() || !info[2].IsBuffer() || !info[3].IsBoolean()) {
139
132
  ExceptionHandler::CreateAndThrow(env, "Wrong argument types");
140
133
  return Napi::Number::New(env, 0);
141
134
  }
142
135
  auto securityPackageName = info[0].ToString();
143
136
  auto targetHost = info[1].ToString();
144
137
  auto applicationDataBuffer = info[2].As<Napi::Buffer<unsigned char>>();
138
+ auto delegate = info[3].ToBoolean();
145
139
 
146
- auto ac = std::make_shared<AuthContext>();
140
+ auto ac = std::make_shared<AuthContext>(delegate);
147
141
  ac->Init(&(securityPackageName.Utf8Value()), &(targetHost.Utf8Value()), applicationDataBuffer, &env);
148
142
  acKey++;
149
143
  acMap[acKey] = ac;
@@ -40,10 +40,10 @@ 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);
package/src/win-sso.ts CHANGED
@@ -25,14 +25,15 @@ 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)
29
- * @param {PeerCertificate | undefined} peerCert The certificate of the target server
30
- * (optional, for HTTPS channel binding)
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
31
31
  */
32
32
  constructor(
33
33
  securityPackage: string,
34
34
  targetHost: string | undefined,
35
- peerCert: PeerCertificate | undefined
35
+ peerCert: PeerCertificate | undefined,
36
+ delegate: boolean
36
37
  ) {
37
38
  this.securityPackage = securityPackage;
38
39
  let applicationData: Buffer;
@@ -47,7 +48,8 @@ export class WinSso {
47
48
  this.authContextId = winSsoAddon.createAuthContext(
48
49
  securityPackage,
49
50
  targetHost,
50
- applicationData
51
+ applicationData,
52
+ delegate
51
53
  );
52
54
  }
53
55