le-challenge-webroot.js/index.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-08-05 07:54:57 +00:00
'use strict';
var fs = require('fs');
var path = require('path');
2016-08-05 08:10:33 +00:00
var defaults = {
2016-08-09 16:57:41 +00:00
webrootPath: [ '~', 'letsencrypt', 'var', 'lib' ].join(path.sep)
2016-08-05 08:10:33 +00:00
, debug: false
2016-08-05 07:54:57 +00:00
};
2016-08-09 16:57:41 +00:00
var Challenge = module.exports;
2016-08-05 08:10:33 +00:00
2016-08-09 16:57:41 +00:00
Challenge.create = function (options) {
2016-08-05 08:10:33 +00:00
var results = {};
2016-08-09 16:57:41 +00:00
Object.keys(Challenge).forEach(function (key) {
results[key] = Challenge[key];
2016-08-05 08:10:33 +00:00
});
2016-08-09 16:57:41 +00:00
results.create = undefined;
2016-08-05 08:10:33 +00:00
Object.keys(defaults).forEach(function (key) {
if (!(key in options)) {
options[key] = defaults[key];
}
});
2016-08-09 16:57:41 +00:00
results._options = options;
2016-08-05 08:10:33 +00:00
results.getOptions = function () {
return results._options;
};
return results;
};
2016-08-09 17:05:36 +00:00
//
// NOTE: the "args" here in `set()` are NOT accessible to `get()` and `remove()`
// They are provided so that you can store them in an implementation-specific way
// if you need access to them.
//
Challenge.set = function (args, domain, challengePath, keyAuthorization, done) {
2016-08-05 07:54:57 +00:00
var mkdirp = require('mkdirp');
2016-08-09 17:05:36 +00:00
mkdirp(args.webrootPath, function (err) {
2016-08-05 07:54:57 +00:00
if (err) {
done(err);
return;
}
2016-08-09 17:05:36 +00:00
fs.writeFile(path.join(args.webrootPath, challengePath), keyAuthorization, 'utf8', function (err) {
2016-08-05 07:54:57 +00:00
done(err);
});
});
};
2016-08-09 17:05:36 +00:00
//
// NOTE: the "defaults" here are still merged and templated, just like "args" would be,
// but if you specifically need "args" you must retrieve them from some storage mechanism
// based on domain and key
//
2016-08-09 16:57:41 +00:00
Challenge.get = function (defaults, domain, key, done) {
fs.readFile(path.join(defaults.webrootPath, key), 'utf8', done);
2016-08-05 07:54:57 +00:00
};
2016-08-09 16:57:41 +00:00
Challenge.remove = function (defaults, domain, key, done) {
fs.unlink(path.join(defaults.webrootPath, key), done);
2016-08-05 07:54:57 +00:00
};