switch to rawacme for more basic letsencrypt access
This commit is contained in:
17
ts/smartacme.classes.helper.ts
Normal file
17
ts/smartacme.classes.helper.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import 'typings-global'
|
||||
let rsaKeygen = require('rsa-keygen')
|
||||
|
||||
export interface IRsaKeypair {
|
||||
publicKey: string
|
||||
privateKey: string
|
||||
}
|
||||
|
||||
export class SmartacmeHelper {
|
||||
createKeypair(bit = 2048): IRsaKeypair {
|
||||
let result = rsaKeygen.generate(bit)
|
||||
return {
|
||||
publicKey: result.public_key,
|
||||
privateKey: result.private_key
|
||||
}
|
||||
}
|
||||
}
|
@ -1,111 +1,76 @@
|
||||
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'
|
||||
|
||||
let ACME = require('le-acme-core').ACME.create()
|
||||
let RSA = require('rsa-compat').RSA
|
||||
import { SmartacmeHelper, IRsaKeypair } from './smartacme.classes.helper'
|
||||
|
||||
let bitlen = 1024
|
||||
let exp = 65537
|
||||
let options = {
|
||||
public: true,
|
||||
pem: true,
|
||||
internal: true
|
||||
}
|
||||
/**
|
||||
* class SmartAcme exports methods for maintaining SSL Certificates
|
||||
*/
|
||||
export class SmartAcme {
|
||||
preparedBool: boolean = false
|
||||
acmeUrls: any
|
||||
productionBool: boolean
|
||||
keyPair: any
|
||||
constructor(productionArg: boolean = false) {
|
||||
this.productionBool = productionArg
|
||||
}
|
||||
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
|
||||
|
||||
/**
|
||||
* prepares the SmartAcme class
|
||||
* the constructor for class SmartAcme
|
||||
*/
|
||||
prepareAcme() {
|
||||
let done = q.defer()
|
||||
if (this.preparedBool === false) {
|
||||
this.getAcmeUrls()
|
||||
.then(() => {
|
||||
return this.createKeyPair()
|
||||
})
|
||||
.then((x) => {
|
||||
console.log('prepared smartacme instance')
|
||||
done.resolve()
|
||||
})
|
||||
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
|
||||
} else {
|
||||
done.resolve()
|
||||
this.acmeUrl = rawacme.LETSENCRYPT_URL
|
||||
}
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* creates an account if not currently present in module
|
||||
* @executes ASYNC
|
||||
*/
|
||||
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)
|
||||
}
|
||||
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
|
||||
}
|
||||
ACME.registerNewAccount(options, (err, regr) => {
|
||||
if(err) {
|
||||
console.log(err)
|
||||
done.reject(err)
|
||||
}
|
||||
done.resolve(regr)
|
||||
})
|
||||
}).catch(err => { console.log(err) })
|
||||
|
||||
return done.promise
|
||||
}
|
||||
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()
|
||||
})
|
||||
|
||||
/**
|
||||
* 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
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
}
|
||||
|
0
ts/test.coffee
Normal file
0
ts/test.coffee
Normal file
Reference in New Issue
Block a user