register_client.mjs 2.9 KB

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