greenlock-express.js/greenlock.js

120 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-10-27 09:59:49 +00:00
"use strict";
module.exports.create = function(opts) {
2019-10-28 07:06:43 +00:00
opts = parsePackage(opts);
opts.packageAgent = addGreenlockAgent(opts);
2019-10-27 09:59:49 +00:00
2019-10-28 07:06:43 +00:00
var Greenlock = require("@root/greenlock");
2019-10-27 09:59:49 +00:00
var greenlock = Greenlock.create(opts);
2019-10-28 07:06:43 +00:00
// TODO move to greenlock proper
2019-10-27 09:59:49 +00:00
greenlock.getAcmeHttp01ChallengeResponse = function(opts) {
// TODO some sort of caching to prevent database hits?
return greenlock
._config({ servername: opts.servername })
.then(function(site) {
if (!site) {
return null;
}
2019-10-27 09:59:49 +00:00
// Hmm... this _should_ be impossible
if (!site.challenges || !site.challenges["http-01"]) {
return null;
}
2019-10-27 09:59:49 +00:00
return Greenlock._loadChallenge(site.challenges, "http-01");
})
.then(function(plugin) {
return plugin
.get({
challenge: {
type: opts.type,
//hostname: opts.servername,
altname: opts.servername,
identifier: { value: opts.servername },
token: opts.token
}
})
.then(function(result) {
var keyAuth;
if (result) {
// backwards compat that shouldn't be dropped
// because new v3 modules had to do this to be
// backwards compatible with Greenlock v2.7 at
// the time.
if (result.challenge) {
result = challenge;
}
keyAuth = result.keyAuthorization;
}
return {
keyAuthorization: keyAuth
};
});
2019-10-27 09:59:49 +00:00
});
};
return greenlock;
};
2019-10-28 07:06:43 +00:00
function addGreenlockAgent(opts) {
// Add greenlock as part of Agent, unless this is greenlock
2019-10-29 17:21:36 +00:00
var packageAgent = opts.packageAgent || "";
if (!/greenlock(-express|-pro)?/i.test(packageAgent)) {
2019-10-28 07:06:43 +00:00
var pkg = require("./package.json");
2019-10-29 17:21:36 +00:00
packageAgent += " Greenlock_Express/" + pkg.version;
2019-10-28 07:06:43 +00:00
}
2019-10-29 17:21:36 +00:00
return packageAgent.trim();
2019-10-28 07:06:43 +00:00
}
// ex: "John Doe <john@example.com> (https://john.doe)"
// ex: "John Doe <john@example.com>"
// ex: "<john@example.com>"
// ex: "john@example.com"
var looseEmailRe = /(^|[\s<])([^'" <>:;`]+@[^'" <>:;`]+\.[^'" <>:;`]+)/;
2019-10-28 07:06:43 +00:00
function parsePackage(opts) {
// 'package' is sometimes a reserved word
var pkg = opts.package || opts.pkg;
if (!pkg) {
opts.maintainerEmail = parseMaintainer(opts.maintainerEmail);
2019-10-28 07:06:43 +00:00
return opts;
}
if (!opts.packageAgent) {
var err = "missing `package.THING`, which is used for the ACME client user agent string";
if (!pkg.name) {
throw new Error(err.replace("THING", "name"));
}
if (!pkg.version) {
throw new Error(err.replace("THING", "version"));
}
opts.packageAgent = pkg.name + "/" + pkg.version;
}
if (!opts.maintainerEmail) {
try {
opts.maintainerEmail = pkg.author.email || pkg.author.match(looseEmailRe)[2];
2019-10-28 07:06:43 +00:00
} catch (e) {}
}
if (!opts.maintainerEmail) {
throw new Error("missing or malformed `package.author`, which is used as the contact for support notices");
}
opts.package = undefined;
opts.maintainerEmail = parseMaintainer(opts.maintainerEmail);
2019-10-28 07:06:43 +00:00
return opts;
}
function parseMaintainer(maintainerEmail) {
try {
maintainerEmail = maintainerEmail.match(looseEmailRe)[2];
} catch (e) {
maintainerEmail = null;
}
if (!maintainerEmail) {
throw new Error("missing or malformed `maintainerEmail`, which is used as the contact for support notices");
}
return maintainerEmail;
}