smartacme/ts/smartacme.classes.smartacme.ts

83 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-01-14 13:14:50 +00:00
// third party modules
2017-04-28 16:56:55 +00:00
import * as q from 'smartq' // promises
2017-01-14 13:14:50 +00:00
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 {
2017-04-28 16:56:55 +00:00
publicKey: string
privateKey: string
2017-01-14 13:14:50 +00:00
}
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-04-28 16:56:55 +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
rawacmeClient
2016-11-01 19:16:43 +00:00
2017-04-28 16:56:55 +00:00
/**
* the constructor for class SmartAcme
*/
constructor(productionArg: boolean = false) {
this.productionBool = productionArg
this.keyPair = helpers.createKeypair()
if (this.productionBool) {
this.acmeUrl = plugins.rawacme.LETSENCRYPT_URL
} else {
this.acmeUrl = plugins.rawacme.LETSENCRYPT_STAGING_URL
2016-11-01 19:16:43 +00:00
}
2017-04-28 16:56:55 +00:00
}
2016-11-07 17:41:52 +00:00
2017-04-28 16:56:55 +00:00
/**
* init the smartacme instance
*/
init() {
let done = q.defer()
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-01 19:16:43 +00:00
2017-04-28 16:56:55 +00:00
// make client available in class
this.rawacmeClient = client
done.resolve()
}
)
return done.promise
}
2017-01-01 23:18:51 +00:00
2017-04-28 16:56:55 +00:00
/**
* creates an account if not currently present in module
* @executes ASYNC
*/
createAcmeAccount() {
let done = q.defer<AcmeAccount>()
let acmeAccount = new AcmeAccount(this)
acmeAccount.register().then(() => {
return acmeAccount.agreeTos()
}).then(() => {
done.resolve(acmeAccount)
})
return done.promise
}
2016-11-01 17:27:57 +00:00
}