-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.ts
41 lines (36 loc) · 1.43 KB
/
authentication.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { RequestOptions, request } from 'http';
import { createNegotiateMessage, parseChallengeMessage, createAuthenticateMessage } from './ntlm-auth';
import { crash } from './utils';
export function authenticateRequestViaNtlm(
options: RequestOptions,
user: string,
password: string,
): Promise<void> {
return new Promise<void>((resolve, _reject) => {
_authenticateRequestViaNtlm(options, user, password, resolve);
});
}
function _authenticateRequestViaNtlm(
options: RequestOptions,
user: string,
password: string,
resolve: () => void,
): void {
Object.assign(options.headers, { Authorization: createNegotiateMessage() });
request(options, response => {
response.resume();
const authenticateHeader = response.headers['www-authenticate'];
if (true
&& authenticateHeader !== undefined
&& (authenticateHeader === 'Negotiate, NTLM' || authenticateHeader === 'NTLM')
) {
// receive NTLM handshake
return _authenticateRequestViaNtlm(options, user, password, resolve);
}
if (authenticateHeader === undefined) return crash('WWW-Authenticate header must be defined.');
const decoded = parseChallengeMessage(authenticateHeader);
options.headers = options.headers || {};
options.headers.Authorization = createAuthenticateMessage(decoded, user, password);
return resolve();
}).end();
}