Improve test.js.

Use request instead of http so it works with either HTTP or HTTPS URLs.
Write DER certificate rather than PEM certificate. I was getting some bytewise
mismatches when checking the output PEM against a downloaded DER.
This commit is contained in:
Jacob Hoffman-Andrews 2015-04-24 12:40:37 -07:00
parent 3814c95594
commit 2edb869086
2 changed files with 87 additions and 125 deletions

View File

@ -6,6 +6,7 @@
"dependencies": {
"cli": "^0.6.5",
"inquirer": "^0.8.2",
"node-forge": "^0.6.21"
"node-forge": "^0.6.21",
"request": "^2.55.0"
}
}

View File

@ -9,6 +9,7 @@ var inquirer = require("inquirer");
var cli = require("cli");
var http = require('http');
var fs = require('fs');
var request = require('request');
var url = require('url');
var util = require("./acme-util");
var crypto = require("./crypto-util");
@ -32,7 +33,7 @@ var questions = {
type: "confirm",
name: "terms",
message: "Do you agree to these terms?",
default: false,
default: true,
}],
domain: [{
@ -64,7 +65,7 @@ var questions = {
type: "input",
name: "certFile",
message: "Name for certificate file",
default: "cert.pem"
default: "cert.der"
}],
};
@ -72,7 +73,7 @@ var state = {
keyPairBits: 512,
keyPair: null,
newRegistrationURL: "http://localhost:4000/acme/new-reg",
newRegistrationURL: "https://www.letsencrypt-demo.org/acme/new-reg",
registrationURL: "",
termsRequired: false,
@ -169,14 +170,12 @@ function register(answers) {
var jws = crypto.generateSignature(state.keyPair, new Buffer(registerMessage));
var payload = JSON.stringify(jws);
var options = url.parse(state.newRegistrationURL);
options.method = "POST";
var req = http.request(options, getTerms);
var req = request.post(state.newRegistrationURL, {}, getTerms);
req.write(payload)
req.end();
}
function getTerms(resp) {
function getTerms(err, resp) {
if (Math.floor(resp.statusCode / 100) != 2) {
// Non-2XX response
console.log("Registration request failed with code " + resp.statusCode);
@ -196,20 +195,13 @@ function getTerms(resp) {
if (state.termsRequired) {
state.termsURL = links["terms-of-service"];
console.log(state.termsURL);
http.get(state.termsURL, getAgreement)
request.get(state.termsURL, getAgreement)
} else {
inquirer.prompt(questions.domain, getChallenges);
}
}
function getAgreement(resp) {
var body = "";
resp.on("data", function(chunk) {
body += chunk;
});
resp.on("end", function(chunk) {
if (chunk) { body += chunk; }
function getAgreement(err, resp, body) {
// TODO: Check content-type
console.log("The CA requires your agreement to terms (not supported).");
console.log();
@ -217,7 +209,6 @@ function getAgreement(resp) {
console.log();
inquirer.prompt(questions.terms, sendAgreement);
});
}
function sendAgreement(answers) {
@ -234,12 +225,7 @@ function sendAgreement(answers) {
var payload = JSON.stringify(jws);
console.log("Posting agreement to: " + state.registrationURL)
var options = url.parse(state.registrationURL);
options.method = "POST";
var req = http.request(options, function(resp) {
var body = "";
resp.on("data", function(chunk) { body += chunk; });
resp.on("end", function() {
var req = request(state.registrationURL, {}, function(err, resp, body) {
if (Math.floor(resp.statusCode / 100) != 2) {
// Non-2XX response
console.log("Couldn't POST agreement back to server, aborting.");
@ -247,7 +233,6 @@ function sendAgreement(answers) {
console.log(body);
process.exit(1);
}
});
inquirer.prompt(questions.domain, getChallenges);
});
@ -268,14 +253,12 @@ function getChallenges(answers) {
var jws = crypto.generateSignature(state.keyPair, new Buffer(authzMessage));
var payload = JSON.stringify(jws);
var options = url.parse(state.newAuthorizationURL);
options.method = "POST";
var req = http.request(options, getReadyToValidate);
var req = request.post(state.newAuthorizationURL, {}, getReadyToValidate);
req.write(payload)
req.end();
}
function getReadyToValidate(resp) {
function getReadyToValidate(err, resp, body) {
if (Math.floor(resp.statusCode / 100) != 2) {
// Non-2XX response
console.log("Authorization request failed with code " + resp.statusCode)
@ -291,13 +274,6 @@ function getReadyToValidate(resp) {
state.authorizationURL = resp.headers["location"];
state.newCertificateURL = links["next"];
var body = ""
resp.on('data', function(chunk) {
body += chunk;
});
resp.on('end', function(chunk) {
if (chunk) { body += chunk; }
var authz = JSON.parse(body);
var simpleHttps = authz.challenges.filter(function(x) { return x.type == "simpleHttps"; });
@ -326,7 +302,6 @@ function getReadyToValidate(resp) {
// > python -m SimpleHTTPServer 5001
inquirer.prompt(questions.readyToValidate, sendResponse);
});
}
function sendResponse() {
@ -340,30 +315,23 @@ function sendResponse() {
var options = url.parse(state.responseURL);
options.method = "POST";
var req = http.request(options, ensureValidation);
var req = request.post(state.responseURL, {}, ensureValidation);
req.write(payload)
req.end();
}
function ensureValidation(resp) {
function ensureValidation(err, resp, body) {
if (Math.floor(resp.statusCode / 100) != 2) {
// Non-2XX response
console.log("Authorization status request failed with code " + resp.statusCode)
return;
}
var body = "";
resp.on('data', function(chunk) {
body += chunk;
});
resp.on('end', function(chunk) {
if (chunk) { body += chunk; }
var authz = JSON.parse(body);
if (authz.status == "pending") {
setTimeout(function() {
http.get(state.authorizationURL, ensureValidation);
request.get(state.authorizationURL, {}, ensureValidation);
}, state.retryDelay);
} else if (authz.status == "valid") {
cli.spinner("Validating domain ... done", true);
@ -377,7 +345,6 @@ function ensureValidation(resp) {
console.log(JSON.stringify(authz, null, " "));
return;
}
});
}
function getCertificate() {
@ -392,22 +359,12 @@ function getCertificate() {
cli.spinner("Requesting certificate");
var options = url.parse(state.newCertificateURL);
options.method = "POST";
var req = http.request(options, downloadCertificate);
var req = request.post(state.newCertificateURL, {}, downloadCertificate);
req.write(payload)
req.end();
}
function downloadCertificate(resp) {
var chunks = [];
resp.on('data', function(chunk) {
chunks.push(chunk);
});
resp.on('end', function(chunk) {
if (chunk) { chunks.push(chunk); }
var body = Buffer.concat(chunks);
function downloadCertificate(err, resp, body) {
if (Math.floor(resp.statusCode / 100) != 2) {
// Non-2XX response
console.log("Certificate request failed with code " + resp.statusCode);
@ -417,19 +374,23 @@ function downloadCertificate(resp) {
cli.spinner("Requesting certificate ... done", true);
console.log();
var certB64 = util.b64enc(body);
state.certificate = certB64;
inquirer.prompt(questions.files, saveFiles);
state.certificate = body;
console.log()
var certURL = resp.headers['location'];
request.get(certURL, function(err, res, body) {
if (body !== state.certificate) {
console.log("ERROR! Cert at", certURL, "did not match returned cert.");
}
});
inquirer.prompt(questions.files, saveFiles);
}
function saveFiles(answers) {
var keyPEM = crypto.privateKeyToPem(state.keyPair.privateKey);
fs.writeFileSync(answers.keyFile, keyPEM);
var certPEM = crypto.certificateToPem(state.certificate);
fs.writeFileSync(answers.certFile, certPEM);
fs.writeFileSync(answers.certFile, state.certificate);
console.log("Done!")
console.log("To try it out:");