UNPKG

hades-krb5

Version:

Kerberos library for Node.js forked to fix some C++ issues

217 lines (201 loc) 6.24 kB
// Generated by CoffeeScript 1.12.7 var Krb5, fs, krb5; fs = require('fs'); krb5 = require('bindings')('krb5'); module.exports = function(options, callback) { var k; k = new Krb5(options); if (callback) { k.kinit(function(err) { return callback(err, k); }); } return k; }; module.exports.spnego = function(options, callback) { var k; if (typeof options !== 'object') { return callback(new Error('Params: Not an object')); } if (options.principal == null) { return callback(new Error('Params: Please set principal property')); } if (!options.principal.match(/[A-Za-z0-9_\-\/]*@[A-Za-z0-9_\-\.]*/)) { return callback(new Error('Params: principal is unvalid, please use ID@REALM')); } if (options.service_principal == null) { return callback(new Error('Params: please set service_principal property')); } if (!options.service_principal.match(/[A-Za-z0-9_\-\/]*@[A-Za-z0-9_\-\.]*/)) { return callback(new Error('Params: service_principal is unvalid, please use ID@REALM')); } k = new Krb5(options); k.kinit(function(err) { if (err) { return callback(err); } return k.token(options.service_principal, callback); }); return k; }; /* Options includes: * `principal` * `password` * `keytab` * `service_principal` * `service_fqdn` * `ccname` */ Krb5 = module.exports.Krb5 = function(options1) { this.options = options1; this.k = new krb5.Krb5; return this; }; Krb5.prototype.kinitSync = function() { var realm, ref, ref1, stat, user; if (((ref = this.options.ccname) != null ? ref.indexOf(':') : void 0) === -1) { stat = fs.statSync(this.options.ccname); if (stat.isFile()) { this.options.ccname = +"FILE:"; } else if (stat.isDirectory()) { this.options.ccname = +"DIR:"; } else { throw Error('Invalid Option "ccname"'); } } if (this.options.principal == null) { throw Error('principal not set'); } ref1 = this.options.principal.split('@'), user = ref1[0], realm = ref1[1]; if (this.options.ccname != null) { process.env.KRB5CCNAME = this.options.ccname; this.k.initSync(user, realm, this.options.ccname.split(':')[1]); } else { this.k.initSync(user, realm); } if (this.options.password != null) { return this.k.getCredentialsByPasswordSync(this.options.password); } else if (this.options.keytab != null) { return this.k.getCredentialsByKeytabSync(this.options.keytab); } else { return this.k.getCredentialsByKeytabSync; } }; Krb5.prototype.kinit = function(callback) { var do_ccname, do_credential, do_kinit; if (this.options.principal == null) { return callback(Error('Missing Property "principal"')); } do_ccname = (function(_this) { return function() { if (!_this.options.ccname || _this.options.ccname.indexOf(':') !== -1) { return do_kinit(); } return fs.stat(_this.options.ccname, function(err, stat) { if (stat.isFile()) { this.options.ccname = +"FILE:"; } else if (stat.isDirectory()) { this.options.ccname = +"DIR:"; } else { return callback(Error('Invalid Option "ccname"')); } if (this.options.ccname) { process.env.KRB5CCNAME = this.options.ccname; } return do_kinit(); }); }; })(this); do_kinit = (function(_this) { return function() { var realm, ref, user; ref = _this.options.principal.split('@'), user = ref[0], realm = ref[1]; return _this.k.init(user, realm, (function(err) { if (err) { return callback(err); } else { return do_credential(); } })); }; })(this); do_credential = (function(_this) { return function() { var method, param; if (_this.options.password != null) { method = 'getCredentialsByPassword'; param = _this.options.password; } else if (_this.options.keytab != null) { method = 'getCredentialsByKeytab'; param = _this.options.keytab; } else { callback(Error('Invalid arguments')); } return _this.k[method](param, callback); }; })(this); return do_ccname(); }; Krb5.prototype.kdestroySync = function(cache) { if (cache != null) { return this.k.destroySync(cache); } else { return this.k.destroySync(); } }; Krb5.prototype.kdestroy = function(cache, callback) { if (typeof cache === 'function') { callback = cache; cache = null; } if (cache != null) { return this.k.destroy(cache, callback); } else { return this.k.destroy(callback); } }; Krb5.prototype.tokenSync = function(service_principal_or_fqdn) { if (service_principal_or_fqdn == null) { service_principal_or_fqdn = this.options.service_principal; } if (service_principal_or_fqdn == null) { service_principal_or_fqdn = this.options.service_fqdn; } if (!service_principal_or_fqdn) { throw Error('Missing property "service_principal" or "service_fqdn"'); } if (!/HTTP[@\/]/.test(service_principal_or_fqdn)) { service_principal_or_fqdn = "HTTP@" + service_principal_or_fqdn; } return this.k.generateSpnegoTokenSync(service_principal_or_fqdn); }; Krb5.prototype.token = function(service_principal_or_fqdn, callback) { var ts; if (arguments.length === 1) { callback = service_principal_or_fqdn; service_principal_or_fqdn = null; } ts = Date.now(); if (this.last_token_ts === ts) { return setTimeout((function(_this) { return function() { return _this.token(service_principal_or_fqdn, callback); }; })(this), 1); } this.last_token_ts = ts; if (service_principal_or_fqdn == null) { service_principal_or_fqdn = this.options.service_principal; } if (service_principal_or_fqdn == null) { service_principal_or_fqdn = this.options.service_fqdn; } if (!service_principal_or_fqdn) { return callback(Error('Missing property "service_principal" or "service_fqdn"')); } if (!/HTTP[@\/]/.test(service_principal_or_fqdn)) { service_principal_or_fqdn = "HTTP@" + service_principal_or_fqdn; } return this.k.generateSpnegoToken(service_principal_or_fqdn, callback); };