From 1cf036bbc609bc34089f07552faf5c2d4d09fa23 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Fri, 15 Sep 2023 15:39:25 +0200 Subject: [PATCH] fix: generate new key --- app/Http/Livewire/PrivateKey/Create.php | 25 ++++++++++++++----- .../views/components/forms/textarea.blade.php | 2 +- .../livewire/private-key/create.blade.php | 8 +++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/Http/Livewire/PrivateKey/Create.php b/app/Http/Livewire/PrivateKey/Create.php index cf8414b3e..4dcaedbe2 100644 --- a/app/Http/Livewire/PrivateKey/Create.php +++ b/app/Http/Livewire/PrivateKey/Create.php @@ -3,16 +3,20 @@ namespace App\Http\Livewire\PrivateKey; use App\Models\PrivateKey; +use DanHarrin\LivewireRateLimiting\WithRateLimiting; use Livewire\Component; use phpseclib3\Crypt\PublicKeyLoader; class Create extends Component { - public ?string $from = null; + use WithRateLimiting; public string $name; - public ?string $description = null; public string $value; + + public ?string $from = null; + public ?string $description = null; public ?string $publicKey = null; + protected $rules = [ 'name' => 'required|string', 'value' => 'required|string', @@ -24,9 +28,14 @@ class Create extends Component public function generateNewKey() { - $this->name = generate_random_name(); - $this->description = 'Created by Coolify'; - ['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey(); + try { + $this->rateLimit(10); + $this->name = generate_random_name(); + $this->description = 'Created by Coolify'; + ['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey(); + } catch(\Throwable $e) { + return handleError($e, $this); + } } public function updated($updateProperty) { @@ -34,7 +43,11 @@ class Create extends Component try { $this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH',['comment' => '']); } catch (\Throwable $e) { - $this->publicKey = "Invalid private key"; + if ($this->$updateProperty === "") { + $this->publicKey = ""; + } else { + $this->publicKey = "Invalid private key"; + } } } $this->validateOnly($updateProperty); diff --git a/resources/views/components/forms/textarea.blade.php b/resources/views/components/forms/textarea.blade.php index be45629ab..bf0948605 100644 --- a/resources/views/components/forms/textarea.blade.php +++ b/resources/views/components/forms/textarea.blade.php @@ -30,7 +30,7 @@ @endif