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;
|
2023-09-15 13:39:25 +00:00
|
|
|
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
2023-05-03 10:38:57 +00:00
|
|
|
use Livewire\Component;
|
2023-09-15 09:55:58 +00:00
|
|
|
use phpseclib3\Crypt\PublicKeyLoader;
|
2023-05-03 10:38:57 +00:00
|
|
|
|
|
|
|
class Create extends Component
|
|
|
|
{
|
2023-09-15 13:39:25 +00:00
|
|
|
use WithRateLimiting;
|
2023-05-12 13:39:07 +00:00
|
|
|
public string $name;
|
|
|
|
public string $value;
|
2023-09-15 13:39:25 +00:00
|
|
|
|
|
|
|
public ?string $from = null;
|
|
|
|
public ?string $description = null;
|
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',
|
|
|
|
];
|
|
|
|
protected $validationAttributes = [
|
2023-06-16 10:35:40 +00:00
|
|
|
'name' => 'name',
|
|
|
|
'value' => 'private Key',
|
2023-06-07 14:47:10 +00:00
|
|
|
];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-09-15 09:55:58 +00:00
|
|
|
public function generateNewKey()
|
|
|
|
{
|
2023-09-15 13:39:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2023-09-15 09:55:58 +00:00
|
|
|
}
|
|
|
|
public function updated($updateProperty)
|
|
|
|
{
|
|
|
|
if ($updateProperty === 'value') {
|
|
|
|
try {
|
|
|
|
$this->publicKey = PublicKeyLoader::load($this->$updateProperty)->getPublicKey()->toString('OpenSSH',['comment' => '']);
|
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:39:25 +00:00
|
|
|
if ($this->$updateProperty === "") {
|
|
|
|
$this->publicKey = "";
|
|
|
|
} else {
|
|
|
|
$this->publicKey = "Invalid private key";
|
|
|
|
}
|
2023-09-15 09:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->validateOnly($updateProperty);
|
|
|
|
}
|
2023-05-03 10:38:57 +00:00
|
|
|
public function createPrivateKey()
|
|
|
|
{
|
2023-06-07 14:47:10 +00: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 15:44:49 +00:00
|
|
|
'team_id' => currentTeam()->id
|
2023-06-07 14:47:10 +00:00
|
|
|
]);
|
|
|
|
if ($this->from === 'server') {
|
2023-12-27 15:45:01 +00:00
|
|
|
return redirect()->route('server.create');
|
2023-06-07 14:47:10 +00:00
|
|
|
}
|
2023-12-27 15:45:01 +00:00
|
|
|
return redirect()->route('security.private-key.show', ['private_key_uuid' => $private_key->uuid]);
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-05-16 09:39:18 +00:00
|
|
|
}
|
2023-05-03 10:38:57 +00:00
|
|
|
}
|
|
|
|
}
|