-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathkerberos_tests.js
172 lines (145 loc) · 5.93 KB
/
kerberos_tests.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const kerberos = require('../lib/index');
const request = require('request');
const chai = require('chai');
const expect = chai.expect;
const os = require('os');
const { test } = require('mocha');
chai.use(require('chai-string'));
// environment variables
const username = process.env.KERBEROS_USERNAME || 'administrator';
const password = process.env.KERBEROS_PASSWORD || 'Password01';
const realm = process.env.KERBEROS_REALM || 'example.com';
const hostname = process.env.KERBEROS_HOSTNAME || 'hostname.example.com';
const port = process.env.KERBEROS_PORT || '80';
describe('Kerberos', function () {
before(function () {
if (os.type() === 'Windows_NT') this.skip();
});
it('should lookup principal details on a server', function (done) {
const expected = `HTTP/${hostname}@${realm.toUpperCase()}`;
kerberos.principalDetails('HTTP', hostname, (err, details) => {
expect(err).to.not.exist;
expect(details).to.equal(expected);
done();
});
});
it('should check a given password against a kerberos server', function (done) {
const service = `HTTP/${hostname}`;
kerberos.checkPassword(username, password, service, realm.toUpperCase(), err => {
expect(err).to.not.exist;
kerberos.checkPassword(username, 'incorrect-password', service, realm.toUpperCase(), err => {
expect(err).to.exist;
done();
});
});
});
it('should authenticate against a kerberos server using GSSAPI', function (done) {
const service = `HTTP@${hostname}`;
kerberos.initializeClient(service, {}, (err, client) => {
expect(err).to.not.exist;
kerberos.initializeServer(service, (err, server) => {
expect(err).to.not.exist;
expect(client.contextComplete).to.be.false;
expect(server.contextComplete).to.be.false;
client.step('', (err, clientResponse) => {
expect(err).to.not.exist;
expect(client.contextComplete).to.be.false;
server.step(clientResponse, (err, serverResponse) => {
expect(err).to.not.exist;
expect(client.contextComplete).to.be.false;
client.step(serverResponse, err => {
expect(err).to.not.exist;
expect(client.contextComplete).to.be.true;
const expectedUsername = `${username}@${realm.toUpperCase()}`;
expect(server.username).to.equal(expectedUsername);
expect(client.username).to.equal(expectedUsername);
expect(server.targetName).to.not.exist;
done();
});
});
});
});
});
});
it('should authenticate against a kerberos HTTP endpoint', function (done) {
const service = `HTTP@${hostname}`;
const url = `http://${hostname}:${port}/`;
// send the initial request un-authenticated
request.get(url, (err, response) => {
expect(err).to.not.exist;
expect(response).to.have.property('statusCode', 401);
// validate the response supports the Negotiate protocol
const authenticateHeader = response.headers['www-authenticate'];
expect(authenticateHeader).to.exist;
expect(authenticateHeader).to.equal('Negotiate');
// generate the first Kerberos token
const mechOID = kerberos.GSS_MECH_OID_KRB5;
kerberos.initializeClient(service, { mechOID }, (err, client) => {
expect(err).to.not.exist;
client.step('', (err, kerberosToken) => {
expect(err).to.not.exist;
// attach the Kerberos token and resend back to the host
request.get(
{ url, headers: { Authorization: `Negotiate ${kerberosToken}` } },
(err, response) => {
expect(err).to.not.exist;
expect(response.statusCode).to.equal(200);
// validate the headers exist and contain a www-authenticate message
const authenticateHeader = response.headers['www-authenticate'];
expect(authenticateHeader).to.exist;
expect(authenticateHeader).to.startWith('Negotiate');
// verify the return Kerberos token
const tokenParts = authenticateHeader.split(' ');
const serverKerberosToken = tokenParts[tokenParts.length - 1];
client.step(serverKerberosToken, err => {
expect(err).to.not.exist;
expect(client.contextComplete).to.be.true;
done();
});
}
);
});
});
});
});
describe('Client.wrap()', function () {
async function establishConext() {
const service = `HTTP@${hostname}`;
client = await kerberos.initializeClient(service, {});
server = await kerberos.initializeServer(service);
const clientResponse = await client.step('');
const serverResponse = await server.step(clientResponse);
await client.step(serverResponse);
expect(client.contextComplete).to.be.true;
return { client, server };
}
let client;
let server;
before(establishConext);
describe('options.protect', function () {
context('valid values for `protect`', function () {
test('no options provided', async function () {
await client.wrap('challenge');
});
test('options provided (protect omitted)', async function () {
await client.wrap('challenge', {});
});
test('protect = false', async function () {
await client.wrap('challenge', { protect: false });
});
test('protect = true', async function () {
await client.wrap('challenge', { protect: true });
});
});
context('when set to an invalid value', function () {
it('throws a TypeError', async function () {
const error = await client.wrap('challenge', { protect: 'non-boolean' }).catch(e => e);
expect(error)
.to.be.instanceOf(TypeError)
.to.match(/options.protect must be a boolean/);
});
});
});
});
});