// @flow

const logger = require('../logger');
const R = require('ramda');
const sshpk = require('sshpk');
const bodyParser = require('body-parser');

const toFingerprint = sshKey =>
  sshpk
    .parseKey(sshKey, 'ssh')
    .fingerprint()
    .toString();


const keysRoute = async (req, res) => {
  const { fingerprint } = req.body;
  const cred = req.credentials;

  if (cred.role !== 'admin') {
    throw new Error('Unauthorized');
  }

  if (!fingerprint) {
    return res.status(500).send('Missing parameter "fingerprint"');
  }

  const dao = req.app.get("context").dao;

  logger.debug(`Accessing keys with fingerprint: ${fingerprint}`);

  const context = req.context;

  const sshKeys = await dao.getCustomerSshKeys(cred);

  const fingerprintKeyMap = R.compose(
    R.fromPairs,
    R.map(sshKey => [toFingerprint(sshKey), sshKey]),
  )(sshKeys);

  const result = R.propOr('', fingerprint, fingerprintKeyMap);

  if (!result) {
    logger.debug(`Unknown fingerprint: ${fingerprint}`);
  }

  res.send(result);
};

module.exports = [bodyParser.json(), keysRoute];

@@ -175,19 +175,38 @@
 shKey =%3E
+ %7B
 %0A  
+try %7B%0A    return 
 sshpk%0A  
@@ -207,16 +207,18 @@
 hpk%0A    
+ 
+ 
 .parseKe
@@ -234,16 +234,18 @@
  'ssh')%0A
+  
     .fin
@@ -259,16 +259,18 @@
 t()%0A    
+  
 .toStrin
@@ -274,16 +274,94 @@
 ring();%0A
+  %7D catch(e) %7B%0A    logger.error(%60Invalid ssh key detected: $%7BsshKey%7D%60);%0A  %7D%0A%7D%0A
 %0A%0Aconst 
@@ -885,16 +885,76 @@
 mPairs,%0A
+    R.reject((%5Bfingerprint%5D) =%3E fingerprint === undefined),%0A
     R.ma

Add error recovery for wrongly formatted sshKeys