le-acme-core.js/example/letsencrypt.js

119 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-12-16 02:44:44 +00:00
/*!
* letiny-core
* Copyright(c) 2015 AJ ONeal <aj@daplie.com> https://daplie.com
* Apache-2.0 OR MIT (and hence also MPL 2.0)
*/
2015-12-16 02:00:41 +00:00
'use strict';
2015-12-16 02:36:10 +00:00
//var LeCore = require('letiny-core');
var LeCore = require('../').ACME.create();
2015-12-16 02:00:41 +00:00
2015-12-16 02:28:09 +00:00
var email = process.argv[2] || 'user@example.com'; // CHANGE TO YOUR EMAIL
var domains = [process.argv[3] || 'example.com']; // CHANGE TO YOUR DOMAIN
var acmeDiscoveryUrl = LeCore.stagingServerUrl;
2015-12-16 02:00:41 +00:00
var challengeStore = require('./challenge-store');
2015-12-16 02:36:10 +00:00
var certStore = require('./cert-store');
2015-12-16 02:00:41 +00:00
var serve = require('./serve');
2015-12-16 04:09:22 +00:00
var closer;
2015-12-16 02:00:41 +00:00
var accountKeypair = null;
var domainKeypair = null;
2015-12-16 02:28:09 +00:00
var acmeUrls = null;
console.log('Using server', acmeDiscoveryUrl);
console.log('Creating account for', email, 'and registering certificates for', domains, 'to that account');
init();
function init() {
getPrivateKeys(function () {
2015-12-16 02:44:44 +00:00
console.log('Getting Acme Urls');
LeCore.getAcmeUrls(acmeDiscoveryUrl, function (err, urls) {
2015-12-16 02:28:09 +00:00
// in production choose LeCore.productionServerUrl
2015-12-16 02:44:44 +00:00
console.log('Got Acme Urls', err, urls);
2015-12-16 02:28:09 +00:00
acmeUrls = urls;
runDemo();
});
});
}
2015-12-16 02:57:17 +00:00
function getPrivateKeys(cb) {
2015-12-16 02:44:44 +00:00
console.log('Generating Account Keypair');
const RSA = require('rsa-compat').RSA;
RSA.generateKeypair(2048, 65537, {}, function (err, pems) {
2015-12-16 02:28:09 +00:00
accountKeypair = pems;
2015-12-16 02:44:44 +00:00
console.log('Generating Domain Keypair');
RSA.generateKeypair(2048, 65537, {}, function (err, pems2) {
2015-12-16 02:28:09 +00:00
domainKeypair = pems2;
2015-12-16 02:57:17 +00:00
cb();
2015-12-16 02:28:09 +00:00
});
});
}
function runDemo() {
2015-12-16 02:44:44 +00:00
console.log('Registering New Account');
2015-12-16 02:00:41 +00:00
LeCore.registerNewAccount(
2015-12-16 02:28:09 +00:00
{ newRegUrl: acmeUrls.newReg
, email: email
, accountKeypair: accountKeypair
2015-12-16 02:28:09 +00:00
, agreeToTerms: function (tosUrl, done) {
2015-12-16 02:00:41 +00:00
2015-12-16 03:23:34 +00:00
// agree to the exact version of these terms
console.log('[tosUrl]:', tosUrl);
2015-12-16 02:28:09 +00:00
done(null, tosUrl);
2015-12-16 02:00:41 +00:00
}
2015-12-16 02:28:09 +00:00
}
, function (err, regr) {
2015-12-16 02:00:41 +00:00
2015-12-16 02:28:09 +00:00
// Note: you should save the registration
// record to disk (or db)
console.log('[regr]');
2015-12-16 03:23:34 +00:00
console.log(err || regr);
2015-12-16 02:00:41 +00:00
2015-12-16 02:44:44 +00:00
console.log('Registering New Certificate');
2015-12-16 02:28:09 +00:00
LeCore.getCertificate(
2015-12-16 03:23:34 +00:00
{ newAuthzUrl: acmeUrls.newAuthz
, newCertUrl: acmeUrls.newCert
, domainKeypair: domainKeypair
, accountKeypair: accountKeypair
2015-12-16 03:23:34 +00:00
, domains: domains
2015-12-16 02:28:09 +00:00
, setChallenge: challengeStore.set
, removeChallenge: challengeStore.remove
}
, function (err, certs) {
// Note: you should save certs to disk (or db)
certStore.set(domains[0], certs, function () {
console.log('[certs]');
2015-12-16 03:23:34 +00:00
console.log(err || certs);
2015-12-16 04:09:22 +00:00
closer();
2015-12-16 02:28:09 +00:00
});
}
);
}
);
}
2015-12-16 02:00:41 +00:00
//
// Setup the Server
//
2015-12-16 04:09:22 +00:00
closer = serve.init({
2015-12-16 02:00:41 +00:00
LeCore: LeCore
// needs a default key and cert chain, anything will do
, httpsOptions: {}
2015-12-16 02:00:41 +00:00
, challengeStore: challengeStore
, certStore: certStore
});