win-sso 1.1.7 → 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 +4 -3
- package/dist/index.d.ts +2 -2
- package/dist/os-supported.d.ts +2 -0
- package/dist/os-supported.js +3 -1
- package/dist/win-sso.d.ts +20 -8
- package/dist/win-sso.js +35 -22
- package/package.json +19 -12
- package/prebuilds/win32-ia32/node.napi.node +0 -0
- package/prebuilds/win32-x64/node.napi.node +0 -0
- package/src/index.ts +2 -2
- package/src/node-addon/auth-context.cc +19 -14
- package/src/node-addon/auth-context.hh +3 -3
- package/src/node-addon/secur32-facade.cc +14 -20
- package/src/node-addon/secur32-facade.hh +2 -2
- package/src/os-supported.ts +4 -2
- package/src/win-sso.ts +37 -22
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/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { WinSso } from
|
|
2
|
-
import { osSupported } from
|
|
1
|
+
import { WinSso } from "./win-sso";
|
|
2
|
+
import { osSupported } from "./os-supported";
|
|
3
3
|
export { WinSso, osSupported };
|
package/dist/os-supported.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns true if the current platform is supported by the win-sso module. Only Windows OSs are supported.
|
|
3
3
|
* If false, all other methods in the module will throw.
|
|
4
|
+
*
|
|
5
|
+
* @returns {boolean} Is the os supported
|
|
4
6
|
*/
|
|
5
7
|
export declare function osSupported(): boolean;
|
package/dist/os-supported.js
CHANGED
|
@@ -8,8 +8,10 @@ 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.
|
|
10
10
|
* If false, all other methods in the module will throw.
|
|
11
|
+
*
|
|
12
|
+
* @returns {boolean} Is the os supported
|
|
11
13
|
*/
|
|
12
14
|
function osSupported() {
|
|
13
|
-
return
|
|
15
|
+
return os_1.default.platform() === "win32";
|
|
14
16
|
}
|
|
15
17
|
exports.osSupported = osSupported;
|
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.
|
|
@@ -11,19 +12,24 @@ export declare class WinSso {
|
|
|
11
12
|
* Creates an authentication context for SSO.
|
|
12
13
|
* This allocates memory buffers, the freeAuthContext method should be called
|
|
13
14
|
* to free them (on error or after authentication is no longer needed)
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
16
|
-
* @param
|
|
15
|
+
*
|
|
16
|
+
* @param {string} securityPackage The name of the security package (NTLM or Negotiate)
|
|
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
|
|
17
20
|
*/
|
|
18
|
-
constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined);
|
|
21
|
+
constructor(securityPackage: string, targetHost: string | undefined, peerCert: PeerCertificate | undefined, delegate: boolean);
|
|
19
22
|
/**
|
|
20
23
|
* Retrieves the username of the logged in user
|
|
24
|
+
*
|
|
21
25
|
* @returns {string} user name including domain
|
|
22
26
|
*/
|
|
23
27
|
static getLogonUserName(): string;
|
|
24
28
|
/**
|
|
25
29
|
* Transforms target TLS certificate into a channel binding application data buffer
|
|
26
|
-
*
|
|
30
|
+
*
|
|
31
|
+
* @param {PeerCertificate} peerCert Target TLS certificate
|
|
32
|
+
* @returns {Buffer} Application data buffer
|
|
27
33
|
*/
|
|
28
34
|
private getChannelBindingsApplicationData;
|
|
29
35
|
/**
|
|
@@ -34,24 +40,30 @@ export declare class WinSso {
|
|
|
34
40
|
freeAuthContext(): void;
|
|
35
41
|
/**
|
|
36
42
|
* Creates an authentication request token
|
|
43
|
+
*
|
|
37
44
|
* @returns {Buffer} Raw token buffer
|
|
38
45
|
*/
|
|
39
46
|
createAuthRequest(): Buffer;
|
|
40
47
|
/**
|
|
41
48
|
* Creates an authentication request header
|
|
49
|
+
*
|
|
42
50
|
* @returns {string} The www-authenticate header
|
|
43
51
|
*/
|
|
44
52
|
createAuthRequestHeader(): string;
|
|
45
53
|
/**
|
|
46
54
|
* Creates an authentication response token
|
|
47
|
-
*
|
|
55
|
+
*
|
|
56
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
57
|
+
* in response to the authentication request
|
|
48
58
|
* @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
|
|
49
59
|
*/
|
|
50
60
|
createAuthResponse(inTokenHeader: string): Buffer;
|
|
51
|
-
private
|
|
61
|
+
private isNtlmV1;
|
|
52
62
|
/**
|
|
53
63
|
* Creates an authentication response header
|
|
54
|
-
*
|
|
64
|
+
*
|
|
65
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
66
|
+
* in response to the authentication request
|
|
55
67
|
* @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
|
|
56
68
|
*/
|
|
57
69
|
createAuthResponseHeader(inTokenHeader: string): string;
|
package/dist/win-sso.js
CHANGED
|
@@ -22,11 +22,13 @@ class WinSso {
|
|
|
22
22
|
* Creates an authentication context for SSO.
|
|
23
23
|
* This allocates memory buffers, the freeAuthContext method should be called
|
|
24
24
|
* to free them (on error or after authentication is no longer needed)
|
|
25
|
-
*
|
|
26
|
-
* @param
|
|
27
|
-
* @param
|
|
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 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
30
|
*/
|
|
29
|
-
constructor(securityPackage, targetHost, peerCert) {
|
|
31
|
+
constructor(securityPackage, targetHost, peerCert, delegate) {
|
|
30
32
|
this.securityPackage = securityPackage;
|
|
31
33
|
let applicationData;
|
|
32
34
|
if (!targetHost) {
|
|
@@ -38,10 +40,11 @@ class WinSso {
|
|
|
38
40
|
else {
|
|
39
41
|
applicationData = Buffer.alloc(0);
|
|
40
42
|
}
|
|
41
|
-
this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData);
|
|
43
|
+
this.authContextId = winSsoAddon.createAuthContext(securityPackage, targetHost, applicationData, delegate);
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
44
46
|
* Retrieves the username of the logged in user
|
|
47
|
+
*
|
|
45
48
|
* @returns {string} user name including domain
|
|
46
49
|
*/
|
|
47
50
|
static getLogonUserName() {
|
|
@@ -49,14 +52,16 @@ class WinSso {
|
|
|
49
52
|
}
|
|
50
53
|
/**
|
|
51
54
|
* Transforms target TLS certificate into a channel binding application data buffer
|
|
52
|
-
*
|
|
55
|
+
*
|
|
56
|
+
* @param {PeerCertificate} peerCert Target TLS certificate
|
|
57
|
+
* @returns {Buffer} Application data buffer
|
|
53
58
|
*/
|
|
54
59
|
getChannelBindingsApplicationData(peerCert) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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);
|
|
60
65
|
applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
|
|
61
66
|
hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
|
|
62
67
|
return applicationDataBuffer;
|
|
@@ -71,38 +76,42 @@ class WinSso {
|
|
|
71
76
|
}
|
|
72
77
|
/**
|
|
73
78
|
* Creates an authentication request token
|
|
79
|
+
*
|
|
74
80
|
* @returns {Buffer} Raw token buffer
|
|
75
81
|
*/
|
|
76
82
|
createAuthRequest() {
|
|
77
|
-
|
|
83
|
+
const token = winSsoAddon.createAuthRequest(this.authContextId);
|
|
78
84
|
(0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication request token", token.toString("base64"));
|
|
79
85
|
return token;
|
|
80
86
|
}
|
|
81
87
|
/**
|
|
82
88
|
* Creates an authentication request header
|
|
89
|
+
*
|
|
83
90
|
* @returns {string} The www-authenticate header
|
|
84
91
|
*/
|
|
85
92
|
createAuthRequestHeader() {
|
|
86
|
-
|
|
93
|
+
const header = this.securityPackage + " " + this.createAuthRequest().toString("base64");
|
|
87
94
|
return header;
|
|
88
95
|
}
|
|
89
96
|
/**
|
|
90
97
|
* Creates an authentication response token
|
|
91
|
-
*
|
|
98
|
+
*
|
|
99
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
100
|
+
* in response to the authentication request
|
|
92
101
|
* @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
|
|
93
102
|
*/
|
|
94
103
|
createAuthResponse(inTokenHeader) {
|
|
95
104
|
(0, debug_logger_1.debug)("Received www-authentication response", inTokenHeader);
|
|
96
|
-
|
|
105
|
+
const packageMatch = new RegExp("^" + this.securityPackage + "\\s([^,\\s]+)").exec(inTokenHeader);
|
|
97
106
|
if (!packageMatch) {
|
|
98
107
|
throw new Error("Invalid input token, missing " +
|
|
99
108
|
this.securityPackage +
|
|
100
109
|
" prefix: " +
|
|
101
110
|
inTokenHeader);
|
|
102
111
|
}
|
|
103
|
-
|
|
112
|
+
const inToken = Buffer.from(packageMatch[1], "base64");
|
|
104
113
|
try {
|
|
105
|
-
|
|
114
|
+
const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
|
|
106
115
|
if (token.length > 0) {
|
|
107
116
|
(0, debug_logger_1.debug)("Created " + this.securityPackage + " authentication response token", token.toString("base64"));
|
|
108
117
|
}
|
|
@@ -114,8 +123,9 @@ class WinSso {
|
|
|
114
123
|
catch (err) {
|
|
115
124
|
if (err.message ===
|
|
116
125
|
"Could not init security context. Result: -2146893054") {
|
|
117
|
-
// If incoming token is for NTLMv1, this error can occur when
|
|
118
|
-
|
|
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)) {
|
|
119
129
|
throw new Error("Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
|
|
120
130
|
"it is likely that the client is prevented from sending such messages. " +
|
|
121
131
|
"Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)");
|
|
@@ -124,7 +134,8 @@ class WinSso {
|
|
|
124
134
|
throw err;
|
|
125
135
|
}
|
|
126
136
|
}
|
|
127
|
-
|
|
137
|
+
// eslint-disable-next-line require-jsdoc
|
|
138
|
+
isNtlmV1(type2message) {
|
|
128
139
|
if (type2message.length >= 24) {
|
|
129
140
|
const inTokenFlags = type2message.readInt32BE(20);
|
|
130
141
|
if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
|
|
@@ -135,7 +146,9 @@ class WinSso {
|
|
|
135
146
|
}
|
|
136
147
|
/**
|
|
137
148
|
* Creates an authentication response header
|
|
138
|
-
*
|
|
149
|
+
*
|
|
150
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
151
|
+
* in response to the authentication request
|
|
139
152
|
* @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
|
|
140
153
|
*/
|
|
141
154
|
createAuthResponseHeader(inTokenHeader) {
|
|
@@ -143,7 +156,7 @@ class WinSso {
|
|
|
143
156
|
if (tokenBuffer.length == 0) {
|
|
144
157
|
return "";
|
|
145
158
|
}
|
|
146
|
-
|
|
159
|
+
const header = this.securityPackage + " " + tokenBuffer.toString("base64");
|
|
147
160
|
return header;
|
|
148
161
|
}
|
|
149
162
|
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "win-sso",
|
|
3
|
-
"version": "1.
|
|
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": {
|
|
7
7
|
"build": "shx rm -rf dist && tsc",
|
|
8
|
-
"lint": "
|
|
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"
|
|
@@ -39,23 +42,27 @@
|
|
|
39
42
|
"bindings": "*",
|
|
40
43
|
"cross-spawn": "^7.0.3",
|
|
41
44
|
"debug": "^4.3.1",
|
|
42
|
-
"node-
|
|
43
|
-
"node-gyp": "^
|
|
44
|
-
"node-gyp-build": "4.3"
|
|
45
|
+
"node-gyp": "^9.0.0",
|
|
46
|
+
"node-gyp-build": "^4.4.0"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@types/debug": "^4.1.7",
|
|
48
50
|
"@types/mocha": "^9.1.0",
|
|
49
|
-
"@types/node": "^
|
|
51
|
+
"@types/node": "^18.7.18",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^5.20.0",
|
|
53
|
+
"@typescript-eslint/parser": "^5.20.0",
|
|
50
54
|
"asn1-parser": "^1.1.8",
|
|
51
|
-
"
|
|
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",
|
|
59
|
+
"eslint-plugin-node": "^11.1.0",
|
|
60
|
+
"mocha": "^10.0.0",
|
|
61
|
+
"node-addon-api": "^5.0.0",
|
|
52
62
|
"prebuildify": "^5.0.0",
|
|
53
63
|
"shx": "^0.3.4",
|
|
54
64
|
"ts-node": "^10.5.0",
|
|
55
|
-
"
|
|
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
|
}
|
|
Binary file
|
|
Binary file
|
package/src/index.ts
CHANGED
|
@@ -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
|
-
|
|
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 =
|
|
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 =
|
|
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,
|
|
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,
|
|
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
|
|
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
|
-
|
|
70
|
+
char* targetHostSpn,
|
|
71
71
|
CredHandle* credHandle,
|
|
72
72
|
struct _SecHandle* ctxHandle,
|
|
73
|
-
unsigned long
|
|
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
|
-
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
103
|
-
|
|
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
|
-
|
|
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() <
|
|
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
|
-
|
|
43
|
+
char* targetHost,
|
|
44
44
|
CredHandle* credHandle,
|
|
45
45
|
struct _SecHandle* ctxHandle,
|
|
46
|
-
unsigned long
|
|
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/os-supported.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import os from
|
|
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
|
|
10
|
+
return os.platform() === "win32";
|
|
9
11
|
}
|
package/src/win-sso.ts
CHANGED
|
@@ -23,14 +23,17 @@ 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
|
-
*
|
|
27
|
-
* @param
|
|
28
|
-
* @param
|
|
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
|
|
29
31
|
*/
|
|
30
32
|
constructor(
|
|
31
33
|
securityPackage: string,
|
|
32
34
|
targetHost: string | undefined,
|
|
33
|
-
peerCert: PeerCertificate | undefined
|
|
35
|
+
peerCert: PeerCertificate | undefined,
|
|
36
|
+
delegate: boolean
|
|
34
37
|
) {
|
|
35
38
|
this.securityPackage = securityPackage;
|
|
36
39
|
let applicationData: Buffer;
|
|
@@ -45,12 +48,14 @@ export class WinSso {
|
|
|
45
48
|
this.authContextId = winSsoAddon.createAuthContext(
|
|
46
49
|
securityPackage,
|
|
47
50
|
targetHost,
|
|
48
|
-
applicationData
|
|
51
|
+
applicationData,
|
|
52
|
+
delegate
|
|
49
53
|
);
|
|
50
54
|
}
|
|
51
55
|
|
|
52
56
|
/**
|
|
53
57
|
* Retrieves the username of the logged in user
|
|
58
|
+
*
|
|
54
59
|
* @returns {string} user name including domain
|
|
55
60
|
*/
|
|
56
61
|
static getLogonUserName(): string {
|
|
@@ -59,14 +64,16 @@ export class WinSso {
|
|
|
59
64
|
|
|
60
65
|
/**
|
|
61
66
|
* Transforms target TLS certificate into a channel binding application data buffer
|
|
62
|
-
*
|
|
67
|
+
*
|
|
68
|
+
* @param {PeerCertificate} peerCert Target TLS certificate
|
|
69
|
+
* @returns {Buffer} Application data buffer
|
|
63
70
|
*/
|
|
64
71
|
private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
const cert: any = peerCert;
|
|
73
|
+
const hash = cert.fingerprint256.replace(/:/g, "");
|
|
74
|
+
const hashBuf = Buffer.from(hash, "hex");
|
|
75
|
+
const tlsServerEndPoint = "tls-server-end-point:";
|
|
76
|
+
const applicationDataBuffer = Buffer.alloc(
|
|
70
77
|
tlsServerEndPoint.length + hashBuf.length
|
|
71
78
|
);
|
|
72
79
|
applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
|
|
@@ -85,10 +92,11 @@ export class WinSso {
|
|
|
85
92
|
|
|
86
93
|
/**
|
|
87
94
|
* Creates an authentication request token
|
|
95
|
+
*
|
|
88
96
|
* @returns {Buffer} Raw token buffer
|
|
89
97
|
*/
|
|
90
98
|
createAuthRequest(): Buffer {
|
|
91
|
-
|
|
99
|
+
const token = winSsoAddon.createAuthRequest(this.authContextId);
|
|
92
100
|
debug(
|
|
93
101
|
"Created " + this.securityPackage + " authentication request token",
|
|
94
102
|
token.toString("base64")
|
|
@@ -98,22 +106,25 @@ export class WinSso {
|
|
|
98
106
|
|
|
99
107
|
/**
|
|
100
108
|
* Creates an authentication request header
|
|
109
|
+
*
|
|
101
110
|
* @returns {string} The www-authenticate header
|
|
102
111
|
*/
|
|
103
112
|
createAuthRequestHeader(): string {
|
|
104
|
-
|
|
113
|
+
const header =
|
|
105
114
|
this.securityPackage + " " + this.createAuthRequest().toString("base64");
|
|
106
115
|
return header;
|
|
107
116
|
}
|
|
108
117
|
|
|
109
118
|
/**
|
|
110
119
|
* Creates an authentication response token
|
|
111
|
-
*
|
|
120
|
+
*
|
|
121
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
122
|
+
* in response to the authentication request
|
|
112
123
|
* @returns {Buffer} Raw token buffer. May be empty if Negotiate handshake is complete.
|
|
113
124
|
*/
|
|
114
125
|
createAuthResponse(inTokenHeader: string): Buffer {
|
|
115
126
|
debug("Received www-authentication response", inTokenHeader);
|
|
116
|
-
|
|
127
|
+
const packageMatch = new RegExp(
|
|
117
128
|
"^" + this.securityPackage + "\\s([^,\\s]+)"
|
|
118
129
|
).exec(inTokenHeader);
|
|
119
130
|
|
|
@@ -125,9 +136,9 @@ export class WinSso {
|
|
|
125
136
|
inTokenHeader
|
|
126
137
|
);
|
|
127
138
|
}
|
|
128
|
-
|
|
139
|
+
const inToken = Buffer.from(packageMatch[1], "base64");
|
|
129
140
|
try {
|
|
130
|
-
|
|
141
|
+
const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
|
|
131
142
|
if (token.length > 0) {
|
|
132
143
|
debug(
|
|
133
144
|
"Created " + this.securityPackage + " authentication response token",
|
|
@@ -142,8 +153,9 @@ export class WinSso {
|
|
|
142
153
|
(err as Error).message ===
|
|
143
154
|
"Could not init security context. Result: -2146893054"
|
|
144
155
|
) {
|
|
145
|
-
// If incoming token is for NTLMv1, this error can occur when
|
|
146
|
-
|
|
156
|
+
// If incoming token is for NTLMv1, this error can occur when
|
|
157
|
+
// LMCompatibilityLevel prevents the client to send NTLMv1 messages
|
|
158
|
+
if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
|
|
147
159
|
throw new Error(
|
|
148
160
|
"Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
|
|
149
161
|
"it is likely that the client is prevented from sending such messages. " +
|
|
@@ -155,7 +167,8 @@ export class WinSso {
|
|
|
155
167
|
}
|
|
156
168
|
}
|
|
157
169
|
|
|
158
|
-
|
|
170
|
+
// eslint-disable-next-line require-jsdoc
|
|
171
|
+
private isNtlmV1(type2message: Buffer): boolean {
|
|
159
172
|
if (type2message.length >= 24) {
|
|
160
173
|
const inTokenFlags = type2message.readInt32BE(20);
|
|
161
174
|
if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
|
|
@@ -167,7 +180,9 @@ export class WinSso {
|
|
|
167
180
|
|
|
168
181
|
/**
|
|
169
182
|
* Creates an authentication response header
|
|
170
|
-
*
|
|
183
|
+
*
|
|
184
|
+
* @param {string} inTokenHeader The www-authentication header received from the target
|
|
185
|
+
* in response to the authentication request
|
|
171
186
|
* @returns {string} The www-authenticate header. May be an empty string if Negotiate handshake is complete.
|
|
172
187
|
*/
|
|
173
188
|
createAuthResponseHeader(inTokenHeader: string): string {
|
|
@@ -175,7 +190,7 @@ export class WinSso {
|
|
|
175
190
|
if (tokenBuffer.length == 0) {
|
|
176
191
|
return "";
|
|
177
192
|
}
|
|
178
|
-
|
|
193
|
+
const header = this.securityPackage + " " + tokenBuffer.toString("base64");
|
|
179
194
|
return header;
|
|
180
195
|
}
|
|
181
196
|
}
|