Path: blob/1.0-develop/app/Http/Requests/Api/Client/Account/StoreSSHKeyRequest.php
10284 views
<?php12namespace Pterodactyl\Http\Requests\Api\Client\Account;34use phpseclib3\Crypt\DSA;5use phpseclib3\Crypt\RSA;6use Pterodactyl\Models\UserSSHKey;7use Illuminate\Validation\Validator;8use phpseclib3\Crypt\PublicKeyLoader;9use phpseclib3\Crypt\Common\PublicKey;10use phpseclib3\Exception\NoKeyLoadedException;11use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;1213class StoreSSHKeyRequest extends ClientApiRequest14{15protected ?PublicKey $key;1617/**18* Returns the rules for this request.19*/20public function rules(): array21{22return [23'name' => UserSSHKey::getRulesForField('name'),24'public_key' => UserSSHKey::getRulesForField('public_key'),25];26}2728/**29* Check to see if this SSH key has already been added to the user's account30* and if so return an error.31*/32public function withValidator(Validator $validator): void33{34$validator->after(function () {35try {36$this->key = PublicKeyLoader::loadPublicKey($this->input('public_key'));37} catch (NoKeyLoadedException $exception) {38$this->validator->errors()->add('public_key', 'The public key provided is not valid.');3940return;41}4243if ($this->key instanceof DSA) {44$this->validator->errors()->add('public_key', 'DSA keys are not supported.');45}4647if ($this->key instanceof RSA && $this->key->getLength() < 2048) {48$this->validator->errors()->add('public_key', 'RSA keys must be at least 2048 bytes in length.');49}5051$fingerprint = $this->key->getFingerprint('sha256');52if ($this->user()->sshKeys()->where('fingerprint', $fingerprint)->exists()) {53$this->validator->errors()->add('public_key', 'The public key provided already exists on your account.');54}55});56}5758/**59* Returns the public key but formatted in a consistent manner.60*/61public function getPublicKey(): string62{63return $this->key->toString('PKCS8');64}6566/**67* Returns the SHA256 fingerprint of the key provided.68*/69public function getKeyFingerprint(): string70{71if (!$this->key) {72throw new \Exception('The public key was not properly loaded for this request.');73}7475return $this->key->getFingerprint('sha256');76}77}787980