|
1 | 1 | 'use strict';
|
2 |
| - |
3 |
| -const expect = require('chai').expect; |
4 |
| -const http = require('http'); |
5 |
| -const Kerberos = require('..').Kerberos; |
| 2 | +const kerberos = require('..'); |
| 3 | +const request = require('request'); |
| 4 | +const chai = require('chai'); |
| 5 | +const expect = chai.expect; |
| 6 | +const SegfaultHandler = require('segfault-handler'); |
| 7 | +SegfaultHandler.registerHandler(); |
| 8 | +chai.use(require('chai-string')); |
| 9 | + |
| 10 | +const username = process.env.KERBEROS_USERNAME || 'administrator'; |
| 11 | +// const password = process.env.KERBEROS_PASSWORD || 'Password01'; |
| 12 | +const realm = process.env.KERBEROS_REALM || 'example.com'; |
| 13 | +const hostname = process.env.KERBEROS_HOSTNAME || 'hostname.example.com'; |
| 14 | +const port = process.env.KERBEROS_PORT || '80'; |
6 | 15 |
|
7 | 16 | describe('Kerberos', function() {
|
8 |
| - it('simple initialize of Kerberos object', function(done) { |
9 |
| - const kerberos = new Kerberos(); |
10 |
| - |
11 |
| - // Initiate kerberos client |
12 |
| - kerberos.authGSSClientInit('[email protected]', Kerberos.GSS_C_MUTUAL_FLAG, function( |
13 |
| - err, |
14 |
| - context |
15 |
| - ) { |
| 17 | + it('should authenticate against a kerberos server using GSSAPI', function(done) { |
| 18 | + const service = `HTTP@${hostname}`; |
| 19 | + |
| 20 | + kerberos.initializeClient(service, {}, (err, client) => { |
16 | 21 | expect(err).to.not.exist;
|
17 |
| - expect(context).to.exist; |
18 | 22 |
|
19 |
| - // Perform the first step |
20 |
| - kerberos.authGSSClientStep(context, function(/* err, result */) { |
21 |
| - // TODO: reenable next lines when osx support is complete |
22 |
| - // expect(err).to.not.exist; |
23 |
| - // expect(result).to.exist; |
| 23 | + kerberos.initializeServer(service, (err, server) => { |
| 24 | + expect(err).to.not.exist; |
| 25 | + expect(client.contextComplete).to.be.false; |
| 26 | + expect(server.contextComplete).to.be.false; |
| 27 | + |
| 28 | + client.step('', (err, clientResponse) => { |
| 29 | + expect(err).to.not.exist; |
| 30 | + expect(client.contextComplete).to.be.false; |
24 | 31 |
|
25 |
| - done(); |
| 32 | + server.step(clientResponse, (err, serverResponse) => { |
| 33 | + expect(err).to.not.exist; |
| 34 | + expect(client.contextComplete).to.be.false; |
| 35 | + |
| 36 | + client.step(serverResponse, err => { |
| 37 | + expect(err).to.not.exist; |
| 38 | + expect(client.contextComplete).to.be.true; |
| 39 | + |
| 40 | + const expectedUsername = `${username}@${realm.toUpperCase()}`; |
| 41 | + expect(server.username).to.equal(expectedUsername); |
| 42 | + expect(client.username).to.equal(expectedUsername); |
| 43 | + expect(server.targetName).to.not.exist; |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
26 | 48 | });
|
27 | 49 | });
|
28 | 50 | });
|
29 | 51 |
|
30 |
| - // for this test, please set the environment variables shown below. |
31 |
| - it('simple username password test', function(done) { |
32 |
| - const kerberos = new Kerberos(); |
| 52 | + it('should authenticate against a kerberos HTTP endpoint', function(done) { |
| 53 | + const service = `HTTP@${hostname}`; |
| 54 | + const url = `http://${hostname}:${port}/`; |
33 | 55 |
|
34 |
| - if (!process.env.KRB5_PW_TEST_USERNAME) { |
35 |
| - return done(); |
36 |
| - } |
| 56 | + // send the initial request un-authenticated |
| 57 | + request.get(url, (err, response) => { |
| 58 | + expect(response.statusCode).to.equal(401); |
37 | 59 |
|
38 |
| - kerberos.authUserKrb5Password( |
39 |
| - process.env.KRB5_PW_TEST_USERNAME, |
40 |
| - process.env.KRB5_PW_TEST_PASSWORD, |
41 |
| - process.env.KRB5_PW_TEST_SERVICE, |
42 |
| - function(err, ok) { |
43 |
| - expect(err).to.not.exist; |
44 |
| - expect(ok).to.be.true; |
45 |
| - done(); |
46 |
| - } |
47 |
| - ); |
48 |
| - }); |
49 |
| - |
50 |
| - //for this test, please set the environment variables shown below. |
51 |
| - it('negotiate HTTP Client Test', function(done) { |
52 |
| - ///// REQUIRED ENVIRONMENT VARIABLES ///// |
53 |
| - // give the host and path to a Negotiate protected resource on your network |
54 |
| - const httpHostname = process.env.NEGOTIATE_TEST_HOSTNAME; |
55 |
| - const httpPath = process.env.NEGOTIATE_TEST_PATH; |
56 |
| - //// OPTIONAL ENVIRONMENT VARIABLES |
57 |
| - // don't use the cache in $KRB5CCNAME, use the one in $NEGOTIATE_TEST_KRB5CCNAME instead |
58 |
| - const krb5CcName = process.env.NEGOTIATE_TEST_KRB5CCNAME || ''; |
59 |
| - ///// |
60 |
| - |
61 |
| - if (!httpHostname) { |
62 |
| - return done(); |
63 |
| - } |
64 |
| - |
65 |
| - const serviceName = 'HTTP@' + httpHostname; |
66 |
| - const kerberos = new Kerberos(); |
67 |
| - |
68 |
| - kerberos.authGSSClientInit(serviceName, 0, krb5CcName, function(err, ctx) { |
69 |
| - expect(err).to.not.exist; |
| 60 | + // validate the response supports the Negotiate protocol |
| 61 | + const authenticateHeader = response.headers['www-authenticate']; |
| 62 | + expect(authenticateHeader).to.exist; |
| 63 | + expect(authenticateHeader).to.equal('Negotiate'); |
70 | 64 |
|
71 |
| - kerberos.authGSSClientStep(ctx, '', function(err) { |
| 65 | + // generate the first Kerberos token |
| 66 | + const mechOID = kerberos.GSS_MECH_OID_KRB5; |
| 67 | + kerberos.initializeClient(service, { mechOID }, (err, client) => { |
72 | 68 | expect(err).to.not.exist;
|
73 | 69 |
|
74 |
| - const cleanupCtx = function() { |
75 |
| - kerberos.authGSSClientClean(ctx, function(err) { |
76 |
| - expect(err).to.not.exist; |
77 |
| - done(); |
78 |
| - }); |
79 |
| - }; |
80 |
| - |
81 |
| - const negotiateHeader = 'Negotiate ' + ctx.response; |
82 |
| - |
83 |
| - const req = http.get( |
84 |
| - { |
85 |
| - hostname: httpHostname, |
86 |
| - path: httpPath, |
87 |
| - headers: { |
88 |
| - authorization: negotiateHeader |
| 70 | + client.step('', (err, kerberosToken) => { |
| 71 | + expect(err).to.not.exist; |
| 72 | + |
| 73 | + // attach the Kerberos token and resend back to the host |
| 74 | + request.get( |
| 75 | + { url, headers: { Authorization: `Negotiate ${kerberosToken}` } }, |
| 76 | + (err, response) => { |
| 77 | + expect(err).to.not.exist; |
| 78 | + expect(response.statusCode).to.equal(200); |
| 79 | + |
| 80 | + // validate the headers exist and contain a www-authenticate message |
| 81 | + const authenticateHeader = response.headers['www-authenticate']; |
| 82 | + expect(authenticateHeader).to.exist; |
| 83 | + expect(authenticateHeader).to.startWith('Negotiate'); |
| 84 | + |
| 85 | + // verify the return Kerberos token |
| 86 | + const tokenParts = authenticateHeader.split(' '); |
| 87 | + const serverKerberosToken = tokenParts[tokenParts.length - 1]; |
| 88 | + client.step(serverKerberosToken, err => { |
| 89 | + expect(err).to.not.exist; |
| 90 | + expect(client.contextComplete).to.be.true; |
| 91 | + done(); |
| 92 | + }); |
89 | 93 | }
|
90 |
| - }, |
91 |
| - function(res) { |
92 |
| - expect(res.statusCode).to.be.at.least(200); |
93 |
| - expect(res.statusCode).to.be.at.most(299); |
94 |
| - |
95 |
| - res.on('data', data => console.log(` >> ${data}`)); |
96 |
| - res.on('end', () => cleanupCtx()); |
97 |
| - } |
98 |
| - ); |
99 |
| - |
100 |
| - req.on('error', function(err) { |
101 |
| - done(`http.get request failed: ${err.message}`); |
102 |
| - cleanupCtx(); |
| 94 | + ); |
103 | 95 | });
|
104 | 96 | });
|
105 | 97 | });
|
|
0 commit comments