can now agree to TOS

This commit is contained in:
2017-01-01 21:20:12 +01:00
parent 2f844dd78d
commit 96e0c4f905
8 changed files with 220 additions and 16 deletions

View File

@ -1,17 +1,53 @@
import 'typings-global'
import * as q from 'q'
let rsaKeygen = require('rsa-keygen')
import { SmartAcme } from './smartacme.classes.smartacme'
export interface IRsaKeypair {
publicKey: string
privateKey: string
}
export class SmartacmeHelper {
parentSmartAcme: SmartAcme
constructor(smartAcmeArg: SmartAcme) {
this.parentSmartAcme = smartAcmeArg
}
/**
* creates a keypair to use with requests and to generate JWK from
*/
createKeypair(bit = 2048): IRsaKeypair {
let result = rsaKeygen.generate(bit)
return {
publicKey: result.public_key,
return {
publicKey: result.public_key,
privateKey: result.private_key
}
}
}
/**
* getReg
* @executes ASYNC
*/
getReg() {
let done = q.defer()
let body = { resource: 'reg' }
this.parentSmartAcme.rawacmeClient.post(
this.parentSmartAcme.location,
body, this.parentSmartAcme.keyPair,
(err, res) => {
if (err) {
console.error('smartacme: something went wrong:')
console.log(err)
done.reject(err)
return
}
console.log(JSON.stringify(res.body))
done.resolve()
}
)
return done.promise
}
}

View File

@ -17,6 +17,9 @@ export class SmartAcme {
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
location: string
link: string
rawacmeClient
JWK
/**
@ -24,12 +27,12 @@ export class SmartAcme {
*/
constructor(productionArg: boolean = false) {
this.productionBool = productionArg
this.helper = new SmartacmeHelper()
this.helper = new SmartacmeHelper(this)
this.keyPair = this.helper.createKeypair()
if (this.productionBool) {
this.acmeUrl = rawacme.LETSENCRYPT_STAGING_URL
} else {
this.acmeUrl = rawacme.LETSENCRYPT_URL
} else {
this.acmeUrl = rawacme.LETSENCRYPT_STAGING_URL
}
}
@ -53,6 +56,10 @@ export class SmartAcme {
return
}
// make client available in class
this.rawacmeClient = client
// create the registration
client.newReg(
{
contact: ['mailto:domains@lossless.org']
@ -65,7 +72,9 @@ export class SmartAcme {
return
}
this.JWK = res.body.key
console.log(this.JWK)
this.link = res.headers.link
console.log(this.link)
this.location = res.headers.location
done.resolve()
})
@ -73,4 +82,46 @@ export class SmartAcme {
)
return done.promise
}
agreeTos() {
let done = q.defer()
let tosPart = this.link.split(',')[1]
let tosLinkPortion = tosPart.split(';')[0]
let url = tosLinkPortion.split(';')[0].trim().replace(/[<>]/g, '')
this.rawacmeClient.post(this.location,{Agreement: url, resource: 'reg'}, (err, res) => {
if (err) {
console.log(err)
done.reject(err)
return
}
done.resolve()
})
return done.promise
}
/**
* requests a certificate
*/
requestCertificate(domainNameArg) {
let done = q.defer()
this.rawacmeClient.newAuthz(
{
identifier: {
type: 'dns',
value: domainNameArg
}
},
this.keyPair,
(err, res) => {
if (err) {
console.error('smartacme: something went wrong:')
console.log(err)
done.reject(err)
}
console.log(JSON.stringify(res.body))
done.resolve()
}
)
return done.promise
}
}