168 lines
12 KiB
JavaScript
168 lines
12 KiB
JavaScript
"use strict";
|
|
// typings
|
|
require("typings-global"); // typings for node
|
|
// third party modules
|
|
const q = require("q"); // promises
|
|
let rsaKeygen = require('rsa-keygen'); // rsa keygen
|
|
let rawacme = require('rawacme'); // acme helper functions
|
|
const smartacme_classes_helper_1 = require("./smartacme.classes.helper");
|
|
/**
|
|
* class SmartAcme exports methods for maintaining SSL Certificates
|
|
*/
|
|
class SmartAcme {
|
|
/**
|
|
* the constructor for class SmartAcme
|
|
*/
|
|
constructor(productionArg = false) {
|
|
this.productionBool = productionArg;
|
|
this.helper = new smartacme_classes_helper_1.SmartacmeHelper(this);
|
|
this.keyPair = this.helper.createKeypair();
|
|
if (this.productionBool) {
|
|
this.acmeUrl = rawacme.LETSENCRYPT_URL;
|
|
}
|
|
else {
|
|
this.acmeUrl = rawacme.LETSENCRYPT_STAGING_URL;
|
|
}
|
|
}
|
|
/**
|
|
* creates an account if not currently present in module
|
|
* @executes ASYNC
|
|
*/
|
|
createAccount() {
|
|
let done = q.defer();
|
|
rawacme.createClient({
|
|
url: this.acmeUrl,
|
|
publicKey: this.keyPair.publicKey,
|
|
privateKey: this.keyPair.privateKey
|
|
}, (err, client) => {
|
|
if (err) {
|
|
console.error('smartacme: something went wrong:');
|
|
console.log(err);
|
|
done.reject(err);
|
|
return;
|
|
}
|
|
// make client available in class
|
|
this.rawacmeClient = client;
|
|
// create the registration
|
|
client.newReg({
|
|
contact: ['mailto:domains@lossless.org']
|
|
}, (err, res) => {
|
|
if (err) {
|
|
console.error('smartacme: something went wrong:');
|
|
console.log(err);
|
|
done.reject(err);
|
|
return;
|
|
}
|
|
this.JWK = res.body.key;
|
|
this.link = res.headers.link;
|
|
console.log(this.link);
|
|
this.location = res.headers.location;
|
|
done.resolve();
|
|
});
|
|
});
|
|
return done.promise;
|
|
}
|
|
agreeTos() {
|
|
let done = q.defer();
|
|
let tosPart = this.link.split(',')[1];
|
|
let tosLinkPortion = tosPart.split(';')[0];
|
|
let url = tosLinkPortion.split(';')[0].trim().replace(/[<>]/g, '');
|
|
this.rawacmeClient.post(this.location, { Agreement: url, resource: 'reg' }, (err, res) => {
|
|
if (err) {
|
|
console.log(err);
|
|
done.reject(err);
|
|
return;
|
|
}
|
|
done.resolve();
|
|
});
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* requests a challenge for a domain
|
|
* @param domainNameArg - the domain name to request a challenge for
|
|
* @param challengeType - the challenge type to request
|
|
*/
|
|
requestChallenge(domainNameArg, challengeTypeArg = 'dns-01') {
|
|
let done = q.defer();
|
|
this.rawacmeClient.newAuthz({
|
|
identifier: {
|
|
type: 'dns',
|
|
value: domainNameArg
|
|
}
|
|
}, this.keyPair, (err, res) => {
|
|
if (err) {
|
|
console.error('smartacme: something went wrong:');
|
|
console.log(err);
|
|
done.reject(err);
|
|
}
|
|
console.log(JSON.stringify(res.body));
|
|
let dnsChallenge = res.body.challenges.filter(x => {
|
|
return x.type === challengeTypeArg;
|
|
})[0];
|
|
this.acceptChallenge(dnsChallenge)
|
|
.then((x) => {
|
|
done.resolve(x);
|
|
});
|
|
});
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* getCertificate - takes care of cooldown, validation polling and certificate retrieval
|
|
*/
|
|
getCertificate() {
|
|
}
|
|
/**
|
|
* validates a challenge
|
|
*/
|
|
validate(challenge) {
|
|
let done = q.defer();
|
|
this.rawacmeClient.poll(challenge.uri, function (err, res) {
|
|
if (err) {
|
|
console.log(err);
|
|
done.reject(err);
|
|
}
|
|
console.log(res.status);
|
|
console.log(JSON.stringify(res.body));
|
|
done.resolve();
|
|
});
|
|
return done.promise;
|
|
}
|
|
/**
|
|
* accept a challenge - for private use only
|
|
*/
|
|
acceptChallenge(challenge) {
|
|
let done = q.defer();
|
|
/**
|
|
* the key is needed to accept the challenge
|
|
*/
|
|
let authKey = rawacme.keyAuthz(challenge.token, this.keyPair.publicKey);
|
|
/**
|
|
* needed in case selected challenge is of type dns-01
|
|
*/
|
|
let keyHash = rawacme.dnsKeyAuthzHash(authKey); // needed if dns challenge is chosen
|
|
/**
|
|
* the return challenge
|
|
*/
|
|
let returnDNSChallenge = {
|
|
uri: challenge.uri,
|
|
type: challenge.type,
|
|
token: challenge.token,
|
|
keyAuthorization: challenge.keyAuthorization,
|
|
keyHash: keyHash,
|
|
status: challenge.status
|
|
};
|
|
this.rawacmeClient.post(challenge.uri, {
|
|
resource: 'challenge',
|
|
keyAuthorization: authKey
|
|
}, this.keyPair, (err, res) => {
|
|
if (err) {
|
|
console.log(err);
|
|
done.reject(err);
|
|
}
|
|
done.resolve(returnDNSChallenge);
|
|
});
|
|
return done.promise;
|
|
}
|
|
}
|
|
exports.SmartAcme = SmartAcme;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuc21hcnRhY21lLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhY21lLmNsYXNzZXMuc21hcnRhY21lLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxVQUFVO0FBQ1YsMEJBQXVCLENBQUMsbUJBQW1CO0FBRTNDLHNCQUFzQjtBQUN0Qix1QkFBc0IsQ0FBQyxXQUFXO0FBRWxDLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxZQUFZLENBQUMsQ0FBQSxDQUFDLGFBQWE7QUFDbkQsSUFBSSxPQUFPLEdBQUcsT0FBTyxDQUFDLFNBQVMsQ0FBQyxDQUFBLENBQUMsd0JBQXdCO0FBT3pELHlFQUF5RTtBQWlCekU7O0dBRUc7QUFDSDtJQVVJOztPQUVHO0lBQ0gsWUFBWSxnQkFBeUIsS0FBSztRQUN0QyxJQUFJLENBQUMsY0FBYyxHQUFHLGFBQWEsQ0FBQTtRQUNuQyxJQUFJLENBQUMsTUFBTSxHQUFHLElBQUksMENBQWUsQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUN2Q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