aboutsummaryrefslogtreecommitdiffstats
path: root/apps/oauth2/lib/Command/ImportLegacyOcClient.php
blob: acdc57cf9918ad3a6716547196ea13fd632b8003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\OAuth2\Command;

use OCA\OAuth2\Db\Client;
use OCA\OAuth2\Db\ClientMapper;
use OCP\IConfig;
use OCP\Security\ICrypto;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ImportLegacyOcClient extends Command {
	private const ARGUMENT_CLIENT_ID = 'client-id';
	private const ARGUMENT_CLIENT_SECRET = 'client-secret';

	public function __construct(
		private readonly IConfig $config,
		private readonly ICrypto $crypto,
		private readonly ClientMapper $clientMapper,
	) {
		parent::__construct();
	}

	protected function configure(): void {
		$this->setName('oauth2:import-legacy-oc-client');
		$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.');
		$this->addArgument(
			self::ARGUMENT_CLIENT_ID,
			InputArgument::REQUIRED,
			'Value of the "identifier" column',
		);
		$this->addArgument(
			self::ARGUMENT_CLIENT_SECRET,
			InputArgument::REQUIRED,
			'Value of the "secret" column',
		);
	}

	public function isEnabled(): bool {
		return $this->config->getSystemValueBool('oauth2.enable_oc_clients', false);
	}

	protected function execute(InputInterface $input, OutputInterface $output): int {
		/** @var string $clientId */
		$clientId = $input->getArgument(self::ARGUMENT_CLIENT_ID);

		/** @var string $clientSecret */
		$clientSecret = $input->getArgument(self::ARGUMENT_CLIENT_SECRET);

		// Should not happen but just to be sure
		if (empty($clientId) || empty($clientSecret)) {
			return 1;
		}

		$hashedClientSecret = bin2hex($this->crypto->calculateHMAC($clientSecret));

		$client = new Client();
		$client->setName('ownCloud Desktop Client');
		$client->setRedirectUri('http://localhost:*');
		$client->setClientIdentifier($clientId);
		$client->setSecret($hashedClientSecret);
		$this->clientMapper->insert($client);

		$output->writeln('<info>Client imported successfully</info>');
		return 0;
	}
}