Compare commits

..

9 Commits

Author SHA1 Message Date
1f38e12bd3 1.0.7 2017-01-15 23:11:54 +01:00
1c777f6f05 improve README 2017-01-15 23:11:51 +01:00
aad113a8ea add async checkDNS 2017-01-15 22:59:58 +01:00
fff63839d1 1.0.6 2017-01-15 22:30:43 +01:00
c8d2cfd4ce update to new standards 2017-01-15 22:30:33 +01:00
dfd7edd330 now has working requestValidation method 2017-01-15 13:33:55 +01:00
4dadcf227c fix som things 2017-01-15 12:21:29 +01:00
fce25c60ed start better segregation of concerns 2017-01-14 18:36:33 +01:00
98cc70dbfb start with certificate signing process 2017-01-14 14:14:50 +01:00
22 changed files with 1032 additions and 399 deletions

View File

@ -26,11 +26,25 @@ import { SmartAcme } from 'smartacme'
let smac = new SmartAcme()
let myAccount = smac.getAccount() // optionally accepts a filePath Arg with a stored acmeaccount.json
let myCert = myAccount.getChallenge('example.com','dns-01') // will return a dnsHash to set in your DNS record
myCert.get().then(() => {
console.log(myCert.certificate) // your certificate, ready to use in whatever way you prefer
})
(async () => { // learn async/await, it'll make your life easier
// optionally accepts a filePath Arg with a stored acmeaccount.json
// will create an account and
let myAccount = await smac.createAcmeAccount()
// will return a dnsHash to set in your DNS record
let myCert = await myAccount.createAcmeCert('example.com')
// gets and accepts the specified challenge
// first argument optional, defaults to dns-01 (which is the cleanest method for production use)
let myChallenge = await myCert.getChallenge('dns-01')
/* ----------
Now you need to set the challenge in your DNS
myChallenge.domainNamePrefixed is the address for the record
myChallenge.dnsKeyHash is the ready to use txt record value expected by letsencrypt
-------------*/
})()
```
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)

View File

@ -0,0 +1,23 @@
/// <reference types="q" />
import * as q from 'q';
import { SmartAcme } from './smartacme.classes.smartacme';
import { AcmeCert } from './smartacme.classes.acmecert';
/**
* class AcmeAccount represents an AcmeAccount
*/
export declare class AcmeAccount {
parentSmartAcme: SmartAcme;
location: string;
link: string;
JWK: any;
constructor(smartAcmeParentArg: SmartAcme);
/**
* register the account with letsencrypt
*/
register(): q.Promise<{}>;
/**
* agree to letsencrypr terms of service
*/
agreeTos(): q.Promise<{}>;
createAcmeCert(domainNameArg: string, countryArg?: string, countryShortArg?: string, city?: string, companyArg?: string, companyShortArg?: string): q.Promise<AcmeCert>;
}

View File

@ -1 +1,71 @@
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuYWNtZWFjY291bnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuY2xhc3Nlcy5hY21lYWNjb3VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
"use strict";
const q = require("q");
const smartacme_classes_acmecert_1 = require("./smartacme.classes.acmecert");
/**
* class AcmeAccount represents an AcmeAccount
*/
class AcmeAccount {
constructor(smartAcmeParentArg) {
this.parentSmartAcme = smartAcmeParentArg;
}
/**
* register the account with letsencrypt
*/
register() {
let done = q.defer();
this.parentSmartAcme.rawacmeClient.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;
}
/**
* agree to letsencrypr terms of service
*/
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.parentSmartAcme.rawacmeClient.post(this.location, { Agreement: url, resource: 'reg' }, (err, res) => {
if (err) {
console.log(err);
done.reject(err);
return;
}
done.resolve();
});
return done.promise;
}
createAcmeCert(domainNameArg, countryArg = 'Germany', countryShortArg = 'DE', city = 'Bremen', companyArg = 'Some Company', companyShortArg = 'SC') {
let done = q.defer();
let acmeCert = new smartacme_classes_acmecert_1.AcmeCert({
bit: 2064,
key: null,
domain: domainNameArg,
country: countryArg,
country_short: countryShortArg,
locality: city,
organization: companyArg,
organization_short: companyShortArg,
password: null,
unstructured: null,
subject_alt_names: null
}, this);
done.resolve(acmeCert);
return done.promise;
}
}
exports.AcmeAccount = AcmeAccount;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuYWNtZWFjY291bnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuY2xhc3Nlcy5hY21lYWNjb3VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsdUJBQXNCO0FBTXRCLDZFQUF1RDtBQUV2RDs7R0FFRztBQUNIO0lBS0ksWUFBWSxrQkFBNkI7UUFDckMsSUFBSSxDQUFDLGVBQWUsR0FBRyxrQkFBa0IsQ0FBQTtJQUM3QyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxRQUFRO1FBQ0osSUFBSSxJQUFJLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQ3BCLElBQUksQ0FBQyxlQUFlLENBQUMsYUFBYSxDQUFDLE1BQU0sQ0FDckM7WUFDSSxPQUFPLEVBQUUsQ0FBQyw2QkFBNkIsQ0FBQztTQUMzQyxFQUNELENBQUMsR0FBRyxFQUFFLEdBQUc7WUFDTCxFQUFFLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO2dCQUNOLE9BQU8sQ0FBQyxLQUFLLENBQUMsa0NBQWtDLENBQUMsQ0FBQTtnQkFDakQsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQTtnQkFDaEIsSUFBSSxDQUFDLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQTtnQkFDaEIsTUFBTSxDQUFBO1lBQ1YsQ0FBQztZQUNELElBQUksQ0FBQyxHQUFHLEdBQUcsR0FBRyxDQUFDLElBQUksQ0FBQyxHQUFHLENBQ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==

View File

@ -0,0 +1,79 @@
/// <reference types="q" />
import * as q from 'q';
import { IRsaKeypair } from './smartacme.classes.smartacme';
import { AcmeAccount } from './smartacme.classes.acmeaccount';
/**
* types of challenges supported by letsencrypt and this module
*/
export declare type TChallengeType = 'dns-01' | 'http-01';
/**
* values that a challenge's status can have
*/
export declare type TChallengeStatus = 'pending';
export interface ISmartAcmeChallenge {
uri: string;
status: TChallengeStatus;
type: TChallengeType;
token: string;
keyAuthorization: string;
}
export interface ISmartAcmeChallengeAccepted extends ISmartAcmeChallenge {
dnsKeyHash: string;
domainName: string;
domainNamePrefixed: string;
}
export interface IAcmeCsrConstructorOptions {
bit: number;
key: string;
domain: string;
country: string;
country_short: string;
locality: string;
organization: string;
organization_short: string;
password: string;
unstructured: string;
subject_alt_names: string[];
}
/**
* class AcmeCert represents a cert for domain
*/
export declare class AcmeCert {
domainName: string;
attributes: any;
acceptedChallenge: ISmartAcmeChallengeAccepted;
fullchain: string;
parentAcmeAccount: AcmeAccount;
csr: any;
validFrom: Date;
validTo: Date;
keypair: IRsaKeypair;
keyPairFinal: IRsaKeypair;
constructor(optionsArg: IAcmeCsrConstructorOptions, parentAcmeAccount: AcmeAccount);
/**
* 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?: TChallengeType): q.Promise<ISmartAcmeChallengeAccepted>;
/**
* checks if DNS records are set, will go through a max of 30 cycles
*/
checkDns(cycleArg?: number): Promise<any>;
/**
* validates a challenge, only call after you have set the challenge at the expected location
*/
requestValidation(): q.Promise<{}>;
/**
* requests a certificate
*/
requestCert(): q.Promise<{}>;
/**
* getCertificate - takes care of cooldown, validation polling and certificate retrieval
*/
getCertificate(): void;
/**
* accept a challenge - for private use only
*/
private acceptChallenge(challengeArg);
}

File diff suppressed because one or more lines are too long

View File

@ -1,21 +0,0 @@
/// <reference types="q" />
import 'typings-global';
import * as q from 'q';
import { SmartAcme } from './smartacme.classes.smartacme';
export interface IRsaKeypair {
publicKey: string;
privateKey: string;
}
export declare class SmartacmeHelper {
parentSmartAcme: SmartAcme;
constructor(smartAcmeArg: SmartAcme);
/**
* creates a keypair to use with requests and to generate JWK from
*/
createKeypair(bit?: number): IRsaKeypair;
/**
* getReg
* @executes ASYNC
*/
getReg(): q.Promise<{}>;
}

View File

@ -1,40 +0,0 @@
"use strict";
require("typings-global");
const q = require("q");
let rsaKeygen = require('rsa-keygen');
class SmartacmeHelper {
constructor(smartAcmeArg) {
this.parentSmartAcme = smartAcmeArg;
}
/**
* creates a keypair to use with requests and to generate JWK from
*/
createKeypair(bit = 2048) {
let result = rsaKeygen.generate(bit);
return {
publicKey: result.public_key,
privateKey: result.private_key
};
}
/**
* getReg
* @executes ASYNC
*/
getReg() {
let done = q.defer();
let body = { resource: 'reg' };
this.parentSmartAcme.rawacmeClient.post(this.parentSmartAcme.location, body, this.parentSmartAcme.keyPair, (err, res) => {
if (err) {
console.error('smartacme: something went wrong:');
console.log(err);
done.reject(err);
return;
}
console.log(JSON.stringify(res.body));
done.resolve();
});
return done.promise;
}
}
exports.SmartacmeHelper = SmartacmeHelper;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuaGVscGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhY21lLmNsYXNzZXMuaGVscGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwwQkFBdUI7QUFDdkIsdUJBQXNCO0FBQ3RCLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxZQUFZLENBQUMsQ0FBQTtBQVNyQztJQUdJLFlBQVksWUFBdUI7UUFDL0IsSUFBSSxDQUFDLGVBQWUsR0FBRyxZQUFZLENBQUE7SUFDdkMsQ0FBQztJQUVEOztPQUVHO0lBQ0gsYUFBYSxDQUFDLEdBQUcsR0FBRyxJQUFJO1FBQ3B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XlDQyJ9

View File

@ -1,42 +1,34 @@
/// <reference types="q" />
import 'typings-global';
import * as q from 'q';
import { SmartacmeHelper, IRsaKeypair } from './smartacme.classes.helper';
export declare type TChallenge = 'dns-01' | 'http-01';
import { AcmeAccount } from './smartacme.classes.acmeaccount';
/**
* a rsa keypair needed for account creation and subsequent requests
*/
export interface IRsaKeypair {
publicKey: string;
privateKey: string;
}
export { AcmeAccount } from './smartacme.classes.acmeaccount';
export { AcmeCert, ISmartAcmeChallenge, ISmartAcmeChallengeAccepted } from './smartacme.classes.acmecert';
/**
* class SmartAcme exports methods for maintaining SSL Certificates
*/
export declare class SmartAcme {
helper: SmartacmeHelper;
acmeUrl: string;
productionBool: boolean;
keyPair: IRsaKeypair;
location: string;
link: string;
rawacmeClient: any;
JWK: any;
/**
* the constructor for class SmartAcme
*/
constructor(productionArg?: boolean);
/**
* init the smartacme instance
*/
init(): q.Promise<{}>;
/**
* creates an account if not currently present in module
* @executes ASYNC
*/
createAccount(): q.Promise<{}>;
agreeTos(): q.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: string, challengeTypeArg?: TChallenge): q.Promise<{}>;
/**
* getCertificate - takes care of cooldown, validation polling and certificate retrieval
*/
getCertificate(): void;
/**
* accept a challenge - for private use only
*/
private acceptChallenge(challenge);
createAcmeAccount(): q.Promise<AcmeAccount>;
}

File diff suppressed because one or more lines are too long

10
dist/smartacme.helpers.d.ts vendored Normal file
View File

@ -0,0 +1,10 @@
import 'typings-global';
import { IRsaKeypair } from './smartacme.classes.smartacme';
/**
* creates a keypair to use with requests and to generate JWK from
*/
export declare let createKeypair: (bit?: number) => IRsaKeypair;
/**
* prefix a domain name to make sure it complies with letsencrypt
*/
export declare let prefixName: (domainNameArg: string) => string;

40
dist/smartacme.helpers.js vendored Normal file
View File

@ -0,0 +1,40 @@
"use strict";
require("typings-global");
const q = require("q");
const plugins = require("./smartacme.plugins");
/**
* creates a keypair to use with requests and to generate JWK from
*/
exports.createKeypair = (bit = 2048) => {
let result = plugins.rsaKeygen.generate(bit);
return {
publicKey: result.public_key,
privateKey: result.private_key
};
};
/**
* prefix a domain name to make sure it complies with letsencrypt
*/
exports.prefixName = (domainNameArg) => {
return '_acme-challenge.' + domainNameArg;
};
/**
* gets an existing registration
* @executes ASYNC
*/
let getReg = (SmartAcmeArg, location) => {
let done = q.defer();
let body = { resource: 'reg' };
SmartAcmeArg.rawacmeClient.post(location, body, SmartAcmeArg.keyPair, (err, res) => {
if (err) {
console.error('smartacme: something went wrong:');
console.log(err);
done.reject(err);
return;
}
console.log(JSON.stringify(res.body));
done.resolve();
});
return done.promise;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBQ3ZCLHVCQUFzQjtBQUV0QiwrQ0FBOEM7QUFLOUM7O0dBRUc7QUFDUSxRQUFBLGFBQWEsR0FBRyxDQUFDLEdBQUcsR0FBRyxJQUFJO0lBQ2xDLElBQUksTUFBTSxHQUFHLE9BQU8sQ0FBQyxTQUFTLENBQUMsUUFBUSxDQUFDLEdBQUcsQ0FBQyxDQUFBO0lBQzVDLE1BQU0sQ0FBQztRQUNILFNBQVMsRUFBRSxNQUFNLENBQUMsVUFBVTtRQUM1QixVQUFVLEVBQUUsTUFBTSxDQUFDLFdBQVc7S0FDakMsQ0FBQTtBQUNMLENBQUMsQ0FBQTtBQUVEOztHQUVHO0FBQ1EsUUFBQSxVQUFVLEdBQUcsQ0FBQyxhQUFxQjtJQUMxQyxNQUFNLENBQUMsa0JBQWtCLEdBQUcsYUFBYSxDQUFBO0FBQzdDLENBQUMsQ0FBQTtBQUVEOzs7R0FHRztBQUNILElBQUksTUFBTSxHQUFHLENBQUMsWUFBdUIsRUFBRSxRQUFnQjtJQUNuRCxJQUFJLElBQUksR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDcEIsSUFBSSxJQUFJLEdBQUcsRUFBRSxRQUFRLEVBQUUsS0FBSyxFQUFFLENBQUE7SUFDOUIsWUFBWSxDQUFDLGFBQWEsQ0FBQyxJQUFJLENBQzNCLFFBQVEsRUFDUixJQUFJLEVBQ0osWUFBWSxDQUFDLE9BQU8sRUFDcEIsQ0FBQyxHQUFHLEVBQUUsR0FBRztRQUNMLEVBQUUsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUM7WUFDTixPQUFPLENBQUMsS0FBSyxDQUFDLGtDQUFrQyxDQUFDLENBQUE7WUFDakQsT0FBTyxDQUFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsQ0FBQTtZQUNoQixJQUFJLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFBO1lBQ2hCLE1BQU0sQ0FBQTtRQUNWLENBQUM7UUFDRCxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUE7UUFDckMsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ2xCLENBQUMsQ0FDSixDQUFBO0lBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7QUFDdkIsQ0FBQyxDQUFBIn0=

8
dist/smartacme.plugins.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
import 'typings-global';
declare let rsaKeygen: any;
declare let rawacme: any;
declare let nodeForge: any;
import * as dnsly from 'dnsly';
import * as smartfile from 'smartfile';
import * as smartstring from 'smartstring';
export { dnsly, rsaKeygen, rawacme, nodeForge, smartfile, smartstring };

16
dist/smartacme.plugins.js vendored Normal file
View File

@ -0,0 +1,16 @@
"use strict";
require("typings-global"); // typings for node
let rsaKeygen = require('rsa-keygen'); // rsa keygen
exports.rsaKeygen = rsaKeygen;
let rawacme = require('rawacme'); // acme helper functions
exports.rawacme = rawacme;
let nodeForge = require('node-forge');
exports.nodeForge = nodeForge;
// push.rocks modules here
const dnsly = require("dnsly");
exports.dnsly = dnsly;
const smartfile = require("smartfile");
exports.smartfile = smartfile;
const smartstring = require("smartstring");
exports.smartstring = smartstring;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCLENBQUMsbUJBQW1CO0FBRzNDLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxZQUFZLENBQUMsQ0FBQSxDQUFDLGFBQWE7QUFXL0MsOEJBQVM7QUFWYixJQUFJLE9BQU8sR0FBRyxPQUFPLENBQUMsU0FBUyxDQUFDLENBQUEsQ0FBQyx3QkFBd0I7QUFXckQsMEJBQU87QUFWWCxJQUFJLFNBQVMsR0FBRyxPQUFPLENBQUMsWUFBWSxDQUFDLENBQUE7QUFXakMsOEJBQVM7QUFUYiwwQkFBMEI7QUFDMUIsK0JBQThCO0FBSzFCLHNCQUFLO0FBSlQsdUNBQXNDO0FBUWxDLDhCQUFTO0FBUGIsMkNBQTBDO0FBUXRDLGtDQUFXIn0=

View File

@ -1,6 +1,6 @@
{
"name": "smartacme",
"version": "1.0.5",
"version": "1.0.7",
"description": "acme implementation in TypeScript",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
@ -23,17 +23,22 @@
},
"homepage": "https://gitlab.com/pushrocks/smartacme#README",
"dependencies": {
"@types/node-forge": "^0.6.5",
"@types/q": "0.x.x",
"dnsly": "^1.0.7",
"node-forge": "^0.6.47",
"q": "^1.4.1",
"rawacme": "^0.2.1",
"rsa-keygen": "^1.0.6",
"smartfile": "^4.1.0",
"smartstring": "^2.0.20",
"smartfile": "^4.1.2",
"smartstring": "^2.0.22",
"typings-global": "^1.0.14"
},
"devDependencies": {
"@types/should": "^8.1.30",
"should": "^11.1.1",
"cflare": "0.0.10",
"qenv": "^1.1.1",
"should": "^11.1.2",
"typings-test": "^1.0.3"
}
}

View File

@ -4,35 +4,56 @@ const should = require("should");
// import the module to test
const smartacme = require("../dist/index");
describe('smartacme', function () {
let testAcme;
it('should create a valid instance', function () {
let testSmartAcme;
let testAcmeAccount;
let testAcmeCert;
let testChallenge;
it('should create a valid instance', function (done) {
this.timeout(10000);
testAcme = new smartacme.SmartAcme();
should(testAcme).be.instanceOf(smartacme.SmartAcme);
testSmartAcme = new smartacme.SmartAcme();
testSmartAcme.init().then(() => {
should(testSmartAcme).be.instanceOf(smartacme.SmartAcme);
done();
}).catch(err => { done(err); });
});
it('should have created keyPair', function () {
should(testAcme.acmeUrl).be.of.type('string');
should(testSmartAcme.acmeUrl).be.of.type('string');
});
it('should register a new account', function (done) {
this.timeout(10000);
testAcme.createAccount().then(x => {
testSmartAcme.createAcmeAccount().then(x => {
testAcmeAccount = x;
done();
}).catch(err => {
console.log(err);
done(err);
});
});
it('should agree to ToS', function (done) {
it('should create a AcmeCert', function () {
testAcmeAccount.createAcmeCert('test1.bleu.de').then(x => {
testAcmeCert = x;
should(testAcmeAccount).be.instanceOf(smartacme.AcmeCert);
});
});
it('should get a challenge for a AcmeCert', function (done) {
this.timeout(10000);
testAcme.agreeTos().then(() => {
testAcmeCert.requestChallenge().then((challengeAccepted) => {
console.log(challengeAccepted);
testChallenge = challengeAccepted;
done();
});
});
it('should request a challenge for a domain', function (done) {
it('should check for a DNS record', function (done) {
testAcmeCert.checkDns().then(x => {
console.log(x);
done();
});
});
it.skip('should poll for validation of a challenge', function (done) {
this.timeout(10000);
testAcme.requestChallenge('bleu.de').then(() => {
testAcmeCert.requestValidation().then(x => {
done();
});
});
});
//# sourceMapp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FDLENBQUM7WUFDbkMsSUFBSSxFQUFFLENBQUE7UUFDVixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==

View File

@ -1,25 +1,34 @@
import 'typings-test'
import * as should from 'should'
import * as cflare from 'cflare'
import * as qenv from 'qenv'
// import the module to test
import * as smartacme from '../dist/index'
describe('smartacme', function () {
let testAcme: smartacme.SmartAcme
let testSmartAcme: smartacme.SmartAcme
let testAcmeAccount: smartacme.AcmeAccount
let testAcmeCert: smartacme.AcmeCert
let testChallenge: smartacme.ISmartAcmeChallengeAccepted
it('should create a valid instance', function () {
it('should create a valid instance', function (done) {
this.timeout(10000)
testAcme = new smartacme.SmartAcme()
should(testAcme).be.instanceOf(smartacme.SmartAcme)
testSmartAcme = new smartacme.SmartAcme()
testSmartAcme.init().then(() => {
should(testSmartAcme).be.instanceOf(smartacme.SmartAcme)
done()
}).catch(err => { done(err) })
})
it('should have created keyPair', function () {
should(testAcme.acmeUrl).be.of.type('string')
should(testSmartAcme.acmeUrl).be.of.type('string')
})
it('should register a new account', function (done) {
this.timeout(10000)
testAcme.createAccount().then(x => {
testSmartAcme.createAcmeAccount().then(x => {
testAcmeAccount = x
done()
}).catch(err => {
console.log(err)
@ -27,16 +36,32 @@ describe('smartacme', function () {
})
})
it('should agree to ToS', function(done) {
it('should create a AcmeCert', function() {
testAcmeAccount.createAcmeCert('test1.bleu.de').then(x => {
testAcmeCert = x
should(testAcmeAccount).be.instanceOf(smartacme.AcmeCert)
})
})
it('should get a challenge for a AcmeCert', function (done) {
this.timeout(10000)
testAcme.agreeTos().then(() => {
testAcmeCert.requestChallenge().then((challengeAccepted) => {
console.log(challengeAccepted)
testChallenge = challengeAccepted
done()
})
})
it('should request a challenge for a domain', function(done) {
it.skip('should check for a DNS record', function(done) {
testAcmeCert.checkDns().then(x => {
console.log(x)
done()
})
})
it.skip('should poll for validation of a challenge', function (done) {
this.timeout(10000)
testAcme.requestChallenge('bleu.de').then(() => {
testAcmeCert.requestValidation().then(x => {
done()
})
})

View File

@ -1,5 +1,94 @@
import 'typings-global'
import * as q from 'q'
import * as plugins from './smartacme.plugins'
import * as helpers from './smartacme.helpers'
import { SmartAcme, IRsaKeypair } from './smartacme.classes.smartacme'
import { AcmeCert } from './smartacme.classes.acmecert'
/**
* class AcmeAccount represents an AcmeAccount
*/
export class AcmeAccount {
parentSmartAcme: SmartAcme
location: string
link: string
JWK
constructor(smartAcmeParentArg: SmartAcme) {
this.parentSmartAcme = smartAcmeParentArg
}
/**
* register the account with letsencrypt
*/
register() {
let done = q.defer()
this.parentSmartAcme.rawacmeClient.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
}
/**
* agree to letsencrypr terms of service
*/
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.parentSmartAcme.rawacmeClient.post(this.location, { Agreement: url, resource: 'reg' }, (err, res) => {
if (err) {
console.log(err)
done.reject(err)
return
}
done.resolve()
})
return done.promise
}
createAcmeCert(
domainNameArg: string,
countryArg = 'Germany',
countryShortArg = 'DE',
city = 'Bremen',
companyArg = 'Some Company',
companyShortArg = 'SC'
) {
let done = q.defer<AcmeCert>()
let acmeCert = new AcmeCert(
{
bit: 2064,
key: null, // not needed right now
domain: domainNameArg,
country: countryArg,
country_short: countryShortArg,
locality: city,
organization: companyArg,
organization_short: companyShortArg,
password: null,
unstructured: null,
subject_alt_names: null
},
this
)
done.resolve(acmeCert)
return done.promise
}
}

View File

@ -1,5 +1,267 @@
import 'typings-global'
import * as q from 'q'
export class AcmeCert {
import * as plugins from './smartacme.plugins'
import * as helpers from './smartacme.helpers'
import { SmartAcme, IRsaKeypair } from './smartacme.classes.smartacme'
import { AcmeAccount } from './smartacme.classes.acmeaccount'
/**
* types of challenges supported by letsencrypt and this module
*/
export type TChallengeType = 'dns-01' | 'http-01'
/**
* values that a challenge's status can have
*/
export type TChallengeStatus = 'pending'
export interface ISmartAcmeChallenge {
uri: string
status: TChallengeStatus
type: TChallengeType
token: string
keyAuthorization: string
}
export interface ISmartAcmeChallengeAccepted extends ISmartAcmeChallenge {
dnsKeyHash: string
domainName: string
domainNamePrefixed: string
}
export interface IAcmeCsrConstructorOptions {
bit: number,
key: string,
domain: string,
country: string,
country_short: string,
locality: string,
organization: string,
organization_short: string,
password: string,
unstructured: string,
subject_alt_names: string[]
}
// Dnsly instance (we really just need one)
let myDnsly = new plugins.dnsly.Dnsly('google')
/**
* class AcmeCert represents a cert for domain
*/
export class AcmeCert {
domainName: string
attributes
acceptedChallenge: ISmartAcmeChallengeAccepted
fullchain: string
parentAcmeAccount: AcmeAccount
csr
validFrom: Date
validTo: Date
keypair: IRsaKeypair
keyPairFinal: IRsaKeypair
constructor(optionsArg: IAcmeCsrConstructorOptions, parentAcmeAccount: AcmeAccount) {
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: TChallengeType = 'dns-01') {
let done = q.defer<ISmartAcmeChallengeAccepted>()
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 dnsChallenge = res.body.challenges.filter(x => {
return x.type === challengeTypeArg
})[0]
this.acceptChallenge(dnsChallenge)
.then((x: ISmartAcmeChallengeAccepted) => {
done.resolve(x)
})
}
)
return done.promise
}
/**
* checks if DNS records are set, will go through a max of 30 cycles
*/
async checkDns(cycleArg = 1) {
console.log(`checkDns failed ${cycleArg} times and has ${30 - cycleArg} cycles to go before it fails permanently!`)
let myRecord
try {
myRecord = await myDnsly.getRecord(helpers.prefixName(this.domainName), 'TXT')
console.log('DNS is set!')
} catch (err) {
if (cycleArg < 30) {
cycleArg++
await this.checkDns(cycleArg)
} else {
console.log('failed permanently...')
throw err
}
}
return myRecord[0][0]
}
/**
* validates a challenge, only call after you have set the challenge at the expected location
*/
requestValidation() {
let done = q.defer()
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.poll(this.acceptedChallenge.uri, (err, res) => {
if (err) {
console.log(err)
done.reject(err)
}
console.log(`Validation response:`)
console.log(JSON.stringify(res.body))
if (res.body.status === 'pending' || 'invalid') {
setTimeout(
() => {
this.requestValidation().then(x => { done.resolve(x) })
},
2000
)
} else {
done.resolve(res.body)
}
})
return done.promise
}
/**
* 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
*/
private acceptChallenge(challengeArg: ISmartAcmeChallenge) {
let done = q.defer()
/**
* the key is needed to accept the challenge
*/
let authKey: string = plugins.rawacme.keyAuthz(
challengeArg.token,
this.parentAcmeAccount.parentSmartAcme.keyPair.publicKey
)
/**
* needed in case selected challenge is of type dns-01
*/
let keyHash: string = plugins.rawacme.dnsKeyAuthzHash(authKey) // needed if dns challenge is chosen
this.parentAcmeAccount.parentSmartAcme.rawacmeClient.post(
challengeArg.uri,
{
resource: 'challenge',
keyAuthorization: authKey
},
this.parentAcmeAccount.parentSmartAcme.keyPair,
(err, res) => {
if (err) {
console.log(err)
done.reject(err)
}
/**
* the return challenge
*/
let returnDNSChallenge: ISmartAcmeChallengeAccepted = {
uri: res.body.uri,
type: res.body.type,
token: res.body.token,
keyAuthorization: res.body.keyAuthorization,
status: res.body.status,
dnsKeyHash: keyHash,
domainName: this.domainName,
domainNamePrefixed: helpers.prefixName(this.domainName)
}
this.acceptedChallenge = returnDNSChallenge
done.resolve(returnDNSChallenge)
}
)
return done.promise
}
}

View File

@ -1,53 +0,0 @@
import 'typings-global'
import * as q from 'q'
let rsaKeygen = require('rsa-keygen')
import { SmartAcme } from './smartacme.classes.smartacme'
export interface IRsaKeypair {
publicKey: string
privateKey: string
}
export class SmartacmeHelper {
parentSmartAcme: SmartAcme
constructor(smartAcmeArg: SmartAcme) {
this.parentSmartAcme = smartAcmeArg
}
/**
* creates a keypair to use with requests and to generate JWK from
*/
createKeypair(bit = 2048): IRsaKeypair {
let result = rsaKeygen.generate(bit)
return {
publicKey: result.public_key,
privateKey: result.private_key
}
}
/**
* getReg
* @executes ASYNC
*/
getReg() {
let done = q.defer()
let body = { resource: 'reg' }
this.parentSmartAcme.rawacmeClient.post(
this.parentSmartAcme.location,
body, this.parentSmartAcme.keyPair,
(err, res) => {
if (err) {
console.error('smartacme: something went wrong:')
console.log(err)
done.reject(err)
return
}
console.log(JSON.stringify(res.body))
done.resolve()
}
)
return done.promise
}
}

View File

@ -1,51 +1,49 @@
import 'typings-global'
import * as q from 'q'
import * as path from 'path'
let rsaKeygen = require('rsa-keygen')
import * as smartfile from 'smartfile'
import * as smartstring from 'smartstring'
let rawacme = require('rawacme')
import * as paths from './smartacme.paths'
// third party modules
import * as q from 'q' // promises
import * as plugins from './smartacme.plugins'
import * as helpers from './smartacme.helpers'
import { SmartacmeHelper, IRsaKeypair } from './smartacme.classes.helper'
import { AcmeAccount } from './smartacme.classes.acmeaccount'
export type TChallenge = 'dns-01' | 'http-01'
/**
* a rsa keypair needed for account creation and subsequent requests
*/
export interface IRsaKeypair {
publicKey: string
privateKey: string
}
export { AcmeAccount } from './smartacme.classes.acmeaccount'
export { AcmeCert, ISmartAcmeChallenge, ISmartAcmeChallengeAccepted } from './smartacme.classes.acmecert'
/**
* class SmartAcme exports methods for maintaining SSL Certificates
*/
export class SmartAcme {
helper: SmartacmeHelper // bundles helper methods that would clutter the main SmartAcme class
acmeUrl: string // the acme url to use
acmeUrl: string // the acme url to use for this instance
productionBool: boolean // a boolean to quickly know wether we are in production or not
keyPair: IRsaKeypair // the keyPair needed for account creation
location: string
link: string
rawacmeClient
JWK
/**
* the constructor for class SmartAcme
*/
constructor(productionArg: boolean = false) {
this.productionBool = productionArg
this.helper = new SmartacmeHelper(this)
this.keyPair = this.helper.createKeypair()
this.keyPair = helpers.createKeypair()
if (this.productionBool) {
this.acmeUrl = rawacme.LETSENCRYPT_URL
this.acmeUrl = plugins.rawacme.LETSENCRYPT_URL
} else {
this.acmeUrl = rawacme.LETSENCRYPT_STAGING_URL
this.acmeUrl = plugins.rawacme.LETSENCRYPT_STAGING_URL
}
}
/**
* creates an account if not currently present in module
* @executes ASYNC
* init the smartacme instance
*/
createAccount() {
init() {
let done = q.defer()
rawacme.createClient(
plugins.rawacme.createClient(
{
url: this.acmeUrl,
publicKey: this.keyPair.publicKey,
@ -61,118 +59,24 @@ export class SmartAcme {
// 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: string, challengeTypeArg: TChallenge = '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
* creates an account if not currently present in module
* @executes ASYNC
*/
getCertificate() {
}
/**
* accept a challenge - for private use only
*/
private acceptChallenge(challenge) {
let done = q.defer()
let authKey: string = rawacme.keyAuthz(challenge.token, this.keyPair.publicKey)
let dnsKeyHash: string = rawacme.dnsKeyAuthzHash(authKey) // needed if dns challenge is chosen
console.log(authKey)
this.rawacmeClient.post(
challenge.uri,
{
resource: 'challenge',
keyAuthorization: authKey
},
this.keyPair,
(err, res) => {
if (err) {
console.log(err)
done.reject(err)
}
console.log('acceptChallenge:')
console.log(JSON.stringify(res.body))
done.resolve(dnsKeyHash)
}
)
createAcmeAccount() {
let done = q.defer<AcmeAccount>()
let acmeAccount = new AcmeAccount(this)
acmeAccount.register().then(() => {
return acmeAccount.agreeTos()
}).then(() => {
done.resolve(acmeAccount)
})
return done.promise
}
}

50
ts/smartacme.helpers.ts Normal file
View File

@ -0,0 +1,50 @@
import 'typings-global'
import * as q from 'q'
import * as plugins from './smartacme.plugins'
import { SmartAcme, IRsaKeypair } from './smartacme.classes.smartacme'
import { AcmeAccount } from './smartacme.classes.acmeaccount'
/**
* creates a keypair to use with requests and to generate JWK from
*/
export let createKeypair = (bit = 2048): IRsaKeypair => {
let result = plugins.rsaKeygen.generate(bit)
return {
publicKey: result.public_key,
privateKey: result.private_key
}
}
/**
* prefix a domain name to make sure it complies with letsencrypt
*/
export let prefixName = (domainNameArg: string): string => {
return '_acme-challenge.' + domainNameArg
}
/**
* gets an existing registration
* @executes ASYNC
*/
let getReg = (SmartAcmeArg: SmartAcme, location: string) => {
let done = q.defer()
let body = { resource: 'reg' }
SmartAcmeArg.rawacmeClient.post(
location,
body,
SmartAcmeArg.keyPair,
(err, res) => {
if (err) {
console.error('smartacme: something went wrong:')
console.log(err)
done.reject(err)
return
}
console.log(JSON.stringify(res.body))
done.resolve()
}
)
return done.promise
}

20
ts/smartacme.plugins.ts Normal file
View File

@ -0,0 +1,20 @@
import 'typings-global' // typings for node
import * as path from 'path' // native node path module
let rsaKeygen = require('rsa-keygen') // rsa keygen
let rawacme = require('rawacme') // acme helper functions
let nodeForge = require('node-forge')
// push.rocks modules here
import * as dnsly from 'dnsly'
import * as smartfile from 'smartfile'
import * as smartstring from 'smartstring'
export {
dnsly,
rsaKeygen,
rawacme,
nodeForge,
smartfile,
smartstring
}