users.js 16 KB

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