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; namespace App\Http\Livewire\PrivateKey;
use App\Models\PrivateKey; use App\Models\PrivateKey;
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Livewire\Component; use Livewire\Component;
use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\PublicKeyLoader;
class Create extends Component class Create extends Component
{ {
public ?string $from = null; use WithRateLimiting;
public string $name; public string $name;
public ?string $description = null;
public string $value; public string $value;
public ?string $from = null;
public ?string $description = null;
public ?string $publicKey = null; public ?string $publicKey = null;
protected $rules = [ protected $rules = [
'name' => 'required|string', 'name' => 'required|string',
'value' => 'required|string', 'value' => 'required|string',
@ -24,9 +28,14 @@ class Create extends Component
public function generateNewKey() public function generateNewKey()
{ {
try {
$this->rateLimit(10);
$this->name = generate_random_name(); $this->name = generate_random_name();
$this->description = 'Created by Coolify'; $this->description = 'Created by Coolify';
['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey(); ['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey();
} catch(\Throwable $e) {
return handleError($e, $this);
}
} }
public function updated($updateProperty) public function updated($updateProperty)
{ {
@ -34,9 +43,13 @@ public function updated($updateProperty)
try { try {
$this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH',['comment' => '']); $this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH',['comment' => '']);
} catch (\Throwable $e) { } catch (\Throwable $e) {
if ($this->$updateProperty === "") {
$this->publicKey = "";
} else {
$this->publicKey = "Invalid private key"; $this->publicKey = "Invalid private key";
} }
} }
}
$this->validateOnly($updateProperty); $this->validateOnly($updateProperty);
} }
public function createPrivateKey() public function createPrivateKey()

View File

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

View File

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