2016-08-05 22:11:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-08-09 18:05:47 +00:00
|
|
|
var utils = require('./utils');
|
|
|
|
|
2016-08-25 20:04:36 +00:00
|
|
|
function _log(debug) {
|
2016-08-09 18:05:47 +00:00
|
|
|
if (debug) {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.shift();
|
|
|
|
args.unshift("[le/lib/middleware.js]");
|
|
|
|
console.log.apply(console, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.create = function (le) {
|
|
|
|
if (!le.challenge || !le.challenge.get) {
|
|
|
|
throw new Error("middleware requires challenge plugin with get method");
|
|
|
|
}
|
2016-08-25 20:04:36 +00:00
|
|
|
var log = le.log || _log;
|
2016-08-09 18:05:47 +00:00
|
|
|
|
|
|
|
log(le.debug, "created middleware");
|
2016-08-11 04:23:31 +00:00
|
|
|
return function (_app) {
|
|
|
|
if (_app && 'function' !== typeof _app) {
|
|
|
|
throw new Error("use le.middleware() or le.middleware(function (req, res) {})");
|
|
|
|
}
|
2016-08-11 04:14:39 +00:00
|
|
|
var prefix = le.acmeChallengePrefix || '/.well-known/acme-challenge/';
|
2016-08-05 22:11:19 +00:00
|
|
|
|
|
|
|
return function (req, res, next) {
|
|
|
|
if (0 !== req.url.indexOf(prefix)) {
|
2016-08-09 18:05:47 +00:00
|
|
|
log(le.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;
|
|
|
|
res.end("[500] Developer Error: app.use('/', le.middleware()) or le.middleware(app)");
|
|
|
|
}
|
2016-08-05 22:11:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-09 18:05:47 +00:00
|
|
|
log(le.debug, "this must be tinder, 'cuz it's a match!");
|
|
|
|
|
|
|
|
var token = req.url.slice(prefix.length);
|
2016-08-09 18:17:26 +00:00
|
|
|
var hostname = req.hostname || (req.headers.host || '').toLowerCase().replace(/:.*/, '');
|
2016-08-05 22:11:19 +00:00
|
|
|
|
2016-08-09 18:05:47 +00:00
|
|
|
log(le.debug, "hostname", hostname, "token", token);
|
|
|
|
|
2016-08-09 18:17:26 +00:00
|
|
|
var copy = utils.merge({ domains: [ hostname ] }, le);
|
2016-08-09 18:05:47 +00:00
|
|
|
copy = utils.tplCopy(copy);
|
|
|
|
|
2016-08-05 22:11:19 +00:00
|
|
|
// TODO tpl copy?
|
2016-08-09 18:05:47 +00:00
|
|
|
le.challenge.get(copy, hostname, token, function (err, secret) {
|
|
|
|
if (err || !token) {
|
|
|
|
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." } }');
|
2016-08-05 22:11:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-09 18:05:47 +00:00
|
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
|
|
|
res.end(secret);
|
2016-08-05 22:11:19 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|