users.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. 'use strict';
  2. const async = require('async');
  3. const config = require('config');
  4. const request = require('request');
  5. const bcrypt = require('bcrypt');
  6. const db = require('../db');
  7. const mail = require('../mail');
  8. const cache = require('../cache');
  9. const utils = require('../utils');
  10. const hooks = require('./hooks');
  11. const sha256 = require('sha256');
  12. const logger = require('../logger');
  13. cache.sub('user.updateUsername', user => {
  14. utils.socketsFromUser(user._id, sockets => {
  15. sockets.forEach(socket => {
  16. socket.emit('event:user.username.changed', user.username);
  17. });
  18. });
  19. });
  20. module.exports = {
  21. /**
  22. * Logs user in
  23. *
  24. * @param {Object} session - the session object automatically added by socket.io
  25. * @param {String} identifier - the email of the user
  26. * @param {String} password - the plaintext of the user
  27. * @param {Function} cb - gets called with the result
  28. */
  29. login: (session, identifier, password, cb) => {
  30. identifier = identifier.toLowerCase();
  31. async.waterfall([
  32. // check if a user with the requested identifier exists
  33. (next) => {
  34. db.models.user.findOne({
  35. $or: [{ 'email.address': identifier }]
  36. }, next)
  37. },
  38. // if the user doesn't exist, respond with a failure
  39. // otherwise compare the requested password and the actual users password
  40. (user, next) => {
  41. if (!user) return next('User not found');
  42. if (!user.services.password || !user.services.password.password) return next('The account you are trying to access uses GitHub to log in.');
  43. bcrypt.compare(sha256(password), user.services.password.password, (err, match) => {
  44. if (err) return next(err);
  45. if (!match) return next('Incorrect password');
  46. next(null, user);
  47. });
  48. },
  49. (user, next) => {
  50. let sessionId = utils.guid();
  51. cache.hset('sessions', sessionId, cache.schemas.session(sessionId, user._id), (err) => {
  52. if (err) return next(err);
  53. next(null, sessionId);
  54. });
  55. }
  56. ], (err, sessionId) => {
  57. if (err && err !== true) {
  58. let error = 'An error occurred.';
  59. if (typeof err === "string") error = err;
  60. else if (err.message) error = err.message;
  61. logger.error("USER_PASSWORD_LOGIN", "Login failed with password for user " + identifier + '. "' + error + '"');
  62. return cb({ status: 'failure', message: error });
  63. }
  64. logger.success("USER_PASSWORD_LOGIN", "Login successful with password for user " + identifier);
  65. cb({ status: 'success', message: 'Login successful', user: {}, SID: sessionId });
  66. });
  67. },
  68. /**
  69. * Registers a new user
  70. *
  71. * @param {Object} session - the session object automatically added by socket.io
  72. * @param {String} username - the username for the new user
  73. * @param {String} email - the email for the new user
  74. * @param {String} password - the plaintext password for the new user
  75. * @param {Object} recaptcha - the recaptcha data
  76. * @param {Function} cb - gets called with the result
  77. */
  78. register: function(session, username, email, password, recaptcha, cb) {
  79. email = email.toLowerCase();
  80. let verificationToken = utils.generateRandomString(64);
  81. async.waterfall([
  82. // verify the request with google recaptcha
  83. (next) => {
  84. request({
  85. url: 'https://www.google.com/recaptcha/api/siteverify',
  86. method: 'POST',
  87. form: {
  88. 'secret': config.get("apis").recaptcha.secret,
  89. 'response': recaptcha
  90. }
  91. }, next);
  92. },
  93. // check if the response from Google recaptcha is successful
  94. // if it is, we check if a user with the requested username already exists
  95. (response, body, next) => {
  96. let json = JSON.parse(body);
  97. if (json.success !== true) return next('Response from recaptcha was not successful.');
  98. db.models.user.findOne({ username: new RegExp(`^${username}$`, 'i') }, next);
  99. },
  100. // if the user already exists, respond with that
  101. // otherwise check if a user with the requested email already exists
  102. (user, next) => {
  103. if (user) return next('A user with that username already exists.');
  104. db.models.user.findOne({ 'email.address': email }, next);
  105. },
  106. // if the user already exists, respond with that
  107. // otherwise, generate a salt to use with hashing the new users password
  108. (user, next) => {
  109. if (user) return next('A user with that email already exists.');
  110. bcrypt.genSalt(10, next);
  111. },
  112. // hash the password
  113. (salt, next) => {
  114. bcrypt.hash(sha256(password), salt, next)
  115. },
  116. // save the new user to the database
  117. (hash, next) => {
  118. db.models.user.create({
  119. _id: utils.generateRandomString(12),//TODO Check if exists
  120. username,
  121. email: {
  122. address: email,
  123. verificationToken: verificationToken
  124. },
  125. services: {
  126. password: {
  127. password: hash
  128. }
  129. }
  130. }, next);
  131. },
  132. // respond with the new user
  133. (newUser, next) => {
  134. //TODO Send verification email
  135. mail.schemas.verifyEmail(email, username, verificationToken, () => {
  136. next();
  137. });
  138. }
  139. ], (err) => {
  140. if (err && err !== true) {
  141. let error = 'An error occurred.';
  142. if (typeof err === "string") error = err;
  143. else if (err.message) error = err.message;
  144. logger.error("USER_PASSWORD_REGISTER", "Register failed with password for user. " + '"' + error + '"');
  145. cb({status: 'failure', message: error});
  146. } else {
  147. module.exports.login(session, email, password, (result) => {
  148. let obj = {status: 'success', message: 'Successfully registered.'};
  149. if (result.status === 'success') {
  150. obj.SID = result.SID;
  151. }
  152. logger.success("USER_PASSWORD_REGISTER", "Register successful with password for user '" + username + "'.");
  153. cb({status: 'success', message: 'Successfully registered.'});
  154. });
  155. }
  156. });
  157. },
  158. /**
  159. * Logs out a user
  160. *
  161. * @param {Object} session - the session object automatically added by socket.io
  162. * @param {Function} cb - gets called with the result
  163. */
  164. logout: (session, cb) => {
  165. async.waterfall([
  166. (next) => {
  167. cache.hget('sessions', session.sessionId, next);
  168. },
  169. (session, next) => {
  170. if (!session) return next('Session not found');
  171. next(null, session);
  172. },
  173. (session, next) => {
  174. cache.hdel('sessions', session.sessionId, next);
  175. }
  176. ], (err) => {
  177. if (err && err !== true) {
  178. let error = 'An error occurred.';
  179. if (typeof err === "string") error = err;
  180. else if (err.message) error = err.message;
  181. logger.error("USER_LOGOUT", `Logout failed. ${error}`);
  182. cb({status: 'failure', message: error});
  183. } else {
  184. logger.success("USER_LOGOUT", `Logout successful.`);
  185. cb({status: 'success', message: 'Successfully logged out.'});
  186. }
  187. });
  188. },
  189. /**
  190. * Gets user object from username (only a few properties)
  191. *
  192. * @param {Object} session - the session object automatically added by socket.io
  193. * @param {String} username - the username of the user we are trying to find
  194. * @param {Function} cb - gets called with the result
  195. */
  196. findByUsername: (session, username, cb) => {
  197. async.waterfall([
  198. (next) => {
  199. db.models.user.findOne({ username: new RegExp(`^${username}$`, 'i') }, next);
  200. },
  201. (account, next) => {
  202. if (!account) return next('User not found.');
  203. next(null, account);
  204. }
  205. ], (err, account) => {
  206. if (err && err !== true) {
  207. let error = 'An error occurred.';
  208. if (typeof err === "string") error = err;
  209. else if (err.message) error = err.message;
  210. logger.error("FIND_BY_USERNAME", `User not found for username '${username}'. ${error}`);
  211. cb({status: 'failure', message: error});
  212. } else {
  213. logger.success("FIND_BY_USERNAME", `User found for username '${username}'.`);
  214. return cb({
  215. status: 'success',
  216. data: {
  217. _id: account._id,
  218. username: account.username,
  219. role: account.role,
  220. email: account.email.address,
  221. createdAt: account.createdAt,
  222. statistics: account.statistics,
  223. liked: account.liked,
  224. disliked: account.disliked
  225. }
  226. });
  227. }
  228. });
  229. },
  230. //TODO Fix security issues
  231. /**
  232. * Gets user info from session
  233. *
  234. * @param {Object} session - the session object automatically added by socket.io
  235. * @param {Function} cb - gets called with the result
  236. */
  237. findBySession: (session, cb) => {
  238. async.waterfall([
  239. (next) => {
  240. cache.hget('sessions', session.sessionId, next);
  241. },
  242. (session, next) => {
  243. if (!session) return next('Session not found.');
  244. next(null, session);
  245. },
  246. (session, next) => {
  247. db.models.user.findOne({ _id: session.userId }, next);
  248. },
  249. (user, next) => {
  250. if (!user) return next('User not found.');
  251. next(null, user);
  252. }
  253. ], (err, user) => {
  254. if (err && err !== true) {
  255. let error = 'An error occurred.';
  256. if (typeof err === "string") error = err;
  257. else if (err.message) error = err.message;
  258. logger.error("FIND_BY_SESSION", `User not found. ${error}`);
  259. cb({status: 'failure', message: error});
  260. } else {
  261. let data = {
  262. email: {
  263. address: user.email.address
  264. },
  265. username: user.username
  266. };
  267. if (user.services.password && user.services.password.password) data.password = true;
  268. logger.success("FIND_BY_SESSION", `User found. '${user.username}'.`);
  269. return cb({
  270. status: 'success',
  271. data
  272. });
  273. }
  274. });
  275. },
  276. /**
  277. * Updates a user's username
  278. *
  279. * @param {Object} session - the session object automatically added by socket.io
  280. * @param {String} newUsername - the new username
  281. * @param {Function} cb - gets called with the result
  282. * @param {String} userId - the userId automatically added by hooks
  283. */
  284. updateUsername: hooks.loginRequired((session, newUsername, cb, userId) => {
  285. async.waterfall([
  286. (next) => {
  287. db.models.user.findOne({ _id: userId }, next);
  288. },
  289. (user, next) => {
  290. if (!user) return next('User not found.');
  291. if (user.username === newUsername) return next('New username can\'t be the same as the old username.');
  292. next(null);
  293. },
  294. (next) => {
  295. db.models.user.findOne({ username: new RegExp(`^${newUsername}$`, 'i') }, next);
  296. },
  297. (user, next) => {
  298. if (!user) return next();
  299. if (user._id === userId) return next();
  300. next('That username is already in use.');
  301. },
  302. (next) => {
  303. db.models.user.update({ _id: userId }, {$set: {username: newUsername}}, next);
  304. }
  305. ], (err) => {
  306. if (err && err !== true) {
  307. let error = 'An error occurred.';
  308. if (typeof err === "string") error = err;
  309. else if (err.message) error = err.message;
  310. logger.error("UPDATE_USERNAME", `Couldn't update username for user '${userId}' to username '${newUsername}'. '${error}'`);
  311. cb({status: 'failure', message: error});
  312. } else {
  313. cache.pub('user.updateUsername', {
  314. username: newUsername,
  315. _id: userId
  316. });
  317. logger.success("UPDATE_USERNAME", `Updated username for user '${userId}' to username '${newUsername}'.`);
  318. cb({ status: 'success', message: 'Username updated successfully' });
  319. }
  320. });
  321. }),
  322. /**
  323. * Updates a user's email
  324. *
  325. * @param {Object} session - the session object automatically added by socket.io
  326. * @param {String} newEmail - the new email
  327. * @param {Function} cb - gets called with the result
  328. * @param {String} userId - the userId automatically added by hooks
  329. */
  330. updateEmail: hooks.loginRequired((session, newEmail, cb, userId) => {
  331. newEmail = newEmail.toLowerCase();
  332. let verificationToken = utils.generateRandomString(64);
  333. async.waterfall([
  334. (next) => {
  335. db.models.user.findOne({ _id: userId }, next);
  336. },
  337. (user, next) => {
  338. if (!user) return next('User not found.');
  339. if (user.email.address === newEmail) return next('New email can\'t be the same as your the old email.');
  340. next();
  341. },
  342. (next) => {
  343. db.models.user.findOne({"email.address": newEmail}, next);
  344. },
  345. (user, next) => {
  346. if (!user) return next();
  347. if (user._id === userId) return next();
  348. next('That email is already in use.');
  349. },
  350. (next) => {
  351. db.models.user.update({_id: userId}, {$set: {"email.address": newEmail, "email.verified": false, "email.verificationToken": verificationToken}}, next);
  352. },
  353. (res, next) => {
  354. db.models.user.findOne({ _id: userId }, next);
  355. },
  356. (user, next) => {
  357. mail.schemas.verifyEmail(newEmail, user.username, verificationToken, next);
  358. }
  359. ], (err) => {
  360. if (err && err !== true) {
  361. let error = 'An error occurred.';
  362. if (typeof err === "string") error = err;
  363. else if (err.message) error = err.message;
  364. logger.error("UPDATE_EMAIL", `Couldn't update email for user '${userId}' to email '${newEmail}'. '${error}'`);
  365. cb({status: 'failure', message: error});
  366. } else {
  367. logger.success("UPDATE_EMAIL", `Updated email for user '${userId}' to email '${newEmail}'.`);
  368. cb({ status: 'success', message: 'Email updated successfully.' });
  369. }
  370. });
  371. }),
  372. /**
  373. * Updates a user's role
  374. *
  375. * @param {Object} session - the session object automatically added by socket.io
  376. * @param {String} updatingUserId - the updating user's id
  377. * @param {String} newRole - the new role
  378. * @param {Function} cb - gets called with the result
  379. * @param {String} userId - the userId automatically added by hooks
  380. */
  381. updateRole: hooks.adminRequired((session, updatingUserId, newRole, cb, userId) => {
  382. newRole = newRole.toLowerCase();
  383. async.waterfall([
  384. (next) => {
  385. db.models.user.update({_id: updatingUserId}, {$set: {role: newRole}}, next);
  386. }
  387. ], (err) => {
  388. if (err && err !== true) {
  389. let error = 'An error occurred.';
  390. if (typeof err === "string") error = err;
  391. else if (err.message) error = err.message;
  392. logger.error("UPDATE_ROLE", `User '${userId}' couldn't update role for user '${updatingUserId}' to role '${newRole}'. '${error}'`);
  393. cb({status: 'failure', message: error});
  394. } else {
  395. logger.success("UPDATE_ROLE", `User '${userId}' updated the role of user '${updatingUserId}' to role '${newRole}'.`);
  396. cb({
  397. status: 'success',
  398. message: 'Role successfully updated.'
  399. });
  400. }
  401. });
  402. }),
  403. /**
  404. * Updates a user's password
  405. *
  406. * @param {Object} session - the session object automatically added by socket.io
  407. * @param {String} newPassword - the new password
  408. * @param {Function} cb - gets called with the result
  409. * @param {String} userId - the userId automatically added by hooks
  410. */
  411. updatePassword: hooks.loginRequired((session, newPassword, cb, userId) => {
  412. async.waterfall([
  413. (next) => {
  414. db.models.user.findOne({_id: userId}, next);
  415. },
  416. (user, next) => {
  417. if (!user.services.password) return next('This account does not have a password set.');
  418. next();
  419. },
  420. (next) => {
  421. bcrypt.genSalt(10, next);
  422. },
  423. // hash the password
  424. (salt, next) => {
  425. bcrypt.hash(sha256(newPassword), salt, next);
  426. },
  427. (hashedPassword, next) => {
  428. db.models.user.update({_id: userId}, {$set: {"services.password.password": hashedPassword}}, next);
  429. }
  430. ], (err) => {
  431. if (err) {
  432. let error = 'An error occurred.';
  433. if (typeof err === "string") error = err;
  434. else if (err.message) error = err.message;
  435. logger.error("UPDATE_PASSWORD", `Failed updating user password of user '${userId}'. '${error}'.`);
  436. return cb({ status: 'failure', message: error });
  437. }
  438. logger.error("UPDATE_PASSWORD", `User '${userId}' updated their password.`);
  439. cb({
  440. status: 'success',
  441. message: 'Password successfully updated.'
  442. });
  443. });
  444. })
  445. };