Files
pterodactyl-panel/app/Actions/Api/CreateAccountApiKey.php
T

71 lines
2.4 KiB
PHP
Raw Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Actions\Api;
use Illuminate\Support\Facades\DB;
2026-10-11 07:53:18 -04:00
use Illuminate\Support\Facades\Validator;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Contracts\Api\CreatesAccountApiKeys;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Extensions\Laravel\Sanctum\NewAccessToken;
use Pterodactyl\Models\User;
2026-10-11 10:00:29 -04:00
use Pterodactyl\Validation\Concerns\ValidatesAllowedIps;
2026-10-05 18:10:18 -04:00
final readonly class CreateAccountApiKey implements CreatesAccountApiKeys
{
2026-10-11 07:53:18 -04:00
use ValidatesAllowedIps;
/**
* An account API key's fields: its description, held to the rules of an API key's memo,
* and the IP addresses or CIDR ranges it may be used from.
*
* @return NormalizedValidationRules
*/
public static function rules(): array
{
$rules = CreateApiKey::rules();
return [
'description' => $rules['memo'],
'allowed_ips' => $rules['allowed_ips'],
'allowed_ips.*' => $rules['allowed_ips.*'],
];
}
/**
* Validates account API key input and returns the description and the allowed IPs, or
* null when none were given.
*
* @param ActionInput $input
* @return array{description: string, allowed_ips: list<string>|null}
*/
public static function attributes(array $input): array
{
$validator = Validator::make($input, self::rules());
$validator->after(fn () => self::validateAllowedIps($validator));
$input = $validator->safe();
return [
'description' => $input->string('description')->toString(),
'allowed_ips' => $input->filled('allowed_ips')
? array_map(fn (int|string $key): string => $input->string('allowed_ips.'.$key)->toString(), array_keys($input->array('allowed_ips')))
: null,
];
}
2026-10-05 18:10:18 -04:00
/** @param list<string>|null $allowedIps */
public function create(User $user, ?string $description, ?array $allowedIps): NewAccessToken
{
2026-10-11 07:53:18 -04:00
$attributes = self::attributes(['description' => $description, 'allowed_ips' => $allowedIps]);
return DB::transaction(function () use ($user, $attributes): NewAccessToken {
2026-10-05 18:10:18 -04:00
$user = User::query()->lockForUpdate()->findOrFail($user->id);
throw_if($user->apiKeys()->count() >= 25, DisplayException::class, 'You have reached the account limit for number of API keys.');
2026-10-11 07:53:18 -04:00
return $user->createToken($attributes['description'], $attributes['allowed_ips']);
2026-10-05 18:10:18 -04:00
});
}
}