20240524135408_add_token_hmac_project_invite_tokens.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* eslint-disable no-unused-vars */
  2. import Helpers from './lib/helpers.mjs'
  3. import Crypto from 'node:crypto'
  4. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  5. const tags = ['server-ce', 'server-pro', 'saas']
  6. // Copied from services/web/app/src/Features/Collaborators/CollaboratorsInviteHelper.js
  7. function hashInviteToken(token) {
  8. return Crypto.createHmac('sha256', 'overleaf-token-invite')
  9. .update(token)
  10. .digest('hex')
  11. }
  12. const index = {
  13. key: {
  14. tokenHmac: 1,
  15. },
  16. name: 'tokenHmac_1',
  17. }
  18. const migrate = async client => {
  19. const { db } = client
  20. await Helpers.addIndexesToCollection(db.projectInvites, [index])
  21. await batchedUpdate(
  22. db.projectInvites,
  23. { tokenHmac: { $exists: false } },
  24. async invites => {
  25. for (const invite of invites) {
  26. const tokenHmac = hashInviteToken(invite.token)
  27. await db.projectInvites.updateOne(
  28. { _id: invite._id },
  29. { $set: { tokenHmac } }
  30. )
  31. }
  32. },
  33. { token: 1 }
  34. )
  35. }
  36. const rollback = async client => {
  37. const { db } = client
  38. await Helpers.dropIndexesFromCollection(db.projectInvites, [index])
  39. }
  40. export default {
  41. tags,
  42. migrate,
  43. rollback,
  44. }