2026-08-18 17:37:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Presentation\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use Application\Security\Exceptions\ClientNotFoundException;
|
|
|
|
|
use Application\Security\Models\Entities\Client;
|
|
|
|
|
use Application\Security\Models\ValueObjects\ClientIdentifier;
|
|
|
|
|
use Application\Security\Usecases\RegenerateClientSecret\Input;
|
|
|
|
|
use Application\Security\Usecases\RegenerateClientSecret\RegenerateClientSecret;
|
|
|
|
|
use Illuminate\Console\Command;
|
2026-08-18 18:46:31 +02:00
|
|
|
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
2026-08-18 17:37:44 +02:00
|
|
|
use Presentation\Console\Presenters\Clients\RegenerateClientSecretPresenter;
|
|
|
|
|
use Presentation\Console\Presenters\Clients\RegenerateClientSecretView;
|
2026-08-18 18:25:08 +02:00
|
|
|
use Presentation\Console\Services\ClientConsoleResolver;
|
2026-08-18 17:37:44 +02:00
|
|
|
use Shared\DDD\Usecase\Builder\UsecaseBuilder;
|
|
|
|
|
use Shared\DDD\Usecase\Builder\UsecaseExecutor;
|
|
|
|
|
use Symfony\Component\Console\Completion\CompletionInput;
|
|
|
|
|
use Symfony\Component\Console\Completion\CompletionSuggestions;
|
|
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
use function Laravel\Prompts\confirm;
|
|
|
|
|
use function Laravel\Prompts\search;
|
|
|
|
|
|
|
|
|
|
final class RegenerateClientSecretCommand extends Command implements PromptsForMissingInput
|
2026-08-18 17:37:44 +02:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $signature = 'client:regenerate-secret
|
2026-08-18 18:46:31 +02:00
|
|
|
{identifier : The identifier of the client}';
|
2026-08-18 17:37:44 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $description = 'Regenerate the secret of an API client';
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
2026-08-18 18:25:08 +02:00
|
|
|
private readonly ClientConsoleResolver $clientResolver,
|
2026-08-18 17:37:44 +02:00
|
|
|
) {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
/**
|
|
|
|
|
* @return array<string, \Closure(): (int|string)>
|
|
|
|
|
*/
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function promptForMissingArgumentsUsing(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'identifier' => fn (): int | string => search(
|
|
|
|
|
label: 'Search client (by name, ID, identifier, or slug):',
|
|
|
|
|
placeholder: 'Type client name, identifier or slug...',
|
|
|
|
|
options: fn (string $value): array => $this->clientResolver->searchOptions($value),
|
|
|
|
|
scroll: 10,
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-18 17:37:44 +02:00
|
|
|
#[\Override]
|
|
|
|
|
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
|
|
|
|
|
{
|
|
|
|
|
parent::complete($input, $suggestions);
|
|
|
|
|
|
|
|
|
|
if ($input->mustSuggestArgumentValuesFor('identifier')) {
|
2026-08-18 18:25:08 +02:00
|
|
|
$this->clientResolver->complete($suggestions);
|
2026-08-18 17:37:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(
|
|
|
|
|
RegenerateClientSecret $usecase,
|
|
|
|
|
UsecaseExecutor $executor,
|
|
|
|
|
): int {
|
2026-08-18 18:26:46 +02:00
|
|
|
$client = $this->resolveClient();
|
|
|
|
|
|
|
|
|
|
if ($client === null) {
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $this->confirmRegeneration($client)) {
|
|
|
|
|
$this->warn('Secret regeneration cancelled.');
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->getInput($client);
|
|
|
|
|
|
|
|
|
|
$view = UsecaseBuilder::using($executor)
|
|
|
|
|
->do($usecase)
|
|
|
|
|
->from($input)
|
|
|
|
|
->with(new RegenerateClientSecretPresenter())
|
|
|
|
|
->run();
|
|
|
|
|
|
|
|
|
|
$this->displayResult($client, $view);
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
|
} catch (ClientNotFoundException | \InvalidArgumentException $e) {
|
|
|
|
|
$this->error($e->getMessage());
|
|
|
|
|
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getInput(Client $client): Input
|
|
|
|
|
{
|
|
|
|
|
return new Input($client->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function resolveClient(): ?Client
|
|
|
|
|
{
|
2026-08-18 18:46:31 +02:00
|
|
|
$raw = $this->argument('identifier');
|
2026-08-18 18:25:08 +02:00
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
if (! is_string($raw) || trim($raw) === '') {
|
2026-08-18 18:25:08 +02:00
|
|
|
$this->error('No client specified.');
|
2026-08-18 17:37:44 +02:00
|
|
|
|
2026-08-18 18:26:46 +02:00
|
|
|
return null;
|
2026-08-18 17:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
$inputString = trim($raw);
|
2026-08-18 18:25:08 +02:00
|
|
|
$client = $this->clientResolver->find($inputString);
|
2026-08-18 17:37:44 +02:00
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
if ($client !== null) {
|
|
|
|
|
return $client;
|
|
|
|
|
}
|
2026-08-18 17:37:44 +02:00
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
try {
|
|
|
|
|
new ClientIdentifier($inputString);
|
|
|
|
|
$this->error("Client '{$inputString}' not found.");
|
|
|
|
|
} catch (\InvalidArgumentException) {
|
|
|
|
|
$this->error("Invalid client identifier '{$inputString}'. Expected a valid client identifier.");
|
2026-08-18 18:25:08 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
return null;
|
2026-08-18 17:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function confirmRegeneration(Client $client): bool
|
|
|
|
|
{
|
|
|
|
|
$this->newLine();
|
|
|
|
|
$this->warn(sprintf("Regenerating the secret will immediately invalidate the existing secret for client '%s'.", $client->name->value));
|
|
|
|
|
|
2026-08-18 18:46:31 +02:00
|
|
|
return confirm(
|
|
|
|
|
label: 'Do you want to proceed?',
|
|
|
|
|
default: false,
|
|
|
|
|
);
|
2026-08-18 17:37:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function displayResult(Client $client, RegenerateClientSecretView $view): void
|
|
|
|
|
{
|
|
|
|
|
$this->newLine();
|
|
|
|
|
$this->info($view->message);
|
|
|
|
|
$this->newLine();
|
|
|
|
|
|
|
|
|
|
$this->table(
|
|
|
|
|
['Field', 'Value'],
|
|
|
|
|
[
|
|
|
|
|
['ID', $client->id->value],
|
|
|
|
|
['Name', $client->name->value],
|
|
|
|
|
['Identifier', $view->identifier],
|
|
|
|
|
['New Secret', $view->secret],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->newLine();
|
|
|
|
|
$this->warn('WARNING: Store the new secret securely, it cannot be retrieved later.');
|
|
|
|
|
}
|
|
|
|
|
}
|