Skip to content

Commit b9ca582

Browse files
authored
Merge pull request #1271 from nextcloud/enh/noid/providers-command
Add command to list providers with settings
2 parents 5efdf4e + ecbece8 commit b9ca582

2 files changed

Lines changed: 86 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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\Db\ProviderMapper;
13+
use OCA\UserOIDC\Service\ProviderService;
14+
use OCP\Security\ICrypto;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Input\InputOption;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
class ListProviders extends Base {
20+
21+
public function __construct(
22+
private ProviderMapper $providerMapper,
23+
private ProviderService $providerService,
24+
private ICrypto $crypto,
25+
) {
26+
parent::__construct();
27+
}
28+
29+
protected function configure() {
30+
$this
31+
->setName('user_oidc:providers')
32+
->setDescription('List all providers and print their configuration')
33+
->addOption('sensitive', 's', InputOption::VALUE_NONE, 'Obfuscate sensitive values like the client ID and the discovery endpoint domain name');
34+
$this->defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY;
35+
parent::configure();
36+
}
37+
38+
protected function execute(InputInterface $input, OutputInterface $output) {
39+
$outputFormat = $input->getOption('output') ?? 'json_pretty';
40+
$sensitive = $input->getOption('sensitive');
41+
42+
$providers = $this->providerMapper->getProviders();
43+
44+
$providersWithSettings = array_map(function ($provider) use ($sensitive) {
45+
$providerSettings = $this->providerService->getSettings($provider->getId());
46+
$serializedProvider = $provider->jsonSerialize();
47+
if ($sensitive) {
48+
$serializedProvider['clientId'] = '********';
49+
$serializedProvider['clientSecret'] = '********';
50+
try {
51+
$discoveryDomainName = parse_url($serializedProvider['discoveryEndpoint'], PHP_URL_HOST);
52+
$serializedProvider['discoveryEndpoint'] = str_replace($discoveryDomainName, '********', $serializedProvider['discoveryEndpoint']);
53+
} catch (\Exception|\Throwable) {
54+
}
55+
} else {
56+
$serializedProvider['clientSecret'] = $this->crypto->decrypt($provider->getClientSecret());
57+
}
58+
return array_merge($serializedProvider, ['settings' => $providerSettings]);
59+
}, $providers);
60+
61+
if ($outputFormat === 'json') {
62+
foreach ($providersWithSettings as $provider) {
63+
$output->writeln(json_encode($provider, JSON_THROW_ON_ERROR));
64+
}
65+
return 0;
66+
}
67+
68+
if ($outputFormat === 'json_pretty') {
69+
foreach ($providersWithSettings as $provider) {
70+
$output->writeln(json_encode($provider, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
71+
}
72+
return 0;
73+
}
74+
75+
$output->writeln(
76+
'<comment>Only "' . self::OUTPUT_FORMAT_JSON . '" and "' . self::OUTPUT_FORMAT_JSON_PRETTY . '" output formats are supported.</comment>',
77+
);
78+
79+
$output->writeln(
80+
'<comment>Use "--output=' . self::OUTPUT_FORMAT_JSON . '" or "--output=' . self::OUTPUT_FORMAT_JSON_PRETTY . '" '
81+
. '(default format is "' . self::OUTPUT_FORMAT_JSON_PRETTY . '")</comment>',
82+
);
83+
return 0;
84+
}
85+
}

0 commit comments

Comments
 (0)