greenlock-express.js/lib/challenge-handlers.js

62 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2015-12-17 00:51:37 +00:00
'use strict';
var fs = require('fs');
var path = require('path');
2015-12-17 05:08:14 +00:00
var mkdirp = require('mkdirp');
2015-12-17 00:51:37 +00:00
// TODO handle templating :hostname in letsencrypt proper
// Note: we're explicitly doing this on the filesystem
// rather than in-memory to support node cluster
module.exports = {
set: function setChallenge(args, hostname, key, value, cb) {
2015-12-17 08:44:55 +00:00
var webrootPath = args.webrootPath;
2015-12-17 05:08:14 +00:00
var keyfile = path.join(webrootPath, key);
if (args.debug) {
2015-12-17 08:44:55 +00:00
console.debug('[LEX] write file', hostname, webrootPath);
console.debug('challenge:', key);
console.debug('response:', value);
2015-12-17 05:08:14 +00:00
}
fs.writeFile(keyfile, value, 'utf8', function (err) {
if (!err) { cb(null); return; }
if (args.debug) {
2015-12-17 08:44:55 +00:00
console.debug('[LEX] mkdirp', webrootPath);
2015-12-17 05:08:14 +00:00
}
2016-02-12 09:51:17 +00:00
mkdirp(webrootPath, function (err) {
2015-12-17 05:08:14 +00:00
if (err) { cb(err); return; }
2015-12-17 00:51:37 +00:00
2015-12-17 05:08:14 +00:00
fs.writeFile(keyfile, value, 'utf8', cb);
});
});
2015-12-17 00:51:37 +00:00
}
, get: function getChallenge(args, hostname, key, cb) {
2015-12-17 08:44:55 +00:00
var keyfile = path.join(args.webrootPath, key);
2015-12-17 00:51:37 +00:00
2015-12-17 05:08:14 +00:00
if (args.debug) {
2015-12-17 08:44:55 +00:00
console.debug('[LEX] getChallenge', keyfile, hostname, key);
2015-12-17 05:08:14 +00:00
}
fs.readFile(keyfile, 'utf8', function (err, text) {
cb(null, text);
});
2015-12-17 00:51:37 +00:00
}
, remove: function removeChallenge(args, hostname, key, cb) {
2015-12-17 08:44:55 +00:00
var keyfile = path.join(args.webrootPath, key);
2015-12-17 00:51:37 +00:00
// Note: it's not actually terribly important that we wait for the unlink callback
// but it's a polite thing to do - and we're polite people!
2015-12-17 05:08:14 +00:00
if (args.debug) {
2015-12-17 08:44:55 +00:00
console.debug('[LEX] removeChallenge', keyfile, hostname, key);
2015-12-17 05:08:14 +00:00
}
fs.unlink(keyfile, function (err) {
if (err) { console.warn(err.stack); }
cb(null);
});
2015-12-17 00:51:37 +00:00
}
};