routes.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* jslint node:true */
  2. 'use strict';
  3. var fs = require('fs'),
  4. async = require('async'),
  5. uuid = require('uuid'),
  6. tags = require('./tags.js'),
  7. things = require('./things.js'),
  8. tokens = require('./tokens.js'),
  9. settings = require('./settings.js'),
  10. superagent = require('superagent'),
  11. HttpError = require('connect-lastmile').HttpError,
  12. HttpSuccess = require('connect-lastmile').HttpSuccess;
  13. exports = module.exports = {
  14. init: init,
  15. auth: auth,
  16. login: login,
  17. logout: logout,
  18. getAll: getAll,
  19. get: get,
  20. add: add,
  21. put: put,
  22. del: del,
  23. getTags: getTags,
  24. settingsSave: settingsSave,
  25. settingsGet: settingsGet,
  26. exportThings: exportThings,
  27. importThings: importThings
  28. };
  29. function init(callback) {
  30. async.series([
  31. tokens.init,
  32. things.init,
  33. tags.init,
  34. settings.init
  35. ], callback);
  36. }
  37. function auth(req, res, next) {
  38. if (!req.query.token) return res.status(401).send('missing token');
  39. tokens.exists(req.query.token, function (error, result) {
  40. if (error) return res.status(500).send('internal error');
  41. if (!result) return res.status(401).send('invalid credentials');
  42. req.token = req.query.token;
  43. next();
  44. });
  45. }
  46. var simpleAuth = process.env.SIMPLE_AUTH_URL && process.env.SIMPLE_AUTH_CLIENT_ID;
  47. function verifyUser(username, password, callback) {
  48. if (!simpleAuth) return callback(null, username === 'test' && password === 'test');
  49. var authPayload = {
  50. clientId: process.env.SIMPLE_AUTH_CLIENT_ID,
  51. username: username,
  52. password: password
  53. };
  54. superagent.post(process.env.SIMPLE_AUTH_URL + '/api/v1/login').send(authPayload).end(function (error, result) {
  55. if (error) return callback(error);
  56. callback(null, result.status === 200);
  57. });
  58. }
  59. function login(req, res, next) {
  60. if (typeof req.body.username !== 'string' || !req.body.username) return next(new HttpError(400, 'missing username'));
  61. if (typeof req.body.password !== 'string' || !req.body.password) return next(new HttpError(400, 'missing password'));
  62. verifyUser(req.body.username, req.body.password, function (error, valid) {
  63. if (error) return next(new HttpError(500, error));
  64. if (!valid) return next(new HttpError(401, 'invalid credentials'));
  65. var token = uuid.v4();
  66. tokens.add(token, function (error) {
  67. if (error) return next(new HttpError(500, error));
  68. next(new HttpSuccess(201, { token: token }));
  69. });
  70. });
  71. }
  72. function logout(req, res, next) {
  73. tokens.remove(req.token, function (error) {
  74. if (error) return next(new HttpError(500, error));
  75. next(new HttpSuccess(200, {}));
  76. });
  77. }
  78. function getAll(req, res, next) {
  79. var query = {};
  80. if (req.query && req.query.filter) {
  81. query = {
  82. $text: { $search: req.query.filter }
  83. };
  84. }
  85. things.getAll(query, function (error, result) {
  86. if (error) return next(new HttpError(500, error));
  87. next(new HttpSuccess(200, { things: result }));
  88. });
  89. }
  90. function get(req, res, next) {
  91. things.get(req.params.id, function (error, result) {
  92. if (error) return next(new HttpError(500, error));
  93. next(new HttpSuccess(200, { thing: result }));
  94. });
  95. }
  96. function add(req, res, next) {
  97. if (typeof req.body.content !== 'string' || !req.body.content) return next(new HttpError(400, 'content must be a string'));
  98. things.add(req.body.content, function (error, id) {
  99. if (error) return next(new HttpError(500, error));
  100. next(new HttpSuccess(201, { id: id }));
  101. });
  102. }
  103. function put(req, res, next) {
  104. things.put(req.params.id, req.body.content, function (error) {
  105. if (error) return next(new HttpError(500, error));
  106. next(new HttpSuccess(201, { id: req.params.id }));
  107. });
  108. }
  109. function del(req, res, next) {
  110. things.del(req.params.id, function (error) {
  111. if (error) return next(new HttpError(500, error));
  112. next(new HttpSuccess(200, {}));
  113. });
  114. }
  115. function getTags(req, res, next) {
  116. tags.get(function (error, result) {
  117. if (error) return next(new HttpError(500, error));
  118. next(new HttpSuccess(200, { tags: result }));
  119. });
  120. }
  121. function settingsSave(req, res, next) {
  122. settings.put(req.body.settings, function (error) {
  123. if (error) return next(new HttpError(500, error));
  124. next(new HttpSuccess(202, {}));
  125. });
  126. }
  127. function settingsGet(req, res, next) {
  128. settings.get(function (error, result) {
  129. if (error) return next(new HttpError(500, error));
  130. next(new HttpSuccess(200, { settings: result }));
  131. });
  132. }
  133. function exportThings(req, res, next) {
  134. things.exp(function (error, fileName) {
  135. if (error) return next(new HttpError(500, error));
  136. res.download(fileName, 'things.json', function (error) {
  137. if (error) console.error(error);
  138. fs.unlink(fileName);
  139. });
  140. });
  141. }
  142. function importThings(req, res, next) {
  143. if (!req.files || !req.files[0]) return next(new HttpError('400', 'missing file'));
  144. var data;
  145. try {
  146. data = JSON.parse(req.files[0].buffer.toString('utf-8'));
  147. } catch (e) {
  148. return next(new HttpError(400, 'content is not JSON'));
  149. }
  150. if (!Array.isArray(data.things)) return next(new HttpError(400, 'content must have a "things" array'));
  151. things.imp(data, function (error) {
  152. if (error) return next(new HttpError(500, error));
  153. next(new HttpSuccess(200, {}));
  154. });
  155. }