Files
pterodactyl-panel/app/Http/Requests/Api/Client/Account/StoreApiKeyRequest.php
T

66 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Requests\Api\Client\Account;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
use IPTools\Exception\IpException;
use IPTools\Exception\NetworkException;
2026-10-05 18:10:18 -04:00
use IPTools\Range;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Validation\ApiKeyRules;
class StoreApiKeyRequest extends ClientApiRequest
{
2026-10-05 18:10:18 -04:00
/**
* @return ValidationRules
*/
public function rules(): array
{
2026-10-05 18:10:18 -04:00
$rules = ApiKeyRules::rules();
return [
'description' => $rules['memo'],
2026-10-05 18:10:18 -04:00
'allowed_ips' => [...$rules['allowed_ips'], 'list', 'max:50'],
'allowed_ips.*' => ['string'],
];
}
/**
* Check that each of the values entered is actually valid.
*/
2026-10-05 18:10:18 -04:00
/**
* @return list<Closure(Validator): void>
*/
public function after(): array
{
2026-10-05 18:10:18 -04:00
return [function (Validator $validator): void {
if (! is_array($ips = $this->input('allowed_ips'))) {
return;
}
foreach ($ips as $index => $ip) {
2026-10-05 18:10:18 -04:00
if (! is_string($ip)) {
$validator->errors()->add("allowed_ips.{$index}", 'The IP address or CIDR range must be a string.');
continue;
}
$valid = false;
try {
$valid = Range::parse($ip)->valid();
2026-10-08 09:54:19 +01:00
} catch (IpException|NetworkException|InvalidArgumentException) {
$valid = false;
}
if (! $valid) {
$validator->errors()->add("allowed_ips.{$index}", '"'.$ip.'" is not a valid IP address or CIDR range.');
}
}
2026-10-05 18:10:18 -04:00
}];
}
}