|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\OAuth2\Command; |
| 11 | + |
| 12 | +use OCA\OAuth2\Db\Client; |
| 13 | +use OCA\OAuth2\Db\ClientMapper; |
| 14 | +use OCP\IConfig; |
| 15 | +use OCP\Security\ICrypto; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class ImportLegacyOcClient extends Command { |
| 22 | + private const ARGUMENT_CLIENT_ID = 'client-id'; |
| 23 | + private const ARGUMENT_CLIENT_SECRET = 'client-secret'; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly IConfig $config, |
| 27 | + private readonly ICrypto $crypto, |
| 28 | + private readonly ClientMapper $clientMapper, |
| 29 | + ) { |
| 30 | + parent::__construct(); |
| 31 | + } |
| 32 | + |
| 33 | + protected function configure(): void { |
| 34 | + $this->setName('oauth2:import-legacy-oc-client'); |
| 35 | + $this->setDescription('This command is only required to be run on instances which were migrated from ownCloud without the oauth2.enable_oc_clients system config! Import a legacy Oauth2 client from an ownCloud instance and migrate it. The data is expected to be straight out of the database table oc_oauth2_clients.'); |
| 36 | + $this->addArgument( |
| 37 | + self::ARGUMENT_CLIENT_ID, |
| 38 | + InputArgument::REQUIRED, |
| 39 | + 'Value of the "identifier" column', |
| 40 | + ); |
| 41 | + $this->addArgument( |
| 42 | + self::ARGUMENT_CLIENT_SECRET, |
| 43 | + InputArgument::REQUIRED, |
| 44 | + 'Value of the "secret" column', |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + public function isEnabled(): bool { |
| 49 | + return $this->config->getSystemValueBool('oauth2.enable_oc_clients', false); |
| 50 | + } |
| 51 | + |
| 52 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 53 | + /** @var string $clientId */ |
| 54 | + $clientId = $input->getArgument(self::ARGUMENT_CLIENT_ID); |
| 55 | + |
| 56 | + /** @var string $clientSecret */ |
| 57 | + $clientSecret = $input->getArgument(self::ARGUMENT_CLIENT_SECRET); |
| 58 | + |
| 59 | + // Should not happen but just to be sure |
| 60 | + if (empty($clientId) || empty($clientSecret)) { |
| 61 | + return 1; |
| 62 | + } |
| 63 | + |
| 64 | + $hashedClientSecret = bin2hex($this->crypto->calculateHMAC($clientSecret)); |
| 65 | + |
| 66 | + $client = new Client(); |
| 67 | + $client->setName('ownCloud Desktop Client'); |
| 68 | + $client->setRedirectUri('http://localhost:*'); |
| 69 | + $client->setClientIdentifier($clientId); |
| 70 | + $client->setSecret($hashedClientSecret); |
| 71 | + $this->clientMapper->insert($client); |
| 72 | + |
| 73 | + $output->writeln('<info>Client imported successfully</info>'); |
| 74 | + return 0; |
| 75 | + } |
| 76 | +} |
0 commit comments