register_client.js 3.1 KB

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