Skip to content

Commit f9e518e

Browse files
committed
add command to list providers with settings, optionally obfuscate sensitive values
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 5efdf4e commit f9e518e

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@
3636
<commands>
3737
<command>OCA\UserOIDC\Command\UpsertProvider</command>
3838
<command>OCA\UserOIDC\Command\DeleteProvider</command>
39+
<command>OCA\UserOIDC\Command\ListProviders</command>
3940
</commands>
4041
</info>

lib/Command/ListProviders.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\UserOIDC\Command;
10+
11+
use OC\Core\Command\Base;
12+
use OCA\UserOIDC\Service\ProviderService;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
class ListProviders extends Base {
18+
19+
public function __construct(
20+
private ProviderService $providerService,
21+
) {
22+
parent::__construct();
23+
}
24+
25+
protected function configure() {
26+
$this
27+
->setName('user_oidc:providers')
28+
->setDescription('List all providers')
29+
->addOption('sensitive', 's', InputOption::VALUE_NONE, 'Obfuscate sensitive values like the client ID and the discovery endpoint domain name');
30+
$this->defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY;
31+
parent::configure();
32+
}
33+
34+
protected function execute(InputInterface $input, OutputInterface $output) {
35+
$outputFormat = $input->getOption('output') ?? 'json_pretty';
36+
37+
$providersWithSettings = $this->providerService->getProvidersWithSettings();
38+
if ($input->getOption('sensitive')) {
39+
$providersWithSettings = array_map(function ($provider) {
40+
$provider['clientId'] = '********';
41+
try {
42+
$discoveryDomainName = parse_url($provider['discoveryEndpoint'], PHP_URL_HOST);
43+
$provider['discoveryEndpoint'] = str_replace($discoveryDomainName, '********', $provider['discoveryEndpoint']);
44+
} catch (\Exception|\Throwable) {
45+
}
46+
return $provider;
47+
}, $providersWithSettings);
48+
}
49+
if ($outputFormat === 'json') {
50+
foreach ($providersWithSettings as $provider) {
51+
$output->writeln(json_encode($provider, JSON_THROW_ON_ERROR));
52+
}
53+
return 0;
54+
}
55+
56+
if ($outputFormat === 'json_pretty') {
57+
foreach ($providersWithSettings as $provider) {
58+
$output->writeln(json_encode($provider, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
59+
}
60+
return 0;
61+
}
62+
63+
$output->writeln(
64+
'<comment>Only "' . self::OUTPUT_FORMAT_JSON . '" and "' . self::OUTPUT_FORMAT_JSON_PRETTY . '" output formats are supported.</comment>',
65+
);
66+
67+
$output->writeln(
68+
'<comment>Use "--output=' . self::OUTPUT_FORMAT_JSON . '" or "--output=' . self::OUTPUT_FORMAT_JSON_PRETTY . '" '
69+
. '(default format is "' . self::OUTPUT_FORMAT_JSON_PRETTY . '")</comment>',
70+
);
71+
return 0;
72+
}
73+
}

0 commit comments

Comments
 (0)