diff --git a/program/management/commands/create_oidc_client.py b/program/management/commands/create_oidc_client.py index b60503caf737ae7ca2b30e827ba470d6281aa0d1..034f4e60edc373bb95e229c524d94e4ebd42b502 100644 --- a/program/management/commands/create_oidc_client.py +++ b/program/management/commands/create_oidc_client.py @@ -12,6 +12,10 @@ class Command(BaseCommand): help='A label that you associate with this client') parser.add_argument('client_type', type=str, choices=['public', 'confidential'], help='The type of client can be either public or confidential') + parser.add_argument('--client-id', type=int, dest='client_id', action='store', help='The client ID ') + parser.set_defaults(client_id=None) + parser.add_argument('--client-secret', type=str, dest='client_secret', action='store', help='The client secret') + parser.set_defaults(client_secret=None) parser.add_argument('--no-require-consent', dest='require_consent', action='store_false', help='By default user consent is required. Use this to skip user consent.') parser.add_argument('--no-reuse-consent', dest='reuse_consent', action='store_false', @@ -31,29 +35,32 @@ class Command(BaseCommand): '(and the client secret in case of confidential clients).') parser.set_defaults(id_only=False) - def handle(self, *args, **options): - # generate a new client ID and secret - client_id = False - counter = 0 - while not client_id: - client_id = random.randint(100000, 999999) - counter += 1 - if counter > 10000: - raise CommandError('Could not find a free client_id. Already'+\ - ' tried 10000 times. There seems to be something seriously'+\ - ' wrong with your setup. Please inspect manually.') - try: - Client.objects.get(client_id=client_id) - except Client.DoesNotExist: - pass - else: - client_id = False + if options['client_id'] and options['client_secret']: + client_id = options['client_id'] + client_secret = options['client_secret'] + else: + # generate a new client ID and secret + client_id = False + counter = 0 + while not client_id: + client_id = random.randint(100000, 999999) + counter += 1 + if counter > 10000: + raise CommandError('Could not find a free client_id. Already'+ \ + ' tried 10000 times. There seems to be something seriously'+ \ + ' wrong with your setup. Please inspect manually.') + try: + Client.objects.get(client_id=client_id) + except Client.DoesNotExist: + pass + else: + client_id = False - client_secret = '' - if options['client_type'] == 'confidential': client_secret = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(32)) + show_results = options['client_id'] is None and options['client_secret'] is None + # initialize lists if no option was provided if options['redirect_uri'] is None: options['redirect_uri'] = [] @@ -62,7 +69,7 @@ class Command(BaseCommand): if options['scope'] is None: options['scope'] = [] - if not options["id_only"]: + if not options["id_only"] and show_results: self.stdout.write(f'Creating client with name {options["name"]}') try: c = Client( @@ -80,7 +87,6 @@ class Command(BaseCommand): raise CommandError('Could not create an OpenID connect client' +\ f' due to the following error: {sys.exc_info()}') - if options['response_types']: try: for r_value in options['response_types']: @@ -90,12 +96,13 @@ class Command(BaseCommand): raise CommandError('Client was stored, but could not set response_types'+\ f' due to the following error: {sys.exc_info()}') - if options["id_only"]: - if options['client_type'] == 'confidential': - self.stdout.write(f'{c.client_id} {c.client_secret}') + if show_results: + if options["id_only"]: + if options['client_type'] == 'confidential': + self.stdout.write(f'{c.client_id} {c.client_secret}') + else: + self.stdout.write(f'{c.client_id}') else: - self.stdout.write(f'{c.client_id}') - else: - self.stdout.write(f'Successfully created new OIDC client, with ID: {c.client_id}') - if options['client_type'] == 'confidential': - self.stdout.write(f'The secret for this confidential client is: {c.client_secret}') + self.stdout.write(f'Successfully created new OIDC client, with ID: {c.client_id}') + if options['client_type'] == 'confidential': + self.stdout.write(f'The secret for this confidential client is: {c.client_secret}')