Compare commits
58 Commits
Author | SHA1 | Date | |
---|---|---|---|
a9f709ee7b | |||
1b11b637a5 | |||
ad54bf41ea | |||
060ebf1b29 | |||
a87c6acb8a | |||
62d27619f4 | |||
0faebf2a79 | |||
29ea50796c | |||
26d1b7cbf0 | |||
c0c97835ea | |||
d4d50b7dcf | |||
2492fd4de2 | |||
bef54799b6 | |||
dbe09f320a | |||
18045dadaf | |||
ee300c3e12 | |||
ed4ba0cb61 | |||
a8ab27045d | |||
975c3ed190 | |||
a99dea549b | |||
f8b78c433a | |||
6c33111074 | |||
280335f6f6 | |||
b90092c043 | |||
9e1c73febf | |||
dcf1915816 | |||
748c911168 | |||
3a48cb4ea8 | |||
a035c5c0b0 | |||
f9c521b7b3 | |||
19cfe8bdc5 | |||
601d6b30d3 | |||
57ffc82c43 | |||
312d3c01cd | |||
8814c1fc62 | |||
223a47c997 | |||
651ef6d281 | |||
9eda0da9a7 | |||
3e350dfed5 | |||
6fc280e168 | |||
a9efae65d6 | |||
0f09bdaf9f | |||
84177cd575 | |||
7d16ada760 | |||
b4de8cc2be | |||
68e570c32a | |||
20ea599f9d | |||
5fa530456b | |||
2cd1794e7e | |||
1f38e12bd3 | |||
1c777f6f05 | |||
aad113a8ea | |||
fff63839d1 | |||
c8d2cfd4ce | |||
dfd7edd330 | |||
4dadcf227c | |||
fce25c60ed | |||
98cc70dbfb |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ node_modules/
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
.nogit/
|
||||
|
133
.gitlab-ci.yml
133
.gitlab-ci.yml
@ -1,59 +1,150 @@
|
||||
image: hosttoday/ht-docker-node:npmts
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
|
||||
stages:
|
||||
- security
|
||||
- test
|
||||
- release
|
||||
- trigger
|
||||
- pages
|
||||
- metadata
|
||||
|
||||
testLEGACY:
|
||||
stage: test
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci test legacy
|
||||
- npmci git mirror
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
sast:
|
||||
stage: security
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-dbase:npmci
|
||||
variables:
|
||||
DOCKER_DRIVER: overlay2
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SAST_CONFIDENCE_LEVEL="${SAST_CONFIDENCE_LEVEL:-3}"
|
||||
--volume "$PWD:/code"
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/sast:$SP_VERSION" /app/bin/run /code
|
||||
artifacts:
|
||||
reports:
|
||||
sast: gl-sast-report.json
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test lts
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
stage: test
|
||||
script:
|
||||
- npmci test stable
|
||||
- npmci npm prepare
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci publish
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
# ====================
|
||||
# metadata stage
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--volume "$PWD":/code
|
||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
||||
artifacts:
|
||||
paths: [codeclimate.json]
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
|
||||
trigger:
|
||||
stage: trigger
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci trigger
|
||||
- npmci trigger
|
||||
only:
|
||||
- tags
|
||||
- tags
|
||||
tags:
|
||||
- docker
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmpage
|
||||
stage: pages
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npmpage --host gitlab
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
- tags
|
||||
artifacts:
|
||||
expire_in: 1 week
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
4
.npmignore
Normal file
4
.npmignore
Normal file
@ -0,0 +1,4 @@
|
||||
node_modules/
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
12
.snyk
Normal file
12
.snyk
Normal file
@ -0,0 +1,12 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.12.0
|
||||
# ignores vulnerabilities until expiry date; change duration by modifying expiry date
|
||||
ignore:
|
||||
'npm:node-forge:20180226':
|
||||
- rsa-compat > node-forge:
|
||||
reason: None given
|
||||
expires: '2018-09-11T19:17:24.148Z'
|
||||
- acme-v2 > rsa-compat > node-forge:
|
||||
reason: None given
|
||||
expires: '2018-09-11T19:17:24.148Z'
|
||||
patch: {}
|
61
README.md
61
README.md
@ -1,36 +1,63 @@
|
||||
# smartacme
|
||||
|
||||
acme implementation in TypeScript
|
||||
|
||||
## Availabililty
|
||||
[](https://www.npmjs.com/package/smartacme)
|
||||
[](https://GitLab.com/pushrocks/smartacme)
|
||||
[](https://github.com/pushrocks/smartacme)
|
||||
[](https://pushrocks.gitlab.io/smartacme/)
|
||||
|
||||
[](https://www.npmjs.com/package/smartacme)
|
||||
[](https://GitLab.com/umbrellazone/smartacme)
|
||||
[](https://github.com/umbrellazone/smartacme)
|
||||
[](https://umbrellazone.gitlab.io/smartacme/)
|
||||
|
||||
## Status for master
|
||||
[](https://GitLab.com/pushrocks/smartacme/commits/master)
|
||||
[](https://GitLab.com/pushrocks/smartacme/commits/master)
|
||||
|
||||
[](https://GitLab.com/umbrellazone/smartacme/commits/master)
|
||||
[](https://GitLab.com/umbrellazone/smartacme/commits/master)
|
||||
[](https://www.npmjs.com/package/smartacme)
|
||||
[](https://david-dm.org/pushrocks/smartacme)
|
||||
[](https://www.bithound.io/github/pushrocks/smartacme/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/pushrocks/smartacme)
|
||||
[](https://david-dm.org/umbrellazone/smartacme)
|
||||
[](https://www.bithound.io/github/umbrellazone/smartacme/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/umbrellazone/smartacme)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](http://standardjs.com/)
|
||||
|
||||
## Usage
|
||||
|
||||
Use TypeScript for best in class instellisense.
|
||||
|
||||
```javascript
|
||||
import { SmartAcme } from 'smartacme'
|
||||
import { SmartAcme } from 'smartacme';
|
||||
|
||||
let smac = new SmartAcme()
|
||||
let smac = new SmartAcme()(async () => {
|
||||
// learn async/await, it'll make your life easier
|
||||
|
||||
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
|
||||
})
|
||||
// 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
|
||||
-------------*/
|
||||
})();
|
||||
```
|
||||
|
||||
[](https://push.rocks)
|
||||
## Other relevant npm modules
|
||||
|
||||
| module name | description |
|
||||
| ----------- | ------------------------------------------------------------------- |
|
||||
| cert | a higlevel production module that uses smartacme to manage certs |
|
||||
| smartnginx | a highlevel production tool for docker environments to manage nginx |
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://umbrella.zone
|
||||
|
1
dist/index.d.ts
vendored
1
dist/index.d.ts
vendored
@ -1 +0,0 @@
|
||||
export * from './smartacme.classes.smartacme';
|
6
dist/index.js
vendored
6
dist/index.js
vendored
@ -1,6 +0,0 @@
|
||||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
__export(require("./smartacme.classes.smartacme"));
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBQUEsbURBQTZDIn0=
|
0
dist/smartacme.classes.acmeaccount.d.ts
vendored
0
dist/smartacme.classes.acmeaccount.d.ts
vendored
1
dist/smartacme.classes.acmeaccount.js
vendored
1
dist/smartacme.classes.acmeaccount.js
vendored
@ -1 +0,0 @@
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuYWNtZWFjY291bnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuY2xhc3Nlcy5hY21lYWNjb3VudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
|
0
dist/smartacme.classes.acmecert.d.ts
vendored
0
dist/smartacme.classes.acmecert.d.ts
vendored
1
dist/smartacme.classes.acmecert.js
vendored
1
dist/smartacme.classes.acmecert.js
vendored
@ -1 +0,0 @@
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuYWNtZWNlcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGFjbWUuY2xhc3Nlcy5hY21lY2VydC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
|
21
dist/smartacme.classes.helper.d.ts
vendored
21
dist/smartacme.classes.helper.d.ts
vendored
@ -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<{}>;
|
||||
}
|
40
dist/smartacme.classes.helper.js
vendored
40
dist/smartacme.classes.helper.js
vendored
@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLmNsYXNzZXMuaGVscGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhY21lLmNsYXNzZXMuaGVscGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwwQkFBdUI7QUFDdkIsdUJBQXNCO0FBQ3RCLElBQUksU0FBUyxHQUFHLE9BQU8sQ0FBQyxZQUFZLENBQUMsQ0FBQTtBQVNyQztJQUdJLFlBQVksWUFBdUI7UUFDL0IsSUFBSSxDQUFDLGVBQWUsR0FBRyxZQUFZLENBQUE7SUFDdkMsQ0FBQztJQUVEOztPQUVHO0lBQ0gsYUFBYSxDQUFDLEdBQUcsR0FBRyxJQUFJO1FBQ3BCLElBQUksTUFBTSxHQUFHLFNBQVMsQ0FBQyxRQUFRLENBQUMsR0FBRyxDQUFDLENBQUE7UUFDcEMsTUFBTSxDQUFDO1lBQ0gsU0FBUyxFQUFFLE1BQU0sQ0FBQyxVQUFVO1lBQzVCLFVBQVUsRUFBRSxNQUFNLENBQUMsV0FBVztTQUNqQyxDQUFBO0lBQ0wsQ0FBQztJQUVEOzs7T0FHRztJQUNILE1BQU07UUFDRixJQUFJLElBQUksR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDcEIsSUFBSSxJQUFJLEdBQUcsRUFBRSxRQUFRLEVBQUUsS0FBSyxFQUFFLENBQUE7UUFDOUIsSUFBSSxDQUFDLGVBQWUsQ0FBQyxhQUFhLENBQUMsSUFBSSxDQUNuQyxJQUFJLENBQUMsZUFBZSxDQUFDLFFBQVEsRUFDN0IsSUFBSSxFQUFFLElBQUksQ0FBQyxlQUFlLENBQUMsT0FBTyxFQUNsQyxDQUFDLEdBQUcsRUFBRSxHQUFHO1lBQ0wsRUFBRSxDQUFDLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztnQkFDTixPQUFPLENBQUMsS0FBSyxDQUFDLGtDQUFrQyxDQUFDLENBQUE7Z0JBQ2pELE9BQU8sQ0FBQyxHQUFHLENBQUMsR0FBRyxDQUFDLENBQUE7Z0JBQ2hCLElBQUksQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQUE7Z0JBQ2hCLE1BQU0sQ0FBQTtZQUNWLENBQUM7WUFDRCxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUE7WUFDckMsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FDSixDQUFBO1FBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztDQUNKO0FBekNELDBDQXlDQyJ9
|
42
dist/smartacme.classes.smartacme.d.ts
vendored
42
dist/smartacme.classes.smartacme.d.ts
vendored
@ -1,42 +0,0 @@
|
||||
/// <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';
|
||||
/**
|
||||
* 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);
|
||||
/**
|
||||
* 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);
|
||||
}
|
136
dist/smartacme.classes.smartacme.js
vendored
136
dist/smartacme.classes.smartacme.js
vendored
File diff suppressed because one or more lines are too long
2
dist/smartacme.paths.d.ts
vendored
2
dist/smartacme.paths.d.ts
vendored
@ -1,2 +0,0 @@
|
||||
export declare let packageDir: string;
|
||||
export declare let assetDir: string;
|
7
dist/smartacme.paths.js
vendored
7
dist/smartacme.paths.js
vendored
@ -1,7 +0,0 @@
|
||||
"use strict";
|
||||
const path = require("path");
|
||||
const smartfile = require("smartfile");
|
||||
exports.packageDir = path.join(__dirname, '../');
|
||||
exports.assetDir = path.join(exports.packageDir, 'assets/');
|
||||
smartfile.fs.ensureDirSync(exports.assetDir);
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhY21lLnBhdGhzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRhY21lLnBhdGhzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw2QkFBNEI7QUFDNUIsdUNBQXNDO0FBRTNCLFFBQUEsVUFBVSxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsU0FBUyxFQUFDLEtBQUssQ0FBQyxDQUFBO0FBQ3ZDLFFBQUEsUUFBUSxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsa0JBQVUsRUFBQyxTQUFTLENBQUMsQ0FBQTtBQUNyRCxTQUFTLENBQUMsRUFBRSxDQUFDLGFBQWEsQ0FBQyxnQkFBUSxDQUFDLENBQUEifQ==
|
@ -1,7 +1,6 @@
|
||||
{
|
||||
"npmci": {
|
||||
"globalNpmTools": [
|
||||
"npmts"
|
||||
]
|
||||
}
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
2578
package-lock.json
generated
Normal file
2578
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
43
package.json
43
package.json
@ -1,15 +1,17 @@
|
||||
{
|
||||
"name": "smartacme",
|
||||
"version": "1.0.5",
|
||||
"name": "@pushrocks/smartacme",
|
||||
"version": "2.0.14",
|
||||
"private": false,
|
||||
"description": "acme implementation in TypeScript",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "(npmts --nodocs)"
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@gitlab.com/pushrocks/smartacme.git"
|
||||
"url": "git+ssh://git@gitlab.com/umbrellazone/smartacme.git"
|
||||
},
|
||||
"keywords": [
|
||||
"TypeScript",
|
||||
@ -19,21 +21,30 @@
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://gitlab.com/pushrocks/smartacme/issues"
|
||||
"url": "https://gitlab.com/umbrellazone/smartacme/issues"
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartacme#README",
|
||||
"homepage": "https://gitlab.com/umbrellazone/smartacme#README",
|
||||
"dependencies": {
|
||||
"@types/q": "0.x.x",
|
||||
"q": "^1.4.1",
|
||||
"rawacme": "^0.2.1",
|
||||
"rsa-keygen": "^1.0.6",
|
||||
"smartfile": "^4.1.0",
|
||||
"smartstring": "^2.0.20",
|
||||
"typings-global": "^1.0.14"
|
||||
"@pushrocks/lik": "^3.0.4",
|
||||
"@pushrocks/smartdata": "^3.1.13",
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smartdns": "^3.0.8",
|
||||
"@pushrocks/smartexpress": "^3.0.0",
|
||||
"@pushrocks/smartpromise": "^2.0.5",
|
||||
"@pushrocks/smartrequest": "^1.1.14",
|
||||
"@pushrocks/smarttime": "^3.0.5",
|
||||
"@pushrocks/smartunique": "^3.0.1",
|
||||
"acme-client": "^2.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/should": "^8.1.30",
|
||||
"should": "^11.1.1",
|
||||
"typings-test": "^1.0.3"
|
||||
"@gitzone/tsbuild": "^2.1.4",
|
||||
"@gitzone/tsrun": "^1.1.17",
|
||||
"@gitzone/tstest": "^1.0.18",
|
||||
"@mojoio/cloudflare": "^2.0.0",
|
||||
"@pushrocks/qenv": "^3.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.18",
|
||||
"tslint": "^5.12.0",
|
||||
"tslint-config-prettier": "^1.17.0"
|
||||
}
|
||||
}
|
||||
|
6
qenv.yml
Normal file
6
qenv.yml
Normal file
@ -0,0 +1,6 @@
|
||||
required:
|
||||
- CF_EMAIL
|
||||
- CF_KEY
|
||||
- MONGODB_URL
|
||||
- MONGODB_PASSWORD
|
||||
- MONGODB_DATABASE
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
||||
import 'typings-test';
|
38
test/test.js
38
test/test.js
@ -1,38 +0,0 @@
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
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 () {
|
||||
this.timeout(10000);
|
||||
testAcme = new smartacme.SmartAcme();
|
||||
should(testAcme).be.instanceOf(smartacme.SmartAcme);
|
||||
});
|
||||
it('should have created keyPair', function () {
|
||||
should(testAcme.acmeUrl).be.of.type('string');
|
||||
});
|
||||
it('should register a new account', function (done) {
|
||||
this.timeout(10000);
|
||||
testAcme.createAccount().then(x => {
|
||||
done();
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
it('should agree to ToS', function (done) {
|
||||
this.timeout(10000);
|
||||
testAcme.agreeTos().then(() => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should request a challenge for a domain', function (done) {
|
||||
this.timeout(10000);
|
||||
testAcme.requestChallenge('bleu.de').then(() => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMsNEJBQTRCO0FBQzVCLDJDQUEwQztBQUUxQyxRQUFRLENBQUMsV0FBVyxFQUFFO0lBQ2xCLElBQUksUUFBNkIsQ0FBQTtJQUVqQyxFQUFFLENBQUMsZ0NBQWdDLEVBQUU7UUFDakMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQTtRQUNuQixRQUFRLEdBQUcsSUFBSSxTQUFTLENBQUMsU0FBUyxFQUFFLENBQUE7UUFDcEMsTUFBTSxDQUFDLFFBQVEsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsU0FBUyxDQUFDLFNBQVMsQ0FBQyxDQUFBO0lBQ3ZELENBQUMsQ0FBQyxDQUFBO0lBRUYsRUFBRSxDQUFDLDZCQUE2QixFQUFFO1FBQzlCLE1BQU0sQ0FBQyxRQUFRLENBQUMsT0FBTyxDQUFDLENBQUMsRUFBRSxDQUFDLEVBQUUsQ0FBQyxJQUFJLENBQUMsUUFBUSxDQUFDLENBQUE7SUFDakQsQ0FBQyxDQUFDLENBQUE7SUFFRixFQUFFLENBQUMsK0JBQStCLEVBQUUsVUFBVSxJQUFJO1FBQzlDLElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7UUFDbkIsUUFBUSxDQUFDLGFBQWEsRUFBRSxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzNCLElBQUksRUFBRSxDQUFBO1FBQ1YsQ0FBQyxDQUFDLENBQUMsS0FBSyxDQUFDLEdBQUc7WUFDUixPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFBO1lBQ2hCLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQTtRQUNiLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7SUFFRixFQUFFLENBQUMscUJBQXFCLEVBQUUsVUFBUyxJQUFJO1FBQ25DLElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7UUFDbkIsUUFBUSxDQUFDLFFBQVEsRUFBRSxDQUFDLElBQUksQ0FBQztZQUNyQixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7SUFFRixFQUFFLENBQUMseUNBQXlDLEVBQUUsVUFBUyxJQUFJO1FBQ3ZELElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUE7UUFDbkIsUUFBUSxDQUFDLGdCQUFnQixDQUFDLFNBQVMsQ0FBQyxDQUFDLElBQUksQ0FBQztZQUN0QyxJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9
|
66
test/test.ts
66
test/test.ts
@ -1,43 +1,33 @@
|
||||
import 'typings-test'
|
||||
import * as should from 'should'
|
||||
import { tap, expect } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
|
||||
// import the module to test
|
||||
import * as smartacme from '../dist/index'
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
describe('smartacme', function () {
|
||||
let testAcme: smartacme.SmartAcme
|
||||
import * as smartacme from '../ts/index';
|
||||
|
||||
it('should create a valid instance', function () {
|
||||
this.timeout(10000)
|
||||
testAcme = new smartacme.SmartAcme()
|
||||
should(testAcme).be.instanceOf(smartacme.SmartAcme)
|
||||
})
|
||||
let smartAcmeInstance: smartacme.SmartAcme;
|
||||
|
||||
it('should have created keyPair', function () {
|
||||
should(testAcme.acmeUrl).be.of.type('string')
|
||||
})
|
||||
tap.test('should create a valid instance of SmartAcme', async () => {
|
||||
smartAcmeInstance = new smartacme.SmartAcme({
|
||||
accountEmail: 'domains@lossless.org',
|
||||
accountPrivateKey: null,
|
||||
mongoDescriptor: {
|
||||
mongoDbName: testQenv.getEnvVarOnDemand('MONGODB_DATABASE'),
|
||||
mongoDbPass: testQenv.getEnvVarOnDemand('MONGODB_PASSWORD'),
|
||||
mongoDbUrl: testQenv.getEnvVarOnDemand('MONGODB_URL')
|
||||
},
|
||||
removeChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
setChallenge: async (...args) => {
|
||||
console.log(args);
|
||||
},
|
||||
validateRemoteRequest: async () => {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
await smartAcmeInstance.init();
|
||||
// await smartAcmeInstance.getCertificateForDomain('bleu.de');
|
||||
});
|
||||
|
||||
it('should register a new account', function (done) {
|
||||
this.timeout(10000)
|
||||
testAcme.createAccount().then(x => {
|
||||
done()
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('should agree to ToS', function(done) {
|
||||
this.timeout(10000)
|
||||
testAcme.agreeTos().then(() => {
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should request a challenge for a domain', function(done) {
|
||||
this.timeout(10000)
|
||||
testAcme.requestChallenge('bleu.de').then(() => {
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
tap.start();
|
||||
|
@ -1 +1,3 @@
|
||||
export * from './smartacme.classes.smartacme'
|
||||
export * from './smartacme.classes.smartacme';
|
||||
|
||||
export * from './smartacme.classes.certremoteclient';
|
||||
|
8
ts/interfaces/accountdata.ts
Normal file
8
ts/interfaces/accountdata.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export interface IAccountData {
|
||||
id: number;
|
||||
key: { kty: 'RSA'; n: string; e: string; kid: string };
|
||||
contact: string[];
|
||||
initialIp: string;
|
||||
createdAt: string;
|
||||
status: string;
|
||||
}
|
9
ts/interfaces/cert.ts
Normal file
9
ts/interfaces/cert.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export type TCertStatus = 'existing' | 'nonexisting' | 'pending' | 'failed';
|
||||
|
||||
export interface ICert {
|
||||
domainName: string;
|
||||
created: number;
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
csr: string;
|
||||
}
|
11
ts/interfaces/certremote.ts
Normal file
11
ts/interfaces/certremote.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { ICert, TCertStatus } from './cert';
|
||||
|
||||
export interface ICertRemoteRequest {
|
||||
secret: string;
|
||||
domainName: string;
|
||||
}
|
||||
|
||||
export interface ICertRemoteResponse {
|
||||
status: TCertStatus;
|
||||
certificate?: ICert;
|
||||
}
|
3
ts/interfaces/index.ts
Normal file
3
ts/interfaces/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './accountdata';
|
||||
export * from './cert';
|
||||
export * from './certremote';
|
@ -1,5 +0,0 @@
|
||||
import 'typings-global'
|
||||
|
||||
export class AcmeAccount {
|
||||
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
import 'typings-global'
|
||||
|
||||
export class AcmeCert {
|
||||
|
||||
}
|
40
ts/smartacme.classes.cert.ts
Normal file
40
ts/smartacme.classes.cert.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
|
||||
import { Collection, svDb, unI } from '@pushrocks/smartdata';
|
||||
import { ICert } from './interfaces';
|
||||
|
||||
@plugins.smartdata.Collection(() => {
|
||||
return CertManager.activeDB;
|
||||
})
|
||||
export class Cert extends plugins.smartdata.SmartDataDbDoc<Cert> implements interfaces.ICert {
|
||||
@unI()
|
||||
public index: string;
|
||||
|
||||
@svDb()
|
||||
public domainName: string;
|
||||
|
||||
@svDb()
|
||||
public created: number;
|
||||
|
||||
@svDb()
|
||||
public privateKey: string;
|
||||
|
||||
@svDb()
|
||||
public publicKey: string;
|
||||
|
||||
@svDb()
|
||||
public csr: string;
|
||||
|
||||
constructor(optionsArg: ICert) {
|
||||
super();
|
||||
this.created = optionsArg.created;
|
||||
this.domainName = optionsArg.domainName;
|
||||
this.privateKey = optionsArg.privateKey;
|
||||
this.publicKey = optionsArg.publicKey;
|
||||
this.csr = optionsArg.csr;
|
||||
}
|
||||
}
|
93
ts/smartacme.classes.certmanager.ts
Normal file
93
ts/smartacme.classes.certmanager.ts
Normal file
@ -0,0 +1,93 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import { Cert } from './smartacme.classes.cert';
|
||||
import { SmartAcme } from './smartacme.classes.smartacme';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
import { ICert } from './interfaces';
|
||||
|
||||
|
||||
export class CertManager {
|
||||
// =========
|
||||
// STATIC
|
||||
// =========
|
||||
public static activeDB: plugins.smartdata.SmartdataDb;
|
||||
|
||||
|
||||
// =========
|
||||
// INSTANCE
|
||||
// =========
|
||||
private mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
public smartdataDb: plugins.smartdata.SmartdataDb;
|
||||
|
||||
public pendingMap: plugins.lik.Stringmap;
|
||||
|
||||
constructor(smartAcmeArg: SmartAcme,optionsArg: {
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
}) {
|
||||
this.mongoDescriptor = optionsArg.mongoDescriptor;
|
||||
}
|
||||
|
||||
public async init () {
|
||||
// Smartdata DB
|
||||
this.smartdataDb = new plugins.smartdata.SmartdataDb(this.mongoDescriptor);
|
||||
await this.smartdataDb.init();
|
||||
CertManager.activeDB = this.smartdataDb;
|
||||
|
||||
// Pending Map
|
||||
this.pendingMap = new plugins.lik.Stringmap();
|
||||
};
|
||||
|
||||
/**
|
||||
* retrieves a certificate
|
||||
* @returns the Cert class or null
|
||||
* @param domainName the domain Name to retrieve the vcertificate for
|
||||
*/
|
||||
public async retrieveCertificate(domainName: string): Promise<Cert> {
|
||||
await this.checkCerts();
|
||||
const existingCertificate: Cert = await Cert.getInstance({
|
||||
name: domainName
|
||||
});
|
||||
|
||||
if(existingCertificate) {
|
||||
return existingCertificate;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* stores the certificate with the
|
||||
* @param publicKeyArg
|
||||
* @param privateKeyArg
|
||||
* @param csrArg
|
||||
*/
|
||||
public async storeCertificate(optionsArg: ICert) {
|
||||
const cert = new Cert(optionsArg);
|
||||
cert.save();
|
||||
};
|
||||
|
||||
public async deleteCertificate(domainNameArg: string) {
|
||||
|
||||
}
|
||||
|
||||
public async getCertificateStatus(domainNameArg: string): Promise<interfaces.TCertStatus> {
|
||||
const isPending = this.pendingMap.checkString('domainNameArg');
|
||||
if (isPending) {
|
||||
return 'pending';
|
||||
}
|
||||
|
||||
// otherwise lets continue
|
||||
const existingCertificate = this.retrieveCertificate(domainNameArg);
|
||||
if (existingCertificate) {
|
||||
return 'existing';
|
||||
}
|
||||
|
||||
return 'nonexisting';
|
||||
}
|
||||
|
||||
/**
|
||||
* checks all certs for expiration
|
||||
*/
|
||||
private async checkCerts() {};
|
||||
}
|
47
ts/smartacme.classes.certremoteclient.ts
Normal file
47
ts/smartacme.classes.certremoteclient.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import { ICertRemoteResponse } from './interfaces';
|
||||
|
||||
// tslint:disable-next-line: max-classes-per-file
|
||||
export class CertRemoteClient {
|
||||
private remoteUrl: string;
|
||||
private secret: string;
|
||||
|
||||
constructor(optionsArg: {
|
||||
remoteUrl: string;
|
||||
secret: string;
|
||||
}) {
|
||||
this.remoteUrl = optionsArg.remoteUrl;
|
||||
this.secret = optionsArg.secret;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param domainNameArg
|
||||
*/
|
||||
async getCertificateForDomain(domainNameArg: string): Promise<interfaces.ICert> {
|
||||
let certificate: interfaces.ICert;
|
||||
const doRequestCycle = async (): Promise<interfaces.ICert> => {
|
||||
const response: ICertRemoteResponse = (await plugins.smartrequest.postJson(this.remoteUrl, {
|
||||
requestBody: <interfaces.ICertRemoteRequest>{
|
||||
domainName: domainNameArg,
|
||||
secret: this.secret
|
||||
}
|
||||
})).body;
|
||||
switch(response.status) {
|
||||
case 'pending':
|
||||
await plugins.smartdelay.delayFor(5000);
|
||||
const finalResponse = await doRequestCycle();
|
||||
return finalResponse;
|
||||
case 'existing':
|
||||
return response.certificate;
|
||||
case 'failed':
|
||||
default:
|
||||
console.log(`could not retrieve certificate for ${domainNameArg}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
certificate = await doRequestCycle();
|
||||
return certificate;
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -1,178 +1,181 @@
|
||||
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'
|
||||
|
||||
import { SmartacmeHelper, IRsaKeypair } from './smartacme.classes.helper'
|
||||
|
||||
export type TChallenge = 'dns-01' | 'http-01'
|
||||
import * as plugins from './smartacme.plugins';
|
||||
import { CertManager } from './smartacme.classes.certmanager';
|
||||
|
||||
import * as interfaces from './interfaces';
|
||||
import { request } from 'http';
|
||||
|
||||
/**
|
||||
* 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
|
||||
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()
|
||||
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: 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
|
||||
*/
|
||||
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)
|
||||
}
|
||||
)
|
||||
return done.promise
|
||||
}
|
||||
|
||||
|
||||
export interface ISmartAcmeOptions {
|
||||
accountPrivateKey?: string;
|
||||
accountEmail: string;
|
||||
mongoDescriptor: plugins.smartdata.IMongoDescriptor;
|
||||
setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
removeChallenge: (domainName: string) => Promise<any>;
|
||||
validateRemoteRequest: () => Promise<boolean>;
|
||||
}
|
||||
|
||||
export class SmartAcme {
|
||||
private options: ISmartAcmeOptions;
|
||||
|
||||
// the acme client
|
||||
private client: any;
|
||||
private smartdns = new plugins.smartdns.Smartdns();
|
||||
|
||||
// the account private key
|
||||
private privateKey: string;
|
||||
|
||||
// challenge fullfillment
|
||||
private setChallenge: (domainName: string, keyAuthorization: string) => Promise<any>;
|
||||
private removeChallenge: (domainName: string) => Promise<any>;
|
||||
private validateRemoteRequest: () => Promise<boolean>;
|
||||
|
||||
// certmanager
|
||||
private certmanager: CertManager;
|
||||
private certremoteHandler: plugins.smartexpress.Handler;
|
||||
|
||||
constructor(optionsArg: ISmartAcmeOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* inits the instance
|
||||
* ```ts
|
||||
* await myCloudlyInstance.init() // does not support options
|
||||
* ```
|
||||
*/
|
||||
public async init() {
|
||||
this.privateKey =
|
||||
this.options.accountPrivateKey || (await plugins.acme.forge.createPrivateKey());
|
||||
this.setChallenge = this.options.setChallenge;
|
||||
this.removeChallenge = this.options.removeChallenge;
|
||||
|
||||
// CertMangaer
|
||||
this.certmanager = new CertManager(this, {
|
||||
mongoDescriptor: this.options.mongoDescriptor
|
||||
});
|
||||
await this.certmanager.init();
|
||||
|
||||
// CertRemoteHandler
|
||||
this.certremoteHandler = new plugins.smartexpress.Handler('POST', async (req, res) => {
|
||||
const requestBody: interfaces.ICertRemoteRequest = req.body;
|
||||
const status: interfaces.TCertStatus = await this.certmanager.getCertificateStatus(requestBody.domainName);
|
||||
const existingCertificate = await this.certmanager.retrieveCertificate(
|
||||
requestBody.domainName
|
||||
);
|
||||
let response: interfaces.ICertRemoteResponse;
|
||||
switch (status) {
|
||||
case 'existing':
|
||||
response = {
|
||||
status,
|
||||
certificate: {
|
||||
created: existingCertificate.created,
|
||||
csr: existingCertificate.csr,
|
||||
domainName: existingCertificate.domainName,
|
||||
privateKey: existingCertificate.privateKey,
|
||||
publicKey: existingCertificate.publicKey
|
||||
}
|
||||
};
|
||||
break;
|
||||
default:
|
||||
response = {
|
||||
status
|
||||
};
|
||||
break;
|
||||
}
|
||||
res.status(200);
|
||||
res.send(response);
|
||||
res.end();
|
||||
});
|
||||
|
||||
// ACME Client
|
||||
this.client = new plugins.acme.Client({
|
||||
directoryUrl: plugins.acme.directory.letsencrypt.staging,
|
||||
accountKey: this.privateKey
|
||||
});
|
||||
|
||||
/* Register account */
|
||||
await this.client.createAccount({
|
||||
termsOfServiceAgreed: true,
|
||||
contact: [`mailto:${this.options.accountEmail}`]
|
||||
});
|
||||
}
|
||||
|
||||
public async stop() {};
|
||||
|
||||
public async getCertificateForDomain(domainArg: string) {
|
||||
const domain = domainArg;
|
||||
|
||||
const retrievedCertificate = await this.certmanager.retrieveCertificate(domain);
|
||||
|
||||
if (retrievedCertificate) {
|
||||
return retrievedCertificate;
|
||||
}
|
||||
|
||||
/* Place new order */
|
||||
const order = await this.client.createOrder({
|
||||
identifiers: [{ type: 'dns', value: domain }, { type: 'dns', value: `*.${domain}` }]
|
||||
});
|
||||
|
||||
/* Get authorizations and select challenges */
|
||||
const authorizations = await this.client.getAuthorizations(order);
|
||||
|
||||
for (const authz of authorizations) {
|
||||
console.log(authz);
|
||||
const domainDnsName: string = `_acme-challenge.${authz.identifier.value}`;
|
||||
const dnsChallenge: string = authz.challenges.find(challengeArg => {
|
||||
return challengeArg.type === 'dns-01';
|
||||
});
|
||||
// process.exit(1);
|
||||
const keyAuthorization: string = await this.client.getChallengeKeyAuthorization(dnsChallenge);
|
||||
|
||||
try {
|
||||
/* Satisfy challenge */
|
||||
await this.setChallenge(domainDnsName, keyAuthorization);
|
||||
await this.smartdns.checkUntilAvailable(domainDnsName, 'TXT', keyAuthorization, 100, 5000);
|
||||
|
||||
/* Verify that challenge is satisfied */
|
||||
await this.client.verifyChallenge(authz, dnsChallenge);
|
||||
|
||||
/* Notify ACME provider that challenge is satisfied */
|
||||
await this.client.completeChallenge(dnsChallenge);
|
||||
|
||||
/* Wait for ACME provider to respond with valid status */
|
||||
await this.client.waitForValidStatus(dnsChallenge);
|
||||
} finally {
|
||||
/* Clean up challenge response */
|
||||
try {
|
||||
await this.removeChallenge(domainDnsName);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Finalize order */
|
||||
const [key, csr] = await plugins.acme.forge.createCsr({
|
||||
commonName: `*.${domain}`,
|
||||
altNames: [domain]
|
||||
});
|
||||
|
||||
await this.client.finalizeOrder(order, csr);
|
||||
const cert = await this.client.getCertificate(order);
|
||||
|
||||
/* Done */
|
||||
console.log(`CSR:\n${csr.toString()}`);
|
||||
console.log(`Private key:\n${key.toString()}`);
|
||||
console.log(`Certificate:\n${cert.toString()}`);
|
||||
|
||||
await this.certmanager.storeCertificate({
|
||||
domainName: domainArg,
|
||||
privateKey: key.toString(),
|
||||
publicKey: cert.toString(),
|
||||
csr: csr.toString(),
|
||||
created: Date.now()
|
||||
});
|
||||
|
||||
const newCertificate = await this.certmanager.retrieveCertificate(domainArg);
|
||||
return newCertificate;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
import * as path from 'path'
|
||||
import * as smartfile from 'smartfile'
|
||||
|
||||
export let packageDir = path.join(__dirname,'../')
|
||||
export let assetDir = path.join(packageDir,'assets/')
|
||||
smartfile.fs.ensureDirSync(assetDir)
|
17
ts/smartacme.plugins.ts
Normal file
17
ts/smartacme.plugins.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// @pushrocks scope
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdata from '@pushrocks/smartdata';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartdns from '@pushrocks/smartdns';
|
||||
import * as smartexpress from '@pushrocks/smartexpress';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { lik, smartdata, smartdelay, smartdns, smartexpress, smartpromise, smartrequest, smartunique, smarttime };
|
||||
|
||||
// thirs party scope
|
||||
import * as acme from 'acme-client';
|
||||
|
||||
export { acme };
|
7
tsconfig.json
Normal file
7
tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"target": "es2017",
|
||||
"module": "commonjs"
|
||||
}
|
||||
}
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
||||
{
|
||||
"extends": "tslint-config-standard"
|
||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||
"rules": {
|
||||
"semicolon": [true, "always"],
|
||||
"no-console": false,
|
||||
"ordered-imports": false,
|
||||
"object-literal-sort-keys": false,
|
||||
"member-ordering": {
|
||||
"options":{
|
||||
"order": [
|
||||
"static-method"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"defaultSeverity": "warning"
|
||||
}
|
||||
|
Reference in New Issue
Block a user