greenlock-express.js/greenlock.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-10-27 09:59:49 +00:00
"use strict";
var pkg = require("./package.json");
module.exports.create = function(opts) {
var Greenlock = require("@root/greenlock");
var packageAgent = pkg.name + "/" + pkg.version;
if ("string" === typeof opts.packageAgent) {
opts.packageAgent += " ";
} else {
opts.packageAgent = "";
}
opts.packageAgent += packageAgent;
var greenlock = Greenlock.create(opts);
greenlock.getAcmeHttp01ChallengeResponse = function(opts) {
return greenlock.find({ servername: opts.servername }).then(function(sites) {
if (!sites.length) {
return null;
}
var site = sites[0];
if (!site.challenges || !site.challenges["http-01"]) {
return null;
}
var plugin;
try {
plugin = require(site.challenges["http-01"].module);
plugin = plugin.create(site.challenges["http-01"]);
} catch (e) {
console.error("error getting acme http-01 plugin");
console.error(e);
return null;
}
return plugin.get(opts).then(function(result) {
// TODO is this the right way?
var ch = (result && result.challenge) || result || {};
return {
keyAuthorization: ch.keyAuthorization
};
});
});
};
return greenlock;
};