smartacme/dist/smartacme.classes.acmecert.js
2017-04-28 18:56:55 +02:00

183 lines
15 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const q = require("smartq");
const plugins = require("./smartacme.plugins");
const helpers = require("./smartacme.helpers");
// Dnsly instance (we really just need one)
let myDnsly = new plugins.dnsly.Dnsly('google');
/**
* class AcmeCert represents a cert for domain
*/
class AcmeCert {
constructor(optionsArg, parentAcmeAccount) {
this.domainName = optionsArg.domain;
this.parentAcmeAccount = parentAcmeAccount;
this.keypair = helpers.createKeypair(optionsArg.bit);
let privateKeyForged = plugins.nodeForge.pki.privateKeyFromPem(this.keypair.privateKey);
let publicKeyForged = plugins.nodeForge.pki.publicKeyToPem(plugins.nodeForge.pki.setRsaPublicKey(privateKeyForged.n, privateKeyForged.e));
this.keyPairFinal = {
privateKey: privateKeyForged,
publicKey: publicKeyForged
};
// set dates
this.validFrom = new Date();
this.validTo = new Date();
this.validTo.setDate(this.validFrom.getDate() + 90);
// set attributes
this.attributes = [
{ name: 'commonName', value: optionsArg.domain },
{ name: 'countryName', value: optionsArg.country },
{ shortName: 'ST', value: optionsArg.country_short },
{ name: 'localityName', value: optionsArg.locality },
{ name: 'organizationName', value: optionsArg.organization },
{ shortName: 'OU', value: optionsArg.organization_short },
{ name: 'challengePassword', value: optionsArg.password },
{ name: 'unstructuredName', value: optionsArg.unstructured }
];
// set up csr
this.csr = plugins.nodeForge.pki.createCertificationRequest();
this.csr.setSubject(this.attributes);
this.csr.setAttributes(this.attributes);
}
/**
* requests a challenge for a domain
* @param domainNameArg - the domain name to request a challenge for
* @param challengeType - the challenge type to request
*/
requestChallenge(challengeTypeArg = 'dns-01') {
let done = q.defer();
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.newAuthz({
identifier: {
type: 'dns',
value: this.domainName
}
}, this.parentAcmeAccount.parentSmartAcme.keyPair, (err, res) => {
if (err) {
console.error('smartacme: something went wrong:');
console.log(err);
done.reject(err);
}
let preChosenChallenge = res.body.challenges.filter(x => {
return x.type === challengeTypeArg;
})[0];
/**
* the key is needed to accept the challenge
*/
let authKey = plugins.rawacme.keyAuthz(preChosenChallenge.token, this.parentAcmeAccount.parentSmartAcme.keyPair.publicKey);
/**
* needed in case selected challenge is of type dns-01
*/
this.dnsKeyHash = plugins.rawacme.dnsKeyAuthzHash(authKey); // needed if dns challenge is chosen
/**
* the return challenge
*/
this.chosenChallenge = {
uri: preChosenChallenge.uri,
type: preChosenChallenge.type,
token: preChosenChallenge.token,
keyAuthorization: authKey,
status: preChosenChallenge.status,
dnsKeyHash: this.dnsKeyHash,
domainName: this.domainName,
domainNamePrefixed: helpers.prefixName(this.domainName)
};
done.resolve(this.chosenChallenge);
});
return done.promise;
}
/**
* checks if DNS records are set, will go through a max of 30 cycles
*/
checkDns(cycleArg = 1) {
return __awaiter(this, void 0, void 0, function* () {
let result = yield myDnsly.checkUntilAvailable(helpers.prefixName(this.domainName), 'TXT', this.dnsKeyHash);
if (result) {
console.log('DNS is set!');
return;
}
else {
throw new Error('DNS not set!');
}
});
}
/**
* validates a challenge, only call after you have set the challenge at the expected location
*/
requestValidation() {
return __awaiter(this, void 0, void 0, function* () {
let makeRequest = () => {
let done = q.defer();
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.poll(this.chosenChallenge.uri, (err, res) => __awaiter(this, void 0, void 0, function* () {
if (err) {
console.log(err);
return;
}
console.log(`Validation response:`);
console.log(JSON.stringify(res.body));
if (res.body.status === 'pending' || res.body.status === 'invalid') {
yield plugins.smartdelay.delayFor(3000);
makeRequest().then((x) => { done.resolve(x); });
}
else {
console.log('perfect!');
done.resolve(res.body);
}
}));
return done.promise;
};
yield makeRequest();
});
}
/**
* requests a certificate
*/
requestCert() {
let done = q.defer();
let payload = {
csr: plugins.rawacme.base64.encode(plugins.rawacme.toDer(plugins.nodeForge.pki.certificationRequestToPem(this.csr))),
notBefore: this.validFrom.toISOString(),
notAfter: this.validTo.toISOString()
};
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.newCert(payload, helpers.createKeypair(), (err, res) => {
if (err) {
console.log(err);
done.reject(err);
}
console.log(res.body);
done.resolve(res.body);
});
return done.promise;
}
/**
* getCertificate - takes care of cooldown, validation polling and certificate retrieval
*/
getCertificate() {
}
/**
* accept a challenge - for private use only
*/
acceptChallenge() {
let done = q.defer();
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.post(this.chosenChallenge.uri, {
resource: 'challenge',
keyAuthorization: this.chosenChallenge.keyAuthorization
}, this.parentAcmeAccount.parentSmartAcme.keyPair, (err, res) => {
if (err) {
console.log(err);
done.reject(err);
}
done.resolve(res.body);
});
return done.promise;
}
}
exports.AcmeCert = AcmeCert;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuYWNtZWNlcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuY2xhc3Nlcy5hY21lY2VydC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsNEJBQTJCO0FBRTNCLCtDQUE4QztBQUM5QywrQ0FBOEM7QUEyQzlDLDJDQUEyQztBQUMzQyxJQUFJLE9BQU8sR0FBRyxJQUFJLE9BQU8sQ0FBQyxLQUFLLENBQUMsS0FBSyxDQUFDLFFBQVEsQ0FBQyxDQUFBO0FBRS9DOztHQUVHO0FBQ0g7SUFZRSxZQUFZLFVBQXNDLEVBQUUsaUJBQThCO1FBQ2hGLElBQUksQ0FBQ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