register_client.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const minimist = require('minimist')
  2. const { ObjectId } = require('mongodb')
  3. const { waitForDb, db } = require('../../app/src/infrastructure/mongodb')
  4. async function main() {
  5. const opts = parseArgs()
  6. await waitForDb()
  7. const application = await getApplication(opts.id)
  8. if (application == null) {
  9. console.log(
  10. `Application ${opts.id} is not registered. Creating a new configuration.`
  11. )
  12. if (opts.name == null) {
  13. console.error('Missing --name option')
  14. process.exit(1)
  15. }
  16. if (opts.secret == null) {
  17. console.error('Missing --secret option')
  18. process.exit(1)
  19. }
  20. } else {
  21. console.log(`Updating configuration for client: ${application.name}`)
  22. if (opts.mongoId != null) {
  23. console.error('Cannot change Mongo ID for an existing client')
  24. process.exit(1)
  25. }
  26. }
  27. await upsertApplication(opts)
  28. }
  29. async function getApplication(clientId) {
  30. return await db.oauthApplications.findOne({ id: clientId })
  31. }
  32. async function upsertApplication(opts) {
  33. const key = { id: opts.id }
  34. const defaults = {}
  35. const updates = {}
  36. if (opts.name != null) {
  37. updates.name = opts.name
  38. }
  39. if (opts.secret != null) {
  40. updates.clientSecret = opts.secret
  41. }
  42. if (opts.grants != null) {
  43. updates.grants = opts.grants
  44. } else {
  45. defaults.grants = []
  46. }
  47. if (opts.scopes != null) {
  48. updates.scopes = opts.scopes
  49. } else {
  50. defaults.scopes = []
  51. }
  52. if (opts.redirectUris != null) {
  53. updates.redirectUris = opts.redirectUris
  54. } else {
  55. defaults.redirectUris = []
  56. }
  57. if (opts.mongoId != null) {
  58. defaults._id = ObjectId(opts.mongoId)
  59. }
  60. await db.oauthApplications.updateOne(
  61. key,
  62. {
  63. $setOnInsert: { ...key, ...defaults },
  64. $set: updates,
  65. },
  66. { upsert: true }
  67. )
  68. }
  69. function parseArgs() {
  70. const args = minimist(process.argv.slice(2), {
  71. boolean: ['help'],
  72. })
  73. if (args.help) {
  74. usage()
  75. process.exit(0)
  76. }
  77. if (args._.length !== 1) {
  78. usage()
  79. process.exit(1)
  80. }
  81. return {
  82. id: args._[0],
  83. mongoId: args['mongo-id'],
  84. name: args.name,
  85. secret: args.secret,
  86. scopes: toArray(args.scope),
  87. grants: toArray(args.grant),
  88. redirectUris: toArray(args['redirect-uri']),
  89. }
  90. }
  91. function usage() {
  92. console.error(`Usage: register_client.js [OPTS...] CLIENT_ID
  93. Creates or updates an OAuth client configuration
  94. Options:
  95. --name Descriptive name for the OAuth client (required for creation)
  96. --secret Client secret (required for creation)
  97. --scope Accepted scope (can be given more than once)
  98. --grant Accepted grant type (can be given more than once)
  99. --redirect-uri Accepted redirect URI (can be given more than once)
  100. --mongo-id Mongo ID to use if the configuration is created (optional)
  101. `)
  102. }
  103. function toArray(value) {
  104. if (value != null && !Array.isArray(value)) {
  105. return [value]
  106. } else {
  107. return value
  108. }
  109. }
  110. main()
  111. .then(() => {
  112. process.exit(0)
  113. })
  114. .catch(err => {
  115. console.error(err)
  116. process.exit(1)
  117. })