From 5ff91ec37e3d6e8dbf12b3a19a90be4569337c00 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 5 Aug 2016 18:11:19 -0400 Subject: [PATCH] most basic middleware function --- lib/middleware.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/middleware.js diff --git a/lib/middleware.js b/lib/middleware.js new file mode 100644 index 0000000..13af19c --- /dev/null +++ b/lib/middleware.js @@ -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."); + }); + }; + }; +};