Path: blob/1.0-develop/app/Http/Requests/Api/Client/Account/StoreApiKeyRequest.php
10280 views
<?php12namespace Pterodactyl\Http\Requests\Api\Client\Account;34use IPTools\Range;5use Pterodactyl\Models\ApiKey;6use Illuminate\Validation\Validator;7use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;89class StoreApiKeyRequest extends ClientApiRequest10{11public function rules(): array12{13$rules = ApiKey::getRules();1415return [16'description' => $rules['memo'],17'allowed_ips' => [...$rules['allowed_ips'], 'max:50'],18'allowed_ips.*' => 'string',19];20}2122/**23* Check that each of the values entered is actually valid.24*/25public function withValidator(Validator $validator): void26{27$validator->after(function (Validator $validator) {28if (!is_array($ips = $this->input('allowed_ips'))) {29return;30}3132foreach ($ips as $index => $ip) {33$valid = false;34try {35$valid = Range::parse($ip)->valid();36} catch (\Exception $exception) {37if ($exception->getMessage() !== 'Invalid IP address format') {38throw $exception;39}40} finally {41$validator->errors()->addIf(!$valid, "allowed_ips.{$index}", '"' . $ip . '" is not a valid IP address or CIDR range.');42}43}44});45}46}474849