2023-04-03 15:45:44 -04:00
|
|
|
const minimist = require('minimist')
|
2024-08-02 12:27:31 -04:00
|
|
|
const { ObjectId } = require('mongodb-legacy')
|
2023-04-03 15:45:44 -04:00
|
|
|
const { waitForDb, db } = require('../../app/src/infrastructure/mongodb')
|
2023-05-03 07:49:52 -04:00
|
|
|
const {
|
|
|
|
hashSecret,
|
|
|
|
} = require('../../modules/oauth2-server/app/src/SecretsHelper')
|
2023-04-03 15:45:44 -04:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const opts = parseArgs()
|
|
|
|
await waitForDb()
|
|
|
|
const application = await getApplication(opts.id)
|
|
|
|
if (application == null) {
|
|
|
|
console.log(
|
|
|
|
`Application ${opts.id} is not registered. Creating a new configuration.`
|
|
|
|
)
|
|
|
|
if (opts.name == null) {
|
|
|
|
console.error('Missing --name option')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
if (opts.secret == null) {
|
|
|
|
console.error('Missing --secret option')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log(`Updating configuration for client: ${application.name}`)
|
|
|
|
if (opts.mongoId != null) {
|
|
|
|
console.error('Cannot change Mongo ID for an existing client')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await upsertApplication(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getApplication(clientId) {
|
|
|
|
return await db.oauthApplications.findOne({ id: clientId })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function upsertApplication(opts) {
|
|
|
|
const key = { id: opts.id }
|
|
|
|
const defaults = {}
|
|
|
|
const updates = {}
|
|
|
|
if (opts.name != null) {
|
|
|
|
updates.name = opts.name
|
|
|
|
}
|
|
|
|
if (opts.secret != null) {
|
2023-05-03 07:49:52 -04:00
|
|
|
updates.clientSecret = hashSecret(opts.secret)
|
2023-04-03 15:45:44 -04:00
|
|
|
}
|
|
|
|
if (opts.grants != null) {
|
|
|
|
updates.grants = opts.grants
|
|
|
|
} else {
|
|
|
|
defaults.grants = []
|
|
|
|
}
|
|
|
|
if (opts.scopes != null) {
|
|
|
|
updates.scopes = opts.scopes
|
|
|
|
} else {
|
|
|
|
defaults.scopes = []
|
|
|
|
}
|
|
|
|
if (opts.redirectUris != null) {
|
|
|
|
updates.redirectUris = opts.redirectUris
|
|
|
|
} else {
|
|
|
|
defaults.redirectUris = []
|
|
|
|
}
|
|
|
|
if (opts.mongoId != null) {
|
2023-12-18 05:54:01 -05:00
|
|
|
defaults._id = new ObjectId(opts.mongoId)
|
2023-04-03 15:45:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
await db.oauthApplications.updateOne(
|
|
|
|
key,
|
|
|
|
{
|
|
|
|
$setOnInsert: { ...key, ...defaults },
|
|
|
|
$set: updates,
|
|
|
|
},
|
|
|
|
{ upsert: true }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseArgs() {
|
|
|
|
const args = minimist(process.argv.slice(2), {
|
|
|
|
boolean: ['help'],
|
|
|
|
})
|
|
|
|
if (args.help) {
|
|
|
|
usage()
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
if (args._.length !== 1) {
|
|
|
|
usage()
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: args._[0],
|
|
|
|
mongoId: args['mongo-id'],
|
|
|
|
name: args.name,
|
|
|
|
secret: args.secret,
|
|
|
|
scopes: toArray(args.scope),
|
|
|
|
grants: toArray(args.grant),
|
|
|
|
redirectUris: toArray(args['redirect-uri']),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function usage() {
|
|
|
|
console.error(`Usage: register_client.js [OPTS...] CLIENT_ID
|
|
|
|
|
|
|
|
Creates or updates an OAuth client configuration
|
|
|
|
|
|
|
|
Options:
|
|
|
|
--name Descriptive name for the OAuth client (required for creation)
|
|
|
|
--secret Client secret (required for creation)
|
|
|
|
--scope Accepted scope (can be given more than once)
|
|
|
|
--grant Accepted grant type (can be given more than once)
|
|
|
|
--redirect-uri Accepted redirect URI (can be given more than once)
|
|
|
|
--mongo-id Mongo ID to use if the configuration is created (optional)
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
function toArray(value) {
|
|
|
|
if (value != null && !Array.isArray(value)) {
|
|
|
|
return [value]
|
|
|
|
} else {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
.then(() => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|