win-sso 1.3.2 → 1.3.3
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/LICENSE +21 -21
- package/README.md +114 -114
- package/binding.gyp +24 -24
- package/install_env.js +12 -12
- package/package.json +65 -65
- package/prebuildify_env.js +12 -12
- package/prebuilds/win32-ia32/win-sso.node +0 -0
- package/prebuilds/win32-x64/win-sso.node +0 -0
- package/src/index.ts +4 -4
- package/src/node-addon/auth-context.cc +162 -162
- package/src/node-addon/auth-context.hh +52 -52
- package/src/node-addon/exception-handler.cc +25 -25
- package/src/node-addon/exception-handler.hh +15 -15
- package/src/node-addon/secur32-facade.cc +243 -243
- package/src/node-addon/secur32-facade.hh +56 -56
- package/src/os-supported.ts +10 -10
- package/src/utils/debug.logger.ts +4 -4
- package/src/utils/i.win-sso-addon.ts +14 -14
- package/src/win-sso.ts +192 -192
package/src/win-sso.ts
CHANGED
|
@@ -1,192 +1,192 @@
|
|
|
1
|
-
import { debug } from "./utils/debug.logger";
|
|
2
|
-
import { PeerCertificate } from "tls";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { IWinSsoAddon } from "./utils/i.win-sso-addon";
|
|
5
|
-
|
|
6
|
-
let winSsoAddon: IWinSsoAddon;
|
|
7
|
-
try {
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
9
|
-
winSsoAddon = require("node-gyp-build")(path.join(__dirname, ".."));
|
|
10
|
-
debug("Loaded win-sso native module");
|
|
11
|
-
} catch {
|
|
12
|
-
debug("Could not load win-sso native module");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
|
|
17
|
-
*/
|
|
18
|
-
export class WinSso {
|
|
19
|
-
private static NEGOTIATE_NTLM2_KEY = 1 << 19;
|
|
20
|
-
|
|
21
|
-
private authContextId: number;
|
|
22
|
-
private securityPackage: string;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Creates an authentication context for SSO.
|
|
26
|
-
* This allocates memory buffers, the freeAuthContext method should be called
|
|
27
|
-
* to free them (on error or after authentication is no longer needed)
|
|
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)
|
|
35
|
-
*/
|
|
36
|
-
constructor(
|
|
37
|
-
securityPackage: string,
|
|
38
|
-
targetHost: string | undefined,
|
|
39
|
-
peerCert: PeerCertificate | undefined,
|
|
40
|
-
flags: number | undefined
|
|
41
|
-
) {
|
|
42
|
-
this.securityPackage = securityPackage;
|
|
43
|
-
let applicationData: Buffer;
|
|
44
|
-
if (!targetHost) {
|
|
45
|
-
targetHost = "";
|
|
46
|
-
}
|
|
47
|
-
if (peerCert) {
|
|
48
|
-
applicationData = this.getChannelBindingsApplicationData(peerCert);
|
|
49
|
-
} else {
|
|
50
|
-
applicationData = Buffer.alloc(0);
|
|
51
|
-
}
|
|
52
|
-
this.authContextId = winSsoAddon.createAuthContext(
|
|
53
|
-
securityPackage,
|
|
54
|
-
targetHost,
|
|
55
|
-
applicationData,
|
|
56
|
-
flags
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Retrieves the username of the logged in user
|
|
62
|
-
* @returns user name including domain
|
|
63
|
-
*/
|
|
64
|
-
static getLogonUserName(): string {
|
|
65
|
-
return winSsoAddon.getLogonUserName();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Transforms target TLS certificate into a channel binding application data buffer
|
|
70
|
-
* @param peerCert Target TLS certificate
|
|
71
|
-
* @returns Application data buffer
|
|
72
|
-
*/
|
|
73
|
-
private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
|
|
74
|
-
const hash = peerCert.fingerprint256.replace(/:/g, "");
|
|
75
|
-
const hashBuf = Buffer.from(hash, "hex");
|
|
76
|
-
const tlsServerEndPoint = "tls-server-end-point:";
|
|
77
|
-
const applicationDataBuffer = Buffer.alloc(
|
|
78
|
-
tlsServerEndPoint.length + hashBuf.length
|
|
79
|
-
);
|
|
80
|
-
applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
|
|
81
|
-
hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
|
|
82
|
-
return applicationDataBuffer;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Releases all allocated resources for the authorization context.
|
|
87
|
-
* Should be called when the context is no longer required, such as when the
|
|
88
|
-
* socket was closed.
|
|
89
|
-
*/
|
|
90
|
-
freeAuthContext() {
|
|
91
|
-
winSsoAddon.freeAuthContext(this.authContextId);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Creates an authentication request token
|
|
96
|
-
* @returns Raw token buffer
|
|
97
|
-
*/
|
|
98
|
-
createAuthRequest(): Buffer {
|
|
99
|
-
const token = winSsoAddon.createAuthRequest(this.authContextId);
|
|
100
|
-
debug(
|
|
101
|
-
"Created " + this.securityPackage + " authentication request token",
|
|
102
|
-
token.toString("base64")
|
|
103
|
-
);
|
|
104
|
-
return token;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Creates an authentication request header
|
|
109
|
-
* @returns The www-authenticate header
|
|
110
|
-
*/
|
|
111
|
-
createAuthRequestHeader(): string {
|
|
112
|
-
const header =
|
|
113
|
-
this.securityPackage + " " + this.createAuthRequest().toString("base64");
|
|
114
|
-
return header;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Creates an authentication response token
|
|
119
|
-
* @param inTokenHeader The www-authentication header received from the target
|
|
120
|
-
* in response to the authentication request
|
|
121
|
-
* @returns Raw token buffer. May be empty if Negotiate handshake is complete.
|
|
122
|
-
*/
|
|
123
|
-
createAuthResponse(inTokenHeader: string): Buffer {
|
|
124
|
-
debug("Received www-authentication response", inTokenHeader);
|
|
125
|
-
const packageMatch = new RegExp(
|
|
126
|
-
"^" + this.securityPackage + "\\s([^,\\s]+)"
|
|
127
|
-
).exec(inTokenHeader);
|
|
128
|
-
|
|
129
|
-
if (!packageMatch) {
|
|
130
|
-
throw new Error(
|
|
131
|
-
"Invalid input token, missing " +
|
|
132
|
-
this.securityPackage +
|
|
133
|
-
" prefix: " +
|
|
134
|
-
inTokenHeader
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
const inToken = Buffer.from(packageMatch[1], "base64");
|
|
138
|
-
try {
|
|
139
|
-
const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
|
|
140
|
-
if (token.length > 0) {
|
|
141
|
-
debug(
|
|
142
|
-
"Created " + this.securityPackage + " authentication response token",
|
|
143
|
-
token.toString("base64")
|
|
144
|
-
);
|
|
145
|
-
} else {
|
|
146
|
-
debug("No response token, authentication complete");
|
|
147
|
-
}
|
|
148
|
-
return token;
|
|
149
|
-
} catch (err) {
|
|
150
|
-
if (
|
|
151
|
-
(err as Error).message ===
|
|
152
|
-
"Could not init security context. Result: -2146893054"
|
|
153
|
-
) {
|
|
154
|
-
// If incoming token is for NTLMv1, this error can occur when
|
|
155
|
-
// LMCompatibilityLevel prevents the client to send NTLMv1 messages
|
|
156
|
-
if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
|
|
157
|
-
throw new Error(
|
|
158
|
-
"Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
|
|
159
|
-
"it is likely that the client is prevented from sending such messages. " +
|
|
160
|
-
"Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)"
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
throw err;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
private isNtlmV1(type2message: Buffer): boolean {
|
|
169
|
-
if (type2message.length >= 24) {
|
|
170
|
-
const inTokenFlags = type2message.readInt32BE(20);
|
|
171
|
-
if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
|
|
172
|
-
return true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return false;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Creates an authentication response header
|
|
180
|
-
* @param inTokenHeader The www-authentication header received from the target
|
|
181
|
-
* in response to the authentication request
|
|
182
|
-
* @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
|
|
183
|
-
*/
|
|
184
|
-
createAuthResponseHeader(inTokenHeader: string): string {
|
|
185
|
-
const tokenBuffer = this.createAuthResponse(inTokenHeader);
|
|
186
|
-
if (tokenBuffer.length == 0) {
|
|
187
|
-
return "";
|
|
188
|
-
}
|
|
189
|
-
const header = this.securityPackage + " " + tokenBuffer.toString("base64");
|
|
190
|
-
return header;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
1
|
+
import { debug } from "./utils/debug.logger";
|
|
2
|
+
import { PeerCertificate } from "tls";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { IWinSsoAddon } from "./utils/i.win-sso-addon";
|
|
5
|
+
|
|
6
|
+
let winSsoAddon: IWinSsoAddon;
|
|
7
|
+
try {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
9
|
+
winSsoAddon = require("node-gyp-build")(path.join(__dirname, ".."));
|
|
10
|
+
debug("Loaded win-sso native module");
|
|
11
|
+
} catch {
|
|
12
|
+
debug("Could not load win-sso native module");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates authentication tokens for NTLM or Negotiate handshake using the executing users credentials.
|
|
17
|
+
*/
|
|
18
|
+
export class WinSso {
|
|
19
|
+
private static NEGOTIATE_NTLM2_KEY = 1 << 19;
|
|
20
|
+
|
|
21
|
+
private authContextId: number;
|
|
22
|
+
private securityPackage: string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates an authentication context for SSO.
|
|
26
|
+
* This allocates memory buffers, the freeAuthContext method should be called
|
|
27
|
+
* to free them (on error or after authentication is no longer needed)
|
|
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)
|
|
35
|
+
*/
|
|
36
|
+
constructor(
|
|
37
|
+
securityPackage: string,
|
|
38
|
+
targetHost: string | undefined,
|
|
39
|
+
peerCert: PeerCertificate | undefined,
|
|
40
|
+
flags: number | undefined
|
|
41
|
+
) {
|
|
42
|
+
this.securityPackage = securityPackage;
|
|
43
|
+
let applicationData: Buffer;
|
|
44
|
+
if (!targetHost) {
|
|
45
|
+
targetHost = "";
|
|
46
|
+
}
|
|
47
|
+
if (peerCert) {
|
|
48
|
+
applicationData = this.getChannelBindingsApplicationData(peerCert);
|
|
49
|
+
} else {
|
|
50
|
+
applicationData = Buffer.alloc(0);
|
|
51
|
+
}
|
|
52
|
+
this.authContextId = winSsoAddon.createAuthContext(
|
|
53
|
+
securityPackage,
|
|
54
|
+
targetHost,
|
|
55
|
+
applicationData,
|
|
56
|
+
flags
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the username of the logged in user
|
|
62
|
+
* @returns user name including domain
|
|
63
|
+
*/
|
|
64
|
+
static getLogonUserName(): string {
|
|
65
|
+
return winSsoAddon.getLogonUserName();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Transforms target TLS certificate into a channel binding application data buffer
|
|
70
|
+
* @param peerCert Target TLS certificate
|
|
71
|
+
* @returns Application data buffer
|
|
72
|
+
*/
|
|
73
|
+
private getChannelBindingsApplicationData(peerCert: PeerCertificate): Buffer {
|
|
74
|
+
const hash = peerCert.fingerprint256.replace(/:/g, "");
|
|
75
|
+
const hashBuf = Buffer.from(hash, "hex");
|
|
76
|
+
const tlsServerEndPoint = "tls-server-end-point:";
|
|
77
|
+
const applicationDataBuffer = Buffer.alloc(
|
|
78
|
+
tlsServerEndPoint.length + hashBuf.length
|
|
79
|
+
);
|
|
80
|
+
applicationDataBuffer.write(tlsServerEndPoint, 0, "ascii");
|
|
81
|
+
hashBuf.copy(applicationDataBuffer, tlsServerEndPoint.length);
|
|
82
|
+
return applicationDataBuffer;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Releases all allocated resources for the authorization context.
|
|
87
|
+
* Should be called when the context is no longer required, such as when the
|
|
88
|
+
* socket was closed.
|
|
89
|
+
*/
|
|
90
|
+
freeAuthContext() {
|
|
91
|
+
winSsoAddon.freeAuthContext(this.authContextId);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates an authentication request token
|
|
96
|
+
* @returns Raw token buffer
|
|
97
|
+
*/
|
|
98
|
+
createAuthRequest(): Buffer {
|
|
99
|
+
const token = winSsoAddon.createAuthRequest(this.authContextId);
|
|
100
|
+
debug(
|
|
101
|
+
"Created " + this.securityPackage + " authentication request token",
|
|
102
|
+
token.toString("base64")
|
|
103
|
+
);
|
|
104
|
+
return token;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates an authentication request header
|
|
109
|
+
* @returns The www-authenticate header
|
|
110
|
+
*/
|
|
111
|
+
createAuthRequestHeader(): string {
|
|
112
|
+
const header =
|
|
113
|
+
this.securityPackage + " " + this.createAuthRequest().toString("base64");
|
|
114
|
+
return header;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates an authentication response token
|
|
119
|
+
* @param inTokenHeader The www-authentication header received from the target
|
|
120
|
+
* in response to the authentication request
|
|
121
|
+
* @returns Raw token buffer. May be empty if Negotiate handshake is complete.
|
|
122
|
+
*/
|
|
123
|
+
createAuthResponse(inTokenHeader: string): Buffer {
|
|
124
|
+
debug("Received www-authentication response", inTokenHeader);
|
|
125
|
+
const packageMatch = new RegExp(
|
|
126
|
+
"^" + this.securityPackage + "\\s([^,\\s]+)"
|
|
127
|
+
).exec(inTokenHeader);
|
|
128
|
+
|
|
129
|
+
if (!packageMatch) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
"Invalid input token, missing " +
|
|
132
|
+
this.securityPackage +
|
|
133
|
+
" prefix: " +
|
|
134
|
+
inTokenHeader
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
const inToken = Buffer.from(packageMatch[1], "base64");
|
|
138
|
+
try {
|
|
139
|
+
const token = winSsoAddon.createAuthResponse(this.authContextId, inToken);
|
|
140
|
+
if (token.length > 0) {
|
|
141
|
+
debug(
|
|
142
|
+
"Created " + this.securityPackage + " authentication response token",
|
|
143
|
+
token.toString("base64")
|
|
144
|
+
);
|
|
145
|
+
} else {
|
|
146
|
+
debug("No response token, authentication complete");
|
|
147
|
+
}
|
|
148
|
+
return token;
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (
|
|
151
|
+
(err as Error).message ===
|
|
152
|
+
"Could not init security context. Result: -2146893054"
|
|
153
|
+
) {
|
|
154
|
+
// If incoming token is for NTLMv1, this error can occur when
|
|
155
|
+
// LMCompatibilityLevel prevents the client to send NTLMv1 messages
|
|
156
|
+
if (this.securityPackage === "NTLM" && this.isNtlmV1(inToken)) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
"Could not create NTLM type 3 message. Incoming type 2 message uses NTLMv1, " +
|
|
159
|
+
"it is likely that the client is prevented from sending such messages. " +
|
|
160
|
+
"Update target host to use NTLMv2 (recommended) or adjust LMCompatibilityLevel on the client (insecure)"
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
throw err;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private isNtlmV1(type2message: Buffer): boolean {
|
|
169
|
+
if (type2message.length >= 24) {
|
|
170
|
+
const inTokenFlags = type2message.readInt32BE(20);
|
|
171
|
+
if ((inTokenFlags & WinSso.NEGOTIATE_NTLM2_KEY) === 0) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Creates an authentication response header
|
|
180
|
+
* @param inTokenHeader The www-authentication header received from the target
|
|
181
|
+
* in response to the authentication request
|
|
182
|
+
* @returns The www-authenticate header. May be an empty string if Negotiate handshake is complete.
|
|
183
|
+
*/
|
|
184
|
+
createAuthResponseHeader(inTokenHeader: string): string {
|
|
185
|
+
const tokenBuffer = this.createAuthResponse(inTokenHeader);
|
|
186
|
+
if (tokenBuffer.length == 0) {
|
|
187
|
+
return "";
|
|
188
|
+
}
|
|
189
|
+
const header = this.securityPackage + " " + tokenBuffer.toString("base64");
|
|
190
|
+
return header;
|
|
191
|
+
}
|
|
192
|
+
}
|