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

119 lines
3.3 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('../');
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
2015-12-16 02:28:09 +00:00
var accountPrivateKeyPem = null;
var domainPrivateKeyPem = null;
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');
2015-12-16 04:11:58 +00:00
console.log("(Note: if you're using forge and not ursa, this will take a long time");
2015-12-16 02:36:10 +00:00
LeCore.leCrypto.generateRsaKeypair(2048, 65537, function (err, pems) {
2015-12-16 02:28:09 +00:00
accountPrivateKeyPem = pems.privateKeyPem;
2015-12-16 02:44:44 +00:00
console.log('Generating Domain Keypair');
2015-12-16 02:36:10 +00:00
LeCore.leCrypto.generateRsaKeypair(2048, 65537, function (err, pems) {
2015-12-16 02:28:09 +00:00
domainPrivateKeyPem = pems.privateKeyPem;
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
, accountPrivateKeyPem: accountPrivateKeyPem
, 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
, domainPrivateKeyPem: domainPrivateKeyPem
2015-12-16 02:28:09 +00:00
, accountPrivateKeyPem: accountPrivateKeyPem
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
});
2017-04-10 20:21:09 +00:00
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
2017-04-10 20:21:09 +00:00
, tlsOptions: require('localhost.daplie.me-certificates')
2015-12-16 02:00:41 +00:00
, challengeStore: challengeStore
2017-04-10 20:21:09 +00:00
, certStore: certStore
2015-12-16 02:00:41 +00:00
});