workin' it

This commit is contained in:
AJ ONeal 2015-12-15 22:35:42 +00:00
parent e651755417
commit aec8958ca8
1 changed files with 99 additions and 98 deletions

View File

@ -6,112 +6,113 @@
*/ */
'use strict'; 'use strict';
module.exports.create = function (deps) {
var NOOP=function () {}, log=NOOP;
var request=require('request');
var importPemPrivateKey=deps.leCrypto.importPemPrivateKey;
var Acme = deps.Acme;
var NOOP=function () {}, log=NOOP; function registerNewAccount(options, cb) {
var request=require('request'); var state = {};
var cryptoUtil=require('./crypto-util');
var Acme = require('./acme-client');
function registerNewAccount(options, cb) { if (!options.accountPrivateKeyPem) {
var state = {}; return handleErr(new Error("options.accountPrivateKeyPem must be an ascii private key pem"));
}
if (!options.accountPrivateKeyPem) { if (!options.agreeToTerms) {
return handleErr(new Error("options.accountPrivateKeyPem must be an ascii private key pem")); cb(new Error("options.agreeToTerms must be function (tosUrl, fn => (err, true))"));
} return;
if (!options.agreeToTerms) { }
cb(new Error("options.agreeToTerms must be function (tosUrl, fn => (err, true))")); if (!options.newReg) {
return; cb(new Error("options.newReg must be the a new registration url"));
} return;
if (!options.newReg) { }
cb(new Error("options.newReg must be the a new registration url")); if (!options.email) {
return; cb(new Error("options.email must be an email"));
} return;
if (!options.email) {
cb(new Error("options.email must be an email"));
return;
}
state.accountKeyPem=options.accountPrivateKeyPem;
state.accountKeyPair=cryptoUtil.importPemPrivateKey(state.accountKeyPem);
state.acme=new Acme(state.accountKeyPair);
register();
function register() {
state.acme.post(options.newReg, {
resource:'new-reg',
contact:['mailto:'+options.email]
}, getTerms);
}
function getTerms(err, res) {
var links;
if (err || Math.floor(res.statusCode/100)!==2) {
return handleErr(err, 'Registration request failed: ' + res.body.toString('utf8'));
} }
links=Acme.parseLink(res.headers.link); state.accountKeyPem=options.accountPrivateKeyPem;
if (!links || !('next' in links)) { state.accountKeyPair=importPemPrivateKey(state.accountKeyPem);
return handleErr(err, 'Server didn\'t provide information to proceed (1)'); state.acme=new Acme(state.accountKeyPair);
register();
function register() {
state.acme.post(options.newReg, {
resource:'new-reg',
contact:['mailto:'+options.email]
}, getTerms);
} }
state.registrationUrl=res.headers.location; function getTerms(err, res) {
// TODO should we pass this along? var links;
//state.newAuthorizationUrl=links.next;
state.termsRequired=('terms-of-service' in links);
if (state.termsRequired) {
state.termsUrl=links['terms-of-service'];
options.agreeToTerms(state.termsUrl, function (err, agree) {
if (err) {
return handleErr(err);
}
if (!agree) {
return handleErr(new Error("You must agree to the terms of use at '" + state.termsUrl + "'"));
}
state.agreeTerms = agree;
state.termsUrl=links['terms-of-service'];
log(state.termsUrl);
request.get(state.termsUrl, getAgreement);
});
} else {
cb();
}
}
function getAgreement(err/*, res, body*/) {
if (err) {
return handleErr(err, 'Couldn\'t get agreement');
}
log('The CA requires your agreement to terms:\n'+state.termsUrl);
sendAgreement();
}
function sendAgreement() {
if (state.termsRequired && !state.agreeTerms) {
return handleErr(null, 'The CA requires your agreement to terms: '+state.termsUrl);
}
log('Posting agreement to: '+state.registrationUrl);
state.acme.post(state.registrationUrl, {
resource:'reg',
agreement:state.termsUrl
}, function(err, res, body) {
if (err || Math.floor(res.statusCode/100)!==2) { if (err || Math.floor(res.statusCode/100)!==2) {
return handleErr(err, 'Couldn\'t POST agreement back to server', body); return handleErr(err, 'Registration request failed: ' + res.body.toString('utf8'));
} else {
cb(null, body);
} }
});
links=Acme.parseLink(res.headers.link);
if (!links || !('next' in links)) {
return handleErr(err, 'Server didn\'t provide information to proceed (1)');
}
state.registrationUrl=res.headers.location;
// TODO should we pass this along?
//state.newAuthorizationUrl=links.next;
state.termsRequired=('terms-of-service' in links);
if (state.termsRequired) {
state.termsUrl=links['terms-of-service'];
options.agreeToTerms(state.termsUrl, function (err, agree) {
if (err) {
return handleErr(err);
}
if (!agree) {
return handleErr(new Error("You must agree to the terms of use at '" + state.termsUrl + "'"));
}
state.agreeTerms = agree;
state.termsUrl=links['terms-of-service'];
log(state.termsUrl);
request.get(state.termsUrl, getAgreement);
});
} else {
cb();
}
}
function getAgreement(err/*, res, body*/) {
if (err) {
return handleErr(err, 'Couldn\'t get agreement');
}
log('The CA requires your agreement to terms:\n'+state.termsUrl);
sendAgreement();
}
function sendAgreement() {
if (state.termsRequired && !state.agreeTerms) {
return handleErr(null, 'The CA requires your agreement to terms: '+state.termsUrl);
}
log('Posting agreement to: '+state.registrationUrl);
state.acme.post(state.registrationUrl, {
resource:'reg',
agreement:state.termsUrl
}, function(err, res, body) {
if (err || Math.floor(res.statusCode/100)!==2) {
return handleErr(err, 'Couldn\'t POST agreement back to server', body);
} else {
cb(null, body);
}
});
}
function handleErr(err, text, info) {
log(text, err, info);
cb(err || new Error(text));
}
} }
function handleErr(err, text, info) { return registerNewAccount;
log(text, err, info); };
cb(err || new Error(text));
}
}
module.exports = registerNewAccount;