overleaf/services/web/scripts/import-oauth/import_oauth_applications.js
nate stemen 9cc5dc968c Merge pull request #1957 from overleaf/ns-decaf-cleanup
.../js/... -> .../src/...

GitOrigin-RevId: ab509c633529440f05247d19c320ebb77d506768
2019-07-11 18:55:42 +00:00

48 lines
1.4 KiB
JavaScript

/* eslint-disable max-len */
/**
* run with: node import_oauth_applications /path/oauth_applications.csv
*
* where csv is generated from v1 with sql statement like:
*
* \copy (SELECT * FROM oauth_applications) to 'oauth_applications.csv' WITH CSV HEADER;
*/
/* eslint-enable */
'use strict'
const OauthApplication = require('../../app/src/models/OauthApplication')
.OauthApplication
const async = require('async')
const csvParser = require('csv-parser')
const fs = require('fs')
const minimist = require('minimist')
const argv = minimist(process.argv.slice(2))
const records = []
fs.createReadStream(argv._[0])
.pipe(csvParser())
.on('data', data => records.push(data))
.on('end', () => {
async.mapSeries(records, loadRecord, function(err) {
if (err) console.error(err)
process.exit()
})
})
function loadRecord(record, cb) {
const newRecord = {
clientSecret: record.secret,
id: record.uid,
// doorkeeper does not define grant types so add all supported
grants: ['authorization_code', 'refresh_token', 'password'],
name: record.name,
// redirect uris are stored new-line separated
redirectUris: record.redirect_uri.split(/\r?\n/),
// scopes are stored space separated
scopes: record.scopes.split(/\s+/)
}
console.log('Creating OauthApplication ' + newRecord.name)
OauthApplication.update(newRecord, newRecord, { upsert: true }, cb)
}