fix: generate new key

This commit is contained in:
Andras Bacsai 2023-09-15 15:39:25 +02:00
parent da4c2ee60f
commit 1cf036bbc6
3 changed files with 24 additions and 11 deletions

View File

@ -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 @@ public function updated($updateProperty)
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);

View File

@ -30,7 +30,7 @@ class="w-4 h-4 stroke-current">
</label>
@endif
<textarea placeholder="{{ $placeholder }}" {{ $attributes->merge(['class' => $defaultClass]) }}
@if ($realtimeValidation) wire:model.debounce.500ms="{{ $id }}"
@if ($realtimeValidation) wire:model.debounce.200ms="{{ $id }}"
@else
wire:model.defer={{ $value ?? $id }}
wire:dirty.class="input-warning"@endif

View File

@ -1,4 +1,5 @@
<div>
<x-forms.button class="mb-4" wire:click="generateNewKey">Generate new SSH key for me</x-forms.button>
<form class="flex flex-col gap-2" wire:submit.prevent='createPrivateKey'>
<div class="flex gap-2">
<x-forms.input id="name" label="Name" required />
@ -6,11 +7,10 @@
</div>
<x-forms.textarea realtimeValidation id="value" rows="10"
placeholder="-----BEGIN OPENSSH PRIVATE KEY-----" label="Private Key" required />
<x-forms.button wire:click="generateNewKey">Generate new SSH key for me</x-forms.button>
<x-forms.textarea id="publicKey" rows="6" readonly label="Public Key" />
<span class="font-bold text-warning">ACTION REQUIRED: Copy the 'Public Key' to your server's
<x-forms.input id="publicKey" readonly label="Public Key" />
<span class="pt-2 pb-4 font-bold text-warning">ACTION REQUIRED: Copy the 'Public Key' to your server's
~/.ssh/authorized_keys
file.</span>
file</span>
<x-forms.button type="submit">
Save Private Key
</x-forms.button>