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

91 lines
2.2 KiB
PHP
Raw Normal View History

2023-05-03 10:38:57 +00:00
<?php
2024-01-07 15:23:41 +00:00
namespace App\Livewire\Security\PrivateKey;
2023-05-03 10:38:57 +00:00
use App\Models\PrivateKey;
use Livewire\Component;
class Create extends Component
{
2024-09-16 15:24:42 +00:00
public string $name = '';
2024-09-23 17:51:31 +00:00
2024-09-16 15:24:42 +00:00
public string $value = '';
2024-09-23 17:51:31 +00:00
2023-09-15 13:39:25 +00:00
public ?string $from = null;
2024-09-23 17:51:31 +00:00
2023-09-15 13:39:25 +00:00
public ?string $description = null;
2024-09-23 17:51:31 +00:00
2023-09-15 09:55:58 +00:00
public ?string $publicKey = null;
2023-09-15 13:39:25 +00:00
2023-06-07 14:47:10 +00:00
protected $rules = [
'name' => 'required|string',
'value' => 'required|string',
];
2024-06-10 20:43:34 +00:00
2024-04-03 11:45:49 +00:00
public function generateNewRSAKey()
2023-09-15 09:55:58 +00:00
{
2024-09-16 15:24:42 +00:00
$this->generateNewKey('rsa');
2023-09-15 09:55:58 +00:00
}
2024-06-10 20:43:34 +00:00
2024-04-03 11:45:49 +00:00
public function generateNewEDKey()
{
2024-09-16 15:24:42 +00:00
$this->generateNewKey('ed25519');
2024-04-03 11:45:49 +00:00
}
2024-06-10 20:43:34 +00:00
2024-09-16 15:24:42 +00:00
private function generateNewKey($type)
2023-09-15 09:55:58 +00:00
{
2024-09-16 15:24:42 +00:00
$keyData = PrivateKey::generateNewKeyPair($type);
$this->setKeyData($keyData);
}
public function updated($property)
{
if ($property === 'value') {
$this->validatePrivateKey();
2023-09-15 09:55:58 +00:00
}
}
2024-06-10 20:43:34 +00:00
2023-05-03 10:38:57 +00:00
public function createPrivateKey()
{
2024-09-16 15:24:42 +00:00
$this->validate();
2023-06-07 14:47:10 +00:00
try {
2024-09-16 15:24:42 +00:00
$privateKey = PrivateKey::createAndStore([
2023-06-07 14:47:10 +00:00
'name' => $this->name,
'description' => $this->description,
2024-09-23 17:51:31 +00:00
'private_key' => trim($this->value)."\n",
2024-06-10 20:43:34 +00:00
'team_id' => currentTeam()->id,
2023-06-07 14:47:10 +00:00
]);
2024-06-10 20:43:34 +00:00
2024-09-16 15:24:42 +00:00
return $this->redirectAfterCreation($privateKey);
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-05-16 09:39:18 +00:00
}
2023-05-03 10:38:57 +00:00
}
2024-09-16 15:24:42 +00:00
private function setKeyData(array $keyData)
{
$this->name = $keyData['name'];
$this->description = $keyData['description'];
$this->value = $keyData['private_key'];
$this->publicKey = $keyData['public_key'];
}
private function validatePrivateKey()
{
$validationResult = PrivateKey::validateAndExtractPublicKey($this->value);
$this->publicKey = $validationResult['publicKey'];
2024-09-23 17:51:31 +00:00
if (! $validationResult['isValid']) {
2024-09-16 15:24:42 +00:00
$this->addError('value', 'Invalid private key');
}
}
private function redirectAfterCreation(PrivateKey $privateKey)
{
return $this->from === 'server'
? redirect()->route('dashboard')
: redirect()->route('security.private-key.show', ['private_key_uuid' => $privateKey->uuid]);
}
2023-05-03 10:38:57 +00:00
}