backfill_project_invites_token_hmac.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { db } from '../app/src/infrastructure/mongodb.js'
  2. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  3. import minimist from 'minimist'
  4. import CollaboratorsInviteHelper from '../app/src/Features/Collaborators/CollaboratorsInviteHelper.js'
  5. import { fileURLToPath } from 'node:url'
  6. const argv = minimist(process.argv.slice(2), {
  7. boolean: ['dry-run', 'help'],
  8. default: {
  9. 'dry-run': true,
  10. },
  11. })
  12. const DRY_RUN = argv['dry-run']
  13. async function addTokenHmacField(DRY_RUN) {
  14. const query = { tokenHmac: { $exists: false } }
  15. await batchedUpdate(
  16. db.projectInvites,
  17. query,
  18. async invites => {
  19. for (const invite of invites) {
  20. console.log(
  21. `=> Missing "tokenHmac" token in invitation: ${invite._id.toString()}`
  22. )
  23. if (DRY_RUN) {
  24. console.log(
  25. `=> DRY RUN - would add "tokenHmac" token to invitation ${invite._id.toString()}`
  26. )
  27. continue
  28. }
  29. const tokenHmac = CollaboratorsInviteHelper.hashInviteToken(
  30. invite.token
  31. )
  32. await db.projectInvites.updateOne(
  33. { _id: invite._id },
  34. { $set: { tokenHmac } }
  35. )
  36. console.log(
  37. `=> Added "tokenHmac" token to invitation ${invite._id.toString()}`
  38. )
  39. }
  40. },
  41. { token: 1 }
  42. )
  43. }
  44. async function main(DRY_RUN) {
  45. await addTokenHmacField(DRY_RUN)
  46. }
  47. export default main
  48. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  49. if (argv.help || argv._.length > 1) {
  50. console.error(`Usage: node scripts/backfill_project_invites_token_hmac.mjs
  51. Adds a "tokenHmac" field (which is a hashed version of the token) to each project invite record.
  52. Options:
  53. --dry-run finds invitations without HMAC token but does not do any updates
  54. `)
  55. process.exit(1)
  56. }
  57. try {
  58. await main(DRY_RUN)
  59. console.error('Done')
  60. process.exit(0)
  61. } catch (error) {
  62. console.error(error)
  63. process.exit(1)
  64. }
  65. }