diff --git a/lib/app.js b/lib/app.js index 2dedc726..854e0dd8 100644 --- a/lib/app.js +++ b/lib/app.js @@ -47,10 +47,9 @@ module.exports = function (options) { app.use(express.bodyParser()); - if (!options.https) { - server = http.createServer(app); - } - else { + var authMiddleware; + + if (options.https) { var serverOptions = { key: options.https.key, cert: options.https.cert, @@ -61,30 +60,28 @@ module.exports = function (options) { }; server = https.createServer(serverOptions, app); - } - var authMiddleware = function (req, res, next) { - req.device = {}; - return next(); - }; - - if (options.https) { authMiddleware = function(req, res, next) { - var fingerprint = req.connection.getPeerCertificate().fingerprint; - - config.isAuthorized(fingerprint, function (err, device) { - if (err) { + config.isAuthorized(routes.getFingerprint(req), function (err, device) { + if (err) { res.json({err: 'Internal Server Error'}); return next(err); } if (!device) { - res.statusCode = 404; - res.json({err: 'Not Found'}); - return next(new Error('Device is unpaired')); + res.json(404, {err: 'Not Found'}); + return next(new Error('Device is unpaired')); } next(); }); }; + + } else { + server = http.createServer(app); + + authMiddleware = function (req, res, next) { + req.device = {}; + return next(); + }; } routes.init(app, config, trader, authMiddleware); diff --git a/lib/routes.js b/lib/routes.js index 83a3b9f5..7a162d28 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -4,6 +4,11 @@ var _trader; var _lamassuConfig; var logger = require('./logger'); +module.exports = { + init: init, + getFingerprint: getFingerprint +}; + // Make sure these are higher than polling interval // or there will be a lot of errors var STALE_TICKER = 180000; @@ -18,13 +23,13 @@ Error.prototype.toJSON = function () { return ret; }; -var poll = function(req, res) { +function poll(req, res) { var rateRec = _trader.rate(); var balanceRec = _trader.balance; var fingerprint = getFingerprint(req); logger.debug('poll request from: %s', fingerprint); - + // `rateRec` and `balanceRec` are both objects, so there's no danger // of misinterpreting rate or balance === 0 as 'Server initializing'. if (!rateRec || !balanceRec) { @@ -54,13 +59,13 @@ var poll = function(req, res) { }); }; -var trade = function (req, res) { +function trade(req, res) { var fingerprint = getFingerprint(req); _trader.trade(req.body, fingerprint); res.json({err: null}); }; -var send = function(req, res) { +function send(req, res) { var fingerprint = getFingerprint(req); _trader.sendBitcoins(fingerprint, req.body, function(err, txHash) { res.json({ @@ -71,7 +76,7 @@ var send = function(req, res) { }); }; -var pair = function(req, res) { +function pair(req, res) { var token = req.body.token; var name = req.body.name; @@ -89,7 +94,7 @@ var pair = function(req, res) { ); }; -exports.init = function(app, config, trader, authMiddleware) { +function init(app, config, trader, authMiddleware) { _lamassuConfig = config; _trader = trader; @@ -102,6 +107,6 @@ exports.init = function(app, config, trader, authMiddleware) { }; function getFingerprint(req) { - return req.connection.getPeerCertificate && + return typeof req.connection.getPeerCertificate === 'function' && req.connection.getPeerCertificate().fingerprint; -} \ No newline at end of file +}; diff --git a/lib/trader.js b/lib/trader.js index ee181f71..48d4c5f5 100644 --- a/lib/trader.js +++ b/lib/trader.js @@ -21,7 +21,12 @@ var Trader = module.exports = function (db) { }; Trader.prototype._findExchange = function (name) { - return require('lamassu-' + name); + try { + return require('lamassu-' + name); + + } catch(_) { + throw new Error(name + ' module is not installed. Try running `npm install --save lamassu-' + name + '` first'); + } }; Trader.prototype._findTicker = function (name) {