smartacme/ts/smartacme.classes.smartacme.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-01-14 13:14:50 +00:00
// third party modules
import * as q from 'q' // promises
import * as plugins from './smartacme.plugins'
import * as helpers from './smartacme.helpers'
2017-01-14 17:36:33 +00:00
import { AcmeAccount } from './smartacme.classes.acmeaccount'
/**
* a rsa keypair needed for account creation and subsequent requests
*/
2017-01-14 13:14:50 +00:00
export interface IRsaKeypair {
publicKey: string
privateKey: string
}
2016-11-01 17:27:57 +00:00
2017-01-14 17:36:33 +00:00
export { AcmeAccount } from './smartacme.classes.acmeaccount'
2017-01-25 01:45:48 +00:00
export { AcmeCert, ISmartAcmeChallenge, ISmartAcmeChallengeChosen } from './smartacme.classes.acmecert'
2017-01-01 23:18:51 +00:00
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-14 17:36:33 +00:00
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
2017-01-01 20:20:12 +00:00
rawacmeClient
2016-11-01 19:16:43 +00:00
2016-11-07 17:41:52 +00:00
/**
* the constructor for class SmartAcme
2016-11-07 17:41:52 +00:00
*/
constructor(productionArg: boolean = false) {
this.productionBool = productionArg
2017-01-14 13:14:50 +00:00
this.keyPair = helpers.createKeypair()
if (this.productionBool) {
2017-01-14 13:14:50 +00:00
this.acmeUrl = plugins.rawacme.LETSENCRYPT_URL
2017-01-01 20:20:12 +00:00
} else {
2017-01-14 13:14:50 +00:00
this.acmeUrl = plugins.rawacme.LETSENCRYPT_STAGING_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
/**
2017-01-14 17:36:33 +00:00
* init the smartacme instance
2016-11-07 17:41:52 +00:00
*/
2017-01-14 17:36:33 +00:00
init() {
2016-11-11 13:17:50 +00:00
let done = q.defer()
2017-01-14 13:14:50 +00:00
plugins.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 20:20:12 +00:00
// make client available in class
this.rawacmeClient = client
2017-01-14 17:36:33 +00:00
done.resolve()
2017-01-01 23:18:51 +00:00
}
)
return done.promise
}
/**
2017-01-14 17:36:33 +00:00
* creates an account if not currently present in module
* @executes ASYNC
2017-01-01 23:18:51 +00:00
*/
2017-01-15 22:11:51 +00:00
createAcmeAccount() {
2017-01-14 17:36:33 +00:00
let done = q.defer<AcmeAccount>()
let acmeAccount = new AcmeAccount(this)
acmeAccount.register().then(() => {
return acmeAccount.agreeTos()
}).then(() => {
done.resolve(acmeAccount)
2017-01-14 13:14:50 +00:00
})
return done.promise
}
2016-11-01 17:27:57 +00:00
}