@@ -3,12 +3,10 @@
|
||||
namespace App\Models;
|
||||
|
||||
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use OpenApi\Attributes as OA;
|
||||
use phpseclib3\Crypt\PublicKeyLoader;
|
||||
use Throwable;
|
||||
|
||||
#[OA\Schema(
|
||||
description: 'Private Key model',
|
||||
@@ -38,6 +36,10 @@ class PrivateKey extends BaseModel
|
||||
'fingerprint',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'private_key' => 'encrypted',
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::saving(function ($key) {
|
||||
@@ -80,7 +82,7 @@ class PrivateKey extends BaseModel
|
||||
PublicKeyLoader::load($privateKey);
|
||||
|
||||
return true;
|
||||
} catch (Throwable $e) {
|
||||
} catch (\Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -109,8 +111,8 @@ class PrivateKey extends BaseModel
|
||||
'private_key' => $keyPair['private'],
|
||||
'public_key' => $keyPair['public'],
|
||||
];
|
||||
} catch (Throwable $e) {
|
||||
throw new Exception("Failed to generate new {$type} key: ".$e->getMessage(), $e->getCode(), $e);
|
||||
} catch (\Throwable $e) {
|
||||
throw new \Exception("Failed to generate new {$type} key: ".$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +122,7 @@ class PrivateKey extends BaseModel
|
||||
$key = PublicKeyLoader::load($privateKey);
|
||||
|
||||
return $key->getPublicKey()->toString('OpenSSH', ['comment' => '']);
|
||||
} catch (Throwable $e) {
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -185,17 +187,10 @@ class PrivateKey extends BaseModel
|
||||
|
||||
public function isInUse()
|
||||
{
|
||||
if ($this->servers()->exists()) {
|
||||
return true;
|
||||
}
|
||||
if ($this->applications()->exists()) {
|
||||
return true;
|
||||
}
|
||||
if ($this->githubApps()->exists()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (bool) $this->gitlabApps()->exists();
|
||||
return $this->servers()->exists()
|
||||
|| $this->applications()->exists()
|
||||
|| $this->githubApps()->exists()
|
||||
|| $this->gitlabApps()->exists();
|
||||
}
|
||||
|
||||
public function safeDelete()
|
||||
@@ -216,22 +211,22 @@ class PrivateKey extends BaseModel
|
||||
$publicKey = $key->getPublicKey();
|
||||
|
||||
return $publicKey->getFingerprint('sha256');
|
||||
} catch (Throwable $e) {
|
||||
} catch (\Throwable $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static function fingerprintExists($fingerprint, $excludeId = null)
|
||||
{
|
||||
$builder = self::query()
|
||||
$query = self::query()
|
||||
->where('fingerprint', $fingerprint)
|
||||
->where('id', '!=', $excludeId);
|
||||
|
||||
if (currentTeam()) {
|
||||
$builder->where('team_id', currentTeam()->id);
|
||||
$query->where('team_id', currentTeam()->id);
|
||||
}
|
||||
|
||||
return $builder->exists();
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
public static function cleanupUnusedKeys()
|
||||
@@ -240,11 +235,4 @@ class PrivateKey extends BaseModel
|
||||
$privateKey->safeDelete();
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'private_key' => 'encrypted',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user