register_client.mjs 3.1 KB

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