2016-11-11 13:17:50 +00:00
|
|
|
import 'typings-global'
|
|
|
|
import * as q from 'q'
|
|
|
|
import * as path from 'path'
|
2017-01-01 17:05:26 +00:00
|
|
|
let rsaKeygen = require('rsa-keygen')
|
2016-11-11 13:17:50 +00:00
|
|
|
import * as smartfile from 'smartfile'
|
|
|
|
import * as smartstring from 'smartstring'
|
2017-01-01 17:05:26 +00:00
|
|
|
let rawacme = require('rawacme')
|
2016-11-11 13:17:50 +00:00
|
|
|
import * as paths from './smartacme.paths'
|
2016-11-01 17:27:57 +00:00
|
|
|
|
2017-01-01 17:05:26 +00:00
|
|
|
import { SmartacmeHelper, IRsaKeypair } from './smartacme.classes.helper'
|
2016-11-11 13:17:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class SmartAcme exports methods for maintaining SSL Certificates
|
|
|
|
*/
|
2016-11-01 17:27:57 +00:00
|
|
|
export class SmartAcme {
|
2017-01-01 17:05:26 +00:00
|
|
|
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
|
|
|
|
JWK
|
2016-11-01 19:16:43 +00:00
|
|
|
|
2016-11-07 17:41:52 +00:00
|
|
|
/**
|
2017-01-01 17:05:26 +00:00
|
|
|
* the constructor for class SmartAcme
|
2016-11-07 17:41:52 +00:00
|
|
|
*/
|
2017-01-01 17:05:26 +00:00
|
|
|
constructor(productionArg: boolean = false) {
|
|
|
|
this.productionBool = productionArg
|
|
|
|
this.helper = new SmartacmeHelper()
|
|
|
|
this.keyPair = this.helper.createKeypair()
|
|
|
|
if (this.productionBool) {
|
|
|
|
this.acmeUrl = rawacme.LETSENCRYPT_STAGING_URL
|
2016-11-11 13:17:50 +00:00
|
|
|
} else {
|
2017-01-01 17:05:26 +00:00
|
|
|
this.acmeUrl = rawacme.LETSENCRYPT_URL
|
2016-11-11 13:17:50 +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
|
|
|
* creates an account if not currently present in module
|
2017-01-01 17:05:26 +00:00
|
|
|
* @executes ASYNC
|
2016-11-07 17:41:52 +00:00
|
|
|
*/
|
2016-11-11 13:17:50 +00:00
|
|
|
createAccount() {
|
|
|
|
let done = q.defer()
|
2017-01-01 17:05:26 +00:00
|
|
|
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
|
2016-11-11 13:17:50 +00:00
|
|
|
}
|
2016-11-01 19:16:43 +00:00
|
|
|
|
2017-01-01 17:05:26 +00:00
|
|
|
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
|
|
|
|
console.log(this.JWK)
|
|
|
|
done.resolve()
|
|
|
|
})
|
2016-11-01 19:16:43 +00:00
|
|
|
|
2016-11-11 13:17:50 +00:00
|
|
|
}
|
2017-01-01 17:05:26 +00:00
|
|
|
)
|
2016-11-11 13:17:50 +00:00
|
|
|
return done.promise
|
|
|
|
}
|
2016-11-01 17:27:57 +00:00
|
|
|
}
|