backfill_project_invites_token_hmac.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  2. const { batchedUpdate } = require('./helpers/batchedUpdate')
  3. const minimist = require('minimist')
  4. const CollaboratorsInviteHelper = require('../app/src/Features/Collaborators/CollaboratorsInviteHelper')
  5. const argv = minimist(process.argv.slice(2), {
  6. boolean: ['dry-run', 'help'],
  7. default: {
  8. 'dry-run': true,
  9. },
  10. })
  11. const DRY_RUN = argv['dry-run']
  12. async function addTokenHmacField(DRY_RUN) {
  13. const query = { tokenHmac: { $exists: false } }
  14. await batchedUpdate(
  15. 'projectInvites',
  16. query,
  17. async invites => {
  18. for (const invite of invites) {
  19. console.log(
  20. `=> Missing "tokenHmac" token in invitation: ${invite._id.toString()}`
  21. )
  22. if (DRY_RUN) {
  23. console.log(
  24. `=> DRY RUN - would add "tokenHmac" token to invitation ${invite._id.toString()}`
  25. )
  26. continue
  27. }
  28. const tokenHmac = CollaboratorsInviteHelper.hashInviteToken(
  29. invite.token
  30. )
  31. await db.projectInvites.updateOne(
  32. { _id: invite._id },
  33. { $set: { tokenHmac } }
  34. )
  35. console.log(
  36. `=> Added "tokenHmac" token to invitation ${invite._id.toString()}`
  37. )
  38. }
  39. },
  40. { token: 1 }
  41. )
  42. }
  43. async function main(DRY_RUN) {
  44. await waitForDb()
  45. await addTokenHmacField(DRY_RUN)
  46. }
  47. module.exports = main
  48. if (require.main === module) {
  49. if (argv.help || argv._.length > 1) {
  50. console.error(`Usage: node scripts/backfill_project_invites_token_hmac.js
  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. main(DRY_RUN)
  58. .then(() => {
  59. console.error('Done')
  60. process.exit(0)
  61. })
  62. .catch(err => {
  63. console.error(err)
  64. process.exit(1)
  65. })
  66. }