register_client.mjs 3.1 KB

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