greenlock-cli.js/lib/webroot.js

62 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2015-12-16 12:42:04 +00:00
'use strict';
2015-12-16 12:51:14 +00:00
module.exports.create = function (defaults) {
2015-12-16 12:42:04 +00:00
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
2015-12-16 12:51:14 +00:00
var handlers = {
2015-12-16 12:42:04 +00:00
//
// set,get,remove challenges
//
2016-08-10 00:04:08 +00:00
getOptions: function () {
2016-08-10 02:39:39 +00:00
return defaults;
2016-08-10 00:04:08 +00:00
}
, set: function (args, domain, token, secret, cb) {
var challengePath = path.join(args.webrootPath || defaults.webrootPath, '.well-known', 'acme-challenge');
2015-12-19 20:50:02 +00:00
mkdirp(challengePath, function (err) {
2015-12-16 12:42:04 +00:00
if (err) {
2015-12-19 20:50:02 +00:00
console.error("Could not create --webroot-path '" + challengePath + "':", err.code);
2015-12-16 12:42:04 +00:00
console.error("Try checking the permissions, maybe?");
cb(err);
return;
}
2016-08-10 00:04:08 +00:00
var tokenfile = path.join(challengePath, token);
2015-12-16 12:42:04 +00:00
2016-08-10 00:04:08 +00:00
fs.writeFile(tokenfile, secret, 'utf8', function (err) {
2015-12-16 12:42:04 +00:00
if (err) {
2016-08-10 00:04:08 +00:00
console.error("Could not write '" + tokenfile + "':", err.code);
2015-12-16 12:42:04 +00:00
cb(err);
return;
}
cb(null);
});
});
}
2016-08-10 00:04:08 +00:00
// handled as file read by web server
, get: function (args, domain, token, cb) {
2018-05-23 09:31:07 +00:00
cb(new Error("get not implemented (on purpose) in gl-cli/lib/webroot.js"));
2016-08-10 00:04:08 +00:00
}
, remove: function (args, domain, token, cb) {
var tokenfile = path.join(args.webrootPath || defaults.webrootPath, '.well-known', 'acme-challenge', token);
fs.unlink(tokenfile, function (err) {
2015-12-16 12:42:04 +00:00
if (err) {
2016-08-10 00:04:08 +00:00
console.error("Could not unlink '" + tokenfile + "':", err.code);
2015-12-16 12:42:04 +00:00
cb(err);
return;
}
cb(null);
});
}
};
2015-12-16 12:51:14 +00:00
return handlers;
2015-12-16 12:42:04 +00:00
};