2019-07-23 05:31:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-31 05:37:03 +00:00
|
|
|
//var request;
|
|
|
|
var promisify = require('util').promisify;
|
|
|
|
var os = require('os');
|
|
|
|
var fs = require('fs');
|
|
|
|
var writeFile = promisify(fs.writeFile);
|
|
|
|
var readFile = promisify(fs.readFile);
|
|
|
|
var unlink = promisify(fs.unlink);
|
|
|
|
var mkdirp = promisify(require('@root/mkdirp'));
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var defaults = {
|
|
|
|
webroot: path.join(require('os').tmpdir(), 'acme-challenge')
|
|
|
|
};
|
2019-07-23 05:31:48 +00:00
|
|
|
|
|
|
|
module.exports.create = function(config) {
|
2019-07-31 05:37:03 +00:00
|
|
|
var webroot = config.webroot || config.webrootPath || defaults.webroot;
|
|
|
|
|
|
|
|
function tpl(str, ch) {
|
|
|
|
return str
|
|
|
|
.replace(/\s*{+\s*domain\s*}+\s*/gi, ch.identifier.value)
|
|
|
|
.replace(/^~/, os.homedir());
|
|
|
|
}
|
|
|
|
|
2019-07-23 05:31:48 +00:00
|
|
|
return {
|
2019-07-31 05:37:03 +00:00
|
|
|
// exposed to make testable
|
|
|
|
_tpl: tpl,
|
|
|
|
|
2019-07-23 05:31:48 +00:00
|
|
|
init: function(opts) {
|
2019-07-31 05:37:03 +00:00
|
|
|
//request = opts.request;
|
2019-07-23 05:31:48 +00:00
|
|
|
return null;
|
|
|
|
},
|
2019-07-31 03:46:43 +00:00
|
|
|
|
2019-07-23 05:31:48 +00:00
|
|
|
set: function(data) {
|
2019-07-31 05:37:03 +00:00
|
|
|
// console.log('Add Key Auth URL', data);
|
|
|
|
|
2019-07-24 04:47:36 +00:00
|
|
|
var ch = data.challenge;
|
2019-07-31 05:37:03 +00:00
|
|
|
var pathname = tpl(webroot, ch);
|
|
|
|
|
|
|
|
return mkdirp(pathname)
|
|
|
|
.then(function() {
|
|
|
|
return writeFile(
|
|
|
|
path.join(pathname, ch.token),
|
|
|
|
ch.keyAuthorization
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
return null;
|
|
|
|
});
|
2019-07-23 05:31:48 +00:00
|
|
|
},
|
2019-07-31 03:46:43 +00:00
|
|
|
|
2019-07-31 05:37:03 +00:00
|
|
|
get: function(data) {
|
|
|
|
// console.log('List Key Auth URL', data);
|
|
|
|
|
2019-07-31 03:46:43 +00:00
|
|
|
var ch = data.challenge;
|
2019-07-31 05:37:03 +00:00
|
|
|
var pathname = tpl(webroot, ch);
|
|
|
|
|
|
|
|
return readFile(path.join(pathname, ch.token), 'utf8')
|
|
|
|
.then(function(keyAuth) {
|
|
|
|
return { keyAuthorization: keyAuth };
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if ('ENOENT' !== err.code) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2019-07-23 05:31:48 +00:00
|
|
|
},
|
2019-07-31 03:46:43 +00:00
|
|
|
|
2019-07-31 05:37:03 +00:00
|
|
|
remove: function(data) {
|
|
|
|
// console.log('Remove Key Auth URL', data);
|
|
|
|
|
2019-07-31 03:46:43 +00:00
|
|
|
var ch = data.challenge;
|
2019-07-31 05:37:03 +00:00
|
|
|
var pathname = tpl(webroot, ch);
|
|
|
|
|
|
|
|
return unlink(path.join(pathname, ch.token)).then(function() {
|
|
|
|
return null;
|
|
|
|
});
|
2019-07-23 05:31:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|