Skip to content

Instantly share code, notes, and snippets.

@barathank
Forked from dmansfield/http_client_spnego.js
Created April 21, 2017 17:29
Show Gist options
  • Select an option

  • Save barathank/18d422718b1389c84a6caba6d49eaeaa to your computer and use it in GitHub Desktop.

Select an option

Save barathank/18d422718b1389c84a6caba6d49eaeaa to your computer and use it in GitHub Desktop.
Node.js HTTP client with kerberos/gssapi/negotiate/spnego authentication
//
// tested with kerberos 0.0.12 on linux against apache running mod_auth_kerb with Samba AD providing KDC
//
var Kerberos = require('kerberos').Kerberos;
var kerberos = new Kerberos();
var http = require('http');
function httpget(opts, callback) {
console.log('submitting to '+(opts.hostname||opts.host)+' with authorization header: '+(opts.headers||{}).authorization);
var req = http.get(opts, function(res) {
if (res.statusCode == 401) {
submitWithAuthorization(req, opts, callback);
return;
}
callback(res);
});
return req;
}
function submitWithAuthorization(oldreq, opts, callback) {
kerberos.authGSSClientInit("HTTP@"+(opts.hostname || opts.host), 0, function(err, ctx) {
if (err) {
throw new Error(""+err);
}
console.log('done init '+ctx);
kerberos.authGSSClientStep(ctx, "", function (err) {
if (err) {
throw new Error(""+err);
}
console.log('done step '+ctx.response);
var headers = opts.headers || {};
headers.authorization = "Negotiate "+ctx.response;
opts.headers = headers;
var newreq = httpget(opts, callback);
// tell oldReq "owner" about newReq. resubmit is an "unofficial" event
oldreq.emit('resubmit', newreq);
kerberos.authGSSClientClean(ctx, function(err) {
if (err) {
throw new Error(""+err);
}
});
});
});
}
// //////////////////////////////////////////////////////////////////
var options = {
hostname : "somehost.protected.by.spnego.example.com"
, path : "/"
};
var req = httpget(options, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log("BODY: "+body);
});
});
req.on('resubmit', function(newreq) {
console.log('request resubmitted');
req = newreq;
});
return;
@Logeswaranbca
Copy link

I got the error :

TypeError: Kerberos is not a constructor
at Object. (Login/test.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment