replace "le" with "greenlock"

This commit is contained in:
AJ ONeal 2018-05-15 16:01:09 -06:00
parent 1931feed1d
commit 8ddeb26063
3 changed files with 191 additions and 195 deletions

286
index.js
View File

@ -4,8 +4,9 @@ var DAY = 24 * 60 * 60 * 1000;
//var MIN = 60 * 1000; //var MIN = 60 * 1000;
var ACME = require('acme-v2/compat').ACME; var ACME = require('acme-v2/compat').ACME;
var LE = module.exports; var Greenlock = module.exports;
LE.LE = LE; Greenlock.Greenlock = Greenlock;
Greenlock.LE = Greenlock;
// in-process cache, shared between all instances // in-process cache, shared between all instances
var ipc = {}; var ipc = {};
@ -13,12 +14,12 @@ function _log(debug) {
if (debug) { if (debug) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
args.shift(); args.shift();
args.unshift("[le/index.js]"); args.unshift("[gl/index.js]");
console.log.apply(console, args); console.log.apply(console, args);
} }
} }
LE.defaults = { Greenlock.defaults = {
productionServerUrl: 'https://acme-v01.api.letsencrypt.org/directory' productionServerUrl: 'https://acme-v01.api.letsencrypt.org/directory'
, stagingServerUrl: 'https://acme-staging.api.letsencrypt.org/directory' , stagingServerUrl: 'https://acme-staging.api.letsencrypt.org/directory'
@ -30,13 +31,13 @@ LE.defaults = {
}; };
// backwards compat // backwards compat
Object.keys(LE.defaults).forEach(function (key) { Object.keys(Greenlock.defaults).forEach(function (key) {
LE[key] = LE.defaults[key]; Greenlock[key] = Greenlock.defaults[key];
}); });
// show all possible options // show all possible options
var u; // undefined var u; // undefined
LE._undefined = { Greenlock._undefined = {
acme: u acme: u
, store: u , store: u
, challenge: u , challenge: u
@ -59,56 +60,51 @@ LE._undefined = {
, duplicate: u , duplicate: u
, _acmeUrls: u , _acmeUrls: u
}; };
LE._undefine = function (le) { Greenlock._undefine = function (gl) {
Object.keys(LE._undefined).forEach(function (key) { Object.keys(Greenlock._undefined).forEach(function (key) {
if (!(key in le)) { if (!(key in gl)) {
le[key] = u; gl[key] = u;
} }
}); });
return le; return gl;
}; };
LE.create = function (le) { Greenlock.create = function (gl) {
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
le.store = le.store || require('le-store-certbot').create({ debug: le.debug }); gl.store = gl.store || require('le-store-certbot').create({ debug: gl.debug });
le.core = require('./lib/core'); gl.core = require('./lib/core');
var log = le.log || _log; var log = gl.log || _log;
if (!le.challenges) { if (!gl.challenges) {
le.challenges = {}; gl.challenges = {};
} }
if (!le.challenges['http-01']) { if (!gl.challenges['http-01']) {
le.challenges['http-01'] = require('le-challenge-fs').create({ debug: le.debug }); gl.challenges['http-01'] = require('le-challenge-fs').create({ debug: gl.debug });
} }
/* if (!gl.challenges['dns-01']) {
if (!le.challenges['tls-sni-01']) {
le.challenges['tls-sni-01'] = require('le-challenge-sni').create({ debug: le.debug });
}
*/
if (!le.challenges['dns-01']) {
try { try {
le.challenges['dns-01'] = require('le-challenge-ddns').create({ debug: le.debug }); gl.challenges['dns-01'] = require('le-challenge-ddns').create({ debug: gl.debug });
} catch(e) { } catch(e) {
try { try {
le.challenges['dns-01'] = require('le-challenge-dns').create({ debug: le.debug }); gl.challenges['dns-01'] = require('le-challenge-dns').create({ debug: gl.debug });
} catch(e) { } catch(e) {
// not yet implemented // not yet implemented
} }
} }
} }
le = LE._undefine(le); gl = Greenlock._undefine(gl);
le.acmeChallengePrefix = LE.acmeChallengePrefix; gl.acmeChallengePrefix = Greenlock.acmeChallengePrefix;
le.rsaKeySize = le.rsaKeySize || LE.rsaKeySize; gl.rsaKeySize = gl.rsaKeySize || Greenlock.rsaKeySize;
le.challengeType = le.challengeType || LE.challengeType; gl.challengeType = gl.challengeType || Greenlock.challengeType;
le._ipc = ipc; gl._ipc = ipc;
le._communityPackage = le._communityPackage || 'greenlock.js'; gl._communityPackage = gl._communityPackage || 'greenlock.js';
le.agreeToTerms = le.agreeToTerms || function (args, agreeCb) { gl.agreeToTerms = gl.agreeToTerms || function (args, agreeCb) {
agreeCb(new Error("'agreeToTerms' was not supplied to LE and 'agreeTos' was not supplied to LE.register")); agreeCb(new Error("'agreeToTerms' was not supplied to Greenlock and 'agreeTos' was not supplied to Greenlock.register"));
}; };
if (!le.renewWithin) { le.renewWithin = 14 * DAY; } if (!gl.renewWithin) { gl.renewWithin = 14 * DAY; }
// renewBy has a default in le-sni-auto // renewBy has a default in le-sni-auto
@ -117,7 +113,7 @@ LE.create = function (le) {
// BEGIN VERSION MADNESS // // BEGIN VERSION MADNESS //
/////////////////////////// ///////////////////////////
if (!le.version) { if (!gl.version) {
//console.warn("Please specify version: 'v01' (Let's Encrypt v1) or 'draft-11' (Let's Encrypt v2 / ACME draft 11)"); //console.warn("Please specify version: 'v01' (Let's Encrypt v1) or 'draft-11' (Let's Encrypt v2 / ACME draft 11)");
console.warn(""); console.warn("");
console.warn(""); console.warn("");
@ -141,40 +137,40 @@ LE.create = function (le) {
console.warn(""); console.warn("");
console.warn(""); console.warn("");
console.warn(""); console.warn("");
} else if ('v02' === le.version) { } else if ('v02' === gl.version) {
le.version = 'draft-11'; gl.version = 'draft-11';
} else if ('v01' !== le.version && 'draft-11' !== le.version) { } else if ('v01' !== gl.version && 'draft-11' !== gl.version) {
throw new Error("Unrecognized version '" + le.version + "'"); throw new Error("Unrecognized version '" + gl.version + "'");
} }
if (!le.server) { if (!gl.server) {
throw new Error("opts.server must specify an ACME directory URL, such as 'https://acme-staging-v02.api.letsencrypt.org/directory'"); throw new Error("opts.server must specify an ACME directory URL, such as 'https://acme-staging-v02.api.letsencrypt.org/directory'");
} }
if ('staging' === le.server) { if ('staging' === gl.server) {
le.server = 'https://acme-staging.api.letsencrypt.org/directory'; gl.server = 'https://acme-staging.api.letsencrypt.org/directory';
le.version = 'v01'; gl.version = 'v01';
console.warn(""); console.warn("");
console.warn(""); console.warn("");
console.warn("=== WARNING ==="); console.warn("=== WARNING ===");
console.warn(""); console.warn("");
console.warn("Due to versioning issues the 'staging' option is deprecated. Please specify the full url and version."); console.warn("Due to versioning issues the 'staging' option is deprecated. Please specify the full url and version.");
console.warn(""); console.warn("");
console.warn("\t--acme-url '" + le.server + "' \\"); console.warn("\t--acme-url '" + gl.server + "' \\");
console.warn("\t--acme-version '" + le.version + "' \\"); console.warn("\t--acme-version '" + gl.version + "' \\");
console.warn(""); console.warn("");
console.warn(""); console.warn("");
} }
else if ('production' === le.server) { else if ('production' === gl.server) {
le.server = 'https://acme-v01.api.letsencrypt.org/directory'; gl.server = 'https://acme-v01.api.letsencrypt.org/directory';
le.version = 'v01'; gl.version = 'v01';
console.warn(""); console.warn("");
console.warn(""); console.warn("");
console.warn("=== WARNING ==="); console.warn("=== WARNING ===");
console.warn(""); console.warn("");
console.warn("Due to versioning issues the 'production' option is deprecated. Please specify the full url and version."); console.warn("Due to versioning issues the 'production' option is deprecated. Please specify the full url and version.");
console.warn(""); console.warn("");
console.warn("\t--acme-url '" + le.server + "' \\"); console.warn("\t--acme-url '" + gl.server + "' \\");
console.warn("\t--acme-version '" + le.version + "' \\"); console.warn("\t--acme-version '" + gl.version + "' \\");
console.warn(""); console.warn("");
console.warn(""); console.warn("");
} }
@ -202,23 +198,23 @@ LE.create = function (le) {
if (-1 !== [ if (-1 !== [
'https://acme-v02.api.letsencrypt.org/directory' 'https://acme-v02.api.letsencrypt.org/directory'
, 'https://acme-staging-v02.api.letsencrypt.org/directory' ].indexOf(le.server) , 'https://acme-staging-v02.api.letsencrypt.org/directory' ].indexOf(gl.server)
) { ) {
if ('draft-11' !== le.version) { if ('draft-11' !== gl.version) {
console.warn("Detected Let's Encrypt v02 URL. Changing version to draft-11."); console.warn("Detected Let's Encrypt v02 URL. Changing version to draft-11.");
le.version = 'draft-11'; gl.version = 'draft-11';
} }
} else if (-1 !== [ } else if (-1 !== [
'https://acme-v01.api.letsencrypt.org/directory' 'https://acme-v01.api.letsencrypt.org/directory'
, 'https://acme-staging.api.letsencrypt.org/directory' ].indexOf(le.server) , 'https://acme-staging.api.letsencrypt.org/directory' ].indexOf(gl.server)
|| 'v01' === le.version || 'v01' === gl.version
) { ) {
if ('v01' !== le.version) { if ('v01' !== gl.version) {
console.warn("Detected Let's Encrypt v01 URL (deprecated). Changing version to v01."); console.warn("Detected Let's Encrypt v01 URL (deprecated). Changing version to v01.");
le.version = 'v01'; gl.version = 'v01';
} }
} }
if ('v01' === le.version) { if ('v01' === gl.version) {
ACME = loadLeV01(); ACME = loadLeV01();
} }
///////////////////////// /////////////////////////
@ -227,28 +223,28 @@ LE.create = function (le) {
le.acme = le.acme || ACME.create({ debug: le.debug }); gl.acme = gl.acme || ACME.create({ debug: gl.debug });
if (le.acme.create) { if (gl.acme.create) {
le.acme = le.acme.create(le); gl.acme = gl.acme.create(gl);
} }
le.acme = PromiseA.promisifyAll(le.acme); gl.acme = PromiseA.promisifyAll(gl.acme);
le._acmeOpts = le.acme.getOptions(); gl._acmeOpts = gl.acme.getOptions();
Object.keys(le._acmeOpts).forEach(function (key) { Object.keys(gl._acmeOpts).forEach(function (key) {
if (!(key in le)) { if (!(key in gl)) {
le[key] = le._acmeOpts[key]; gl[key] = gl._acmeOpts[key];
} }
}); });
if (le.store.create) { if (gl.store.create) {
le.store = le.store.create(le); gl.store = gl.store.create(gl);
} }
le.store = PromiseA.promisifyAll(le.store); gl.store = PromiseA.promisifyAll(gl.store);
le.store.accounts = PromiseA.promisifyAll(le.store.accounts); gl.store.accounts = PromiseA.promisifyAll(gl.store.accounts);
le.store.certificates = PromiseA.promisifyAll(le.store.certificates); gl.store.certificates = PromiseA.promisifyAll(gl.store.certificates);
le._storeOpts = le.store.getOptions(); gl._storeOpts = gl.store.getOptions();
Object.keys(le._storeOpts).forEach(function (key) { Object.keys(gl._storeOpts).forEach(function (key) {
if (!(key in le)) { if (!(key in gl)) {
le[key] = le._storeOpts[key]; gl[key] = gl._storeOpts[key];
} }
}); });
@ -256,118 +252,118 @@ LE.create = function (le) {
// //
// Backwards compat for <= v2.1.7 // Backwards compat for <= v2.1.7
// //
if (le.challenge) { if (gl.challenge) {
console.warn("Deprecated use of le.challenge. Use le.challenges['" + LE.challengeType + "'] instead."); console.warn("Deprecated use of gl.challenge. Use gl.challenges['" + Greenlock.challengeType + "'] instead.");
le.challenges[le.challengeType] = le.challenge; gl.challenges[gl.challengeType] = gl.challenge;
} }
LE.challengeTypes.forEach(function (challengeType) { Greenlock.challengeTypes.forEach(function (challengeType) {
var challenger = le.challenges[challengeType]; var challenger = gl.challenges[challengeType];
if (!challenger) { if (!challenger) {
return; return;
} }
if (challenger.create) { if (challenger.create) {
challenger = le.challenges[challengeType] = challenger.create(le); challenger = gl.challenges[challengeType] = challenger.create(gl);
} }
challenger = le.challenges[challengeType] = PromiseA.promisifyAll(challenger); challenger = gl.challenges[challengeType] = PromiseA.promisifyAll(challenger);
le['_challengeOpts_' + challengeType] = challenger.getOptions(); gl['_challengeOpts_' + challengeType] = challenger.getOptions();
Object.keys(le['_challengeOpts_' + challengeType]).forEach(function (key) { Object.keys(gl['_challengeOpts_' + challengeType]).forEach(function (key) {
if (!(key in le)) { if (!(key in gl)) {
le[key] = le['_challengeOpts_' + challengeType][key]; gl[key] = gl['_challengeOpts_' + challengeType][key];
} }
}); });
// TODO wrap these here and now with tplCopy? // TODO wrap these here and now with tplCopy?
if (!challenger.set || 5 !== challenger.set.length) { if (!challenger.set || 5 !== challenger.set.length) {
throw new Error("le.challenges[" + challengeType + "].set receives the wrong number of arguments." throw new Error("gl.challenges[" + challengeType + "].set receives the wrong number of arguments."
+ " You must define setChallenge as function (opts, domain, token, keyAuthorization, cb) { }"); + " You must define setChallenge as function (opts, domain, token, keyAuthorization, cb) { }");
} }
if (challenger.get && 4 !== challenger.get.length) { if (challenger.get && 4 !== challenger.get.length) {
throw new Error("le.challenges[" + challengeType + "].get receives the wrong number of arguments." throw new Error("gl.challenges[" + challengeType + "].get receives the wrong number of arguments."
+ " You must define getChallenge as function (opts, domain, token, cb) { }"); + " You must define getChallenge as function (opts, domain, token, cb) { }");
} }
if (!challenger.remove || 4 !== challenger.remove.length) { if (!challenger.remove || 4 !== challenger.remove.length) {
throw new Error("le.challenges[" + challengeType + "].remove receives the wrong number of arguments." throw new Error("gl.challenges[" + challengeType + "].remove receives the wrong number of arguments."
+ " You must define removeChallenge as function (opts, domain, token, cb) { }"); + " You must define removeChallenge as function (opts, domain, token, cb) { }");
} }
/* /*
if (!le._challengeWarn && (!challenger.loopback || 4 !== challenger.loopback.length)) { if (!gl._challengeWarn && (!challenger.loopback || 4 !== challenger.loopback.length)) {
le._challengeWarn = true; gl._challengeWarn = true;
console.warn("le.challenges[" + challengeType + "].loopback should be defined as function (opts, domain, token, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed"); console.warn("gl.challenges[" + challengeType + "].loopback should be defined as function (opts, domain, token, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed");
} }
else if (!le._challengeWarn && (!challenger.test || 5 !== challenger.test.length)) { else if (!gl._challengeWarn && (!challenger.test || 5 !== challenger.test.length)) {
le._challengeWarn = true; gl._challengeWarn = true;
console.warn("le.challenges[" + challengeType + "].test should be defined as function (opts, domain, token, keyAuthorization, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed"); console.warn("gl.challenges[" + challengeType + "].test should be defined as function (opts, domain, token, keyAuthorization, cb) { ... } and should prove (by external means) that the ACME server challenge '" + challengeType + "' will succeed");
} }
*/ */
}); });
le.sni = le.sni || null; gl.sni = gl.sni || null;
le.tlsOptions = le.tlsOptions || le.httpsOptions || {}; gl.tlsOptions = gl.tlsOptions || gl.httpsOptions || {};
if (!le.tlsOptions.SNICallback) { if (!gl.tlsOptions.SNICallback) {
if (!le.getCertificatesAsync && !le.getCertificates) { if (!gl.getCertificatesAsync && !gl.getCertificates) {
if (Array.isArray(le.approveDomains)) { if (Array.isArray(gl.approveDomains)) {
le.approvedDomains = le.approveDomains; gl.approvedDomains = gl.approveDomains;
le.approveDomains = null; gl.approveDomains = null;
} }
if (!le.approveDomains) { if (!gl.approveDomains) {
le.approvedDomains = le.approvedDomains || []; gl.approvedDomains = gl.approvedDomains || [];
le.approveDomains = function (lexOpts, certs, cb) { gl.approveDomains = function (lexOpts, certs, cb) {
if (!le.email) { if (!gl.email) {
throw new Error("le-sni-auto is not properly configured. Missing email"); throw new Error("le-sni-auto is not properly configured. Missing email");
} }
if (!le.agreeTos) { if (!gl.agreeTos) {
throw new Error("le-sni-auto is not properly configured. Missing agreeTos"); throw new Error("le-sni-auto is not properly configured. Missing agreeTos");
} }
if (!le.approvedDomains.length) { if (!gl.approvedDomains.length) {
throw new Error("le-sni-auto is not properly configured. Missing approveDomains(domain, certs, callback)"); throw new Error("le-sni-auto is not properly configured. Missing approveDomains(domain, certs, callback)");
} }
if (lexOpts.domains.every(function (domain) { if (lexOpts.domains.every(function (domain) {
return -1 !== le.approvedDomains.indexOf(domain); return -1 !== gl.approvedDomains.indexOf(domain);
})) { })) {
lexOpts.domains = le.approvedDomains.slice(0); lexOpts.domains = gl.approvedDomains.slice(0);
lexOpts.email = le.email; lexOpts.email = gl.email;
lexOpts.agreeTos = le.agreeTos; lexOpts.agreeTos = gl.agreeTos;
lexOpts.communityMember = lexOpts.communityMember; lexOpts.communityMember = lexOpts.communityMember;
return cb(null, { options: lexOpts, certs: certs }); return cb(null, { options: lexOpts, certs: certs });
} }
log(le.debug, 'unapproved domain', lexOpts.domains, le.approvedDomains); log(gl.debug, 'unapproved domain', lexOpts.domains, gl.approvedDomains);
cb(new Error("unapproved domain")); cb(new Error("unapproved domain"));
}; };
} }
le.getCertificates = function (domain, certs, cb) { gl.getCertificates = function (domain, certs, cb) {
// certs come from current in-memory cache, not lookup // certs come from current in-memory cache, not lookup
log(le.debug, 'le.getCertificates called for', domain, 'with certs for', certs && certs.altnames || 'NONE'); log(gl.debug, 'gl.getCertificates called for', domain, 'with certs for', certs && certs.altnames || 'NONE');
var opts = { domain: domain, domains: certs && certs.altnames || [ domain ] }; var opts = { domain: domain, domains: certs && certs.altnames || [ domain ] };
try { try {
le.approveDomains(opts, certs, function (_err, results) { gl.approveDomains(opts, certs, function (_err, results) {
if (_err) { if (_err) {
log(le.debug, 'le.approveDomains called with error', _err); log(gl.debug, 'gl.approveDomains called with error', _err);
cb(_err); cb(_err);
return; return;
} }
log(le.debug, 'le.approveDomains called with certs for', results.certs && results.certs.altnames || 'NONE', 'and options:'); log(gl.debug, 'gl.approveDomains called with certs for', results.certs && results.certs.altnames || 'NONE', 'and options:');
log(le.debug, results.options); log(gl.debug, results.options);
var promise; var promise;
if (results.certs) { if (results.certs) {
log(le.debug, 'le renewing'); log(gl.debug, 'gl renewing');
promise = le.core.certificates.renewAsync(results.options, results.certs); promise = gl.core.certificates.renewAsync(results.options, results.certs);
} }
else { else {
log(le.debug, 'le getting from disk or registering new'); log(gl.debug, 'gl getting from disk or registering new');
promise = le.core.certificates.getAsync(results.options); promise = gl.core.certificates.getAsync(results.options);
} }
return promise.then(function (certs) { cb(null, certs); }, function (e) { return promise.then(function (certs) { cb(null, certs); }, function (e) {
if (le.debug) { console.debug("Error"); console.debug(e); } if (gl.debug) { console.debug("Error"); console.debug(e); }
cb(e); cb(e);
}); });
}); });
@ -378,13 +374,13 @@ LE.create = function (le) {
} }
}; };
} }
le.sni = le.sni || require('le-sni-auto'); gl.sni = gl.sni || require('le-sni-auto');
if (le.sni.create) { if (gl.sni.create) {
le.sni = le.sni.create(le); gl.sni = gl.sni.create(gl);
} }
le.tlsOptions.SNICallback = function (domain, cb) { gl.tlsOptions.SNICallback = function (domain, cb) {
try { try {
le.sni.sniCallback(domain, cb); gl.sni.sniCallback(domain, cb);
} catch(e) { } catch(e) {
console.error("[ERROR] Something went wrong in the SNICallback:"); console.error("[ERROR] Something went wrong in the SNICallback:");
console.error(e); console.error(e);
@ -395,29 +391,29 @@ LE.create = function (le) {
// We want to move to using tlsOptions instead of httpsOptions, but we also need to make // We want to move to using tlsOptions instead of httpsOptions, but we also need to make
// sure anything that uses this object will still work if looking for httpsOptions. // sure anything that uses this object will still work if looking for httpsOptions.
le.httpsOptions = le.tlsOptions; gl.httpsOptions = gl.tlsOptions;
if (le.core.create) { if (gl.core.create) {
le.core = le.core.create(le); gl.core = gl.core.create(gl);
} }
le.renew = function (args, certs) { gl.renew = function (args, certs) {
return le.core.certificates.renewAsync(args, certs); return gl.core.certificates.renewAsync(args, certs);
}; };
le.register = function (args) { gl.register = function (args) {
return le.core.certificates.getAsync(args); return gl.core.certificates.getAsync(args);
}; };
le.check = function (args) { gl.check = function (args) {
// TODO must return email, domains, tos, pems // TODO must return email, domains, tos, pems
return le.core.certificates.checkAsync(args); return gl.core.certificates.checkAsync(args);
}; };
le.middleware = le.middleware || require('./lib/middleware'); gl.middleware = gl.middleware || require('./lib/middleware');
if (le.middleware.create) { if (gl.middleware.create) {
le.middleware = le.middleware.create(le); gl.middleware = gl.middleware.create(gl);
} }
return le; return gl;
}; };

View File

@ -4,16 +4,16 @@ function _log(debug) {
if (debug) { if (debug) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
args.shift(); args.shift();
args.unshift("[le/lib/core.js]"); args.unshift("[greenlock/lib/core.js]");
console.log.apply(console, args); console.log.apply(console, args);
} }
} }
module.exports.create = function (le) { module.exports.create = function (gl) {
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
var utils = require('./utils'); var utils = require('./utils');
var RSA = PromiseA.promisifyAll(require('rsa-compat').RSA); var RSA = PromiseA.promisifyAll(require('rsa-compat').RSA);
var log = le.log || _log; // allow custom log var log = gl.log || _log; // allow custom log
var pendingRegistrations = {}; var pendingRegistrations = {};
var core = { var core = {
@ -24,15 +24,15 @@ module.exports.create = function (le) {
var now = Date.now(); var now = Date.now();
// TODO check response header on request for cache time // TODO check response header on request for cache time
if ((now - le._ipc.acmeUrlsUpdatedAt) < 10 * 60 * 1000) { if ((now - gl._ipc.acmeUrlsUpdatedAt) < 10 * 60 * 1000) {
return PromiseA.resolve(le._ipc.acmeUrls); return PromiseA.resolve(gl._ipc.acmeUrls);
} }
return le.acme.getAcmeUrlsAsync(args.server).then(function (data) { return gl.acme.getAcmeUrlsAsync(args.server).then(function (data) {
le._ipc.acmeUrlsUpdatedAt = Date.now(); gl._ipc.acmeUrlsUpdatedAt = Date.now();
le._ipc.acmeUrls = data; gl._ipc.acmeUrls = data;
return le._ipc.acmeUrls; return gl._ipc.acmeUrls;
}); });
} }
@ -48,7 +48,7 @@ module.exports.create = function (le) {
// Accounts // Accounts
registerAsync: function (args) { registerAsync: function (args) {
var err; var err;
var copy = utils.merge(args, le); var copy = utils.merge(args, gl);
var disagreeTos; var disagreeTos;
args = utils.tplCopy(copy); args = utils.tplCopy(copy);
@ -65,20 +65,20 @@ module.exports.create = function (le) {
return utils.testEmail(args.email).then(function () { return utils.testEmail(args.email).then(function () {
var keypairOpts = { public: true, pem: true }; var keypairOpts = { public: true, pem: true };
var promise = le.store.accounts.checkKeypairAsync(args).then(function (keypair) { var promise = gl.store.accounts.checkKeypairAsync(args).then(function (keypair) {
if (keypair) { if (keypair) {
return RSA.import(keypair); return RSA.import(keypair);
} }
if (args.accountKeypair) { if (args.accountKeypair) {
return le.store.accounts.setKeypairAsync(args, RSA.import(args.accountKeypair)); return gl.store.accounts.setKeypairAsync(args, RSA.import(args.accountKeypair));
} }
return RSA.generateKeypairAsync(args.rsaKeySize, 65537, keypairOpts).then(function (keypair) { return RSA.generateKeypairAsync(args.rsaKeySize, 65537, keypairOpts).then(function (keypair) {
keypair.privateKeyPem = RSA.exportPrivatePem(keypair); keypair.privateKeyPem = RSA.exportPrivatePem(keypair);
keypair.publicKeyPem = RSA.exportPublicPem(keypair); keypair.publicKeyPem = RSA.exportPublicPem(keypair);
keypair.privateKeyJwk = RSA.exportPrivateJwk(keypair); keypair.privateKeyJwk = RSA.exportPrivateJwk(keypair);
return le.store.accounts.setKeypairAsync(args, keypair); return gl.store.accounts.setKeypairAsync(args, keypair);
}); });
}); });
@ -88,11 +88,11 @@ module.exports.create = function (le) {
return core.getAcmeUrlsAsync(args).then(function (urls) { return core.getAcmeUrlsAsync(args).then(function (urls) {
args._acmeUrls = urls; args._acmeUrls = urls;
return le.acme.registerNewAccountAsync({ return gl.acme.registerNewAccountAsync({
email: args.email email: args.email
, newRegUrl: args._acmeUrls.newReg , newRegUrl: args._acmeUrls.newReg
, agreeToTerms: function (tosUrl, agreeCb) { , agreeToTerms: function (tosUrl, agreeCb) {
if (true === args.agreeTos || tosUrl === args.agreeTos || tosUrl === le.agreeToTerms) { if (true === args.agreeTos || tosUrl === args.agreeTos || tosUrl === gl.agreeToTerms) {
agreeCb(null, tosUrl); agreeCb(null, tosUrl);
return; return;
} }
@ -100,11 +100,11 @@ module.exports.create = function (le) {
// args.email = email; // already there // args.email = email; // already there
// args.domains = domains // already there // args.domains = domains // already there
args.tosUrl = tosUrl; args.tosUrl = tosUrl;
le.agreeToTerms(args, agreeCb); gl.agreeToTerms(args, agreeCb);
} }
, accountKeypair: keypair , accountKeypair: keypair
, debug: le.debug || args.debug , debug: gl.debug || args.debug
}).then(function (receipt) { }).then(function (receipt) {
var reg = { var reg = {
keypair: keypair keypair: keypair
@ -113,7 +113,7 @@ module.exports.create = function (le) {
}; };
// TODO move templating of arguments to right here? // TODO move templating of arguments to right here?
return le.store.accounts.setAsync(args, reg).then(function (account) { return gl.store.accounts.setAsync(args, reg).then(function (account) {
// should now have account.id and account.accountId // should now have account.id and account.accountId
args.account = account; args.account = account;
args.accountId = account.id; args.accountId = account.id;
@ -145,10 +145,10 @@ module.exports.create = function (le) {
)); ));
} }
var copy = utils.merge(args, le); var copy = utils.merge(args, gl);
args = utils.tplCopy(copy); args = utils.tplCopy(copy);
return le.store.accounts.checkAsync(args).then(function (account) { return gl.store.accounts.checkAsync(args).then(function (account) {
if (!account) { if (!account) {
return null; return null;
@ -166,9 +166,9 @@ module.exports.create = function (le) {
// Certificates // Certificates
registerAsync: function (args) { registerAsync: function (args) {
var err; var err;
var challengeDefaults = le['_challengeOpts_' + (args.challengeType || le.challengeType)] || {}; var challengeDefaults = gl['_challengeOpts_' + (args.challengeType || gl.challengeType)] || {};
var copy = utils.merge(args, challengeDefaults || {}); var copy = utils.merge(args, challengeDefaults || {});
copy = utils.merge(copy, le); copy = utils.merge(copy, gl);
args = utils.tplCopy(copy); args = utils.tplCopy(copy);
if (!Array.isArray(args.domains)) { if (!Array.isArray(args.domains)) {
@ -224,13 +224,13 @@ module.exports.create = function (le) {
return core.accounts.getAsync(args).then(function (account) { return core.accounts.getAsync(args).then(function (account) {
args.account = account; args.account = account;
var promise = le.store.certificates.checkKeypairAsync(args).then(function (keypair) { var promise = gl.store.certificates.checkKeypairAsync(args).then(function (keypair) {
if (keypair) { if (keypair) {
return RSA.import(keypair); return RSA.import(keypair);
} }
if (args.domainKeypair) { if (args.domainKeypair) {
return le.store.certificates.setKeypairAsync(args, RSA.import(args.domainKeypair)); return gl.store.certificates.setKeypairAsync(args, RSA.import(args.domainKeypair));
} }
var keypairOpts = { public: true, pem: true }; var keypairOpts = { public: true, pem: true };
@ -238,7 +238,7 @@ module.exports.create = function (le) {
keypair.privateKeyPem = RSA.exportPrivatePem(keypair); keypair.privateKeyPem = RSA.exportPrivatePem(keypair);
keypair.publicKeyPem = RSA.exportPublicPem(keypair); keypair.publicKeyPem = RSA.exportPublicPem(keypair);
keypair.privateKeyJwk = RSA.exportPrivateJwk(keypair); keypair.privateKeyJwk = RSA.exportPrivateJwk(keypair);
return le.store.certificates.setKeypairAsync(args, keypair); return gl.store.certificates.setKeypairAsync(args, keypair);
}); });
}); });
@ -252,7 +252,7 @@ module.exports.create = function (le) {
args._acmeUrls = urls; args._acmeUrls = urls;
var certReq = { var certReq = {
debug: args.debug || le.debug debug: args.debug || gl.debug
, newAuthzUrl: args._acmeUrls.newAuthz , newAuthzUrl: args._acmeUrls.newAuthz
, newCertUrl: args._acmeUrls.newCert , newCertUrl: args._acmeUrls.newCert
@ -277,23 +277,23 @@ module.exports.create = function (le) {
certReq.setChallenge = function (domain, key, value, done) { certReq.setChallenge = function (domain, key, value, done) {
log(args.debug, "setChallenge called for '" + domain + "'"); log(args.debug, "setChallenge called for '" + domain + "'");
var copy = utils.merge({ domains: [domain] }, args); var copy = utils.merge({ domains: [domain] }, args);
copy = utils.merge(copy, le); copy = utils.merge(copy, gl);
utils.tplCopy(copy); utils.tplCopy(copy);
// TODO need to save challengeType // TODO need to save challengeType
le.challenges[args.challengeType].set(copy, domain, key, value, done); gl.challenges[args.challengeType].set(copy, domain, key, value, done);
}; };
certReq.removeChallenge = function (domain, key, done) { certReq.removeChallenge = function (domain, key, done) {
log(args.debug, "removeChallenge called for '" + domain + "'"); log(args.debug, "removeChallenge called for '" + domain + "'");
var copy = utils.merge({ domains: [domain] }, le); var copy = utils.merge({ domains: [domain] }, gl);
utils.tplCopy(copy); utils.tplCopy(copy);
le.challenges[args.challengeType].remove(copy, domain, key, done); gl.challenges[args.challengeType].remove(copy, domain, key, done);
}; };
log(args.debug, 'calling le.acme.getCertificateAsync', certReq.domains); log(args.debug, 'calling greenlock.acme.getCertificateAsync', certReq.domains);
return le.acme.getCertificateAsync(certReq).then(utils.attachCertInfo); return gl.acme.getCertificateAsync(certReq).then(utils.attachCertInfo);
}); });
}).then(function (results) { }).then(function (results) {
// { cert, chain, privkey /*TODO, subject, altnames, issuedAt, expiresAt */ } // { cert, chain, privkey /*TODO, subject, altnames, issuedAt, expiresAt */ }
@ -301,7 +301,7 @@ module.exports.create = function (le) {
args.certs = results; args.certs = results;
// args.pems is deprecated // args.pems is deprecated
args.pems = results; args.pems = results;
return le.store.certificates.setAsync(args).then(function () { return gl.store.certificates.setAsync(args).then(function () {
return results; return results;
}); });
}); });
@ -362,14 +362,14 @@ module.exports.create = function (le) {
return false; return false;
} }
, _getRenewableAt: function (args, certs) { , _getRenewableAt: function (args, certs) {
return certs.expiresAt - (args.renewWithin || le.renewWithin); return certs.expiresAt - (args.renewWithin || gl.renewWithin);
} }
, checkAsync: function (args) { , checkAsync: function (args) {
var copy = utils.merge(args, le); var copy = utils.merge(args, gl);
utils.tplCopy(copy); utils.tplCopy(copy);
// returns pems // returns pems
return le.store.certificates.checkAsync(copy).then(function (cert) { return gl.store.certificates.checkAsync(copy).then(function (cert) {
if (cert) { if (cert) {
log(args.debug, 'checkAsync found existing certificates'); log(args.debug, 'checkAsync found existing certificates');
return utils.attachCertInfo(cert); return utils.attachCertInfo(cert);
@ -381,7 +381,7 @@ module.exports.create = function (le) {
} }
// Certificates // Certificates
, getAsync: function (args) { , getAsync: function (args) {
var copy = utils.merge(args, le); var copy = utils.merge(args, gl);
args = utils.tplCopy(copy); args = utils.tplCopy(copy);
return core.certificates.checkAsync(args).then(function (certs) { return core.certificates.checkAsync(args).then(function (certs) {

View File

@ -6,27 +6,27 @@ function _log(debug) {
if (debug) { if (debug) {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
args.shift(); args.shift();
args.unshift("[le/lib/middleware.js]"); args.unshift("[greenlock/lib/middleware.js]");
console.log.apply(console, args); console.log.apply(console, args);
} }
} }
module.exports.create = function (le) { module.exports.create = function (gl) {
if (!le.challenges['http-01'] || !le.challenges['http-01'].get) { if (!gl.challenges['http-01'] || !gl.challenges['http-01'].get) {
throw new Error("middleware requires challenge plugin with get method"); throw new Error("middleware requires challenge plugin with get method");
} }
var log = le.log || _log; var log = gl.log || _log;
log(le.debug, "created middleware"); log(gl.debug, "created middleware");
return function (_app) { return function (_app) {
if (_app && 'function' !== typeof _app) { if (_app && 'function' !== typeof _app) {
throw new Error("use le.middleware() or le.middleware(function (req, res) {})"); throw new Error("use greenlock.middleware() or greenlock.middleware(function (req, res) {})");
} }
var prefix = le.acmeChallengePrefix || '/.well-known/acme-challenge/'; var prefix = gl.acmeChallengePrefix || '/.well-known/acme-challenge/';
return function (req, res, next) { return function (req, res, next) {
if (0 !== req.url.indexOf(prefix)) { if (0 !== req.url.indexOf(prefix)) {
log(le.debug, "no match, skipping middleware"); log(gl.debug, "no match, skipping middleware");
if ('function' === typeof _app) { if ('function' === typeof _app) {
_app(req, res, next); _app(req, res, next);
} }
@ -35,24 +35,24 @@ module.exports.create = function (le) {
} }
else { else {
res.statusCode = 500; res.statusCode = 500;
res.end("[500] Developer Error: app.use('/', le.middleware()) or le.middleware(app)"); res.end("[500] Developer Error: app.use('/', greenlock.middleware()) or greenlock.middleware(app)");
} }
return; return;
} }
log(le.debug, "this must be tinder, 'cuz it's a match!"); log(gl.debug, "this must be tinder, 'cuz it's a match!");
var token = req.url.slice(prefix.length); var token = req.url.slice(prefix.length);
var hostname = req.hostname || (req.headers.host || '').toLowerCase().replace(/:.*/, ''); var hostname = req.hostname || (req.headers.host || '').toLowerCase().replace(/:.*/, '');
log(le.debug, "hostname", hostname, "token", token); log(gl.debug, "hostname", hostname, "token", token);
var copy = utils.merge({ domains: [ hostname ] }, le); var copy = utils.merge({ domains: [ hostname ] }, gl);
copy = utils.tplCopy(copy); copy = utils.tplCopy(copy);
// TODO tpl copy? // TODO tpl copy?
// TODO need to restore challengeType // TODO need to restore challengeType
le.challenges['http-01'].get(copy, hostname, token, function (err, secret) { gl.challenges['http-01'].get(copy, hostname, token, function (err, secret) {
if (err || !token) { if (err || !token) {
res.statusCode = 404; res.statusCode = 404;
res.setHeader('Content-Type', 'application/json; charset=utf-8'); res.setHeader('Content-Type', 'application/json; charset=utf-8');