-
-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(oauth2): Add commands for adding and deleting clients #61289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CarlSchwan
wants to merge
1
commit into
master
Choose a base branch
from
carl/oauth2-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH | ||
| * SPDX-FileContributor: Carl Schwan | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\OAuth2\Command; | ||
|
|
||
| use OC\Core\Command\Base; | ||
| use OCA\OAuth2\Service\ClientService; | ||
| 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 AddClient extends Base { | ||
| private const ARGUMENT_CLIENT_NAME = 'client-name'; | ||
| private const ARGUMENT_CLIENT_REDIRECT_URI = 'client-redirect-uri'; | ||
|
|
||
| public function __construct( | ||
| private readonly ClientService $clientService, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function configure(): void { | ||
| parent::configure(); | ||
|
|
||
| $this->setName('oauth2:add-client'); | ||
| $this->setDescription('This command adds a new oauth2 client.'); | ||
| $this->addArgument( | ||
| self::ARGUMENT_CLIENT_NAME, | ||
| InputArgument::REQUIRED, | ||
| 'Name of the oauth2 client', | ||
| ); | ||
| $this->addArgument( | ||
| self::ARGUMENT_CLIENT_REDIRECT_URI, | ||
| InputArgument::REQUIRED, | ||
| 'Redirection uri of the oauth2 client ', | ||
| ); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| /** @var string $name */ | ||
| $name = $input->getArgument(self::ARGUMENT_CLIENT_NAME); | ||
|
|
||
| /** @var string $redirectUri */ | ||
| $redirectUri = $input->getArgument(self::ARGUMENT_CLIENT_REDIRECT_URI); | ||
|
|
||
| // Should not happen but just to be sure | ||
| if (empty($redirectUri) || empty($name)) { | ||
| $output->writeln('<error>Redirect uri or name is empty</error>'); | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| if (filter_var($redirectUri, FILTER_VALIDATE_URL) === false) { | ||
| $output->writeln('<error>Your redirect URL needs to be a full URL for example: https://yourdomain.com/path</error>'); | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $result = $this->clientService->addClient($name, $redirectUri); | ||
| $this->writeArrayInOutputFormat($input, $output, $result); | ||
| return Command::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH | ||
| * SPDX-FileContributor: Carl Schwan | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\OAuth2\Command; | ||
|
|
||
| use OC\Core\Command\Base; | ||
| use OCA\OAuth2\Service\ClientService; | ||
| 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 DeleteClient extends Base { | ||
| private const ARGUMENT_CLIENT_ID = 'client-id'; | ||
|
|
||
| public function __construct( | ||
| private readonly ClientService $clientService, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function configure(): void { | ||
| parent::configure(); | ||
|
|
||
| $this->setName('oauth2:delete-client'); | ||
| $this->setDescription('This command removes an existing oauth2 client.'); | ||
| $this->addArgument( | ||
| self::ARGUMENT_CLIENT_ID, | ||
| InputArgument::REQUIRED, | ||
| 'Id of the oauth2 client', | ||
| ); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function execute(InputInterface $input, OutputInterface $output): int { | ||
| $id = (int)$input->getArgument(self::ARGUMENT_CLIENT_ID); | ||
|
CarlSchwan marked this conversation as resolved.
|
||
| if ($id === 0) { | ||
| $output->writeln('<error>The given id is not a valid positive integer.</error>'); | ||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| try { | ||
| $this->clientService->deleteClient($id); | ||
| } catch (\Exception $exception) { | ||
| $output->writeln('<error>' . $exception->getMessage() . '</error>'); | ||
| return Command::FAILURE; | ||
| } | ||
| return Command::SUCCESS; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH | ||
| * SPDX-FileContributor: Carl Schwan | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\OAuth2\Service; | ||
|
|
||
| use OC\Authentication\Token\IProvider as IAuthTokenProvider; | ||
| use OCA\OAuth2\Db\AccessTokenMapper; | ||
| use OCA\OAuth2\Db\Client; | ||
| use OCA\OAuth2\Db\ClientMapper; | ||
| use OCP\Authentication\Exceptions\InvalidTokenException; | ||
| use OCP\Authentication\Exceptions\WipeTokenException; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\Security\ICrypto; | ||
| use OCP\Security\ISecureRandom; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class ClientService { | ||
| public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
|
|
||
| public function __construct( | ||
| private readonly ISecureRandom $secureRandom, | ||
| private readonly ICrypto $crypto, | ||
| private readonly ClientMapper $clientMapper, | ||
| private readonly IUserManager $userManager, | ||
| private readonly IAuthTokenProvider $tokenProvider, | ||
| private readonly LoggerInterface $logger, | ||
| private readonly AccessTokenMapper $accessTokenMapper, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param non-empty-string $name | ||
| * @param non-empty-string $redirectUri | ||
| * @return array{ | ||
| * id: int, | ||
| * name: string, | ||
| * redirectUri: string, | ||
| * clientId: string, | ||
| * clientSecret: string, | ||
| * } | ||
| */ | ||
| public function addClient(string $name, string $redirectUri): array { | ||
| $client = new Client(); | ||
| $client->setName($name); | ||
| $client->setRedirectUri($redirectUri); | ||
| $secret = $this->secureRandom->generate(64, self::validChars); | ||
| $hashedSecret = bin2hex($this->crypto->calculateHMAC($secret)); | ||
| $client->setSecret($hashedSecret); | ||
| $client->setClientIdentifier($this->secureRandom->generate(64, self::validChars)); | ||
| $client = $this->clientMapper->insert($client); | ||
|
|
||
| return [ | ||
| 'id' => $client->getId(), | ||
| 'name' => $client->getName(), | ||
| 'redirectUri' => $client->getRedirectUri(), | ||
| 'clientId' => $client->getClientIdentifier(), | ||
| 'clientSecret' => $secret, | ||
| ]; | ||
| } | ||
|
|
||
| public function deleteClient(int $id): void { | ||
| $client = $this->clientMapper->getByUid($id); | ||
|
|
||
| $this->userManager->callForSeenUsers(function (IUser $user) use ($client): void { | ||
| // Skip tokens that are marked for remote wipe so revoking the | ||
| // OAuth2 client does not silently cancel a pending wipe. | ||
| $tokens = $this->tokenProvider->getTokenByUser($user->getUID()); | ||
| foreach ($tokens as $token) { | ||
| if ($token->getName() !== $client->getName()) { | ||
| continue; | ||
| } | ||
| try { | ||
| $this->tokenProvider->getTokenById($token->getId()); | ||
| } catch (WipeTokenException) { | ||
| $this->logger->info('Preserving token {tokenId} of user {uid}: marked for remote wipe, OAuth2 client revoke would cancel the wipe.', [ | ||
| 'tokenId' => $token->getId(), | ||
| 'uid' => $user->getUID(), | ||
| ]); | ||
| continue; | ||
| } catch (InvalidTokenException) { | ||
| // Token already invalid; let invalidateTokenById handle it. | ||
| } | ||
| $this->tokenProvider->invalidateTokenById($user->getUID(), $token->getId()); | ||
| } | ||
| }); | ||
|
|
||
| $this->accessTokenMapper->deleteByClientId($id); | ||
| $this->clientMapper->delete($client); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.