/**
 * Module dependencies
 */

var cwd = process.cwd()
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var pem2jwk = require('pem-jwk').pem2jwk
var exec = require('child_process').execFileSync

/**
 * Paths and file names
 */

var keyDirectory = path.join(cwd, 'keys')
var defaultPublicKeyFile = path.join(keyDirectory, 'public.pem')
var defaultPrivateKeyFile = path.join(keyDirectory, 'private.pem')

/**
 * Load Keys
 */

function loadKeys () {
  var keys = null

  // Try to read the key files. If they are available locally,
  // they should override the environment variables.
  try {
    keys = {
      privateKey: fs.readFileSync(defaultPrivateKeyFile).toString('ascii'),
      publicKey: fs.readFileSync(defaultPublicKeyFile).toString('ascii')
    }
  } catch (err) {}

  return keys
}

/**
 * Try loading
 */

var keys = loadKeys()

/**
 * Generate keys if not present
 */

if (!keys) {

  try {
    mkdirp.sync(keyDirectory)

    exec('openssl', [
      'genrsa',
      '-out',
      defaultPrivateKeyFile,
      '4096'
    ])

    exec('openssl', [
      'rsa',
      '-pubout',
      '-in',
      defaultPrivateKeyFile,
      '-out',
      defaultPublicKeyFile
    ])

  } catch (e) {
    console.log(
      'Failed to generate keys using OpenSSL. Please ensure you have OpenSSL ' +
      'installed and configured on your system.'
    )
    process.exit(1)
  }

  // Try again
  keys = loadKeys()

  // If they still can't be loaded, kill the process
  if (!keys) {
    console.log(
      'Unable to read the token-signing key pair from ' + keyDirectory
    )
    process.exit(1)
  }
}

/**
 * Create JWK from public key
 */

var jwk = pem2jwk(keys.publicKey)

/**
 * JWK Set
 */

keys.jwks = {
  keys: [{
    kty: jwk.kty,
    use: 'sig',
    alg: 'RS256',
    n: jwk.n,
    e: jwk.e
  }]
}

/**
 * Export
 */

module.exports = keys

@@ -300,29 +300,22 @@
 s')%0Avar 
-defaultPublic
+sigPub
 KeyFile 
@@ -345,39 +345,162 @@
 y, '
-public.pem')%0Avar defaultPrivate
+sig.rsa.pub.pem')%0Avar sigPrvKeyFile = path.join(keyDirectory, 'sig.rsa.prv.pem')%0Avar encPubKeyFile = path.join(keyDirectory, 'enc.rsa.pub.pem')%0Avar encPrv
 KeyF
@@ -530,23 +530,27 @@
 ctory, '
-private
+enc.rsa.prv
 .pem')%0A%0A
@@ -760,50 +760,260 @@
     
-privateKey: fs.readFileSync(defaultPrivate
+sig: %7B%0A        pub: fs.readFileSync(sigPubKeyFile).toString('ascii'),%0A        prv: fs.readFileSync(sigPrvKeyFile).toString('ascii')%0A      %7D,%0A      enc: %7B%0A        pub: fs.readFileSync(encPubKeyFile).toString('ascii'),%0A        prv: fs.readFileSync(encPrv
 KeyF
@@ -1026,32 +1026,40 @@
 oString('ascii')
+%0A      %7D
 ,%0A      publicKe
@@ -1077,29 +1077,90 @@
 ileSync(
-defaultPublic
+sigPubKeyFile).toString('ascii'),%0A      privateKey: fs.readFileSync(sigPrv
 KeyFile)
@@ -1232,103 +1232,67 @@
 %0A * 
-Try loading%0A */%0A%0Avar keys = loadKeys()%0A%0A/**%0A * G
+Generate Keys%0A */%0A%0Afunction g
 enerate
- k
+K
 eys 
-if not present%0A */%0A%0Aif (!keys
+(dir, pub, prv
 ) %7B%0A
-%0A
   tr
@@ -1311,28 +1311,19 @@
 rp.sync(
-keyDirectory
+dir
 )%0A%0A    e
@@ -1375,37 +1375,19 @@
 ,%0A      
-defaultPrivateKeyFile
+prv
 ,%0A      
@@ -1476,29 +1476,11 @@
     
-defaultPrivateKeyFile
+prv
 ,%0A  
@@ -1501,36 +1501,19 @@
     
-defaultPublicKeyFile
+pub
 %0A    %5D)%0A
 %0A  %7D
@@ -1508,17 +1508,16 @@
 %0A    %5D)%0A
-%0A
   %7D catc
@@ -1700,16 +1700,239 @@
 t(1)%0A  %7D
+%0A%7D%0A%0A/**%0A * Try loading%0A */%0A%0Avar keys = loadKeys()%0A%0A/**%0A * Generate keys if not present%0A */%0A%0Aif (!keys) %7B%0A%0A  generateKeys(keyDirectory, sigPubKeyFile, sigPrvKeyFile)%0A  generateKeys(keyDirectory, encPubKeyFile, encPrvKeyFile)
 %0A%0A  // T

add keypair for encryption and rename files