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

102 lines
2.7 KiB
JavaScript
Raw Normal View History

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 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 () {
LeCore.getAcmeUrls(acmeDiscoveryUrl, function (urls) {
// in production choose LeCore.productionServerUrl
acmeUrls = urls;
runDemo();
});
});
}
function getPrivateKeys() {
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: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:00:41 +00:00
2015-12-16 02:28:09 +00:00
});
});
}
function runDemo() {
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 02:28:09 +00:00
// agree to these exact terms
console.log('[tosUrl]');
console.log(tosUrl);
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]');
console.log(regr);
2015-12-16 02:00:41 +00:00
2015-12-16 02:28:09 +00:00
LeCore.getCertificate(
{ domainPrivateKeyPem: domainPrivateKeyPem
, accountPrivateKeyPem: accountPrivateKeyPem
, setChallenge: challengeStore.set
, removeChallenge: challengeStore.remove
, domains: domains
}
, function (err, certs) {
// Note: you should save certs to disk (or db)
certStore.set(domains[0], certs, function () {
console.log('[certs]');
console.log(certs);
});
}
);
}
);
}
2015-12-16 02:00:41 +00:00
//
// Setup the Server
//
serve.init({
LeCore: LeCore
// needs a default key and cert chain, anything will do
, httpsOptions: require('localhost.daplie.com-certificates')
, challengeStore: challengeStore
, certStore: certStore
});