node-posh
Version:
PKIX Over Secure HTTP (POSH) tools for node.js
127 lines (105 loc) • 3.35 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var FileCert, NewCert, Q, a, args, argv, c, cn, complain, f, fs, pem, posh, usage, _i, _len, _ref;
pem = require('pem');
fs = require('fs');
posh = require('./index');
Q = require('q');
usage = function() {
process.stderr.write("Usage: genposh [options] [cert filename...]\n\nOptions:\n --help, -h Show this message and exit\n --out, -o Directory in which to output files [default: \".\"]\n --days, -d Days of validity for the generated certificate [default: 365]\n --service, -s SRV-style service name for the POSH file [default: \"_xmpp-server._tcp\"]\n --maxcerts, -m The maximum number of certs to output in the\n x5c field. 0 means all. [default: 0]\n --commonname, -c Create a new certificate, with this common name (multiple ok)");
return process.exit(64);
};
complain = function(er) {
console.log(er);
return process.exit(1);
};
NewCert = (function() {
function NewCert(cn) {
this.cn = cn;
this.days = 365;
this.dir = '.';
}
NewCert.prototype.create = function() {
var _this = this;
return Q.nfcall(pem.createCertificate, {
days: this.days,
selfSigned: true,
commonName: this.cn
}).then(function(keys) {
return Q.all([Q.nfcall(fs.writeFile, "" + _this.dir + "/" + _this.cn + "-key.pem", keys.clientKey), Q.nfcall(fs.writeFile, "" + _this.dir + "/" + _this.cn + ".pem", keys.certificate)]).then(function() {
return keys.certificate;
});
});
};
return NewCert;
})();
FileCert = (function() {
function FileCert(fn) {
this.fn = fn;
}
FileCert.prototype.create = function() {
return Q.nfcall(fs.readFile, this.fn).then(function(data) {
return data.toString('utf8');
});
};
return FileCert;
})();
argv = {
help: false,
maxcerts: 0,
service: '_xmpp-server._tcp',
days: 365,
out: '.',
certs: []
};
args = process.argv.slice(2);
while (args.length) {
a = args.shift();
switch (a) {
case '-h':
case '--help':
usage();
break;
case '-o':
case '--out':
argv.out = args.shift() || usage();
break;
case '-d':
case '--days':
argv.days = parseInt(args.shift()) || usage();
break;
case '-s':
case '--service':
argv.service = args.shift() || usage();
break;
case '-m':
case '--maxcerts':
argv.maxcerts = parseInt(args.shift()) || usage();
break;
case '-c':
case '--commonname':
cn = args.shift() || usage();
argv.certs.push(new NewCert(cn));
break;
default:
argv.certs.push(new FileCert(a));
}
}
_ref = argv.certs;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
if (!(c instanceof NewCert)) {
continue;
}
c.days = argv.days;
c.dir = argv.out;
}
f = argv.certs.map(function(cert) {
return c.create();
});
Q.all(f).then(function(certs) {
return posh.create(certs, argv.maxcerts).then(function(json) {
return posh.write(argv.out, argv.service, json);
});
}, complain);
}).call(this);