greenlock.js/examples/simple.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-08-04 22:49:35 +00:00
'use strict';
2018-05-19 23:42:49 +00:00
//var Greenlock = require('greenlock');
var Greenlock = require('../');
2016-08-04 22:49:35 +00:00
var db = {};
var config = {
2019-10-08 10:51:15 +00:00
server: 'https://acme-v02.api.letsencrypt.org/directory',
version: 'draft-11',
2016-08-04 22:49:35 +00:00
2019-10-08 10:51:15 +00:00
configDir: require('os').homedir() + '/acme/etc', // or /etc/acme or wherever
2016-08-04 22:49:35 +00:00
2019-10-08 10:51:15 +00:00
privkeyPath: ':config/live/:hostname/privkey.pem', //
fullchainPath: ':config/live/:hostname/fullchain.pem', // Note: both that :config and :hostname
certPath: ':config/live/:hostname/cert.pem', // will be templated as expected
chainPath: ':config/live/:hostname/chain.pem', //
2016-08-04 22:49:35 +00:00
2019-10-08 10:51:15 +00:00
rsaKeySize: 2048,
2016-08-04 22:49:35 +00:00
2019-10-08 10:51:15 +00:00
debug: true
2016-08-04 22:49:35 +00:00
};
var handlers = {
2019-10-08 10:51:15 +00:00
setChallenge: function(opts, hostname, key, val, cb) {
// called during the ACME server handshake, before validation
db[key] = {
hostname: hostname,
key: key,
val: val
};
2016-08-04 22:49:35 +00:00
2019-10-08 10:51:15 +00:00
cb(null);
},
removeChallenge: function(opts, hostname, key, cb) {
// called after validation on both success and failure
db[key] = null;
cb(null);
},
getChallenge: function(opts, hostname, key, cb) {
// this is special because it is called by the webserver
cb(null, db[key].val); // (see greenlock-cli/bin & greenlock-express/standalone),
// not by the library itself
},
agreeToTerms: function(tosUrl, cb) {
// gives you an async way to expose the legal agreement
cb(null, tosUrl); // (terms of use) to your users before accepting
}
2016-08-04 22:49:35 +00:00
};
2018-05-19 23:42:49 +00:00
var greenlock = Greenlock.create(config, handlers);
2019-10-08 10:51:15 +00:00
console.error(
'CHANGE THE EMAIL, DOMAINS, AND AGREE TOS IN THE EXAMPLE BEFORE RUNNING IT'
);
2018-05-15 21:42:04 +00:00
process.exit(1);
2019-10-08 10:51:15 +00:00
// checks :conf/renewal/:hostname.conf
greenlock.register(
{
// and either renews or registers
domains: ['example.com'], // CHANGE TO YOUR DOMAIN
email: 'user@email.com', // CHANGE TO YOUR EMAIL
agreeTos: false, // set to true to automatically accept an agreement
// which you have pre-approved (not recommended)
rsaKeySize: 2048
},
function(err) {
if (err) {
// Note: you must have a webserver running
// and expose handlers.getChallenge to it
// in order to pass validation
// See greenlock-cli and or greenlock-express
console.error('[Error]: greenlock/examples/standalone');
console.error(err.stack);
} else {
console.log('success');
}
}
);