most basic middleware function

This commit is contained in:
AJ ONeal 2016-08-05 18:11:19 -04:00
parent 9ae3c7d6f6
commit 5ff91ec37e
1 changed files with 31 additions and 0 deletions

31
lib/middleware.js Normal file
View File

@ -0,0 +1,31 @@
'use strict';
module.exports = function (le) {
return function () {
var prefix = le.acmeChallengePrefix; // /.well-known/acme-challenge/:token
return function (req, res, next) {
if (0 !== req.url.indexOf(prefix)) {
next();
return;
}
var key = req.url.slice(prefix.length);
var hostname = req.hostname || (req.headers.host || '').toLowerCase().replace(/:*/, '');
// TODO tpl copy?
le.challenger.getAsync(le, hostname, key).then(function (token) {
if (!token) {
res.status = 404;
res.send("Error: These aren't the tokens you're looking for. Move along.");
return;
}
res.send(token);
}, function (/*err*/) {
res.status = 404;
res.send("Error: These aren't the tokens you're looking for. Move along.");
});
};
};
};