smartacme/ts/smartacme.classes.smartacme.ts

112 lines
2.9 KiB
TypeScript
Raw Normal View History

2016-11-11 13:17:50 +00:00
import 'typings-global'
import * as q from 'q'
import * as path from 'path'
import * as smartfile from 'smartfile'
import * as smartstring from 'smartstring'
import * as paths from './smartacme.paths'
2016-11-01 17:27:57 +00:00
2016-11-11 13:17:50 +00:00
let ACME = require('le-acme-core').ACME.create()
let RSA = require('rsa-compat').RSA
let bitlen = 1024
let exp = 65537
let options = {
public: true,
pem: true,
internal: true
}
/**
* class SmartAcme exports methods for maintaining SSL Certificates
*/
2016-11-01 17:27:57 +00:00
export class SmartAcme {
2016-11-11 13:17:50 +00:00
preparedBool: boolean = false
acmeUrls: any
productionBool: boolean
keyPair: any
constructor(productionArg: boolean = false) {
this.productionBool = productionArg
2016-11-01 17:27:57 +00:00
}
2016-11-01 19:16:43 +00:00
2016-11-07 17:41:52 +00:00
/**
2016-11-11 13:17:50 +00:00
* prepares the SmartAcme class
2016-11-07 17:41:52 +00:00
*/
2016-11-11 13:17:50 +00:00
prepareAcme() {
let done = q.defer()
if (this.preparedBool === false) {
this.getAcmeUrls()
.then(() => {
return this.createKeyPair()
})
.then((x) => {
console.log('prepared smartacme instance')
done.resolve()
})
} else {
done.resolve()
}
return done.promise
2016-11-01 19:16:43 +00:00
}
2016-11-07 17:41:52 +00:00
/**
2016-11-11 13:17:50 +00:00
* creates an account if not currently present in module
2016-11-07 17:41:52 +00:00
*/
2016-11-11 13:17:50 +00:00
createAccount() {
let done = q.defer()
this.prepareAcme()
.then(() => {
let options = {
newRegUrl: this.acmeUrls.newReg,
email: 'domains@lossless.org', // valid email (server checks MX records)
accountKeypair: { // privateKeyPem or privateKeyJwt
privateKeyPem: this.keyPair
},
agreeToTerms: function (tosUrl, done) {
done(null, tosUrl)
}
}
ACME.registerNewAccount(options, (err, regr) => {
if(err) {
console.log(err)
done.reject(err)
}
done.resolve(regr)
2016-11-17 12:32:16 +00:00
})
2016-11-11 13:17:50 +00:00
}).catch(err => { console.log(err) })
return done.promise
2016-11-07 17:41:52 +00:00
}
2016-11-01 19:16:43 +00:00
2016-11-11 13:17:50 +00:00
/**
* creates a keyPair
*/
createKeyPair() {
let done = q.defer()
RSA.generateKeypair(bitlen, exp, options, (err, keypair) => {
if (err) {
console.log(err)
done.reject(err)
}
console.log(keypair)
this.keyPair = keypair
2016-11-17 12:36:43 +00:00
done.resolve()
2016-11-11 13:17:50 +00:00
})
return done.promise
}
2016-11-01 19:16:43 +00:00
2016-11-11 13:17:50 +00:00
/**
* gets the Acme Urls
*/
getAcmeUrls() {
let done = q.defer()
ACME.getAcmeUrls(ACME.stagingServerUrl, (err, urls) => {
if (err) {
throw err
}
this.acmeUrls = urls
console.log(this.acmeUrls)
done.resolve()
})
return done.promise
}
2016-11-01 17:27:57 +00:00
}