import_oauth_access_grants.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* eslint-disable max-len */
  2. /**
  3. * run with: node import_oauth_access_grants /path/oauth_access_grants.csv
  4. *
  5. * where csv is generated from v1 with sql statement like:
  6. *
  7. * \copy ( SELECT oag.token, oag.scopes, oag.redirect_uri, resource_owner_id AS user_id, oa.uid AS client_id, oag.created_at + oag.expires_in * interval '1 second' AS expires_at FROM oauth_access_grants oag JOIN oauth_applications oa ON oag.application_id = oa.id WHERE oa.id = 1 AND oag.revoked_at IS NULL AND oag.created_at + oag.expires_in * interval '1 second' > NOW() ) to 'oauth_access_grants.csv' WITH CSV HEADER;
  8. *
  9. * this query exports the most non-expired oauth authorization codes for collabractec (1)
  10. *
  11. /* eslint-enable */
  12. 'use strict'
  13. const OauthApplication = require('../../app/src/models/OauthApplication')
  14. .OauthApplication
  15. const OauthAuthorizationCode = require('../../app/src/models/OauthAuthorizationCode')
  16. .OauthAuthorizationCode
  17. const User = require('../../app/src/models/User').User
  18. const async = require('async')
  19. const csvParser = require('csv-parser')
  20. const fs = require('fs')
  21. const minimist = require('minimist')
  22. const argv = minimist(process.argv.slice(2))
  23. let lineNum = 0
  24. const records = []
  25. fs.createReadStream(argv._[0])
  26. .pipe(csvParser())
  27. .on('data', data => {
  28. data.lineNum = ++lineNum
  29. records.push(data)
  30. })
  31. .on('end', () => {
  32. async.mapSeries(records, loadRecord, function(err) {
  33. if (err) console.error(err)
  34. process.exit()
  35. })
  36. })
  37. function loadRecord(record, cb) {
  38. getOauthApplication(record.client_id, function(err, oauthApplication) {
  39. if (err) return cb(err)
  40. User.findOne(
  41. { 'overleaf.id': parseInt(record.user_id) },
  42. { _id: 1 },
  43. function(err, user) {
  44. if (err) return cb(err)
  45. if (!user) {
  46. console.log(
  47. record.lineNum +
  48. ': User not found for ' +
  49. record.user_id +
  50. ' - skipping'
  51. )
  52. return cb()
  53. }
  54. const newRecord = {
  55. authorizationCode: record.token,
  56. expiresAt: record.expires_at,
  57. oauthApplication_id: oauthApplication._id,
  58. redirectUri: record.redirect_uri,
  59. scope: record.scopes,
  60. user_id: user._id
  61. }
  62. console.log(
  63. record.lineNum +
  64. 'Creating OauthAuthorizationCode for User ' +
  65. user._id
  66. )
  67. OauthAuthorizationCode.update(
  68. newRecord,
  69. newRecord,
  70. { upsert: true },
  71. cb
  72. )
  73. }
  74. )
  75. })
  76. }
  77. const oauthApplications = {}
  78. function getOauthApplication(clientId, cb) {
  79. if (oauthApplications[clientId]) return cb(null, oauthApplications[clientId])
  80. OauthApplication.findOne({ id: clientId }, { _id: 1 }, function(
  81. err,
  82. oauthApplication
  83. ) {
  84. if (err) return cb(err)
  85. if (!oauthApplication) return cb(new Error('oauthApplication not found'))
  86. oauthApplications[clientId] = oauthApplication
  87. cb(null, oauthApplication)
  88. })
  89. }