UserEmailsConfirmationHandler.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const EmailHelper = require('../Helpers/EmailHelper')
  2. const EmailHandler = require('../Email/EmailHandler')
  3. const OneTimeTokenHandler = require('../Security/OneTimeTokenHandler')
  4. const settings = require('@overleaf/settings')
  5. const Errors = require('../Errors/Errors')
  6. const UserUpdater = require('./UserUpdater')
  7. const UserGetter = require('./UserGetter')
  8. const { callbackify, promisify } = require('util')
  9. // Reject email confirmation tokens after 90 days
  10. const TOKEN_EXPIRY_IN_S = 90 * 24 * 60 * 60
  11. const TOKEN_USE = 'email_confirmation'
  12. function sendConfirmationEmail(userId, email, emailTemplate, callback) {
  13. if (arguments.length === 3) {
  14. callback = emailTemplate
  15. emailTemplate = 'confirmEmail'
  16. }
  17. // when force-migrating accounts to v2 from v1, we don't want to send confirmation messages -
  18. // setting this env var allows us to turn this behaviour off
  19. if (process.env.SHARELATEX_NO_CONFIRMATION_MESSAGES != null) {
  20. return callback(null)
  21. }
  22. email = EmailHelper.parseEmail(email)
  23. if (!email) {
  24. return callback(new Error('invalid email'))
  25. }
  26. const data = { user_id: userId, email }
  27. OneTimeTokenHandler.getNewToken(
  28. TOKEN_USE,
  29. data,
  30. { expiresIn: TOKEN_EXPIRY_IN_S },
  31. function (err, token) {
  32. if (err) {
  33. return callback(err)
  34. }
  35. const emailOptions = {
  36. to: email,
  37. confirmEmailUrl: `${settings.siteUrl}/user/emails/confirm?token=${token}`,
  38. sendingUser_id: userId,
  39. }
  40. EmailHandler.sendEmail(emailTemplate, emailOptions, callback)
  41. }
  42. )
  43. }
  44. async function sendReconfirmationEmail(userId, email) {
  45. email = EmailHelper.parseEmail(email)
  46. if (!email) {
  47. throw new Error('invalid email')
  48. }
  49. const data = { user_id: userId, email }
  50. const token = await OneTimeTokenHandler.promises.getNewToken(
  51. TOKEN_USE,
  52. data,
  53. { expiresIn: TOKEN_EXPIRY_IN_S }
  54. )
  55. const emailOptions = {
  56. to: email,
  57. confirmEmailUrl: `${settings.siteUrl}/user/emails/confirm?token=${token}`,
  58. sendingUser_id: userId,
  59. }
  60. await EmailHandler.promises.sendEmail('reconfirmEmail', emailOptions)
  61. }
  62. const UserEmailsConfirmationHandler = {
  63. sendConfirmationEmail,
  64. sendReconfirmationEmail: callbackify(sendReconfirmationEmail),
  65. confirmEmailFromToken(token, callback) {
  66. OneTimeTokenHandler.getValueFromTokenAndExpire(
  67. TOKEN_USE,
  68. token,
  69. function (error, data) {
  70. if (error) {
  71. return callback(error)
  72. }
  73. if (!data) {
  74. return callback(new Errors.NotFoundError('no token found'))
  75. }
  76. const userId = data.user_id
  77. const email = data.email
  78. if (!userId || email !== EmailHelper.parseEmail(email)) {
  79. return callback(new Errors.NotFoundError('invalid data'))
  80. }
  81. UserGetter.getUser(userId, {}, function (error, user) {
  82. if (error) {
  83. return callback(error)
  84. }
  85. if (!user) {
  86. return callback(new Errors.NotFoundError('user not found'))
  87. }
  88. const emailExists = user.emails.some(
  89. emailData => emailData.email === email
  90. )
  91. if (!emailExists) {
  92. return callback(new Errors.NotFoundError('email missing for user'))
  93. }
  94. UserUpdater.confirmEmail(userId, email, callback)
  95. })
  96. }
  97. )
  98. },
  99. }
  100. UserEmailsConfirmationHandler.promises = {
  101. sendConfirmationEmail: promisify(sendConfirmationEmail),
  102. }
  103. module.exports = UserEmailsConfirmationHandler