coolify/app/Http/Livewire/PrivateKey/Create.php

65 lines
2.0 KiB
PHP
Raw Normal View History

2023-05-03 12:38:57 +02:00
<?php
namespace App\Http\Livewire\PrivateKey;
use App\Models\PrivateKey;
use Livewire\Component;
2023-09-15 11:55:58 +02:00
use phpseclib3\Crypt\PublicKeyLoader;
2023-05-03 12:38:57 +02:00
class Create extends Component
{
2023-09-15 11:55:58 +02:00
public ?string $from = null;
2023-05-12 15:39:07 +02:00
public string $name;
2023-09-15 11:55:58 +02:00
public ?string $description = null;
2023-05-12 15:39:07 +02:00
public string $value;
2023-09-15 11:55:58 +02:00
public ?string $publicKey = null;
2023-06-07 16:47:10 +02:00
protected $rules = [
'name' => 'required|string',
'value' => 'required|string',
];
protected $validationAttributes = [
2023-06-16 12:35:40 +02:00
'name' => 'name',
'value' => 'private Key',
2023-06-07 16:47:10 +02:00
];
2023-09-15 11:55:58 +02:00
public function generateNewKey()
{
$this->name = generate_random_name();
$this->description = 'Created by Coolify';
['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey();
}
public function updated($updateProperty)
{
if ($updateProperty === 'value') {
try {
$this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH',['comment' => '']);
} catch (\Throwable $e) {
$this->publicKey = "Invalid private key";
}
}
$this->validateOnly($updateProperty);
}
2023-05-03 12:38:57 +02:00
public function createPrivateKey()
{
2023-06-07 16:47:10 +02:00
$this->validate();
try {
$this->value = trim($this->value);
if (!str_ends_with($this->value, "\n")) {
$this->value .= "\n";
}
$private_key = PrivateKey::create([
'name' => $this->name,
'description' => $this->description,
'private_key' => $this->value,
2023-08-22 17:44:49 +02:00
'team_id' => currentTeam()->id
2023-06-07 16:47:10 +02:00
]);
if ($this->from === 'server') {
return redirect()->route('server.create');
}
return redirect()->route('security.private-key.show', ['private_key_uuid' => $private_key->uuid]);
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-16 11:39:18 +02:00
}
2023-05-03 12:38:57 +02:00
}
}