greenlock.js/lib/middleware.js

95 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2016-08-05 22:11:19 +00:00
'use strict';
2016-08-09 18:05:47 +00:00
var utils = require('./utils');
function _log(debug) {
2016-08-09 18:05:47 +00:00
if (debug) {
var args = Array.prototype.slice.call(arguments);
args.shift();
2018-05-15 22:01:09 +00:00
args.unshift("[greenlock/lib/middleware.js]");
2016-08-09 18:05:47 +00:00
console.log.apply(console, args);
}
}
2018-05-15 22:01:09 +00:00
module.exports.create = function (gl) {
if (!gl.challenges['http-01'] || !gl.challenges['http-01'].get) {
2016-08-09 18:05:47 +00:00
throw new Error("middleware requires challenge plugin with get method");
}
2018-05-15 22:01:09 +00:00
var log = gl.log || _log;
2016-08-09 18:05:47 +00:00
2018-05-15 22:01:09 +00:00
log(gl.debug, "created middleware");
2016-08-11 04:23:31 +00:00
return function (_app) {
if (_app && 'function' !== typeof _app) {
2018-05-15 22:01:09 +00:00
throw new Error("use greenlock.middleware() or greenlock.middleware(function (req, res) {})");
2016-08-11 04:23:31 +00:00
}
2018-05-15 22:01:09 +00:00
var prefix = gl.acmeChallengePrefix || '/.well-known/acme-challenge/';
2016-08-05 22:11:19 +00:00
return function (req, res, next) {
if (0 !== req.url.indexOf(prefix)) {
2018-05-15 22:01:09 +00:00
log(gl.debug, "no match, skipping middleware");
2016-08-16 00:42:24 +00:00
if ('function' === typeof _app) {
2016-08-11 04:23:31 +00:00
_app(req, res, next);
}
2016-08-16 00:42:24 +00:00
else if ('function' === typeof next) {
2016-08-11 04:23:31 +00:00
next();
}
2016-08-16 00:42:24 +00:00
else {
res.statusCode = 500;
2018-05-15 22:01:09 +00:00
res.end("[500] Developer Error: app.use('/', greenlock.middleware()) or greenlock.middleware(app)");
2016-08-16 00:42:24 +00:00
}
2016-08-05 22:11:19 +00:00
return;
}
2018-05-15 22:01:09 +00:00
log(gl.debug, "this must be tinder, 'cuz it's a match!");
2016-08-09 18:05:47 +00:00
var token = req.url.slice(prefix.length);
var hostname = req.hostname || (req.headers.host || '').toLowerCase().replace(/:.*/, '');
2016-08-05 22:11:19 +00:00
2018-05-15 22:01:09 +00:00
log(gl.debug, "hostname", hostname, "token", token);
2016-08-09 18:05:47 +00:00
2018-05-15 22:01:09 +00:00
var copy = utils.merge({ domains: [ hostname ] }, gl);
2016-08-09 18:05:47 +00:00
copy = utils.tplCopy(copy);
2019-04-06 01:45:24 +00:00
copy.challenge = {};
copy.challenge.type = 'http-01'; // obviously...
copy.challenge.identifier = { type: 'dns', value: hostname };
copy.challenge.wildcard = false;
copy.challenge.token = token;
copy.challenge.altname = hostname;
2016-08-09 18:05:47 +00:00
2019-04-06 01:45:24 +00:00
function cb(opts) {
var secret = opts.keyAuthorization || opts;
if (secret && 'string' === typeof secret) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end(secret);
2016-08-05 22:11:19 +00:00
return;
}
2019-04-06 01:45:24 +00:00
eb(new Error("couldn't retrieve keyAuthorization"));
return;
}
function eb(/*err*/) {
res.statusCode = 404;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end('{ "error": { "message": "Error: These aren\'t the tokens you\'re looking for. Move along." } }');
return;
}
function mb(err, result) {
if (err) { eb(err); return; }
cb(result);
}
var challenger = gl.challenges['http-01'].get;
if (1 === challenger.length) {
/*global Promise*/
return Promise.resolve().then(function () {
return gl.challenges['http-01'].get(copy);
}).then(cb).catch(eb);
} else if (2 === challenger.length) {
gl.challenges['http-01'].get(copy, mb);
} else {
gl.challenges['http-01'].get(copy, hostname, token, mb);
}
2016-08-05 22:11:19 +00:00
};
};
};