greenlock-challenge-manual.js/index.js

130 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-08-10 03:20:19 +00:00
'use strict';
2019-04-06 05:11:00 +00:00
/*global Promise*/
2016-08-10 03:20:19 +00:00
var Challenge = module.exports;
2019-04-06 05:11:00 +00:00
Challenge.create = function (config) {
// If your implementation needs config options, set them. Otherwise, don't bother (duh).
var http01 = require('le-challenge-http').create(config);
var dns01 = require('le-challenge-dns').create(config);
2019-04-06 05:11:00 +00:00
var challenger = {};
// Note: normally you'd implement these right here, but for the sake of
// documentation I've abstracted them out "Table of Contents"-style.
// call out to set the challenge, wherever
challenger.set = function (opts, cb) {
// Note: this can be defined as a thunk (like this) or a Promise
2019-04-06 05:11:00 +00:00
var ch = opts.challenge;
if ('http-01' === ch.type) {
return http01.set(opts, cb);
2019-04-06 05:11:00 +00:00
} else if ('dns-01' === ch.type) {
return dns01.set(opts, cb);
2019-04-06 05:11:00 +00:00
} else {
return Challenge._setAny(opts, cb);
}
};
// call out to remove the challenge, wherever
challenger.remove = function (opts) {
// Note: this can be defined synchronously (like this) or as a Promise, or a thunk
2019-04-06 05:11:00 +00:00
var ch = opts.challenge;
if ('http-01' === ch.type) {
return http01.remove(opts);
2019-04-06 05:11:00 +00:00
} else if ('dns-01' === ch.type) {
return dns01.remove(opts);
2019-04-06 05:11:00 +00:00
} else {
return Challenge._removeAny(opts);
}
};
// only really useful for http
// (and tls-alpn-01, which isn't implemented yet)
2019-04-06 05:11:00 +00:00
challenger.get = function (opts) {
// Note: this can be defined as a Promise (like this) or synchronously, or a thunk
2019-04-06 05:11:00 +00:00
var ch = opts.challenge;
if ('http-01' === ch.type) {
return http01.get(opts);
2019-04-06 05:11:00 +00:00
} else if ('dns-01' === ch.type) {
return dns01.get(opts);
2019-04-06 05:11:00 +00:00
} else {
2019-04-06 06:19:12 +00:00
return Challenge._get(opts);
2016-08-10 03:20:19 +00:00
}
};
2019-04-06 05:11:00 +00:00
// Whatever you set to 'options' will be merged into 'opts' just before each call
// (for convenience, so you don't have to merge it yourself).
challenger.options = { debug: config.debug };
return challenger;
2016-08-10 03:20:19 +00:00
};
2019-04-06 05:11:00 +00:00
Challenge._setAny = function (args, cb) {
var ch = args.challenge;
console.info("[ACME " + ch.type + " '" + ch.altname + "' CHALLENGE]");
console.info("Your mission (since you chose to accept it):");
console.info("You must, by whatever means necessary, use the following information"
+ " to make a device or service ready to respond to a '" + ch.type + "' request.");
2019-04-06 05:11:00 +00:00
console.info("");
console.info(JSON.stringify(ch, null, 2).replace(/^/gm, '\t'));
console.info("");
console.info("Press the any key once the response is ready to continue with the '" + ch.type + "' challenge process");
2019-04-06 05:11:00 +00:00
console.info("[Press the ANY key to continue...]");
process.stdin.resume();
2019-04-06 06:19:12 +00:00
process.stdin.once('data', function () {
2019-04-06 05:11:00 +00:00
process.stdin.pause();
2019-04-06 06:19:12 +00:00
cb(null, null);
2019-04-06 05:11:00 +00:00
});
2016-08-10 03:20:19 +00:00
};
2019-04-06 05:11:00 +00:00
Challenge._removeAny = function (args) {
var ch = args.challenge;
console.info("");
console.info("[ACME " + ch.type + " '" + ch.altname + "' COMPLETE]: " + ch.status);
console.info("You may now undo whatever you did to create and ready the response.");
2019-04-06 05:11:00 +00:00
console.info("");
return null;
};
// This can be used for http-01 and tls-alpn-01 (when it's available), but not dns-01.
// And not all http-01 or tls-alpn-01 strategies will need to implement this.
2019-04-06 06:19:12 +00:00
Challenge._get = function (args) {
var ch = args.challenge;
if (!Challenge._getCache[ch.altname + ':' + ch.token]) {
Challenge._getCache[ch.altname + ':' + ch.token] = true;
console.info("");
console.info("[ACME " + ch.type + " '" + ch.altname + "' REQUEST]: " + ch.status);
console.info("The '" + ch.type + "' challenge request has arrived!");
console.info("It's now time to painstakingly type out the expected response object with your bear hands.");
console.log("Yes. Your bear hands.");
console.log('ex: { "keyAuthorization": "xxxxxxxx.yyyyyyyy" }');
2019-04-06 06:19:12 +00:00
process.stdout.write("> ");
}
2019-04-06 05:11:00 +00:00
return new Promise(function (resolve, reject) {
process.stdin.resume();
2019-04-06 06:19:12 +00:00
process.stdin.once('error', reject);
process.stdin.once('data', function (chunk) {
2019-04-06 05:11:00 +00:00
process.stdin.pause();
var result = chunk.toString();
try {
result = JSON.parse(result);
} catch(e) {
2019-04-06 06:19:12 +00:00
args.challenge.keyAuthorization = result;
result = args.challenge;
2019-04-06 05:11:00 +00:00
}
2019-04-06 06:19:12 +00:00
resolve(result);
2019-04-06 05:11:00 +00:00
});
});
2016-08-10 03:20:19 +00:00
};
2019-04-06 06:19:12 +00:00
// Because the ACME server will hammer us with requests, and that's confusing during a manual test:
Challenge._getCache = {};