From c48de554c2d83e23d648dce8da86a5e4a553f3de Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 15 Dec 2015 15:40:44 +0000 Subject: [PATCH] Certificate registration completes successfully! :-) --- backends/ursa.js | 5 +++++ index.js | 32 ++++++++++++++++++++++++++++---- lib/default-handlers.js | 3 ++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/backends/ursa.js b/backends/ursa.js index 1e692e3..53874d8 100644 --- a/backends/ursa.js +++ b/backends/ursa.js @@ -184,6 +184,11 @@ function getCertificateAsync(account, args, defaults, handlers) { domains: args.domains , accountPrivateKeyPem: account.privateKeyPem , domainPrivateKeyPem: domain.privateKeyPem + , getChallenge: function (domain, key, done) { + args.domains = [domain]; + args.webrootPath = args.webrootPath || defaults.webrootPath; + handlers.getChallenge(args, key, done); + } , setChallenge: function (domain, key, value, done) { args.domains = [domain]; args.webrootPath = args.webrootPath || defaults.webrootPath; diff --git a/index.js b/index.js index 641ca1d..dc0b80b 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,22 @@ LE.create = function (backend, defaults, handlers) { cb(null, null); }; } + if (!handlers.getChallenge) { + if (!defaults.webrootPath) { + // GET /.well-known/acme-challenge/{{challengeKey}} should return {{tokenValue}} + throw new Error("handlers.getChallenge or defaults.webrootPath must be set"); + } + handlers.getChallenge = function (hostname, key, done) { + // TODO associate by hostname? + // hmm... I don't think there's a direct way to associate this with + // the request it came from... it's kinda stateless in that way + // but realistically there only needs to be one handler and one + // "directory" for this. It's not that big of a deal. + var defaultos = LE.merge(defaults, {}); + defaultos.domains = [hostname]; + require('./lib/default-handlers').getChallenge(defaultos, key, done); + }; + } if (!handlers.setChallenge) { if (!defaults.webrootPath) { // GET /.well-known/acme-challenge/{{challengeKey}} should return {{tokenValue}} @@ -49,7 +65,7 @@ LE.create = function (backend, defaults, handlers) { if (!handlers.removeChallenge) { if (!defaults.webrootPath) { // GET /.well-known/acme-challenge/{{challengeKey}} should return {{tokenValue}} - throw new Error("handlers.setChallenge or defaults.webrootPath must be set"); + throw new Error("handlers.removeChallenge or defaults.webrootPath must be set"); } handlers.removeChallenge = require('./lib/default-handlers').removeChallenge; } @@ -143,17 +159,25 @@ LE.create = function (backend, defaults, handlers) { cb(null, true); } , middleware: function () { - //console.log('[DEBUG] webrootPath', defaults.webrootPath); - var serveStatic = require('serve-static')(defaults.webrootPath, { dotfiles: 'allow' }); var prefix = '/.well-known/acme-challenge/'; return function (req, res, next) { if (0 !== req.url.indexOf(prefix)) { + console.log('[LE middleware]: pass'); next(); return; } - serveStatic(req, res, next); + //args.domains = [req.hostname]; + console.log('[LE middleware]:', req.hostname, req.url, req.url.slice(prefix.length)); + handlers.getChallenge(req.hostname, req.url.slice(prefix.length), function (err, token) { + if (err) { + res.send("Error: These aren't the tokens you're looking for. Move along."); + return; + } + + res.send(token); + }); }; } , SNICallback: sniCallback diff --git a/lib/default-handlers.js b/lib/default-handlers.js index 6c35e46..a4bcacc 100644 --- a/lib/default-handlers.js +++ b/lib/default-handlers.js @@ -29,7 +29,8 @@ module.exports.setChallenge = function (args, challengePath, keyAuthorization, d module.exports.getChallenge = function (args, key, done) { //var hostname = args.domains[0]; - fs.readFile(path.join(args.webroot, key), 'utf8', done); + console.log("getting the challenge", args, key); + fs.readFile(path.join(args.webrootPath, key), 'utf8', done); }; module.exports.removeChallenge = function (args, key, done) {